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