AppCompatEditText.java revision dd5571652e2ea0eea4af8dbbf10304675d640e1a
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.support.annotation.Nullable;
23import android.support.v4.view.TintableBackgroundView;
24import android.support.v7.appcompat.R;
25import android.support.v7.internal.widget.TintContextWrapper;
26import android.support.v7.internal.widget.TintInfo;
27import android.support.v7.internal.widget.TintManager;
28import android.support.v7.internal.widget.TintTypedArray;
29import android.util.AttributeSet;
30import android.widget.EditText;
31
32/**
33 * A tint aware {@link android.widget.EditText}.
34 * <p>
35 * This will automatically be used when you use {@link android.widget.EditText} in your
36 * layouts. You should only need to manually use this class when writing custom views.
37 */
38public class AppCompatEditText extends EditText implements TintableBackgroundView {
39
40    private static final int[] TINT_ATTRS = {
41            android.R.attr.background
42    };
43
44    private TintInfo mBackgroundTint;
45
46    public AppCompatEditText(Context context) {
47        this(context, null);
48    }
49
50    public AppCompatEditText(Context context, AttributeSet attrs) {
51        this(context, attrs, R.attr.editTextStyle);
52    }
53
54    public AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) {
55        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
56
57        if (TintManager.SHOULD_BE_USED) {
58            TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
59                    TINT_ATTRS, defStyleAttr, 0);
60            if (a.hasValue(0)) {
61                ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1));
62                if (tint != null) {
63                    setSupportBackgroundTintList(tint);
64                }
65            }
66            a.recycle();
67        }
68    }
69
70    /**
71     * This should be accessed via
72     * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View,
73     * android.content.res.ColorStateList)}
74     *
75     * @hide
76     */
77    @Override
78    public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
79        if (mBackgroundTint == null) {
80            mBackgroundTint = new TintInfo();
81        }
82        mBackgroundTint.mTintList = tint;
83        mBackgroundTint.mHasTintList = true;
84
85        applySupportBackgroundTint();
86    }
87
88    /**
89     * This should be accessed via
90     * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)}
91     *
92     * @hide
93     */
94    @Override
95    @Nullable
96    public ColorStateList getSupportBackgroundTintList() {
97        return mBackgroundTint != null ? mBackgroundTint.mTintList : null;
98    }
99
100    /**
101     * This should be accessed via
102     * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, android.graphics.PorterDuff.Mode)}
103     *
104     * @hide
105     */
106    @Override
107    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) {
108        if (mBackgroundTint == null) {
109            mBackgroundTint = new TintInfo();
110        }
111        mBackgroundTint.mTintMode = tintMode;
112        mBackgroundTint.mHasTintMode = true;
113
114        applySupportBackgroundTint();
115    }
116
117    /**
118     * This should be accessed via
119     * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)}
120     *
121     * @hide
122     */
123    @Override
124    @Nullable
125    public PorterDuff.Mode getSupportBackgroundTintMode() {
126        return mBackgroundTint != null ? mBackgroundTint.mTintMode : null;
127    }
128
129    @Override
130    protected void drawableStateChanged() {
131        super.drawableStateChanged();
132        applySupportBackgroundTint();
133    }
134
135    private void applySupportBackgroundTint() {
136        if (getBackground() != null && mBackgroundTint != null) {
137            TintManager.tintViewBackground(this, mBackgroundTint);
138        }
139    }
140}
141