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