blob: 6f687fe5f14c7de7ba3b526a95a58a34d4920a15 [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 Leme6d553872016-12-08 17:13:25 -080037import android.view.autofill.AutoFillType;
38import android.view.autofill.AutoFillValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Aurimas Liutikas99441c52016-10-11 16:48:32 -070040import com.android.internal.R;
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
43 * <p>
44 * A button with two states, checked and unchecked. When the button is pressed
45 * or clicked, the state changes automatically.
46 * </p>
47 *
48 * <p><strong>XML attributes</strong></p>
49 * <p>
50 * See {@link android.R.styleable#CompoundButton
51 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
52 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
53 * android.R.styleable#View View Attributes}
54 * </p>
55 */
56public abstract class CompoundButton extends Button implements Checkable {
Felipe Leme6d553872016-12-08 17:13:25 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 private boolean mChecked;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070062 private ColorStateList mButtonTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070063 private PorterDuff.Mode mButtonTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070064 private boolean mHasButtonTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070065 private boolean mHasButtonTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private OnCheckedChangeListener mOnCheckedChangeListener;
68 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
69
70 private static final int[] CHECKED_STATE_SET = {
71 R.attr.state_checked
72 };
73
74 public CompoundButton(Context context) {
75 this(context, null);
76 }
77
78 public CompoundButton(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
Alan Viverette617feb92013-09-09 18:09:13 -070082 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
83 this(context, attrs, defStyleAttr, 0);
84 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
Alan Viverette617feb92013-09-09 18:09:13 -070086 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
87 super(context, attrs, defStyleAttr, defStyleRes);
88
89 final TypedArray a = context.obtainStyledAttributes(
90 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Alan Viverette91174362014-06-17 14:51:45 -070092 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 if (d != null) {
94 setButtonDrawable(d);
95 }
96
Alan Viveretteb56f5d22014-09-14 15:48:50 -070097 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
98 mButtonTintMode = Drawable.parseTintMode(a.getInt(
99 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
100 mHasButtonTintMode = true;
101 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700102
Alan Viverette91174362014-06-17 14:51:45 -0700103 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700104 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700105 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700106 }
107
108 final boolean checked = a.getBoolean(
109 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 setChecked(checked);
111
112 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700113
114 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116
Felipe Leme6d553872016-12-08 17:13:25 -0800117 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public void toggle() {
119 setChecked(!mChecked);
120 }
121
122 @Override
123 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700125
126 final boolean handled = super.performClick();
127 if (!handled) {
128 // View only makes a sound effect if the onClickListener was
129 // called, so we'll need to make one here instead.
130 playSoundEffect(SoundEffectConstants.CLICK);
131 }
132
133 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135
Steve Zeigler7a367882010-02-23 16:39:08 -0800136 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800137 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public boolean isChecked() {
139 return mChecked;
140 }
141
142 /**
143 * <p>Changes the checked state of this button.</p>
144 *
145 * @param checked true to check the button, false to uncheck it
146 */
Felipe Leme6d553872016-12-08 17:13:25 -0800147 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public void setChecked(boolean checked) {
149 if (mChecked != checked) {
150 mChecked = checked;
151 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700152 notifyViewAccessibilityStateChangedIfNeeded(
153 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155 // Avoid infinite recursions if setChecked() is called from a listener
156 if (mBroadcasting) {
157 return;
158 }
159
160 mBroadcasting = true;
161 if (mOnCheckedChangeListener != null) {
162 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
163 }
164 if (mOnCheckedChangeWidgetListener != null) {
165 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
166 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700167
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700168 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170 }
171
172 /**
173 * Register a callback to be invoked when the checked state of this button
174 * changes.
175 *
176 * @param listener the callback to call on checked state change
177 */
Jason Longacdaaea502016-12-01 22:54:37 -0800178 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 mOnCheckedChangeListener = listener;
180 }
181
182 /**
183 * Register a callback to be invoked when the checked state of this button
184 * changes. This callback is used for internal purpose only.
185 *
186 * @param listener the callback to call on checked state change
187 * @hide
188 */
189 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
190 mOnCheckedChangeWidgetListener = listener;
191 }
192
193 /**
194 * Interface definition for a callback to be invoked when the checked state
195 * of a compound button changed.
196 */
197 public static interface OnCheckedChangeListener {
198 /**
199 * Called when the checked state of a compound button has changed.
200 *
201 * @param buttonView The compound button view whose state has changed.
202 * @param isChecked The new checked state of buttonView.
203 */
204 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
205 }
206
207 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800208 * Sets a drawable as the compound button image given its resource
209 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800211 * @param resId the resource identifier of the drawable
212 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800214 public void setButtonDrawable(@DrawableRes int resId) {
215 final Drawable d;
216 if (resId != 0) {
217 d = getContext().getDrawable(resId);
218 } else {
219 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221 setButtonDrawable(d);
222 }
223
224 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800225 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800227 * @param drawable the drawable to set
228 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800230 public void setButtonDrawable(@Nullable Drawable drawable) {
231 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 if (mButtonDrawable != null) {
233 mButtonDrawable.setCallback(null);
234 unscheduleDrawable(mButtonDrawable);
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Alan Viverette6a394f42015-02-12 15:03:22 -0800237 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700238
Alan Viverette6a394f42015-02-12 15:03:22 -0800239 if (drawable != null) {
240 drawable.setCallback(this);
241 drawable.setLayoutDirection(getLayoutDirection());
242 if (drawable.isStateful()) {
243 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700244 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800245 drawable.setVisible(getVisibility() == VISIBLE, false);
246 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700247 applyButtonTint();
248 }
249 }
250 }
251
252 /**
Doris Liu3380e692015-06-30 11:26:47 -0700253 * @hide
254 */
255 @Override
256 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
257 super.onResolveDrawables(layoutDirection);
258 if (mButtonDrawable != null) {
259 mButtonDrawable.setLayoutDirection(layoutDirection);
260 }
261 }
262
263 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800264 * @return the drawable used as the compound button image
265 * @see #setButtonDrawable(Drawable)
266 * @see #setButtonDrawable(int)
267 */
268 @Nullable
269 public Drawable getButtonDrawable() {
270 return mButtonDrawable;
271 }
272
273 /**
Alan Viverette91174362014-06-17 14:51:45 -0700274 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700275 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700276 * <p>
277 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
278 * automatically mutate the drawable and apply the specified tint and tint
279 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700280 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700281 *
282 * @param tint the tint to apply, may be {@code null} to clear tint
283 *
284 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700285 * @see #setButtonTintList(ColorStateList)
286 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700287 */
Alan Viverettea4264452014-07-28 16:02:55 -0700288 public void setButtonTintList(@Nullable ColorStateList tint) {
289 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700290 mHasButtonTint = true;
291
292 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700293 }
294
295 /**
296 * @return the tint applied to the button drawable
297 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700298 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700299 */
300 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700301 public ColorStateList getButtonTintList() {
302 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700303 }
304
305 /**
306 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700307 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700308 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700309 *
310 * @param tintMode the blending mode used to apply the tint, may be
311 * {@code null} to clear tint
312 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700313 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700314 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700315 */
316 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700317 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700318 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700319
320 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700321 }
322
323 /**
324 * @return the blending mode used to apply the tint to the button drawable
325 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700326 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700327 */
328 @Nullable
329 public PorterDuff.Mode getButtonTintMode() {
330 return mButtonTintMode;
331 }
332
333 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700334 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700335 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700336
337 if (mHasButtonTint) {
338 mButtonDrawable.setTintList(mButtonTintList);
339 }
340
341 if (mHasButtonTintMode) {
342 mButtonDrawable.setTintMode(mButtonTintMode);
343 }
Alan Viveretted5133792014-10-28 14:41:36 -0700344
345 // The drawable (or one of its children) may not have been
346 // stateful before applying the tint, so let's try again.
347 if (mButtonDrawable.isStateful()) {
348 mButtonDrawable.setState(getDrawableState());
349 }
Alan Viverette91174362014-06-17 14:51:45 -0700350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
352
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800353 @Override
354 public CharSequence getAccessibilityClassName() {
355 return CompoundButton.class.getName();
356 }
357
Alan Viverettea54956a2015-01-07 16:05:02 -0800358 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800360 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
361 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700362 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700363 }
364
Alan Viverettea54956a2015-01-07 16:05:02 -0800365 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700366 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800367 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
368 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700369 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700370 info.setChecked(mChecked);
371 }
372
373 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700374 public int getCompoundPaddingLeft() {
375 int padding = super.getCompoundPaddingLeft();
376 if (!isLayoutRtl()) {
377 final Drawable buttonDrawable = mButtonDrawable;
378 if (buttonDrawable != null) {
379 padding += buttonDrawable.getIntrinsicWidth();
380 }
381 }
382 return padding;
383 }
384
385 @Override
386 public int getCompoundPaddingRight() {
387 int padding = super.getCompoundPaddingRight();
388 if (isLayoutRtl()) {
389 final Drawable buttonDrawable = mButtonDrawable;
390 if (buttonDrawable != null) {
391 padding += buttonDrawable.getIntrinsicWidth();
392 }
393 }
394 return padding;
395 }
396
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800397 /**
398 * @hide
399 */
400 @Override
401 public int getHorizontalOffsetForDrawables() {
402 final Drawable buttonDrawable = mButtonDrawable;
403 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
404 }
405
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700406 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 final Drawable buttonDrawable = mButtonDrawable;
409 if (buttonDrawable != null) {
410 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700411 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
412 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413
Alan Viverette61956602014-04-22 19:07:06 -0700414 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 switch (verticalGravity) {
416 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700417 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 break;
419 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700420 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 break;
Alan Viverette61956602014-04-22 19:07:06 -0700422 default:
423 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
Alan Viverette61956602014-04-22 19:07:06 -0700425 final int bottom = top + drawableHeight;
426 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
427 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700429 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700430
431 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700432 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700433 background.setHotspotBounds(left, top, right, bottom);
434 }
435 }
436
437 super.onDraw(canvas);
438
439 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700440 final int scrollX = mScrollX;
441 final int scrollY = mScrollY;
442 if (scrollX == 0 && scrollY == 0) {
443 buttonDrawable.draw(canvas);
444 } else {
445 canvas.translate(scrollX, scrollY);
446 buttonDrawable.draw(canvas);
447 canvas.translate(-scrollX, -scrollY);
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451
452 @Override
453 protected int[] onCreateDrawableState(int extraSpace) {
454 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
455 if (isChecked()) {
456 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
457 }
458 return drawableState;
459 }
460
461 @Override
462 protected void drawableStateChanged() {
463 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400464
465 final Drawable buttonDrawable = mButtonDrawable;
466 if (buttonDrawable != null && buttonDrawable.isStateful()
467 && buttonDrawable.setState(getDrawableState())) {
468 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470 }
471
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700472 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700473 public void drawableHotspotChanged(float x, float y) {
474 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700475
476 if (mButtonDrawable != null) {
477 mButtonDrawable.setHotspot(x, y);
478 }
479 }
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500482 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 return super.verifyDrawable(who) || who == mButtonDrawable;
484 }
485
Dianne Hackborne2136772010-11-04 15:08:59 -0700486 @Override
487 public void jumpDrawablesToCurrentState() {
488 super.jumpDrawablesToCurrentState();
489 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
490 }
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 static class SavedState extends BaseSavedState {
493 boolean checked;
494
495 /**
496 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
497 */
498 SavedState(Parcelable superState) {
499 super(superState);
500 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /**
503 * Constructor called from {@link #CREATOR}
504 */
505 private SavedState(Parcel in) {
506 super(in);
507 checked = (Boolean)in.readValue(null);
508 }
509
510 @Override
511 public void writeToParcel(Parcel out, int flags) {
512 super.writeToParcel(out, flags);
513 out.writeValue(checked);
514 }
515
516 @Override
517 public String toString() {
518 return "CompoundButton.SavedState{"
519 + Integer.toHexString(System.identityHashCode(this))
520 + " checked=" + checked + "}";
521 }
522
Felipe Leme6d553872016-12-08 17:13:25 -0800523 @SuppressWarnings("hiding")
524 public static final Parcelable.Creator<SavedState> CREATOR =
525 new Parcelable.Creator<SavedState>() {
526 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 public SavedState createFromParcel(Parcel in) {
528 return new SavedState(in);
529 }
530
Felipe Leme6d553872016-12-08 17:13:25 -0800531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public SavedState[] newArray(int size) {
533 return new SavedState[size];
534 }
535 };
536 }
537
538 @Override
539 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 Parcelable superState = super.onSaveInstanceState();
541
542 SavedState ss = new SavedState(superState);
543
544 ss.checked = isChecked();
545 return ss;
546 }
547
548 @Override
549 public void onRestoreInstanceState(Parcelable state) {
550 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 super.onRestoreInstanceState(ss.getSuperState());
553 setChecked(ss.checked);
554 requestLayout();
555 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700556
557 /** @hide */
558 @Override
559 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
560 super.encodeProperties(stream);
561 stream.addProperty("checked", isChecked());
562 }
Felipe Leme6d553872016-12-08 17:13:25 -0800563
Felipe Lemebab851c2017-02-03 18:45:08 -0800564 // TODO(b/33197203): add unit/CTS tests for auto-fill methods (and make sure they handle enable)
Felipe Leme6d553872016-12-08 17:13:25 -0800565
Felipe Lemebab851c2017-02-03 18:45:08 -0800566 // TODO(b/33197203): override onProvideAutoFillStructure and add a change listener
Felipe Leme0200d9e2017-01-24 15:10:26 -0800567
568 @Override
Felipe Leme6d553872016-12-08 17:13:25 -0800569 public void autoFill(AutoFillValue value) {
Felipe Lemebab851c2017-02-03 18:45:08 -0800570 if (!isEnabled()) return;
571
Felipe Leme6d553872016-12-08 17:13:25 -0800572 setChecked(value.getToggleValue());
573 }
574
575 @Override
576 public AutoFillType getAutoFillType() {
577 return AutoFillType.forToggle();
578 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800579
580 @Override
581 public AutoFillValue getAutoFillValue() {
582 return isEnabled() ? null : AutoFillValue.forToggle(isChecked());
583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584}