// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageButton;

import org.chromium.chrome.R;

/**
 * Implementation of ImageButton that allows to tint the color of the image button for all
 * image button states using chrome:tint attribute in XML.
 */
public class TintedImageButton extends ImageButton {
    private ColorStateList mTint;

    public TintedImageButton(Context context) {
        super(context);
    }

    public TintedImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TintedImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.TintedImageButton, defStyle, 0);
        setTintInternal(a.getColorStateList(R.styleable.TintedImageButton_tint));
        a.recycle();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        // Workaround for API 21 where the image state does not get reset correctly when image
        // resource is changed.
        refreshDrawableState();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        updateTintColor();
    }

    /**
     * Sets the tint color for the given ImageButton for all button states.
     * @param tint The set of colors to use to color the ImageButton.
     */
    public void setTint(ColorStateList tint) {
        setTintInternal(tint);
        updateTintColor();
    }

    private void setTintInternal(ColorStateList tint) {
        // TODO(aurimas): start using Build.VERSION_CODES.LOLLIPOP once all the bots are updated.
        if (Build.VERSION.SDK_INT >= 21) {
            setImageTintList(tint);
            setImageTintMode(PorterDuff.Mode.SRC_ATOP);
        } else {
            mTint = tint;
        }
    }

    private void updateTintColor() {
        if (mTint == null || !mTint.isStateful()) return;
        setColorFilter(mTint.getColorForState(getDrawableState(), 0));
    }
}

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageButton;

import org.chromium.chrome.R;

/**
 * Implementation of ImageButton that allows to tint the color of the image button for all
 * image button states using chrome:tint attribute in XML.
 */
public class TintedImageButton extends ImageButton {
    private ColorStateList mTint;

    public TintedImageButton(Context context) {
        super(context);
    }

    public TintedImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TintedImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.TintedImageButton, defStyle, 0);
        setTintInternal(a.getColorStateList(R.styleable.TintedImageButton_tint));
        a.recycle();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        // Workaround for API 21 where the image state does not get reset correctly when image
        // resource is changed.
        refreshDrawableState();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        updateTintColor();
    }

    /**
     * Sets the tint color for the given ImageButton for all button states.
     * @param tint The set of colors to use to color the ImageButton.
     */
    public void setTint(ColorStateList tint) {
        setTintInternal(tint);
        updateTintColor();
    }

    private void setTintInternal(ColorStateList tint) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setImageTintList(tint);
            setImageTintMode(PorterDuff.Mode.SRC_ATOP);
        } else {
            mTint = tint;
        }
    }

    private void updateTintColor() {
        if (mTint == null || !mTint.isStateful()) return;
        setColorFilter(mTint.getColorForState(getDrawableState(), 0));
    }
}

Revert of Change the SDK version check to a hardcoded number. (patchset #1 id:1 of https://chromiumcodereview.appspot.com/678323005/)