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 androidx.appcompat.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.inputmethod.EditorInfo;
22 import android.view.inputmethod.InputConnection;
23 import android.widget.CheckedTextView;
24 
25 import androidx.annotation.DrawableRes;
26 import androidx.appcompat.content.res.AppCompatResources;
27 
28 /**
29  * A {@link CheckedTextView} which supports compatible features on older versions of the platform.
30  *
31  * <p>This will automatically be used when you use {@link CheckedTextView} in your layouts
32  * and the top-level activity / dialog is provided by
33  * <a href="{@docRoot}topic/libraries/support-library/packages.html#v7-appcompat">appcompat</a>.
34  * You should only need to manually use this class when writing custom views.</p>
35  */
36 public class AppCompatCheckedTextView extends CheckedTextView {
37 
38     private static final int[] TINT_ATTRS = {
39             android.R.attr.checkMark
40     };
41 
42     private final AppCompatTextHelper mTextHelper;
43 
AppCompatCheckedTextView(Context context)44     public AppCompatCheckedTextView(Context context) {
45         this(context, null);
46     }
47 
AppCompatCheckedTextView(Context context, AttributeSet attrs)48     public AppCompatCheckedTextView(Context context, AttributeSet attrs) {
49         this(context, attrs, android.R.attr.checkedTextViewStyle);
50     }
51 
AppCompatCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr)52     public AppCompatCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
53         super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
54 
55         mTextHelper = new AppCompatTextHelper(this);
56         mTextHelper.loadFromAttributes(attrs, defStyleAttr);
57         mTextHelper.applyCompoundDrawablesTints();
58 
59         TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
60                 TINT_ATTRS, defStyleAttr, 0);
61         setCheckMarkDrawable(a.getDrawable(0));
62         a.recycle();
63     }
64 
65     @Override
setCheckMarkDrawable(@rawableRes int resId)66     public void setCheckMarkDrawable(@DrawableRes int resId) {
67         setCheckMarkDrawable(AppCompatResources.getDrawable(getContext(), resId));
68     }
69 
70     @Override
setTextAppearance(Context context, int resId)71     public void setTextAppearance(Context context, int resId) {
72         super.setTextAppearance(context, resId);
73         if (mTextHelper != null) {
74             mTextHelper.onSetTextAppearance(context, resId);
75         }
76     }
77 
78     @Override
drawableStateChanged()79     protected void drawableStateChanged() {
80         super.drawableStateChanged();
81         if (mTextHelper != null) {
82             mTextHelper.applyCompoundDrawablesTints();
83         }
84     }
85 
86     @Override
onCreateInputConnection(EditorInfo outAttrs)87     public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
88         return AppCompatHintHelper.onCreateInputConnection(super.onCreateInputConnection(outAttrs),
89                 outAttrs, this);
90     }
91 }
92