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