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