blob: f2c2af511f81e802344c234703e2d4331cb8b609 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.widget;
18
Alan Viverette6a394f42015-02-12 15:03:22 -080019import android.annotation.DrawableRes;
Siva Velusamy94a6d152015-05-05 15:07:00 -070020import android.annotation.NonNull;
Alan Viverette91174362014-06-17 14:51:45 -070021import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Alan Viverette91174362014-06-17 14:51:45 -070023import android.content.res.ColorStateList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.res.TypedArray;
25import android.graphics.Canvas;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070026import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.graphics.drawable.Drawable;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
31import android.view.Gravity;
Alan Viveretted4e77902014-10-27 17:50:51 -070032import android.view.SoundEffectConstants;
Steve Zeigler7a367882010-02-23 16:39:08 -080033import android.view.ViewDebug;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070034import android.view.ViewHierarchyEncoder;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070035import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070036import android.view.accessibility.AccessibilityNodeInfo;
Felipe Leme5882c4f2017-02-16 21:46:46 -080037import android.view.autofill.AutoFillManager;
Felipe Leme6d553872016-12-08 17:13:25 -080038import android.view.autofill.AutoFillType;
39import android.view.autofill.AutoFillValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Aurimas Liutikas99441c52016-10-11 16:48:32 -070041import com.android.internal.R;
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043/**
44 * <p>
45 * A button with two states, checked and unchecked. When the button is pressed
46 * or clicked, the state changes automatically.
47 * </p>
48 *
49 * <p><strong>XML attributes</strong></p>
50 * <p>
51 * See {@link android.R.styleable#CompoundButton
52 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
53 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
54 * android.R.styleable#View View Attributes}
55 * </p>
56 */
57public abstract class CompoundButton extends Button implements Checkable {
Felipe Leme6d553872016-12-08 17:13:25 -080058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private boolean mChecked;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070063 private ColorStateList mButtonTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070064 private PorterDuff.Mode mButtonTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070065 private boolean mHasButtonTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070066 private boolean mHasButtonTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 private OnCheckedChangeListener mOnCheckedChangeListener;
69 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
70
71 private static final int[] CHECKED_STATE_SET = {
72 R.attr.state_checked
73 };
74
75 public CompoundButton(Context context) {
76 this(context, null);
77 }
78
79 public CompoundButton(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
Alan Viverette617feb92013-09-09 18:09:13 -070083 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
84 this(context, attrs, defStyleAttr, 0);
85 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Alan Viverette617feb92013-09-09 18:09:13 -070087 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
88 super(context, attrs, defStyleAttr, defStyleRes);
89
90 final TypedArray a = context.obtainStyledAttributes(
91 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Alan Viverette91174362014-06-17 14:51:45 -070093 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 if (d != null) {
95 setButtonDrawable(d);
96 }
97
Alan Viveretteb56f5d22014-09-14 15:48:50 -070098 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
99 mButtonTintMode = Drawable.parseTintMode(a.getInt(
100 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
101 mHasButtonTintMode = true;
102 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700103
Alan Viverette91174362014-06-17 14:51:45 -0700104 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700105 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700106 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700107 }
108
109 final boolean checked = a.getBoolean(
110 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 setChecked(checked);
112
113 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700114
115 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117
Felipe Leme6d553872016-12-08 17:13:25 -0800118 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public void toggle() {
120 setChecked(!mChecked);
121 }
122
123 @Override
124 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700126
127 final boolean handled = super.performClick();
128 if (!handled) {
129 // View only makes a sound effect if the onClickListener was
130 // called, so we'll need to make one here instead.
131 playSoundEffect(SoundEffectConstants.CLICK);
132 }
133
134 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
Steve Zeigler7a367882010-02-23 16:39:08 -0800137 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800138 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public boolean isChecked() {
140 return mChecked;
141 }
142
143 /**
144 * <p>Changes the checked state of this button.</p>
145 *
146 * @param checked true to check the button, false to uncheck it
147 */
Felipe Leme6d553872016-12-08 17:13:25 -0800148 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public void setChecked(boolean checked) {
150 if (mChecked != checked) {
151 mChecked = checked;
152 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700153 notifyViewAccessibilityStateChangedIfNeeded(
154 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 // Avoid infinite recursions if setChecked() is called from a listener
157 if (mBroadcasting) {
158 return;
159 }
160
161 mBroadcasting = true;
162 if (mOnCheckedChangeListener != null) {
163 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
164 }
165 if (mOnCheckedChangeWidgetListener != null) {
166 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
167 }
Felipe Leme5882c4f2017-02-16 21:46:46 -0800168 final AutoFillManager afm = mContext.getSystemService(AutoFillManager.class);
169 if (afm != null) {
170 afm.valueChanged(this);
171 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700172
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700173 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 }
175 }
176
177 /**
178 * Register a callback to be invoked when the checked state of this button
179 * changes.
180 *
181 * @param listener the callback to call on checked state change
182 */
Jason Longacdaaea502016-12-01 22:54:37 -0800183 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 mOnCheckedChangeListener = listener;
185 }
186
187 /**
188 * Register a callback to be invoked when the checked state of this button
189 * changes. This callback is used for internal purpose only.
190 *
191 * @param listener the callback to call on checked state change
192 * @hide
193 */
194 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
195 mOnCheckedChangeWidgetListener = listener;
196 }
197
198 /**
199 * Interface definition for a callback to be invoked when the checked state
200 * of a compound button changed.
201 */
202 public static interface OnCheckedChangeListener {
203 /**
204 * Called when the checked state of a compound button has changed.
205 *
206 * @param buttonView The compound button view whose state has changed.
207 * @param isChecked The new checked state of buttonView.
208 */
209 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
210 }
211
212 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800213 * Sets a drawable as the compound button image given its resource
214 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800216 * @param resId the resource identifier of the drawable
217 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800219 public void setButtonDrawable(@DrawableRes int resId) {
220 final Drawable d;
221 if (resId != 0) {
222 d = getContext().getDrawable(resId);
223 } else {
224 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 }
226 setButtonDrawable(d);
227 }
228
229 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800230 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800232 * @param drawable the drawable to set
233 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800235 public void setButtonDrawable(@Nullable Drawable drawable) {
236 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 if (mButtonDrawable != null) {
238 mButtonDrawable.setCallback(null);
239 unscheduleDrawable(mButtonDrawable);
240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Alan Viverette6a394f42015-02-12 15:03:22 -0800242 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700243
Alan Viverette6a394f42015-02-12 15:03:22 -0800244 if (drawable != null) {
245 drawable.setCallback(this);
246 drawable.setLayoutDirection(getLayoutDirection());
247 if (drawable.isStateful()) {
248 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700249 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800250 drawable.setVisible(getVisibility() == VISIBLE, false);
251 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700252 applyButtonTint();
253 }
254 }
255 }
256
257 /**
Doris Liu3380e692015-06-30 11:26:47 -0700258 * @hide
259 */
260 @Override
261 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
262 super.onResolveDrawables(layoutDirection);
263 if (mButtonDrawable != null) {
264 mButtonDrawable.setLayoutDirection(layoutDirection);
265 }
266 }
267
268 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800269 * @return the drawable used as the compound button image
270 * @see #setButtonDrawable(Drawable)
271 * @see #setButtonDrawable(int)
272 */
273 @Nullable
274 public Drawable getButtonDrawable() {
275 return mButtonDrawable;
276 }
277
278 /**
Alan Viverette91174362014-06-17 14:51:45 -0700279 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700280 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700281 * <p>
282 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
283 * automatically mutate the drawable and apply the specified tint and tint
284 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700285 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700286 *
287 * @param tint the tint to apply, may be {@code null} to clear tint
288 *
289 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700290 * @see #setButtonTintList(ColorStateList)
291 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700292 */
Alan Viverettea4264452014-07-28 16:02:55 -0700293 public void setButtonTintList(@Nullable ColorStateList tint) {
294 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700295 mHasButtonTint = true;
296
297 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700298 }
299
300 /**
301 * @return the tint applied to the button drawable
302 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700303 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700304 */
305 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700306 public ColorStateList getButtonTintList() {
307 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700308 }
309
310 /**
311 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700312 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700313 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700314 *
315 * @param tintMode the blending mode used to apply the tint, may be
316 * {@code null} to clear tint
317 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700318 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700319 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700320 */
321 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700322 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700323 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700324
325 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700326 }
327
328 /**
329 * @return the blending mode used to apply the tint to the button drawable
330 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700331 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700332 */
333 @Nullable
334 public PorterDuff.Mode getButtonTintMode() {
335 return mButtonTintMode;
336 }
337
338 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700339 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700340 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700341
342 if (mHasButtonTint) {
343 mButtonDrawable.setTintList(mButtonTintList);
344 }
345
346 if (mHasButtonTintMode) {
347 mButtonDrawable.setTintMode(mButtonTintMode);
348 }
Alan Viveretted5133792014-10-28 14:41:36 -0700349
350 // The drawable (or one of its children) may not have been
351 // stateful before applying the tint, so let's try again.
352 if (mButtonDrawable.isStateful()) {
353 mButtonDrawable.setState(getDrawableState());
354 }
Alan Viverette91174362014-06-17 14:51:45 -0700355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
357
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800358 @Override
359 public CharSequence getAccessibilityClassName() {
360 return CompoundButton.class.getName();
361 }
362
Alan Viverettea54956a2015-01-07 16:05:02 -0800363 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800365 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
366 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700367 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700368 }
369
Alan Viverettea54956a2015-01-07 16:05:02 -0800370 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700371 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800372 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
373 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700374 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700375 info.setChecked(mChecked);
376 }
377
378 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700379 public int getCompoundPaddingLeft() {
380 int padding = super.getCompoundPaddingLeft();
381 if (!isLayoutRtl()) {
382 final Drawable buttonDrawable = mButtonDrawable;
383 if (buttonDrawable != null) {
384 padding += buttonDrawable.getIntrinsicWidth();
385 }
386 }
387 return padding;
388 }
389
390 @Override
391 public int getCompoundPaddingRight() {
392 int padding = super.getCompoundPaddingRight();
393 if (isLayoutRtl()) {
394 final Drawable buttonDrawable = mButtonDrawable;
395 if (buttonDrawable != null) {
396 padding += buttonDrawable.getIntrinsicWidth();
397 }
398 }
399 return padding;
400 }
401
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800402 /**
403 * @hide
404 */
405 @Override
406 public int getHorizontalOffsetForDrawables() {
407 final Drawable buttonDrawable = mButtonDrawable;
408 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
409 }
410
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700411 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 final Drawable buttonDrawable = mButtonDrawable;
414 if (buttonDrawable != null) {
415 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700416 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
417 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
Alan Viverette61956602014-04-22 19:07:06 -0700419 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 switch (verticalGravity) {
421 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700422 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 break;
424 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700425 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 break;
Alan Viverette61956602014-04-22 19:07:06 -0700427 default:
428 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
Alan Viverette61956602014-04-22 19:07:06 -0700430 final int bottom = top + drawableHeight;
431 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
432 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700434 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700435
436 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700437 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700438 background.setHotspotBounds(left, top, right, bottom);
439 }
440 }
441
442 super.onDraw(canvas);
443
444 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700445 final int scrollX = mScrollX;
446 final int scrollY = mScrollY;
447 if (scrollX == 0 && scrollY == 0) {
448 buttonDrawable.draw(canvas);
449 } else {
450 canvas.translate(scrollX, scrollY);
451 buttonDrawable.draw(canvas);
452 canvas.translate(-scrollX, -scrollY);
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455 }
456
457 @Override
458 protected int[] onCreateDrawableState(int extraSpace) {
459 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
460 if (isChecked()) {
461 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
462 }
463 return drawableState;
464 }
465
466 @Override
467 protected void drawableStateChanged() {
468 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400469
470 final Drawable buttonDrawable = mButtonDrawable;
471 if (buttonDrawable != null && buttonDrawable.isStateful()
472 && buttonDrawable.setState(getDrawableState())) {
473 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475 }
476
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700477 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700478 public void drawableHotspotChanged(float x, float y) {
479 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700480
481 if (mButtonDrawable != null) {
482 mButtonDrawable.setHotspot(x, y);
483 }
484 }
485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500487 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 return super.verifyDrawable(who) || who == mButtonDrawable;
489 }
490
Dianne Hackborne2136772010-11-04 15:08:59 -0700491 @Override
492 public void jumpDrawablesToCurrentState() {
493 super.jumpDrawablesToCurrentState();
494 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
495 }
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 static class SavedState extends BaseSavedState {
498 boolean checked;
499
500 /**
501 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
502 */
503 SavedState(Parcelable superState) {
504 super(superState);
505 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 /**
508 * Constructor called from {@link #CREATOR}
509 */
510 private SavedState(Parcel in) {
511 super(in);
512 checked = (Boolean)in.readValue(null);
513 }
514
515 @Override
516 public void writeToParcel(Parcel out, int flags) {
517 super.writeToParcel(out, flags);
518 out.writeValue(checked);
519 }
520
521 @Override
522 public String toString() {
523 return "CompoundButton.SavedState{"
524 + Integer.toHexString(System.identityHashCode(this))
525 + " checked=" + checked + "}";
526 }
527
Felipe Leme6d553872016-12-08 17:13:25 -0800528 @SuppressWarnings("hiding")
529 public static final Parcelable.Creator<SavedState> CREATOR =
530 new Parcelable.Creator<SavedState>() {
531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public SavedState createFromParcel(Parcel in) {
533 return new SavedState(in);
534 }
535
Felipe Leme6d553872016-12-08 17:13:25 -0800536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public SavedState[] newArray(int size) {
538 return new SavedState[size];
539 }
540 };
541 }
542
543 @Override
544 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 Parcelable superState = super.onSaveInstanceState();
546
547 SavedState ss = new SavedState(superState);
548
549 ss.checked = isChecked();
550 return ss;
551 }
552
553 @Override
554 public void onRestoreInstanceState(Parcelable state) {
555 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 super.onRestoreInstanceState(ss.getSuperState());
558 setChecked(ss.checked);
559 requestLayout();
560 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700561
562 /** @hide */
563 @Override
564 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
565 super.encodeProperties(stream);
566 stream.addProperty("checked", isChecked());
567 }
Felipe Leme6d553872016-12-08 17:13:25 -0800568
Felipe Lemebab851c2017-02-03 18:45:08 -0800569 // TODO(b/33197203): add unit/CTS tests for auto-fill methods (and make sure they handle enable)
Felipe Leme6d553872016-12-08 17:13:25 -0800570
Felipe Leme0200d9e2017-01-24 15:10:26 -0800571 @Override
Felipe Leme6d553872016-12-08 17:13:25 -0800572 public void autoFill(AutoFillValue value) {
Felipe Lemebab851c2017-02-03 18:45:08 -0800573 if (!isEnabled()) return;
574
Felipe Leme6d553872016-12-08 17:13:25 -0800575 setChecked(value.getToggleValue());
576 }
577
578 @Override
579 public AutoFillType getAutoFillType() {
580 return AutoFillType.forToggle();
581 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800582
583 @Override
584 public AutoFillValue getAutoFillValue() {
Felipe Leme5882c4f2017-02-16 21:46:46 -0800585 return isEnabled() ? AutoFillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587}