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