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 | |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 19 | import android.annotation.DrawableRes; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Artur Satayev | ed5a6ae | 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 | 9117436 | 2014-06-17 14:51:45 -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; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -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; |
| 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
| 32 | import android.util.AttributeSet; |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 33 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.view.Gravity; |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 35 | import android.view.SoundEffectConstants; |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 36 | import android.view.ViewDebug; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 37 | import android.view.ViewHierarchyEncoder; |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 38 | import android.view.ViewStructure; |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 39 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 40 | import android.view.accessibility.AccessibilityNodeInfo; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 41 | import android.view.autofill.AutofillManager; |
| 42 | import android.view.autofill.AutofillValue; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 43 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 45 | import com.android.internal.R; |
| 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | /** |
| 48 | * <p> |
| 49 | * A button with two states, checked and unchecked. When the button is pressed |
| 50 | * or clicked, the state changes automatically. |
| 51 | * </p> |
| 52 | * |
| 53 | * <p><strong>XML attributes</strong></p> |
| 54 | * <p> |
| 55 | * See {@link android.R.styleable#CompoundButton |
| 56 | * CompoundButton Attributes}, {@link android.R.styleable#Button Button |
| 57 | * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link |
| 58 | * android.R.styleable#View View Attributes} |
| 59 | * </p> |
| 60 | */ |
| 61 | public abstract class CompoundButton extends Button implements Checkable { |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 62 | private static final String LOG_TAG = CompoundButton.class.getSimpleName(); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | private boolean mChecked; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 65 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | private boolean mBroadcasting; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 67 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 68 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | private Drawable mButtonDrawable; |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 70 | private ColorStateList mButtonTintList = null; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 71 | private BlendMode mButtonBlendMode = null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 72 | private boolean mHasButtonTint = false; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 73 | private boolean mHasButtonBlendMode = false; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 74 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 75 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | private OnCheckedChangeListener mOnCheckedChangeListener; |
| 77 | private OnCheckedChangeListener mOnCheckedChangeWidgetListener; |
| 78 | |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 79 | // Indicates whether the toggle state was set from resources or dynamically, so it can be used |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 80 | // to sanitize autofill requests. |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 81 | private boolean mCheckedFromResource = false; |
| 82 | |
yingleiw | 473fc12 | 2019-10-11 15:10:55 -0700 | [diff] [blame] | 83 | private CharSequence mCustomStateDescription = null; |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private static final int[] CHECKED_STATE_SET = { |
| 86 | R.attr.state_checked |
| 87 | }; |
| 88 | |
| 89 | public CompoundButton(Context context) { |
| 90 | this(context, null); |
| 91 | } |
| 92 | |
| 93 | public CompoundButton(Context context, AttributeSet attrs) { |
| 94 | this(context, attrs, 0); |
| 95 | } |
| 96 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 97 | public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 98 | this(context, attrs, defStyleAttr, 0); |
| 99 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 101 | public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 102 | super(context, attrs, defStyleAttr, defStyleRes); |
| 103 | |
| 104 | final TypedArray a = context.obtainStyledAttributes( |
| 105 | attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 106 | saveAttributeDataForStyleable(context, com.android.internal.R.styleable.CompoundButton, |
| 107 | attrs, a, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 109 | final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | if (d != null) { |
| 111 | setButtonDrawable(d); |
| 112 | } |
| 113 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 114 | if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 115 | mButtonBlendMode = Drawable.parseBlendMode(a.getInt( |
| 116 | R.styleable.CompoundButton_buttonTintMode, -1), mButtonBlendMode); |
| 117 | mHasButtonBlendMode = true; |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 118 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 119 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 120 | if (a.hasValue(R.styleable.CompoundButton_buttonTint)) { |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 121 | mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 122 | mHasButtonTint = true; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | final boolean checked = a.getBoolean( |
| 126 | com.android.internal.R.styleable.CompoundButton_checked, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | setChecked(checked); |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 128 | mCheckedFromResource = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
| 130 | a.recycle(); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 131 | |
| 132 | applyButtonTint(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 135 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | public void toggle() { |
| 137 | setChecked(!mChecked); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean performClick() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | toggle(); |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 143 | |
| 144 | final boolean handled = super.performClick(); |
| 145 | if (!handled) { |
| 146 | // View only makes a sound effect if the onClickListener was |
| 147 | // called, so we'll need to make one here instead. |
| 148 | playSoundEffect(SoundEffectConstants.CLICK); |
| 149 | } |
| 150 | |
| 151 | return handled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 154 | @InspectableProperty |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 155 | @ViewDebug.ExportedProperty |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 156 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | public boolean isChecked() { |
| 158 | return mChecked; |
| 159 | } |
| 160 | |
yingleiw | 473fc12 | 2019-10-11 15:10:55 -0700 | [diff] [blame] | 161 | /** @hide */ |
| 162 | @NonNull |
| 163 | protected CharSequence getButtonStateDescription() { |
| 164 | if (isChecked()) { |
| 165 | return getResources().getString(R.string.checked); |
| 166 | } else { |
| 167 | return getResources().getString(R.string.not_checked); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This function is called when an instance or subclass sets the state description. Once this |
| 173 | * is called and the argument is not null, the app developer will be responsible for updating |
| 174 | * state description when checked state changes and we will not set state description |
| 175 | * in {@link #setChecked}. App developers can restore the default behavior by setting the |
| 176 | * argument to null. If {@link #setChecked} is called first and then setStateDescription is |
| 177 | * called, two state change events will be merged by event throttling and we can still get |
| 178 | * the correct state description. |
| 179 | * |
| 180 | * @param stateDescription The state description. |
| 181 | */ |
| 182 | @Override |
| 183 | public void setStateDescription(@Nullable CharSequence stateDescription) { |
| 184 | mCustomStateDescription = stateDescription; |
| 185 | if (stateDescription == null) { |
| 186 | setDefaultStateDescritption(); |
| 187 | } else { |
| 188 | super.setStateDescription(stateDescription); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** @hide **/ |
| 193 | protected void setDefaultStateDescritption() { |
| 194 | if (mCustomStateDescription == null) { |
| 195 | super.setStateDescription(getButtonStateDescription()); |
| 196 | } |
| 197 | } |
| 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | /** |
| 200 | * <p>Changes the checked state of this button.</p> |
| 201 | * |
| 202 | * @param checked true to check the button, false to uncheck it |
| 203 | */ |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 204 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | public void setChecked(boolean checked) { |
| 206 | if (mChecked != checked) { |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 207 | mCheckedFromResource = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | mChecked = checked; |
| 209 | refreshDrawableState(); |
| 210 | |
| 211 | // Avoid infinite recursions if setChecked() is called from a listener |
| 212 | if (mBroadcasting) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | mBroadcasting = true; |
| 217 | if (mOnCheckedChangeListener != null) { |
| 218 | mOnCheckedChangeListener.onCheckedChanged(this, mChecked); |
| 219 | } |
| 220 | if (mOnCheckedChangeWidgetListener != null) { |
| 221 | mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked); |
| 222 | } |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 223 | final AutofillManager afm = mContext.getSystemService(AutofillManager.class); |
Felipe Leme | 5882c4f | 2017-02-16 21:46:46 -0800 | [diff] [blame] | 224 | if (afm != null) { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 225 | afm.notifyValueChanged(this); |
Felipe Leme | 5882c4f | 2017-02-16 21:46:46 -0800 | [diff] [blame] | 226 | } |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 227 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 228 | mBroadcasting = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
yingleiw | 473fc12 | 2019-10-11 15:10:55 -0700 | [diff] [blame] | 230 | // setStateDescription will not send out event if the description is unchanged. |
| 231 | setDefaultStateDescritption(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Register a callback to be invoked when the checked state of this button |
| 236 | * changes. |
| 237 | * |
| 238 | * @param listener the callback to call on checked state change |
| 239 | */ |
Jason Long | acdaaea50 | 2016-12-01 22:54:37 -0800 | [diff] [blame] | 240 | public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | mOnCheckedChangeListener = listener; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Register a callback to be invoked when the checked state of this button |
| 246 | * changes. This callback is used for internal purpose only. |
| 247 | * |
| 248 | * @param listener the callback to call on checked state change |
| 249 | * @hide |
| 250 | */ |
| 251 | void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) { |
| 252 | mOnCheckedChangeWidgetListener = listener; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Interface definition for a callback to be invoked when the checked state |
| 257 | * of a compound button changed. |
| 258 | */ |
| 259 | public static interface OnCheckedChangeListener { |
| 260 | /** |
| 261 | * Called when the checked state of a compound button has changed. |
| 262 | * |
| 263 | * @param buttonView The compound button view whose state has changed. |
| 264 | * @param isChecked The new checked state of buttonView. |
| 265 | */ |
| 266 | void onCheckedChanged(CompoundButton buttonView, boolean isChecked); |
| 267 | } |
| 268 | |
| 269 | /** |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 270 | * Sets a drawable as the compound button image given its resource |
| 271 | * identifier. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | * |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 273 | * @param resId the resource identifier of the drawable |
| 274 | * @attr ref android.R.styleable#CompoundButton_button |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | */ |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 276 | public void setButtonDrawable(@DrawableRes int resId) { |
| 277 | final Drawable d; |
| 278 | if (resId != 0) { |
| 279 | d = getContext().getDrawable(resId); |
| 280 | } else { |
| 281 | d = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | setButtonDrawable(d); |
| 284 | } |
| 285 | |
| 286 | /** |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 287 | * Sets a drawable as the compound button image. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | * |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 289 | * @param drawable the drawable to set |
| 290 | * @attr ref android.R.styleable#CompoundButton_button |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | */ |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 292 | public void setButtonDrawable(@Nullable Drawable drawable) { |
| 293 | if (mButtonDrawable != drawable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | if (mButtonDrawable != null) { |
| 295 | mButtonDrawable.setCallback(null); |
| 296 | unscheduleDrawable(mButtonDrawable); |
| 297 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 299 | mButtonDrawable = drawable; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 300 | |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 301 | if (drawable != null) { |
| 302 | drawable.setCallback(this); |
| 303 | drawable.setLayoutDirection(getLayoutDirection()); |
| 304 | if (drawable.isStateful()) { |
| 305 | drawable.setState(getDrawableState()); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 306 | } |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 307 | drawable.setVisible(getVisibility() == VISIBLE, false); |
| 308 | setMinHeight(drawable.getIntrinsicHeight()); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 309 | applyButtonTint(); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
Doris Liu | 3380e69 | 2015-06-30 11:26:47 -0700 | [diff] [blame] | 315 | * @hide |
| 316 | */ |
| 317 | @Override |
| 318 | public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) { |
| 319 | super.onResolveDrawables(layoutDirection); |
| 320 | if (mButtonDrawable != null) { |
| 321 | mButtonDrawable.setLayoutDirection(layoutDirection); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 326 | * @return the drawable used as the compound button image |
| 327 | * @see #setButtonDrawable(Drawable) |
| 328 | * @see #setButtonDrawable(int) |
| 329 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 330 | @InspectableProperty(name = "button") |
Alan Viverette | 6a394f4 | 2015-02-12 15:03:22 -0800 | [diff] [blame] | 331 | @Nullable |
| 332 | public Drawable getButtonDrawable() { |
| 333 | return mButtonDrawable; |
| 334 | } |
| 335 | |
| 336 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 337 | * Applies a tint to the button drawable. Does not modify the current tint |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 338 | * mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 339 | * <p> |
| 340 | * Subsequent calls to {@link #setButtonDrawable(Drawable)} will |
| 341 | * automatically mutate the drawable and apply the specified tint and tint |
| 342 | * mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 343 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 344 | * |
| 345 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 346 | * |
| 347 | * @attr ref android.R.styleable#CompoundButton_buttonTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 348 | * @see #setButtonTintList(ColorStateList) |
| 349 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 350 | */ |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 351 | public void setButtonTintList(@Nullable ColorStateList tint) { |
| 352 | mButtonTintList = tint; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 353 | mHasButtonTint = true; |
| 354 | |
| 355 | applyButtonTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @return the tint applied to the button drawable |
| 360 | * @attr ref android.R.styleable#CompoundButton_buttonTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 361 | * @see #setButtonTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 362 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 363 | @InspectableProperty(name = "buttonTint") |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 364 | @Nullable |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 365 | public ColorStateList getButtonTintList() { |
| 366 | return mButtonTintList; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 371 | * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 372 | * default mode is {@link PorterDuff.Mode#SRC_IN}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 373 | * |
| 374 | * @param tintMode the blending mode used to apply the tint, may be |
| 375 | * {@code null} to clear tint |
| 376 | * @attr ref android.R.styleable#CompoundButton_buttonTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 377 | * @see #getButtonTintMode() |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 378 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 379 | */ |
| 380 | public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 381 | setButtonTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Specifies the blending mode used to apply the tint specified by |
| 386 | * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The |
| 387 | * default mode is {@link PorterDuff.Mode#SRC_IN}. |
| 388 | * |
| 389 | * @param tintMode the blending mode used to apply the tint, may be |
| 390 | * {@code null} to clear tint |
| 391 | * @attr ref android.R.styleable#CompoundButton_buttonTintMode |
| 392 | * @see #getButtonTintMode() |
| 393 | * @see Drawable#setTintBlendMode(BlendMode) |
| 394 | */ |
| 395 | public void setButtonTintBlendMode(@Nullable BlendMode tintMode) { |
| 396 | mButtonBlendMode = tintMode; |
| 397 | mHasButtonBlendMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 398 | |
| 399 | applyButtonTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @return the blending mode used to apply the tint to the button drawable |
| 404 | * @attr ref android.R.styleable#CompoundButton_buttonTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 405 | * @see #setButtonTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 406 | */ |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 407 | @InspectableProperty(name = "buttonTintMode") |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 408 | @Nullable |
| 409 | public PorterDuff.Mode getButtonTintMode() { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 410 | return mButtonBlendMode != null ? BlendMode.blendModeToPorterDuffMode(mButtonBlendMode) : |
| 411 | null; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @return the blending mode used to apply the tint to the button drawable |
| 416 | * @attr ref android.R.styleable#CompoundButton_buttonTintMode |
| 417 | * @see #setButtonTintBlendMode(BlendMode) |
| 418 | */ |
| 419 | @InspectableProperty(name = "buttonBlendMode", |
| 420 | attributeId = R.styleable.CompoundButton_buttonTintMode) |
| 421 | @Nullable |
| 422 | public BlendMode getButtonTintBlendMode() { |
| 423 | return mButtonBlendMode; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | private void applyButtonTint() { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 427 | if (mButtonDrawable != null && (mHasButtonTint || mHasButtonBlendMode)) { |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 428 | mButtonDrawable = mButtonDrawable.mutate(); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 429 | |
| 430 | if (mHasButtonTint) { |
| 431 | mButtonDrawable.setTintList(mButtonTintList); |
| 432 | } |
| 433 | |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 434 | if (mHasButtonBlendMode) { |
| 435 | mButtonDrawable.setTintBlendMode(mButtonBlendMode); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 436 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 437 | |
| 438 | // The drawable (or one of its children) may not have been |
| 439 | // stateful before applying the tint, so let's try again. |
| 440 | if (mButtonDrawable.isStateful()) { |
| 441 | mButtonDrawable.setState(getDrawableState()); |
| 442 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 443 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 446 | @Override |
| 447 | public CharSequence getAccessibilityClassName() { |
| 448 | return CompoundButton.class.getName(); |
| 449 | } |
| 450 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 451 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 453 | public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 454 | super.onInitializeAccessibilityEventInternal(event); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 455 | event.setChecked(mChecked); |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 458 | /** @hide */ |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 459 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 460 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 461 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svetoslav Ganov | 0f55cc3 | 2011-07-17 10:51:49 -0700 | [diff] [blame] | 462 | info.setCheckable(true); |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 463 | info.setChecked(mChecked); |
| 464 | } |
| 465 | |
| 466 | @Override |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 467 | public int getCompoundPaddingLeft() { |
| 468 | int padding = super.getCompoundPaddingLeft(); |
| 469 | if (!isLayoutRtl()) { |
| 470 | final Drawable buttonDrawable = mButtonDrawable; |
| 471 | if (buttonDrawable != null) { |
| 472 | padding += buttonDrawable.getIntrinsicWidth(); |
| 473 | } |
| 474 | } |
| 475 | return padding; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public int getCompoundPaddingRight() { |
| 480 | int padding = super.getCompoundPaddingRight(); |
| 481 | if (isLayoutRtl()) { |
| 482 | final Drawable buttonDrawable = mButtonDrawable; |
| 483 | if (buttonDrawable != null) { |
| 484 | padding += buttonDrawable.getIntrinsicWidth(); |
| 485 | } |
| 486 | } |
| 487 | return padding; |
| 488 | } |
| 489 | |
Fabrice Di Meglio | b878ddb | 2012-11-27 17:44:33 -0800 | [diff] [blame] | 490 | /** |
| 491 | * @hide |
| 492 | */ |
| 493 | @Override |
| 494 | public int getHorizontalOffsetForDrawables() { |
| 495 | final Drawable buttonDrawable = mButtonDrawable; |
| 496 | return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0; |
| 497 | } |
| 498 | |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 499 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | protected void onDraw(Canvas canvas) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | final Drawable buttonDrawable = mButtonDrawable; |
| 502 | if (buttonDrawable != null) { |
| 503 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 504 | final int drawableHeight = buttonDrawable.getIntrinsicHeight(); |
| 505 | final int drawableWidth = buttonDrawable.getIntrinsicWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 507 | final int top; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | switch (verticalGravity) { |
| 509 | case Gravity.BOTTOM: |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 510 | top = getHeight() - drawableHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | break; |
| 512 | case Gravity.CENTER_VERTICAL: |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 513 | top = (getHeight() - drawableHeight) / 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | break; |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 515 | default: |
| 516 | top = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 518 | final int bottom = top + drawableHeight; |
| 519 | final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0; |
| 520 | final int right = isLayoutRtl() ? getWidth() : drawableWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 522 | buttonDrawable.setBounds(left, top, right, bottom); |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 523 | |
| 524 | final Drawable background = getBackground(); |
Alan Viverette | c80ad99 | 2014-05-19 15:46:17 -0700 | [diff] [blame] | 525 | if (background != null) { |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 526 | background.setHotspotBounds(left, top, right, bottom); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | super.onDraw(canvas); |
| 531 | |
| 532 | if (buttonDrawable != null) { |
Alan Viverette | b95c336 | 2014-10-17 17:19:12 -0700 | [diff] [blame] | 533 | final int scrollX = mScrollX; |
| 534 | final int scrollY = mScrollY; |
| 535 | if (scrollX == 0 && scrollY == 0) { |
| 536 | buttonDrawable.draw(canvas); |
| 537 | } else { |
| 538 | canvas.translate(scrollX, scrollY); |
| 539 | buttonDrawable.draw(canvas); |
| 540 | canvas.translate(-scrollX, -scrollY); |
| 541 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | protected int[] onCreateDrawableState(int extraSpace) { |
| 547 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 548 | if (isChecked()) { |
| 549 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 550 | } |
| 551 | return drawableState; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | protected void drawableStateChanged() { |
| 556 | super.drawableStateChanged(); |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 557 | |
| 558 | final Drawable buttonDrawable = mButtonDrawable; |
| 559 | if (buttonDrawable != null && buttonDrawable.isStateful() |
| 560 | && buttonDrawable.setState(getDrawableState())) { |
| 561 | invalidateDrawable(buttonDrawable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 565 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 566 | public void drawableHotspotChanged(float x, float y) { |
| 567 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 568 | |
| 569 | if (mButtonDrawable != null) { |
| 570 | mButtonDrawable.setHotspot(x, y); |
| 571 | } |
| 572 | } |
| 573 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | @Override |
Alan Viverette | f6d87ec | 2016-03-11 10:09:14 -0500 | [diff] [blame] | 575 | protected boolean verifyDrawable(@NonNull Drawable who) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | return super.verifyDrawable(who) || who == mButtonDrawable; |
| 577 | } |
| 578 | |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 579 | @Override |
| 580 | public void jumpDrawablesToCurrentState() { |
| 581 | super.jumpDrawablesToCurrentState(); |
| 582 | if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState(); |
| 583 | } |
| 584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | static class SavedState extends BaseSavedState { |
| 586 | boolean checked; |
| 587 | |
| 588 | /** |
| 589 | * Constructor called from {@link CompoundButton#onSaveInstanceState()} |
| 590 | */ |
| 591 | SavedState(Parcelable superState) { |
| 592 | super(superState); |
| 593 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
| 596 | * Constructor called from {@link #CREATOR} |
| 597 | */ |
| 598 | private SavedState(Parcel in) { |
| 599 | super(in); |
| 600 | checked = (Boolean)in.readValue(null); |
| 601 | } |
| 602 | |
| 603 | @Override |
| 604 | public void writeToParcel(Parcel out, int flags) { |
| 605 | super.writeToParcel(out, flags); |
| 606 | out.writeValue(checked); |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public String toString() { |
| 611 | return "CompoundButton.SavedState{" |
| 612 | + Integer.toHexString(System.identityHashCode(this)) |
| 613 | + " checked=" + checked + "}"; |
| 614 | } |
| 615 | |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 616 | @SuppressWarnings("hiding") |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 617 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 618 | new Parcelable.Creator<SavedState>() { |
| 619 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | public SavedState createFromParcel(Parcel in) { |
| 621 | return new SavedState(in); |
| 622 | } |
| 623 | |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 624 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | public SavedState[] newArray(int size) { |
| 626 | return new SavedState[size]; |
| 627 | } |
| 628 | }; |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public Parcelable onSaveInstanceState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | Parcelable superState = super.onSaveInstanceState(); |
| 634 | |
| 635 | SavedState ss = new SavedState(superState); |
| 636 | |
| 637 | ss.checked = isChecked(); |
| 638 | return ss; |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void onRestoreInstanceState(Parcelable state) { |
| 643 | SavedState ss = (SavedState) state; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 644 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | super.onRestoreInstanceState(ss.getSuperState()); |
| 646 | setChecked(ss.checked); |
| 647 | requestLayout(); |
| 648 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 649 | |
| 650 | /** @hide */ |
| 651 | @Override |
| 652 | protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 653 | super.encodeProperties(stream); |
| 654 | stream.addProperty("checked", isChecked()); |
| 655 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 656 | |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 657 | |
Felipe Leme | 92736c1 | 2018-11-13 12:00:59 -0800 | [diff] [blame] | 658 | /** @hide */ |
| 659 | @Override |
| 660 | protected void onProvideStructure(@NonNull ViewStructure structure, |
| 661 | @ViewStructureType int viewFor, int flags) { |
| 662 | super.onProvideStructure(structure, viewFor, flags); |
| 663 | |
| 664 | if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) { |
| 665 | structure.setDataIsSensitive(!mCheckedFromResource); |
| 666 | } |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | @Override |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 670 | public void autofill(AutofillValue value) { |
| 671 | if (!isEnabled()) return; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 672 | |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 673 | if (!value.isToggle()) { |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 674 | Log.w(LOG_TAG, value + " could not be autofilled into " + this); |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 675 | return; |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 676 | } |
Philip P. Moltmann | 7b77116 | 2017-03-03 17:22:57 -0800 | [diff] [blame] | 677 | |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 678 | setChecked(value.getToggleValue()); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | @Override |
Felipe Leme | 8931e30 | 2017-03-06 13:44:35 -0800 | [diff] [blame] | 682 | public @AutofillType int getAutofillType() { |
| 683 | return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE; |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 684 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 685 | |
| 686 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 687 | public AutofillValue getAutofillValue() { |
| 688 | return isEnabled() ? AutofillValue.forToggle(isChecked()) : null; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 689 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |