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.support.annotation.DrawableRes;
21import android.support.v7.content.res.AppCompatResources;
22import android.util.AttributeSet;
23import android.widget.CheckedTextView;
24
25/**
26 * A {@link CheckedTextView} which supports compatible features on older version of the platform.
27 *
28 * <p>This will automatically be used when you use {@link CheckedTextView} in your layouts.
29 * You should only need to manually use this class when writing custom views.</p>
30 */
31public class AppCompatCheckedTextView extends CheckedTextView {
32
33    private static final int[] TINT_ATTRS = {
34            android.R.attr.checkMark
35    };
36
37    private final AppCompatTextHelper mTextHelper;
38
39    public AppCompatCheckedTextView(Context context) {
40        this(context, null);
41    }
42
43    public AppCompatCheckedTextView(Context context, AttributeSet attrs) {
44        this(context, attrs, android.R.attr.checkedTextViewStyle);
45    }
46
47    public AppCompatCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
48        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
49
50        mTextHelper = AppCompatTextHelper.create(this);
51        mTextHelper.loadFromAttributes(attrs, defStyleAttr);
52        mTextHelper.applyCompoundDrawablesTints();
53
54        TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
55                TINT_ATTRS, defStyleAttr, 0);
56        setCheckMarkDrawable(a.getDrawable(0));
57        a.recycle();
58    }
59
60    @Override
61    public void setCheckMarkDrawable(@DrawableRes int resId) {
62        setCheckMarkDrawable(AppCompatResources.getDrawable(getContext(), resId));
63    }
64
65    @Override
66    public void setTextAppearance(Context context, int resId) {
67        super.setTextAppearance(context, resId);
68        if (mTextHelper != null) {
69            mTextHelper.onSetTextAppearance(context, resId);
70        }
71    }
72
73    @Override
74    protected void drawableStateChanged() {
75        super.drawableStateChanged();
76        if (mTextHelper != null) {
77            mTextHelper.applyCompoundDrawablesTints();
78        }
79    }
80}
81