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