blob: 500f381e6bb8d1fc9689a7f9db8e795a5a513621 [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;
Felipe Leme0200d9e2017-01-24 15:10:26 -080035import android.view.ViewStructure;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070036import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070037import android.view.accessibility.AccessibilityNodeInfo;
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 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700168
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700169 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171 }
172
173 /**
174 * Register a callback to be invoked when the checked state of this button
175 * changes.
176 *
177 * @param listener the callback to call on checked state change
178 */
Jason Longacdaaea502016-12-01 22:54:37 -0800179 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 mOnCheckedChangeListener = listener;
181 }
182
183 /**
184 * Register a callback to be invoked when the checked state of this button
185 * changes. This callback is used for internal purpose only.
186 *
187 * @param listener the callback to call on checked state change
188 * @hide
189 */
190 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
191 mOnCheckedChangeWidgetListener = listener;
192 }
193
194 /**
195 * Interface definition for a callback to be invoked when the checked state
196 * of a compound button changed.
197 */
198 public static interface OnCheckedChangeListener {
199 /**
200 * Called when the checked state of a compound button has changed.
201 *
202 * @param buttonView The compound button view whose state has changed.
203 * @param isChecked The new checked state of buttonView.
204 */
205 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
206 }
207
208 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800209 * Sets a drawable as the compound button image given its resource
210 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800212 * @param resId the resource identifier of the drawable
213 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800215 public void setButtonDrawable(@DrawableRes int resId) {
216 final Drawable d;
217 if (resId != 0) {
218 d = getContext().getDrawable(resId);
219 } else {
220 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222 setButtonDrawable(d);
223 }
224
225 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800226 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800228 * @param drawable the drawable to set
229 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800231 public void setButtonDrawable(@Nullable Drawable drawable) {
232 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 if (mButtonDrawable != null) {
234 mButtonDrawable.setCallback(null);
235 unscheduleDrawable(mButtonDrawable);
236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
Alan Viverette6a394f42015-02-12 15:03:22 -0800238 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700239
Alan Viverette6a394f42015-02-12 15:03:22 -0800240 if (drawable != null) {
241 drawable.setCallback(this);
242 drawable.setLayoutDirection(getLayoutDirection());
243 if (drawable.isStateful()) {
244 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700245 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800246 drawable.setVisible(getVisibility() == VISIBLE, false);
247 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700248 applyButtonTint();
249 }
250 }
251 }
252
253 /**
Doris Liu3380e692015-06-30 11:26:47 -0700254 * @hide
255 */
256 @Override
257 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
258 super.onResolveDrawables(layoutDirection);
259 if (mButtonDrawable != null) {
260 mButtonDrawable.setLayoutDirection(layoutDirection);
261 }
262 }
263
264 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800265 * @return the drawable used as the compound button image
266 * @see #setButtonDrawable(Drawable)
267 * @see #setButtonDrawable(int)
268 */
269 @Nullable
270 public Drawable getButtonDrawable() {
271 return mButtonDrawable;
272 }
273
274 /**
Alan Viverette91174362014-06-17 14:51:45 -0700275 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700276 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700277 * <p>
278 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
279 * automatically mutate the drawable and apply the specified tint and tint
280 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700281 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700282 *
283 * @param tint the tint to apply, may be {@code null} to clear tint
284 *
285 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700286 * @see #setButtonTintList(ColorStateList)
287 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700288 */
Alan Viverettea4264452014-07-28 16:02:55 -0700289 public void setButtonTintList(@Nullable ColorStateList tint) {
290 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700291 mHasButtonTint = true;
292
293 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700294 }
295
296 /**
297 * @return the tint applied to the button drawable
298 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700299 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700300 */
301 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700302 public ColorStateList getButtonTintList() {
303 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700304 }
305
306 /**
307 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700308 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700309 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700310 *
311 * @param tintMode the blending mode used to apply the tint, may be
312 * {@code null} to clear tint
313 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700314 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700315 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700316 */
317 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700318 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700319 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700320
321 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700322 }
323
324 /**
325 * @return the blending mode used to apply the tint to the button drawable
326 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700327 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700328 */
329 @Nullable
330 public PorterDuff.Mode getButtonTintMode() {
331 return mButtonTintMode;
332 }
333
334 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700335 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700336 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700337
338 if (mHasButtonTint) {
339 mButtonDrawable.setTintList(mButtonTintList);
340 }
341
342 if (mHasButtonTintMode) {
343 mButtonDrawable.setTintMode(mButtonTintMode);
344 }
Alan Viveretted5133792014-10-28 14:41:36 -0700345
346 // The drawable (or one of its children) may not have been
347 // stateful before applying the tint, so let's try again.
348 if (mButtonDrawable.isStateful()) {
349 mButtonDrawable.setState(getDrawableState());
350 }
Alan Viverette91174362014-06-17 14:51:45 -0700351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800354 @Override
355 public CharSequence getAccessibilityClassName() {
356 return CompoundButton.class.getName();
357 }
358
Alan Viverettea54956a2015-01-07 16:05:02 -0800359 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800361 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
362 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700363 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700364 }
365
Alan Viverettea54956a2015-01-07 16:05:02 -0800366 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700367 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800368 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
369 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700370 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700371 info.setChecked(mChecked);
372 }
373
374 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700375 public int getCompoundPaddingLeft() {
376 int padding = super.getCompoundPaddingLeft();
377 if (!isLayoutRtl()) {
378 final Drawable buttonDrawable = mButtonDrawable;
379 if (buttonDrawable != null) {
380 padding += buttonDrawable.getIntrinsicWidth();
381 }
382 }
383 return padding;
384 }
385
386 @Override
387 public int getCompoundPaddingRight() {
388 int padding = super.getCompoundPaddingRight();
389 if (isLayoutRtl()) {
390 final Drawable buttonDrawable = mButtonDrawable;
391 if (buttonDrawable != null) {
392 padding += buttonDrawable.getIntrinsicWidth();
393 }
394 }
395 return padding;
396 }
397
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800398 /**
399 * @hide
400 */
401 @Override
402 public int getHorizontalOffsetForDrawables() {
403 final Drawable buttonDrawable = mButtonDrawable;
404 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
405 }
406
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700407 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 final Drawable buttonDrawable = mButtonDrawable;
410 if (buttonDrawable != null) {
411 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700412 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
413 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414
Alan Viverette61956602014-04-22 19:07:06 -0700415 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 switch (verticalGravity) {
417 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700418 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 break;
420 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700421 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 break;
Alan Viverette61956602014-04-22 19:07:06 -0700423 default:
424 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 }
Alan Viverette61956602014-04-22 19:07:06 -0700426 final int bottom = top + drawableHeight;
427 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
428 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700430 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700431
432 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700433 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700434 background.setHotspotBounds(left, top, right, bottom);
435 }
436 }
437
438 super.onDraw(canvas);
439
440 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700441 final int scrollX = mScrollX;
442 final int scrollY = mScrollY;
443 if (scrollX == 0 && scrollY == 0) {
444 buttonDrawable.draw(canvas);
445 } else {
446 canvas.translate(scrollX, scrollY);
447 buttonDrawable.draw(canvas);
448 canvas.translate(-scrollX, -scrollY);
449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451 }
452
453 @Override
454 protected int[] onCreateDrawableState(int extraSpace) {
455 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
456 if (isChecked()) {
457 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
458 }
459 return drawableState;
460 }
461
462 @Override
463 protected void drawableStateChanged() {
464 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400465
466 final Drawable buttonDrawable = mButtonDrawable;
467 if (buttonDrawable != null && buttonDrawable.isStateful()
468 && buttonDrawable.setState(getDrawableState())) {
469 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471 }
472
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700473 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700474 public void drawableHotspotChanged(float x, float y) {
475 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700476
477 if (mButtonDrawable != null) {
478 mButtonDrawable.setHotspot(x, y);
479 }
480 }
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500483 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 return super.verifyDrawable(who) || who == mButtonDrawable;
485 }
486
Dianne Hackborne2136772010-11-04 15:08:59 -0700487 @Override
488 public void jumpDrawablesToCurrentState() {
489 super.jumpDrawablesToCurrentState();
490 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
491 }
492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 static class SavedState extends BaseSavedState {
494 boolean checked;
495
496 /**
497 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
498 */
499 SavedState(Parcelable superState) {
500 super(superState);
501 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
504 * Constructor called from {@link #CREATOR}
505 */
506 private SavedState(Parcel in) {
507 super(in);
508 checked = (Boolean)in.readValue(null);
509 }
510
511 @Override
512 public void writeToParcel(Parcel out, int flags) {
513 super.writeToParcel(out, flags);
514 out.writeValue(checked);
515 }
516
517 @Override
518 public String toString() {
519 return "CompoundButton.SavedState{"
520 + Integer.toHexString(System.identityHashCode(this))
521 + " checked=" + checked + "}";
522 }
523
Felipe Leme6d553872016-12-08 17:13:25 -0800524 @SuppressWarnings("hiding")
525 public static final Parcelable.Creator<SavedState> CREATOR =
526 new Parcelable.Creator<SavedState>() {
527 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 public SavedState createFromParcel(Parcel in) {
529 return new SavedState(in);
530 }
531
Felipe Leme6d553872016-12-08 17:13:25 -0800532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public SavedState[] newArray(int size) {
534 return new SavedState[size];
535 }
536 };
537 }
538
539 @Override
540 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 Parcelable superState = super.onSaveInstanceState();
542
543 SavedState ss = new SavedState(superState);
544
545 ss.checked = isChecked();
546 return ss;
547 }
548
549 @Override
550 public void onRestoreInstanceState(Parcelable state) {
551 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 super.onRestoreInstanceState(ss.getSuperState());
554 setChecked(ss.checked);
555 requestLayout();
556 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700557
558 /** @hide */
559 @Override
560 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
561 super.encodeProperties(stream);
562 stream.addProperty("checked", isChecked());
563 }
Felipe Leme6d553872016-12-08 17:13:25 -0800564
565 // TODO(b/33197203): add unit/CTS tests for auto-fill methods
566
567 @Override
Felipe Leme0200d9e2017-01-24 15:10:26 -0800568 public void onProvideAutoFillStructure(ViewStructure structure, int flags) {
569 super.onProvideAutoFillStructure(structure, flags);
570 structure.setAutoFillValue(AutoFillValue.forToggle(isChecked()));
571 // TODO(b/33197203): add unit/CTS tests for auto-fill methods
572 }
573
574 @Override
Felipe Leme6d553872016-12-08 17:13:25 -0800575 public void autoFill(AutoFillValue value) {
576 setChecked(value.getToggleValue());
577 }
578
579 @Override
580 public AutoFillType getAutoFillType() {
581 return AutoFillType.forToggle();
582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583}