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