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