1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v7.widget;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.graphics.PorterDuff;
22import android.graphics.drawable.Drawable;
23import android.support.annotation.DrawableRes;
24import android.support.annotation.Nullable;
25import android.support.annotation.RestrictTo;
26import android.support.v4.view.TintableBackgroundView;
27import android.support.v7.appcompat.R;
28import android.util.AttributeSet;
29import android.widget.ImageButton;
30
31import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
32
33/**
34 * A {@link ImageButton} which supports compatible features on older version of the platform,
35 * including:
36 * <ul>
37 *     <li>Allows dynamic tint of it background via the background tint methods in
38 *     {@link android.support.v4.view.ViewCompat}.</li>
39 *     <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and
40 *     {@link R.attr#backgroundTintMode}.</li>
41 * </ul>
42 *
43 * <p>This will automatically be used when you use {@link android.widget.ImageButton} in your
44 * layouts. You should only need to manually use this class when writing custom views.</p>
45 */
46public class AppCompatImageButton extends ImageButton implements TintableBackgroundView {
47
48    private AppCompatBackgroundHelper mBackgroundTintHelper;
49    private AppCompatImageHelper mImageHelper;
50
51    public AppCompatImageButton(Context context) {
52        this(context, null);
53    }
54
55    public AppCompatImageButton(Context context, AttributeSet attrs) {
56        this(context, attrs, R.attr.imageButtonStyle);
57    }
58
59    public AppCompatImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
60        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
61
62        mBackgroundTintHelper = new AppCompatBackgroundHelper(this);
63        mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
64
65        mImageHelper = new AppCompatImageHelper(this);
66        mImageHelper.loadFromAttributes(attrs, defStyleAttr);
67    }
68
69    @Override
70    public void setImageResource(@DrawableRes int resId) {
71        // Intercept this call and instead retrieve the Drawable via the image helper
72        mImageHelper.setImageResource(resId);
73    }
74
75    @Override
76    public void setBackgroundResource(@DrawableRes int resId) {
77        super.setBackgroundResource(resId);
78        if (mBackgroundTintHelper != null) {
79            mBackgroundTintHelper.onSetBackgroundResource(resId);
80        }
81    }
82
83    @Override
84    public void setBackgroundDrawable(Drawable background) {
85        super.setBackgroundDrawable(background);
86        if (mBackgroundTintHelper != null) {
87            mBackgroundTintHelper.onSetBackgroundDrawable(background);
88        }
89    }
90
91    /**
92     * This should be accessed via
93     * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
94     *
95     * @hide
96     */
97    @RestrictTo(GROUP_ID)
98    @Override
99    public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
100        if (mBackgroundTintHelper != null) {
101            mBackgroundTintHelper.setSupportBackgroundTintList(tint);
102        }
103    }
104
105    /**
106     * This should be accessed via
107     * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)}
108     *
109     * @hide
110     */
111    @RestrictTo(GROUP_ID)
112    @Override
113    @Nullable
114    public ColorStateList getSupportBackgroundTintList() {
115        return mBackgroundTintHelper != null
116                ? mBackgroundTintHelper.getSupportBackgroundTintList() : null;
117    }
118
119    /**
120     * This should be accessed via
121     * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)}
122     *
123     * @hide
124     */
125    @RestrictTo(GROUP_ID)
126    @Override
127    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) {
128        if (mBackgroundTintHelper != null) {
129            mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode);
130        }
131    }
132
133    /**
134     * This should be accessed via
135     * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)}
136     *
137     * @hide
138     */
139    @RestrictTo(GROUP_ID)
140    @Override
141    @Nullable
142    public PorterDuff.Mode getSupportBackgroundTintMode() {
143        return mBackgroundTintHelper != null
144                ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null;
145    }
146
147    @Override
148    protected void drawableStateChanged() {
149        super.drawableStateChanged();
150        if (mBackgroundTintHelper != null) {
151            mBackgroundTintHelper.applySupportBackgroundTint();
152        }
153    }
154
155    public boolean hasOverlappingRendering() {
156        return mImageHelper.hasOverlappingRendering() && super.hasOverlappingRendering();
157    }
158}
159