The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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.widget; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.DrawableRes; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 24 | import android.content.res.ColorStateList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.res.TypedArray; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 26 | import android.graphics.BlendMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 28 | import android.graphics.PorterDuff; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Aurimas Liutikas | e3fa176 | 2015-11-17 14:28:04 -0800 | [diff] [blame] | 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.util.AttributeSet; |
| 33 | import android.view.Gravity; |
Alan Viverette | 2356c5e | 2014-05-22 22:43:59 -0700 | [diff] [blame] | 34 | import android.view.RemotableViewMethod; |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 35 | import android.view.ViewDebug; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 36 | import android.view.ViewHierarchyEncoder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 34ffaab | 2011-09-12 16:19:32 -0700 | [diff] [blame] | 38 | import android.view.accessibility.AccessibilityNodeInfo; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 39 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 41 | import com.android.internal.R; |
| 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 44 | * An extension to {@link TextView} that supports the {@link Checkable} |
| 45 | * interface and displays. |
| 46 | * <p> |
| 47 | * This is useful when used in a {@link android.widget.ListView ListView} where |
| 48 | * the {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has |
| 49 | * been set to something other than |
| 50 | * {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | * |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 52 | * @attr ref android.R.styleable#CheckedTextView_checked |
| 53 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | */ |
Romain Guy | 8b5e7c0 | 2009-04-29 11:48:22 -0700 | [diff] [blame] | 55 | public class CheckedTextView extends TextView implements Checkable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | private boolean mChecked; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | private int mCheckMarkResource; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 59 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | private Drawable mCheckMarkDrawable; |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 61 | private ColorStateList mCheckMarkTintList = null; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 62 | private BlendMode mCheckMarkBlendMode = null; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 63 | private boolean mHasCheckMarkTint = false; |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 64 | private boolean mHasCheckMarkTintMode = false; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 65 | |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 66 | private int mBasePadding; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | private int mCheckMarkWidth; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 68 | @UnsupportedAppUsage |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 69 | private int mCheckMarkGravity = Gravity.END; |
| 70 | |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 71 | private boolean mNeedRequestlayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
| 73 | private static final int[] CHECKED_STATE_SET = { |
| 74 | R.attr.state_checked |
| 75 | }; |
| 76 | |
| 77 | public CheckedTextView(Context context) { |
| 78 | this(context, null); |
| 79 | } |
| 80 | |
| 81 | public CheckedTextView(Context context, AttributeSet attrs) { |
Fabrice Di Meglio | b023a58 | 2012-09-30 15:31:06 -0700 | [diff] [blame] | 82 | this(context, attrs, R.attr.checkedTextViewStyle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 85 | public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 86 | this(context, attrs, defStyleAttr, 0); |
| 87 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 89 | public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 90 | super(context, attrs, defStyleAttr, defStyleRes); |
| 91 | |
| 92 | final TypedArray a = context.obtainStyledAttributes( |
| 93 | attrs, R.styleable.CheckedTextView, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 94 | saveAttributeDataForStyleable(context, R.styleable.CheckedTextView, |
| 95 | attrs, a, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 97 | final Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | if (d != null) { |
| 99 | setCheckMarkDrawable(d); |
| 100 | } |
| 101 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 102 | if (a.hasValue(R.styleable.CheckedTextView_checkMarkTintMode)) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 103 | mCheckMarkBlendMode = Drawable.parseBlendMode(a.getInt( |
| 104 | R.styleable.CheckedTextView_checkMarkTintMode, -1), |
| 105 | mCheckMarkBlendMode); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 106 | mHasCheckMarkTintMode = true; |
| 107 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 108 | |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 109 | if (a.hasValue(R.styleable.CheckedTextView_checkMarkTint)) { |
| 110 | mCheckMarkTintList = a.getColorStateList(R.styleable.CheckedTextView_checkMarkTint); |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 111 | mHasCheckMarkTint = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 114 | mCheckMarkGravity = a.getInt(R.styleable.CheckedTextView_checkMarkGravity, Gravity.END); |
| 115 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 116 | final boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | setChecked(checked); |
| 118 | |
| 119 | a.recycle(); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 120 | |
| 121 | applyCheckMarkTint(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public void toggle() { |
| 125 | setChecked(!mChecked); |
| 126 | } |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 127 | |
| 128 | @ViewDebug.ExportedProperty |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 129 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | public boolean isChecked() { |
| 131 | return mChecked; |
| 132 | } |
| 133 | |
| 134 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 135 | * Sets the checked state of this view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 137 | * @param checked {@code true} set the state to checked, {@code false} to |
| 138 | * uncheck |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | */ |
| 140 | public void setChecked(boolean checked) { |
| 141 | if (mChecked != checked) { |
| 142 | mChecked = checked; |
| 143 | refreshDrawableState(); |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 144 | notifyViewAccessibilityStateChangedIfNeeded( |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 145 | AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 150 | * Sets the check mark to the drawable with the specified resource ID. |
| 151 | * <p> |
| 152 | * When this view is checked, the drawable's state set will include |
| 153 | * {@link android.R.attr#state_checked}. |
Fabrice Di Meglio | 343e113 | 2012-09-28 18:01:17 -0700 | [diff] [blame] | 154 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 155 | * @param resId the resource identifier of drawable to use as the check |
| 156 | * mark |
| 157 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 158 | * @see #setCheckMarkDrawable(Drawable) |
| 159 | * @see #getCheckMarkDrawable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 161 | public void setCheckMarkDrawable(@DrawableRes int resId) { |
| 162 | if (resId != 0 && resId == mCheckMarkResource) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Alan Viverette | c60feca | 2015-08-12 16:26:37 -0400 | [diff] [blame] | 166 | final Drawable d = resId != 0 ? getContext().getDrawable(resId) : null; |
| 167 | setCheckMarkDrawableInternal(d, resId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 171 | * Set the check mark to the specified drawable. |
| 172 | * <p> |
| 173 | * When this view is checked, the drawable's state set will include |
| 174 | * {@link android.R.attr#state_checked}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 176 | * @param d the drawable to use for the check mark |
| 177 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 178 | * @see #setCheckMarkDrawable(int) |
| 179 | * @see #getCheckMarkDrawable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | */ |
Alan Viverette | c60feca | 2015-08-12 16:26:37 -0400 | [diff] [blame] | 181 | public void setCheckMarkDrawable(@Nullable Drawable d) { |
| 182 | setCheckMarkDrawableInternal(d, 0); |
| 183 | } |
| 184 | |
| 185 | private void setCheckMarkDrawableInternal(@Nullable Drawable d, @DrawableRes int resId) { |
Leon Scroggins | a8da173 | 2009-10-19 19:04:30 -0400 | [diff] [blame] | 186 | if (mCheckMarkDrawable != null) { |
| 187 | mCheckMarkDrawable.setCallback(null); |
| 188 | unscheduleDrawable(mCheckMarkDrawable); |
| 189 | } |
Alan Viverette | c60feca | 2015-08-12 16:26:37 -0400 | [diff] [blame] | 190 | |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 191 | mNeedRequestlayout = (d != mCheckMarkDrawable); |
Alan Viverette | c60feca | 2015-08-12 16:26:37 -0400 | [diff] [blame] | 192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | if (d != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | d.setCallback(this); |
| 195 | d.setVisible(getVisibility() == VISIBLE, false); |
| 196 | d.setState(CHECKED_STATE_SET); |
Fabrice Di Meglio | 343e113 | 2012-09-28 18:01:17 -0700 | [diff] [blame] | 197 | |
Alan Viverette | 28fabe5 | 2016-04-25 13:15:39 -0400 | [diff] [blame] | 198 | // Record the intrinsic dimensions when in "checked" state. |
| 199 | setMinHeight(d.getIntrinsicHeight()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | mCheckMarkWidth = d.getIntrinsicWidth(); |
Alan Viverette | 28fabe5 | 2016-04-25 13:15:39 -0400 | [diff] [blame] | 201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | d.setState(getDrawableState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } else { |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 204 | mCheckMarkWidth = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } |
Alan Viverette | c60feca | 2015-08-12 16:26:37 -0400 | [diff] [blame] | 206 | |
Leon Scroggins | a8da173 | 2009-10-19 19:04:30 -0400 | [diff] [blame] | 207 | mCheckMarkDrawable = d; |
Alan Viverette | c60feca | 2015-08-12 16:26:37 -0400 | [diff] [blame] | 208 | mCheckMarkResource = resId; |
Alan Viverette | 2356c5e | 2014-05-22 22:43:59 -0700 | [diff] [blame] | 209 | |
Alan Viverette | 28fabe5 | 2016-04-25 13:15:39 -0400 | [diff] [blame] | 210 | applyCheckMarkTint(); |
| 211 | |
Alan Viverette | 2356c5e | 2014-05-22 22:43:59 -0700 | [diff] [blame] | 212 | // Do padding resolution. This will call internalSetPadding() and do a |
| 213 | // requestLayout() if needed. |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 214 | resolvePadding(); |
| 215 | } |
| 216 | |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Applies a tint to the check mark drawable. Does not modify the |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 219 | * current tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 220 | * <p> |
| 221 | * Subsequent calls to {@link #setCheckMarkDrawable(Drawable)} will |
| 222 | * automatically mutate the drawable and apply the specified tint and |
| 223 | * tint mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 224 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 225 | * |
| 226 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 227 | * |
| 228 | * @attr ref android.R.styleable#CheckedTextView_checkMarkTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 229 | * @see #getCheckMarkTintList() |
| 230 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 231 | */ |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 232 | public void setCheckMarkTintList(@Nullable ColorStateList tint) { |
| 233 | mCheckMarkTintList = tint; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 234 | mHasCheckMarkTint = true; |
| 235 | |
| 236 | applyCheckMarkTint(); |
| 237 | } |
| 238 | |
| 239 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 240 | * Returns the tint applied to the check mark drawable, if specified. |
| 241 | * |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 242 | * @return the tint applied to the check mark drawable |
| 243 | * @attr ref android.R.styleable#CheckedTextView_checkMarkTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 244 | * @see #setCheckMarkTintList(ColorStateList) |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 245 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 246 | @InspectableProperty(name = "checkMarkTint") |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 247 | @Nullable |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 248 | public ColorStateList getCheckMarkTintList() { |
| 249 | return mCheckMarkTintList; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 254 | * {@link #setCheckMarkTintList(ColorStateList)} to the check mark |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 255 | * drawable. The default mode is {@link PorterDuff.Mode#SRC_IN}. |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 256 | * |
| 257 | * @param tintMode the blending mode used to apply the tint, may be |
| 258 | * {@code null} to clear tint |
| 259 | * @attr ref android.R.styleable#CheckedTextView_checkMarkTintMode |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 260 | * @see #setCheckMarkTintList(ColorStateList) |
| 261 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 262 | */ |
| 263 | public void setCheckMarkTintMode(@Nullable PorterDuff.Mode tintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 264 | setCheckMarkTintBlendMode(tintMode != null |
| 265 | ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Specifies the blending mode used to apply the tint specified by |
| 270 | * {@link #setCheckMarkTintList(ColorStateList)} to the check mark |
| 271 | * drawable. The default mode is {@link PorterDuff.Mode#SRC_IN}. |
| 272 | * |
| 273 | * @param tintMode the blending mode used to apply the tint, may be |
| 274 | * {@code null} to clear tint |
| 275 | * @attr ref android.R.styleable#CheckedTextView_checkMarkTintMode |
| 276 | * @see #setCheckMarkTintList(ColorStateList) |
| 277 | * @see Drawable#setTintBlendMode(BlendMode) |
| 278 | */ |
| 279 | public void setCheckMarkTintBlendMode(@Nullable BlendMode tintMode) { |
| 280 | mCheckMarkBlendMode = tintMode; |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 281 | mHasCheckMarkTintMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 282 | |
| 283 | applyCheckMarkTint(); |
| 284 | } |
| 285 | |
| 286 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 287 | * Returns the blending mode used to apply the tint to the check mark |
| 288 | * drawable, if specified. |
| 289 | * |
| 290 | * @return the blending mode used to apply the tint to the check mark |
| 291 | * drawable |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 292 | * @attr ref android.R.styleable#CheckedTextView_checkMarkTintMode |
| 293 | * @see #setCheckMarkTintMode(PorterDuff.Mode) |
| 294 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 295 | @InspectableProperty |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 296 | @Nullable |
| 297 | public PorterDuff.Mode getCheckMarkTintMode() { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 298 | return mCheckMarkBlendMode != null |
| 299 | ? BlendMode.blendModeToPorterDuffMode(mCheckMarkBlendMode) : null; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Returns the blending mode used to apply the tint to the check mark |
| 304 | * drawable, if specified. |
| 305 | * |
| 306 | * @return the blending mode used to apply the tint to the check mark |
| 307 | * drawable |
| 308 | * @attr ref android.R.styleable#CheckedTextView_checkMarkTintMode |
| 309 | * @see #setCheckMarkTintMode(PorterDuff.Mode) |
| 310 | */ |
| 311 | @InspectableProperty(attributeId = android.R.styleable.CheckedTextView_checkMarkTintMode) |
| 312 | @Nullable |
| 313 | public BlendMode getCheckMarkTintBlendMode() { |
| 314 | return mCheckMarkBlendMode; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | private void applyCheckMarkTint() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 318 | if (mCheckMarkDrawable != null && (mHasCheckMarkTint || mHasCheckMarkTintMode)) { |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 319 | mCheckMarkDrawable = mCheckMarkDrawable.mutate(); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 320 | |
| 321 | if (mHasCheckMarkTint) { |
| 322 | mCheckMarkDrawable.setTintList(mCheckMarkTintList); |
| 323 | } |
| 324 | |
| 325 | if (mHasCheckMarkTintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 326 | mCheckMarkDrawable.setTintBlendMode(mCheckMarkBlendMode); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 327 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 328 | |
| 329 | // The drawable (or one of its children) may not have been |
| 330 | // stateful before applying the tint, so let's try again. |
| 331 | if (mCheckMarkDrawable.isStateful()) { |
| 332 | mCheckMarkDrawable.setState(getDrawableState()); |
| 333 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Alan Viverette | 2356c5e | 2014-05-22 22:43:59 -0700 | [diff] [blame] | 337 | @RemotableViewMethod |
| 338 | @Override |
| 339 | public void setVisibility(int visibility) { |
| 340 | super.setVisibility(visibility); |
| 341 | |
| 342 | if (mCheckMarkDrawable != null) { |
| 343 | mCheckMarkDrawable.setVisible(visibility == VISIBLE, false); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void jumpDrawablesToCurrentState() { |
| 349 | super.jumpDrawablesToCurrentState(); |
| 350 | |
| 351 | if (mCheckMarkDrawable != null) { |
| 352 | mCheckMarkDrawable.jumpToCurrentState(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | @Override |
Alan Viverette | f6d87ec | 2016-03-11 10:09:14 -0500 | [diff] [blame] | 357 | protected boolean verifyDrawable(@NonNull Drawable who) { |
Alan Viverette | 2356c5e | 2014-05-22 22:43:59 -0700 | [diff] [blame] | 358 | return who == mCheckMarkDrawable || super.verifyDrawable(who); |
| 359 | } |
| 360 | |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Gets the checkmark drawable |
| 363 | * |
| 364 | * @return The drawable use to represent the checkmark, if any. |
| 365 | * |
| 366 | * @see #setCheckMarkDrawable(Drawable) |
| 367 | * @see #setCheckMarkDrawable(int) |
| 368 | * |
| 369 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
| 370 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 371 | @InspectableProperty(name = "checkMark") |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 372 | public Drawable getCheckMarkDrawable() { |
| 373 | return mCheckMarkDrawable; |
| 374 | } |
| 375 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 376 | /** |
| 377 | * @hide |
| 378 | */ |
| 379 | @Override |
| 380 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
| 381 | super.internalSetPadding(left, top, right, bottom); |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 382 | setBasePadding(isCheckMarkAtStart()); |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 385 | @Override |
Fabrice Di Meglio | 343e113 | 2012-09-28 18:01:17 -0700 | [diff] [blame] | 386 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 387 | super.onRtlPropertiesChanged(layoutDirection); |
Fabrice Di Meglio | 15bbde4 | 2012-09-28 15:49:38 -0700 | [diff] [blame] | 388 | updatePadding(); |
| 389 | } |
| 390 | |
| 391 | private void updatePadding() { |
Fabrice Di Meglio | 47fb191 | 2012-09-28 19:50:18 -0700 | [diff] [blame] | 392 | resetPaddingToInitialValues(); |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 393 | int newPadding = (mCheckMarkDrawable != null) ? |
| 394 | mCheckMarkWidth + mBasePadding : mBasePadding; |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 395 | if (isCheckMarkAtStart()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 396 | mNeedRequestlayout |= (mPaddingLeft != newPadding); |
Fabrice Di Meglio | e2386c1 | 2012-07-26 15:17:16 -0700 | [diff] [blame] | 397 | mPaddingLeft = newPadding; |
| 398 | } else { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 399 | mNeedRequestlayout |= (mPaddingRight != newPadding); |
Fabrice Di Meglio | e2386c1 | 2012-07-26 15:17:16 -0700 | [diff] [blame] | 400 | mPaddingRight = newPadding; |
| 401 | } |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 402 | if (mNeedRequestlayout) { |
| 403 | requestLayout(); |
| 404 | mNeedRequestlayout = false; |
| 405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
Fabrice Di Meglio | 15bbde4 | 2012-09-28 15:49:38 -0700 | [diff] [blame] | 407 | |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 408 | private void setBasePadding(boolean checkmarkAtStart) { |
| 409 | if (checkmarkAtStart) { |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 410 | mBasePadding = mPaddingLeft; |
| 411 | } else { |
| 412 | mBasePadding = mPaddingRight; |
| 413 | } |
Fabrice Di Meglio | bf923eb | 2012-03-07 16:20:22 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 416 | private boolean isCheckMarkAtStart() { |
| 417 | final int gravity = Gravity.getAbsoluteGravity(mCheckMarkGravity, getLayoutDirection()); |
| 418 | final int hgrav = gravity & Gravity.HORIZONTAL_GRAVITY_MASK; |
| 419 | return hgrav == Gravity.LEFT; |
| 420 | } |
| 421 | |
Fabrice Di Meglio | bf923eb | 2012-03-07 16:20:22 -0800 | [diff] [blame] | 422 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | protected void onDraw(Canvas canvas) { |
| 424 | super.onDraw(canvas); |
| 425 | |
| 426 | final Drawable checkMarkDrawable = mCheckMarkDrawable; |
| 427 | if (checkMarkDrawable != null) { |
| 428 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
| 429 | final int height = checkMarkDrawable.getIntrinsicHeight(); |
| 430 | |
| 431 | int y = 0; |
| 432 | |
| 433 | switch (verticalGravity) { |
| 434 | case Gravity.BOTTOM: |
| 435 | y = getHeight() - height; |
| 436 | break; |
| 437 | case Gravity.CENTER_VERTICAL: |
| 438 | y = (getHeight() - height) / 2; |
| 439 | break; |
| 440 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 441 | |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 442 | final boolean checkMarkAtStart = isCheckMarkAtStart(); |
Fabrice Di Meglio | e2386c1 | 2012-07-26 15:17:16 -0700 | [diff] [blame] | 443 | final int width = getWidth(); |
| 444 | final int top = y; |
| 445 | final int bottom = top + height; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 446 | final int left; |
| 447 | final int right; |
Adam Powell | 16f2b90 | 2014-08-11 17:30:19 -0700 | [diff] [blame] | 448 | if (checkMarkAtStart) { |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 449 | left = mBasePadding; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 450 | right = left + mCheckMarkWidth; |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 451 | } else { |
| 452 | right = width - mBasePadding; |
| 453 | left = right - mCheckMarkWidth; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 454 | } |
Jorn Jacobsson | 78cdc55 | 2013-01-31 17:52:20 +0100 | [diff] [blame] | 455 | checkMarkDrawable.setBounds(mScrollX + left, top, mScrollX + right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | checkMarkDrawable.draw(canvas); |
Alan Viverette | 2356c5e | 2014-05-22 22:43:59 -0700 | [diff] [blame] | 457 | |
| 458 | final Drawable background = getBackground(); |
| 459 | if (background != null) { |
| 460 | background.setHotspotBounds(mScrollX + left, top, mScrollX + right, bottom); |
| 461 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 464 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | @Override |
| 466 | protected int[] onCreateDrawableState(int extraSpace) { |
| 467 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 468 | if (isChecked()) { |
| 469 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 470 | } |
| 471 | return drawableState; |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | protected void drawableStateChanged() { |
| 476 | super.drawableStateChanged(); |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 477 | |
| 478 | final Drawable checkMarkDrawable = mCheckMarkDrawable; |
| 479 | if (checkMarkDrawable != null && checkMarkDrawable.isStateful() |
| 480 | && checkMarkDrawable.setState(getDrawableState())) { |
| 481 | invalidateDrawable(checkMarkDrawable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 484 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 485 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 486 | public void drawableHotspotChanged(float x, float y) { |
| 487 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 488 | |
| 489 | if (mCheckMarkDrawable != null) { |
| 490 | mCheckMarkDrawable.setHotspot(x, y); |
| 491 | } |
| 492 | } |
| 493 | |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 494 | @Override |
| 495 | public CharSequence getAccessibilityClassName() { |
| 496 | return CheckedTextView.class.getName(); |
| 497 | } |
| 498 | |
Aurimas Liutikas | e3fa176 | 2015-11-17 14:28:04 -0800 | [diff] [blame] | 499 | static class SavedState extends BaseSavedState { |
| 500 | boolean checked; |
| 501 | |
| 502 | /** |
| 503 | * Constructor called from {@link CheckedTextView#onSaveInstanceState()} |
| 504 | */ |
| 505 | SavedState(Parcelable superState) { |
| 506 | super(superState); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Constructor called from {@link #CREATOR} |
| 511 | */ |
| 512 | private SavedState(Parcel in) { |
| 513 | super(in); |
| 514 | checked = (Boolean)in.readValue(null); |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void writeToParcel(Parcel out, int flags) { |
| 519 | super.writeToParcel(out, flags); |
| 520 | out.writeValue(checked); |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public String toString() { |
| 525 | return "CheckedTextView.SavedState{" |
| 526 | + Integer.toHexString(System.identityHashCode(this)) |
| 527 | + " checked=" + checked + "}"; |
| 528 | } |
| 529 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 530 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR |
Aurimas Liutikas | e3fa176 | 2015-11-17 14:28:04 -0800 | [diff] [blame] | 531 | = new Parcelable.Creator<SavedState>() { |
| 532 | public SavedState createFromParcel(Parcel in) { |
| 533 | return new SavedState(in); |
| 534 | } |
| 535 | |
| 536 | public SavedState[] newArray(int size) { |
| 537 | return new SavedState[size]; |
| 538 | } |
| 539 | }; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public Parcelable onSaveInstanceState() { |
| 544 | Parcelable superState = super.onSaveInstanceState(); |
| 545 | |
| 546 | SavedState ss = new SavedState(superState); |
| 547 | |
| 548 | ss.checked = isChecked(); |
| 549 | return ss; |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public void onRestoreInstanceState(Parcelable state) { |
| 554 | SavedState ss = (SavedState) state; |
| 555 | |
| 556 | super.onRestoreInstanceState(ss.getSuperState()); |
| 557 | setChecked(ss.checked); |
| 558 | requestLayout(); |
| 559 | } |
| 560 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 561 | /** @hide */ |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 562 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 563 | public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 564 | super.onInitializeAccessibilityEventInternal(event); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 565 | event.setChecked(mChecked); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 566 | } |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 567 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 568 | /** @hide */ |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 569 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 570 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 571 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svetoslav Ganov | c85015c | 2012-04-30 11:37:43 -0700 | [diff] [blame] | 572 | info.setCheckable(true); |
Svetoslav Ganov | 34ffaab | 2011-09-12 16:19:32 -0700 | [diff] [blame] | 573 | info.setChecked(mChecked); |
| 574 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 575 | |
| 576 | /** @hide */ |
| 577 | @Override |
| 578 | protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 579 | super.encodeProperties(stream); |
| 580 | stream.addProperty("text:checked", isChecked()); |
| 581 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } |