Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / widget / TintedDrawable.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.widget;
6
7 import android.content.res.ColorStateList;
8 import android.content.res.Resources;
9 import android.graphics.Bitmap;
10 import android.graphics.BitmapFactory;
11 import android.graphics.PorterDuff;
12 import android.graphics.drawable.BitmapDrawable;
13
14 import org.chromium.chrome.R;
15
16 /**
17  * Implementation of BitmapDrawable that allows to tint the color of the drawable for all
18  * bitmap drawable states using chrome:tint attribute in XML.
19  */
20 public class TintedDrawable extends BitmapDrawable {
21     /**
22      * The set of colors that just be used for tinting this bitmap drawable.
23      */
24     protected ColorStateList mTint;
25
26     public TintedDrawable(Resources res, Bitmap bitmap) {
27         super(res, bitmap);
28         mTint = res.getColorStateList(R.color.dark_mode_tint);
29     }
30
31     @Override
32     protected boolean onStateChange(int[] state) {
33         boolean ret = updateTintColor();
34         super.onStateChange(state);
35         return ret;
36     }
37
38     @Override
39     public boolean isStateful() {
40         return true;
41     }
42
43     /**
44      * Sets the tint color for the given Drawable for all button states.
45      * @param tint The set of colors to use to color the ImageButton.
46      */
47     public void setTint(ColorStateList tint) {
48         if (mTint == tint) return;
49         mTint = tint;
50         updateTintColor();
51     }
52
53     public static TintedDrawable constructTintedDrawable(Resources res, int drawableId) {
54         Bitmap icon = BitmapFactory.decodeResource(res, drawableId);
55         return new TintedDrawable(res, icon);
56     }
57
58     private boolean updateTintColor() {
59         if (mTint == null) return false;
60         setColorFilter(mTint.getColorForState(getState(), 0), PorterDuff.Mode.SRC_IN);
61         return true;
62     }
63 }