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.ImageView;
30
31import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
32
33/**
34 * A {@link ImageView} 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.ImageView} in your
44 * layouts. You should only need to manually use this class when writing custom views.</p>
45 */
46public class AppCompatImageView extends ImageView implements TintableBackgroundView {
47
48    private AppCompatBackgroundHelper mBackgroundTintHelper;
49    private AppCompatImageHelper mImageHelper;
50
51    public AppCompatImageView(Context context) {
52        this(context, null);
53    }
54
55    public AppCompatImageView(Context context, AttributeSet attrs) {
56        this(context, attrs, 0);
57    }
58
59    public AppCompatImageView(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    /**
70     * Sets a drawable as the content of this ImageView.
71     *
72     * <p>Allows the use of vector drawables when running on older versions of the platform.</p>
73     *
74     * @param resId the resource identifier of the drawable
75     * @see ImageView#setImageResource(int)
76     * @attr ref R.styleable#AppCompatImageView_srcCompat
77     */
78    @Override
79    public void setImageResource(@DrawableRes int resId) {
80        // Intercept this call and instead retrieve the Drawable via the image helper
81        mImageHelper.setImageResource(resId);
82    }
83
84    @Override
85    public void setBackgroundResource(@DrawableRes int resId) {
86        super.setBackgroundResource(resId);
87        if (mBackgroundTintHelper != null) {
88            mBackgroundTintHelper.onSetBackgroundResource(resId);
89        }
90    }
91
92    @Override
93    public void setBackgroundDrawable(Drawable background) {
94        super.setBackgroundDrawable(background);
95        if (mBackgroundTintHelper != null) {
96            mBackgroundTintHelper.onSetBackgroundDrawable(background);
97        }
98    }
99
100    /**
101     * This should be accessed via
102     * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
103     *
104     * @hide
105     */
106    @RestrictTo(GROUP_ID)
107    @Override
108    public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
109        if (mBackgroundTintHelper != null) {
110            mBackgroundTintHelper.setSupportBackgroundTintList(tint);
111        }
112    }
113
114    /**
115     * This should be accessed via
116     * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)}
117     *
118     * @hide
119     */
120    @RestrictTo(GROUP_ID)
121    @Override
122    @Nullable
123    public ColorStateList getSupportBackgroundTintList() {
124        return mBackgroundTintHelper != null
125                ? mBackgroundTintHelper.getSupportBackgroundTintList() : null;
126    }
127
128    /**
129     * This should be accessed via
130     * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)}
131     *
132     * @hide
133     */
134    @RestrictTo(GROUP_ID)
135    @Override
136    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) {
137        if (mBackgroundTintHelper != null) {
138            mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode);
139        }
140    }
141
142    /**
143     * This should be accessed via
144     * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)}
145     *
146     * @hide
147     */
148    @RestrictTo(GROUP_ID)
149    @Override
150    @Nullable
151    public PorterDuff.Mode getSupportBackgroundTintMode() {
152        return mBackgroundTintHelper != null
153                ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null;
154    }
155
156    @Override
157    protected void drawableStateChanged() {
158        super.drawableStateChanged();
159        if (mBackgroundTintHelper != null) {
160            mBackgroundTintHelper.applySupportBackgroundTint();
161        }
162    }
163
164    public boolean hasOverlappingRendering() {
165        return mImageHelper.hasOverlappingRendering() && super.hasOverlappingRendering();
166    }
167}
168