blob: 887c59a2d71cd82d9023bdd5543f99120e325029 [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 Lemec01a8732017-02-22 17:26:06 -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 Leme5882c4f2017-02-16 21:46:46 -080038import android.view.autofill.AutoFillManager;
Felipe Leme6d553872016-12-08 17:13:25 -080039import android.view.autofill.AutoFillType;
40import android.view.autofill.AutoFillValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Aurimas Liutikas99441c52016-10-11 16:48:32 -070042import com.android.internal.R;
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044/**
45 * <p>
46 * A button with two states, checked and unchecked. When the button is pressed
47 * or clicked, the state changes automatically.
48 * </p>
49 *
50 * <p><strong>XML attributes</strong></p>
51 * <p>
52 * See {@link android.R.styleable#CompoundButton
53 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
54 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
55 * android.R.styleable#View View Attributes}
56 * </p>
57 */
58public abstract class CompoundButton extends Button implements Checkable {
Felipe Leme6d553872016-12-08 17:13:25 -080059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 private boolean mChecked;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070064 private ColorStateList mButtonTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070065 private PorterDuff.Mode mButtonTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070066 private boolean mHasButtonTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070067 private boolean mHasButtonTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 private OnCheckedChangeListener mOnCheckedChangeListener;
70 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
71
Felipe Lemec01a8732017-02-22 17:26:06 -080072 // Indicates whether the toggle state was set from resources or dynamically, so it can be used
73 // to sanitize auto-fill requests.
74 private boolean mCheckedFromResource = false;
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 private static final int[] CHECKED_STATE_SET = {
77 R.attr.state_checked
78 };
79
80 public CompoundButton(Context context) {
81 this(context, null);
82 }
83
84 public CompoundButton(Context context, AttributeSet attrs) {
85 this(context, attrs, 0);
86 }
87
Alan Viverette617feb92013-09-09 18:09:13 -070088 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
89 this(context, attrs, defStyleAttr, 0);
90 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Alan Viverette617feb92013-09-09 18:09:13 -070092 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
93 super(context, attrs, defStyleAttr, defStyleRes);
94
95 final TypedArray a = context.obtainStyledAttributes(
96 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Alan Viverette91174362014-06-17 14:51:45 -070098 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 if (d != null) {
100 setButtonDrawable(d);
101 }
102
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700103 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
104 mButtonTintMode = Drawable.parseTintMode(a.getInt(
105 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
106 mHasButtonTintMode = true;
107 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700108
Alan Viverette91174362014-06-17 14:51:45 -0700109 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700110 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700111 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700112 }
113
114 final boolean checked = a.getBoolean(
115 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 setChecked(checked);
Felipe Lemec01a8732017-02-22 17:26:06 -0800117 mCheckedFromResource = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700120
121 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 }
123
Felipe Leme6d553872016-12-08 17:13:25 -0800124 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public void toggle() {
126 setChecked(!mChecked);
127 }
128
129 @Override
130 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700132
133 final boolean handled = super.performClick();
134 if (!handled) {
135 // View only makes a sound effect if the onClickListener was
136 // called, so we'll need to make one here instead.
137 playSoundEffect(SoundEffectConstants.CLICK);
138 }
139
140 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142
Steve Zeigler7a367882010-02-23 16:39:08 -0800143 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800144 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 public boolean isChecked() {
146 return mChecked;
147 }
148
149 /**
150 * <p>Changes the checked state of this button.</p>
151 *
152 * @param checked true to check the button, false to uncheck it
153 */
Felipe Leme6d553872016-12-08 17:13:25 -0800154 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 public void setChecked(boolean checked) {
156 if (mChecked != checked) {
Felipe Lemec01a8732017-02-22 17:26:06 -0800157 mCheckedFromResource = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 mChecked = checked;
159 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700160 notifyViewAccessibilityStateChangedIfNeeded(
161 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163 // Avoid infinite recursions if setChecked() is called from a listener
164 if (mBroadcasting) {
165 return;
166 }
167
168 mBroadcasting = true;
169 if (mOnCheckedChangeListener != null) {
170 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
171 }
172 if (mOnCheckedChangeWidgetListener != null) {
173 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
174 }
Felipe Leme5882c4f2017-02-16 21:46:46 -0800175 final AutoFillManager afm = mContext.getSystemService(AutoFillManager.class);
176 if (afm != null) {
177 afm.valueChanged(this);
178 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700179
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700180 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 }
182 }
183
184 /**
185 * Register a callback to be invoked when the checked state of this button
186 * changes.
187 *
188 * @param listener the callback to call on checked state change
189 */
Jason Longacdaaea502016-12-01 22:54:37 -0800190 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 mOnCheckedChangeListener = listener;
192 }
193
194 /**
195 * Register a callback to be invoked when the checked state of this button
196 * changes. This callback is used for internal purpose only.
197 *
198 * @param listener the callback to call on checked state change
199 * @hide
200 */
201 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
202 mOnCheckedChangeWidgetListener = listener;
203 }
204
205 /**
206 * Interface definition for a callback to be invoked when the checked state
207 * of a compound button changed.
208 */
209 public static interface OnCheckedChangeListener {
210 /**
211 * Called when the checked state of a compound button has changed.
212 *
213 * @param buttonView The compound button view whose state has changed.
214 * @param isChecked The new checked state of buttonView.
215 */
216 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
217 }
218
219 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800220 * Sets a drawable as the compound button image given its resource
221 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800223 * @param resId the resource identifier of the drawable
224 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800226 public void setButtonDrawable(@DrawableRes int resId) {
227 final Drawable d;
228 if (resId != 0) {
229 d = getContext().getDrawable(resId);
230 } else {
231 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233 setButtonDrawable(d);
234 }
235
236 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800237 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800239 * @param drawable the drawable to set
240 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800242 public void setButtonDrawable(@Nullable Drawable drawable) {
243 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 if (mButtonDrawable != null) {
245 mButtonDrawable.setCallback(null);
246 unscheduleDrawable(mButtonDrawable);
247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Alan Viverette6a394f42015-02-12 15:03:22 -0800249 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700250
Alan Viverette6a394f42015-02-12 15:03:22 -0800251 if (drawable != null) {
252 drawable.setCallback(this);
253 drawable.setLayoutDirection(getLayoutDirection());
254 if (drawable.isStateful()) {
255 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700256 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800257 drawable.setVisible(getVisibility() == VISIBLE, false);
258 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700259 applyButtonTint();
260 }
261 }
262 }
263
264 /**
Doris Liu3380e692015-06-30 11:26:47 -0700265 * @hide
266 */
267 @Override
268 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
269 super.onResolveDrawables(layoutDirection);
270 if (mButtonDrawable != null) {
271 mButtonDrawable.setLayoutDirection(layoutDirection);
272 }
273 }
274
275 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800276 * @return the drawable used as the compound button image
277 * @see #setButtonDrawable(Drawable)
278 * @see #setButtonDrawable(int)
279 */
280 @Nullable
281 public Drawable getButtonDrawable() {
282 return mButtonDrawable;
283 }
284
285 /**
Alan Viverette91174362014-06-17 14:51:45 -0700286 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700287 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700288 * <p>
289 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
290 * automatically mutate the drawable and apply the specified tint and tint
291 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700292 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700293 *
294 * @param tint the tint to apply, may be {@code null} to clear tint
295 *
296 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700297 * @see #setButtonTintList(ColorStateList)
298 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700299 */
Alan Viverettea4264452014-07-28 16:02:55 -0700300 public void setButtonTintList(@Nullable ColorStateList tint) {
301 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700302 mHasButtonTint = true;
303
304 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700305 }
306
307 /**
308 * @return the tint applied to the button drawable
309 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700310 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700311 */
312 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700313 public ColorStateList getButtonTintList() {
314 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700315 }
316
317 /**
318 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700319 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700320 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700321 *
322 * @param tintMode the blending mode used to apply the tint, may be
323 * {@code null} to clear tint
324 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700325 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700326 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700327 */
328 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700329 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700330 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700331
332 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700333 }
334
335 /**
336 * @return the blending mode used to apply the tint to the button drawable
337 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700338 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700339 */
340 @Nullable
341 public PorterDuff.Mode getButtonTintMode() {
342 return mButtonTintMode;
343 }
344
345 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700346 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700347 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700348
349 if (mHasButtonTint) {
350 mButtonDrawable.setTintList(mButtonTintList);
351 }
352
353 if (mHasButtonTintMode) {
354 mButtonDrawable.setTintMode(mButtonTintMode);
355 }
Alan Viveretted5133792014-10-28 14:41:36 -0700356
357 // The drawable (or one of its children) may not have been
358 // stateful before applying the tint, so let's try again.
359 if (mButtonDrawable.isStateful()) {
360 mButtonDrawable.setState(getDrawableState());
361 }
Alan Viverette91174362014-06-17 14:51:45 -0700362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800365 @Override
366 public CharSequence getAccessibilityClassName() {
367 return CompoundButton.class.getName();
368 }
369
Alan Viverettea54956a2015-01-07 16:05:02 -0800370 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800372 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
373 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700374 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700375 }
376
Alan Viverettea54956a2015-01-07 16:05:02 -0800377 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700378 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800379 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
380 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700381 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700382 info.setChecked(mChecked);
383 }
384
385 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700386 public int getCompoundPaddingLeft() {
387 int padding = super.getCompoundPaddingLeft();
388 if (!isLayoutRtl()) {
389 final Drawable buttonDrawable = mButtonDrawable;
390 if (buttonDrawable != null) {
391 padding += buttonDrawable.getIntrinsicWidth();
392 }
393 }
394 return padding;
395 }
396
397 @Override
398 public int getCompoundPaddingRight() {
399 int padding = super.getCompoundPaddingRight();
400 if (isLayoutRtl()) {
401 final Drawable buttonDrawable = mButtonDrawable;
402 if (buttonDrawable != null) {
403 padding += buttonDrawable.getIntrinsicWidth();
404 }
405 }
406 return padding;
407 }
408
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800409 /**
410 * @hide
411 */
412 @Override
413 public int getHorizontalOffsetForDrawables() {
414 final Drawable buttonDrawable = mButtonDrawable;
415 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
416 }
417
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700418 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 final Drawable buttonDrawable = mButtonDrawable;
421 if (buttonDrawable != null) {
422 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700423 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
424 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425
Alan Viverette61956602014-04-22 19:07:06 -0700426 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 switch (verticalGravity) {
428 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700429 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 break;
431 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700432 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 break;
Alan Viverette61956602014-04-22 19:07:06 -0700434 default:
435 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
Alan Viverette61956602014-04-22 19:07:06 -0700437 final int bottom = top + drawableHeight;
438 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
439 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700441 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700442
443 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700444 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700445 background.setHotspotBounds(left, top, right, bottom);
446 }
447 }
448
449 super.onDraw(canvas);
450
451 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700452 final int scrollX = mScrollX;
453 final int scrollY = mScrollY;
454 if (scrollX == 0 && scrollY == 0) {
455 buttonDrawable.draw(canvas);
456 } else {
457 canvas.translate(scrollX, scrollY);
458 buttonDrawable.draw(canvas);
459 canvas.translate(-scrollX, -scrollY);
460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
463
464 @Override
465 protected int[] onCreateDrawableState(int extraSpace) {
466 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
467 if (isChecked()) {
468 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
469 }
470 return drawableState;
471 }
472
473 @Override
474 protected void drawableStateChanged() {
475 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400476
477 final Drawable buttonDrawable = mButtonDrawable;
478 if (buttonDrawable != null && buttonDrawable.isStateful()
479 && buttonDrawable.setState(getDrawableState())) {
480 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 }
482 }
483
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700484 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700485 public void drawableHotspotChanged(float x, float y) {
486 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700487
488 if (mButtonDrawable != null) {
489 mButtonDrawable.setHotspot(x, y);
490 }
491 }
492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500494 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 return super.verifyDrawable(who) || who == mButtonDrawable;
496 }
497
Dianne Hackborne2136772010-11-04 15:08:59 -0700498 @Override
499 public void jumpDrawablesToCurrentState() {
500 super.jumpDrawablesToCurrentState();
501 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
502 }
503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 static class SavedState extends BaseSavedState {
505 boolean checked;
506
507 /**
508 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
509 */
510 SavedState(Parcelable superState) {
511 super(superState);
512 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 /**
515 * Constructor called from {@link #CREATOR}
516 */
517 private SavedState(Parcel in) {
518 super(in);
519 checked = (Boolean)in.readValue(null);
520 }
521
522 @Override
523 public void writeToParcel(Parcel out, int flags) {
524 super.writeToParcel(out, flags);
525 out.writeValue(checked);
526 }
527
528 @Override
529 public String toString() {
530 return "CompoundButton.SavedState{"
531 + Integer.toHexString(System.identityHashCode(this))
532 + " checked=" + checked + "}";
533 }
534
Felipe Leme6d553872016-12-08 17:13:25 -0800535 @SuppressWarnings("hiding")
536 public static final Parcelable.Creator<SavedState> CREATOR =
537 new Parcelable.Creator<SavedState>() {
538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 public SavedState createFromParcel(Parcel in) {
540 return new SavedState(in);
541 }
542
Felipe Leme6d553872016-12-08 17:13:25 -0800543 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 public SavedState[] newArray(int size) {
545 return new SavedState[size];
546 }
547 };
548 }
549
550 @Override
551 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 Parcelable superState = super.onSaveInstanceState();
553
554 SavedState ss = new SavedState(superState);
555
556 ss.checked = isChecked();
557 return ss;
558 }
559
560 @Override
561 public void onRestoreInstanceState(Parcelable state) {
562 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 super.onRestoreInstanceState(ss.getSuperState());
565 setChecked(ss.checked);
566 requestLayout();
567 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700568
569 /** @hide */
570 @Override
571 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
572 super.encodeProperties(stream);
573 stream.addProperty("checked", isChecked());
574 }
Felipe Leme6d553872016-12-08 17:13:25 -0800575
Felipe Lemebab851c2017-02-03 18:45:08 -0800576 // TODO(b/33197203): add unit/CTS tests for auto-fill methods (and make sure they handle enable)
Felipe Leme6d553872016-12-08 17:13:25 -0800577
Felipe Leme0200d9e2017-01-24 15:10:26 -0800578 @Override
Felipe Lemec01a8732017-02-22 17:26:06 -0800579 public void onProvideAutoFillStructure(ViewStructure structure, int flags) {
580 super.onProvideAutoFillStructure(structure, flags);
581
582 structure.setSanitized(mCheckedFromResource);
583 }
584
585 @Override
Felipe Leme6d553872016-12-08 17:13:25 -0800586 public void autoFill(AutoFillValue value) {
Felipe Lemebab851c2017-02-03 18:45:08 -0800587 if (!isEnabled()) return;
588
Felipe Leme6d553872016-12-08 17:13:25 -0800589 setChecked(value.getToggleValue());
590 }
591
592 @Override
593 public AutoFillType getAutoFillType() {
594 return AutoFillType.forToggle();
595 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800596
597 @Override
598 public AutoFillValue getAutoFillValue() {
Felipe Leme5882c4f2017-02-16 21:46:46 -0800599 return isEnabled() ? AutoFillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601}