1415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes/*
2415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * Copyright (C) 2014 The Android Open Source Project
3415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes *
4415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
5415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * you may not use this file except in compliance with the License.
6415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * You may obtain a copy of the License at
7415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes *
8415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
9415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes *
10415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * Unless required by applicable law or agreed to in writing, software
11415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
12415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * See the License for the specific language governing permissions and
14415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes * limitations under the License.
15415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes */
16415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
172cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banespackage android.support.v7.widget;
18415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
19415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banesimport android.content.Context;
20091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banesimport android.content.res.ColorStateList;
21091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banesimport android.graphics.PorterDuff;
2291fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banesimport android.graphics.drawable.Drawable;
232cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banesimport android.support.annotation.DrawableRes;
24091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banesimport android.support.annotation.Nullable;
25c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo;
26091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banesimport android.support.v4.widget.TintableCompoundButton;
272cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banesimport android.support.v7.appcompat.R;
284c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v7.content.res.AppCompatResources;
29415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banesimport android.util.AttributeSet;
30415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banesimport android.widget.CheckBox;
31415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
32c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport static android.support.annotation.RestrictTo.Scope.GROUP_ID;
33c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette
34415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes/**
35091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * A {@link CheckBox} which supports compatible features on older version of the platform,
36091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * including:
37091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * <ul>
38091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes *     <li>Allows dynamic tint of it background via the background tint methods in
39091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes *     {@link android.support.v4.widget.CompoundButtonCompat}.</li>
40091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes *     <li>Allows setting of the background tint using {@link R.attr#buttonTint} and
41091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes *     {@link R.attr#buttonTintMode}.</li>
42091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * </ul>
43091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes *
44091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * <p>This will automatically be used when you use {@link CheckBox} in your layouts.
45091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * You should only need to manually use this class when writing custom views.</p>
46415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes */
47091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banespublic class AppCompatCheckBox extends CheckBox implements TintableCompoundButton {
48415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
49091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    private AppCompatCompoundButtonHelper mCompoundButtonHelper;
50415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
512cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public AppCompatCheckBox(Context context) {
52415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes        this(context, null);
53415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes    }
54415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
552cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public AppCompatCheckBox(Context context, AttributeSet attrs) {
562cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes        this(context, attrs, R.attr.checkboxStyle);
57415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes    }
58415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
592cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public AppCompatCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
60e4beadba70aecabbd7f6677943ab7c0b94809b8aChris Banes        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
614c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this);
62091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr);
63415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes    }
64415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes
65415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes    @Override
6691fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes    public void setButtonDrawable(Drawable buttonDrawable) {
6791fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes        super.setButtonDrawable(buttonDrawable);
68091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        if (mCompoundButtonHelper != null) {
69091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes            mCompoundButtonHelper.onSetButtonDrawable();
70091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        }
7191fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes    }
7291fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes
7391fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes    @Override
742cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public void setButtonDrawable(@DrawableRes int resId) {
754c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        setButtonDrawable(AppCompatResources.getDrawable(getContext(), resId));
76415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes    }
7791fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes
7891fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes    @Override
7991fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes    public int getCompoundPaddingLeft() {
80091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        final int value = super.getCompoundPaddingLeft();
81091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        return mCompoundButtonHelper != null
82091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes                ? mCompoundButtonHelper.getCompoundPaddingLeft(value)
83091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes                : value;
84091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    }
85091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes
86091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    /**
87091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat}
88091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * @hide
89091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     */
90c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
91091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    @Override
92091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    public void setSupportButtonTintList(@Nullable ColorStateList tint) {
93091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        if (mCompoundButtonHelper != null) {
94091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes            mCompoundButtonHelper.setSupportButtonTintList(tint);
9591fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes        }
9691fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes    }
9791fcb7588ef27ffdb5a221a401c05bb77aecc00eChris Banes
98091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    /**
99091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat}
100091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * @hide
101091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     */
102c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
103091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    @Nullable
104091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    @Override
105091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    public ColorStateList getSupportButtonTintList() {
106091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        return mCompoundButtonHelper != null
107091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes                ? mCompoundButtonHelper.getSupportButtonTintList()
108091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes                : null;
109091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    }
110091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes
111091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    /**
112091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat}
113091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * @hide
114091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     */
115c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
116091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    @Override
117091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    public void setSupportButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
118091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        if (mCompoundButtonHelper != null) {
119091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes            mCompoundButtonHelper.setSupportButtonTintMode(tintMode);
120091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        }
121091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    }
122091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes
123091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    /**
124091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat}
125091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     * @hide
126091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes     */
127c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
128091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    @Nullable
129091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    @Override
130091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    public PorterDuff.Mode getSupportButtonTintMode() {
131091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes        return mCompoundButtonHelper != null
132091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes                ? mCompoundButtonHelper.getSupportButtonTintMode()
133091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes                : null;
134091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes    }
135415f740df4981ef2f5fb462a50c7cf095cc21128Chris Banes}
136