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