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