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