1/*
2 * Copyright (C) 2014 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.v4.view.TintableBackgroundView;
26import android.support.v7.appcompat.R;
27import android.util.AttributeSet;
28import android.widget.MultiAutoCompleteTextView;
29
30/**
31 * A {@link MultiAutoCompleteTextView} which supports compatible features on older version of the
32 * platform, including:
33 * <ul>
34 *     <li>Supports {@link R.attr#textAllCaps} style attribute which works back to
35 *     {@link android.os.Build.VERSION_CODES#ECLAIR_MR1 Eclair MR1}.</li>
36 *     <li>Allows dynamic tint of it background via the background tint methods in
37 *     {@link android.support.v4.view.ViewCompat}.</li>
38 *     <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and
39 *     {@link R.attr#backgroundTintMode}.</li>
40 * </ul>
41 *
42 * <p>This will automatically be used when you use {@link MultiAutoCompleteTextView} in your layouts.
43 * You should only need to manually use this class when writing custom views.</p>
44 */
45public class AppCompatMultiAutoCompleteTextView extends MultiAutoCompleteTextView
46        implements TintableBackgroundView {
47
48    private static final int[] TINT_ATTRS = {
49            android.R.attr.popupBackground
50    };
51
52    private AppCompatDrawableManager mDrawableManager;
53    private AppCompatBackgroundHelper mBackgroundTintHelper;
54    private AppCompatTextHelper mTextHelper;
55
56    public AppCompatMultiAutoCompleteTextView(Context context) {
57        this(context, null);
58    }
59
60    public AppCompatMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
61        this(context, attrs, R.attr.autoCompleteTextViewStyle);
62    }
63
64    public AppCompatMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
65        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
66
67        mDrawableManager = AppCompatDrawableManager.get();
68
69        TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
70                TINT_ATTRS, defStyleAttr, 0);
71        if (a.hasValue(0)) {
72            setDropDownBackgroundDrawable(a.getDrawable(0));
73        }
74        a.recycle();
75
76        mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mDrawableManager);
77        mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
78
79        mTextHelper = AppCompatTextHelper.create(this);
80        mTextHelper.loadFromAttributes(attrs, defStyleAttr);
81        mTextHelper.applyCompoundDrawablesTints();
82    }
83
84    @Override
85    public void setDropDownBackgroundResource(@DrawableRes int resId) {
86        if (mDrawableManager != null) {
87            setDropDownBackgroundDrawable(mDrawableManager.getDrawable(getContext(), resId));
88        } else {
89            super.setDropDownBackgroundResource(resId);
90        }
91    }
92
93    @Override
94    public void setBackgroundResource(@DrawableRes int resId) {
95        super.setBackgroundResource(resId);
96        if (mBackgroundTintHelper != null) {
97            mBackgroundTintHelper.onSetBackgroundResource(resId);
98        }
99    }
100
101    @Override
102    public void setBackgroundDrawable(Drawable background) {
103        super.setBackgroundDrawable(background);
104        if (mBackgroundTintHelper != null) {
105            mBackgroundTintHelper.onSetBackgroundDrawable(background);
106        }
107    }
108
109    /**
110     * This should be accessed via
111     * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
112     *
113     * @hide
114     */
115    @Override
116    public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
117        if (mBackgroundTintHelper != null) {
118            mBackgroundTintHelper.setSupportBackgroundTintList(tint);
119        }
120    }
121
122    /**
123     * This should be accessed via
124     * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)}
125     *
126     * @hide
127     */
128    @Override
129    @Nullable
130    public ColorStateList getSupportBackgroundTintList() {
131        return mBackgroundTintHelper != null
132                ? mBackgroundTintHelper.getSupportBackgroundTintList() : null;
133    }
134
135    /**
136     * This should be accessed via
137     * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)}
138     *
139     * @hide
140     */
141    @Override
142    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) {
143        if (mBackgroundTintHelper != null) {
144            mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode);
145        }
146    }
147
148    /**
149     * This should be accessed via
150     * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)}
151     *
152     * @hide
153     */
154    @Override
155    @Nullable
156    public PorterDuff.Mode getSupportBackgroundTintMode() {
157        return mBackgroundTintHelper != null
158                ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null;
159    }
160
161    @Override
162    protected void drawableStateChanged() {
163        super.drawableStateChanged();
164        if (mBackgroundTintHelper != null) {
165            mBackgroundTintHelper.applySupportBackgroundTint();
166        }
167        if (mTextHelper != null) {
168            mTextHelper.applyCompoundDrawablesTints();
169        }
170    }
171
172    @Override
173    public void setTextAppearance(Context context, int resId) {
174        super.setTextAppearance(context, resId);
175        if (mTextHelper != null) {
176            mTextHelper.onSetTextAppearance(context, resId);
177        }
178    }
179}
180