blob: 6a95fe3b6ff3e703bcd949c27985f7408b0facad [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 Leme640f30a2017-03-06 15:44:06 -080038import android.view.autofill.AutofillManager;
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
Felipe Lemec01a8732017-02-22 17:26:06 -080071 // Indicates whether the toggle state was set from resources or dynamically, so it can be used
Felipe Leme640f30a2017-03-06 15:44:06 -080072 // to sanitize autofill requests.
Felipe Lemec01a8732017-02-22 17:26:06 -080073 private boolean mCheckedFromResource = false;
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private static final int[] CHECKED_STATE_SET = {
76 R.attr.state_checked
77 };
78
79 public CompoundButton(Context context) {
80 this(context, null);
81 }
82
83 public CompoundButton(Context context, AttributeSet attrs) {
84 this(context, attrs, 0);
85 }
86
Alan Viverette617feb92013-09-09 18:09:13 -070087 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
88 this(context, attrs, defStyleAttr, 0);
89 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
Alan Viverette617feb92013-09-09 18:09:13 -070091 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
92 super(context, attrs, defStyleAttr, defStyleRes);
93
94 final TypedArray a = context.obtainStyledAttributes(
95 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Alan Viverette91174362014-06-17 14:51:45 -070097 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 if (d != null) {
99 setButtonDrawable(d);
100 }
101
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700102 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
103 mButtonTintMode = Drawable.parseTintMode(a.getInt(
104 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
105 mHasButtonTintMode = true;
106 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700107
Alan Viverette91174362014-06-17 14:51:45 -0700108 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700109 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700110 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700111 }
112
113 final boolean checked = a.getBoolean(
114 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 setChecked(checked);
Felipe Lemec01a8732017-02-22 17:26:06 -0800116 mCheckedFromResource = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
118 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700119
120 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 }
122
Felipe Leme6d553872016-12-08 17:13:25 -0800123 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public void toggle() {
125 setChecked(!mChecked);
126 }
127
128 @Override
129 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700131
132 final boolean handled = super.performClick();
133 if (!handled) {
134 // View only makes a sound effect if the onClickListener was
135 // called, so we'll need to make one here instead.
136 playSoundEffect(SoundEffectConstants.CLICK);
137 }
138
139 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 }
141
Steve Zeigler7a367882010-02-23 16:39:08 -0800142 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800143 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 public boolean isChecked() {
145 return mChecked;
146 }
147
148 /**
149 * <p>Changes the checked state of this button.</p>
150 *
151 * @param checked true to check the button, false to uncheck it
152 */
Felipe Leme6d553872016-12-08 17:13:25 -0800153 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public void setChecked(boolean checked) {
155 if (mChecked != checked) {
Felipe Lemec01a8732017-02-22 17:26:06 -0800156 mCheckedFromResource = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 mChecked = checked;
158 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700159 notifyViewAccessibilityStateChangedIfNeeded(
160 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
162 // Avoid infinite recursions if setChecked() is called from a listener
163 if (mBroadcasting) {
164 return;
165 }
166
167 mBroadcasting = true;
168 if (mOnCheckedChangeListener != null) {
169 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
170 }
171 if (mOnCheckedChangeWidgetListener != null) {
172 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
173 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800174 final AutofillManager afm = mContext.getSystemService(AutofillManager.class);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800175 if (afm != null) {
176 afm.valueChanged(this);
177 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700178
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700179 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
181 }
182
183 /**
184 * Register a callback to be invoked when the checked state of this button
185 * changes.
186 *
187 * @param listener the callback to call on checked state change
188 */
Jason Longacdaaea502016-12-01 22:54:37 -0800189 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 mOnCheckedChangeListener = listener;
191 }
192
193 /**
194 * Register a callback to be invoked when the checked state of this button
195 * changes. This callback is used for internal purpose only.
196 *
197 * @param listener the callback to call on checked state change
198 * @hide
199 */
200 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
201 mOnCheckedChangeWidgetListener = listener;
202 }
203
204 /**
205 * Interface definition for a callback to be invoked when the checked state
206 * of a compound button changed.
207 */
208 public static interface OnCheckedChangeListener {
209 /**
210 * Called when the checked state of a compound button has changed.
211 *
212 * @param buttonView The compound button view whose state has changed.
213 * @param isChecked The new checked state of buttonView.
214 */
215 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
216 }
217
218 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800219 * Sets a drawable as the compound button image given its resource
220 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800222 * @param resId the resource identifier of the drawable
223 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800225 public void setButtonDrawable(@DrawableRes int resId) {
226 final Drawable d;
227 if (resId != 0) {
228 d = getContext().getDrawable(resId);
229 } else {
230 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232 setButtonDrawable(d);
233 }
234
235 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800236 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800238 * @param drawable the drawable to set
239 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800241 public void setButtonDrawable(@Nullable Drawable drawable) {
242 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 if (mButtonDrawable != null) {
244 mButtonDrawable.setCallback(null);
245 unscheduleDrawable(mButtonDrawable);
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Alan Viverette6a394f42015-02-12 15:03:22 -0800248 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700249
Alan Viverette6a394f42015-02-12 15:03:22 -0800250 if (drawable != null) {
251 drawable.setCallback(this);
252 drawable.setLayoutDirection(getLayoutDirection());
253 if (drawable.isStateful()) {
254 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700255 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800256 drawable.setVisible(getVisibility() == VISIBLE, false);
257 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700258 applyButtonTint();
259 }
260 }
261 }
262
263 /**
Doris Liu3380e692015-06-30 11:26:47 -0700264 * @hide
265 */
266 @Override
267 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
268 super.onResolveDrawables(layoutDirection);
269 if (mButtonDrawable != null) {
270 mButtonDrawable.setLayoutDirection(layoutDirection);
271 }
272 }
273
274 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800275 * @return the drawable used as the compound button image
276 * @see #setButtonDrawable(Drawable)
277 * @see #setButtonDrawable(int)
278 */
279 @Nullable
280 public Drawable getButtonDrawable() {
281 return mButtonDrawable;
282 }
283
284 /**
Alan Viverette91174362014-06-17 14:51:45 -0700285 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700286 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700287 * <p>
288 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
289 * automatically mutate the drawable and apply the specified tint and tint
290 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700291 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700292 *
293 * @param tint the tint to apply, may be {@code null} to clear tint
294 *
295 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700296 * @see #setButtonTintList(ColorStateList)
297 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700298 */
Alan Viverettea4264452014-07-28 16:02:55 -0700299 public void setButtonTintList(@Nullable ColorStateList tint) {
300 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700301 mHasButtonTint = true;
302
303 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700304 }
305
306 /**
307 * @return the tint applied to the button drawable
308 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700309 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700310 */
311 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700312 public ColorStateList getButtonTintList() {
313 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700314 }
315
316 /**
317 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700318 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700319 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700320 *
321 * @param tintMode the blending mode used to apply the tint, may be
322 * {@code null} to clear tint
323 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700324 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700325 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700326 */
327 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700328 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700329 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700330
331 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700332 }
333
334 /**
335 * @return the blending mode used to apply the tint to the button drawable
336 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700337 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700338 */
339 @Nullable
340 public PorterDuff.Mode getButtonTintMode() {
341 return mButtonTintMode;
342 }
343
344 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700345 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700346 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700347
348 if (mHasButtonTint) {
349 mButtonDrawable.setTintList(mButtonTintList);
350 }
351
352 if (mHasButtonTintMode) {
353 mButtonDrawable.setTintMode(mButtonTintMode);
354 }
Alan Viveretted5133792014-10-28 14:41:36 -0700355
356 // The drawable (or one of its children) may not have been
357 // stateful before applying the tint, so let's try again.
358 if (mButtonDrawable.isStateful()) {
359 mButtonDrawable.setState(getDrawableState());
360 }
Alan Viverette91174362014-06-17 14:51:45 -0700361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
363
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800364 @Override
365 public CharSequence getAccessibilityClassName() {
366 return CompoundButton.class.getName();
367 }
368
Alan Viverettea54956a2015-01-07 16:05:02 -0800369 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800371 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
372 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700373 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700374 }
375
Alan Viverettea54956a2015-01-07 16:05:02 -0800376 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700377 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800378 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
379 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700380 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700381 info.setChecked(mChecked);
382 }
383
384 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700385 public int getCompoundPaddingLeft() {
386 int padding = super.getCompoundPaddingLeft();
387 if (!isLayoutRtl()) {
388 final Drawable buttonDrawable = mButtonDrawable;
389 if (buttonDrawable != null) {
390 padding += buttonDrawable.getIntrinsicWidth();
391 }
392 }
393 return padding;
394 }
395
396 @Override
397 public int getCompoundPaddingRight() {
398 int padding = super.getCompoundPaddingRight();
399 if (isLayoutRtl()) {
400 final Drawable buttonDrawable = mButtonDrawable;
401 if (buttonDrawable != null) {
402 padding += buttonDrawable.getIntrinsicWidth();
403 }
404 }
405 return padding;
406 }
407
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800408 /**
409 * @hide
410 */
411 @Override
412 public int getHorizontalOffsetForDrawables() {
413 final Drawable buttonDrawable = mButtonDrawable;
414 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
415 }
416
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700417 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 final Drawable buttonDrawable = mButtonDrawable;
420 if (buttonDrawable != null) {
421 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700422 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
423 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424
Alan Viverette61956602014-04-22 19:07:06 -0700425 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 switch (verticalGravity) {
427 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700428 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 break;
430 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700431 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 break;
Alan Viverette61956602014-04-22 19:07:06 -0700433 default:
434 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
Alan Viverette61956602014-04-22 19:07:06 -0700436 final int bottom = top + drawableHeight;
437 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
438 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700440 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700441
442 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700443 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700444 background.setHotspotBounds(left, top, right, bottom);
445 }
446 }
447
448 super.onDraw(canvas);
449
450 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700451 final int scrollX = mScrollX;
452 final int scrollY = mScrollY;
453 if (scrollX == 0 && scrollY == 0) {
454 buttonDrawable.draw(canvas);
455 } else {
456 canvas.translate(scrollX, scrollY);
457 buttonDrawable.draw(canvas);
458 canvas.translate(-scrollX, -scrollY);
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 }
462
463 @Override
464 protected int[] onCreateDrawableState(int extraSpace) {
465 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
466 if (isChecked()) {
467 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
468 }
469 return drawableState;
470 }
471
472 @Override
473 protected void drawableStateChanged() {
474 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400475
476 final Drawable buttonDrawable = mButtonDrawable;
477 if (buttonDrawable != null && buttonDrawable.isStateful()
478 && buttonDrawable.setState(getDrawableState())) {
479 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481 }
482
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700483 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700484 public void drawableHotspotChanged(float x, float y) {
485 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700486
487 if (mButtonDrawable != null) {
488 mButtonDrawable.setHotspot(x, y);
489 }
490 }
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500493 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 return super.verifyDrawable(who) || who == mButtonDrawable;
495 }
496
Dianne Hackborne2136772010-11-04 15:08:59 -0700497 @Override
498 public void jumpDrawablesToCurrentState() {
499 super.jumpDrawablesToCurrentState();
500 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
501 }
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 static class SavedState extends BaseSavedState {
504 boolean checked;
505
506 /**
507 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
508 */
509 SavedState(Parcelable superState) {
510 super(superState);
511 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 /**
514 * Constructor called from {@link #CREATOR}
515 */
516 private SavedState(Parcel in) {
517 super(in);
518 checked = (Boolean)in.readValue(null);
519 }
520
521 @Override
522 public void writeToParcel(Parcel out, int flags) {
523 super.writeToParcel(out, flags);
524 out.writeValue(checked);
525 }
526
527 @Override
528 public String toString() {
529 return "CompoundButton.SavedState{"
530 + Integer.toHexString(System.identityHashCode(this))
531 + " checked=" + checked + "}";
532 }
533
Felipe Leme6d553872016-12-08 17:13:25 -0800534 @SuppressWarnings("hiding")
535 public static final Parcelable.Creator<SavedState> CREATOR =
536 new Parcelable.Creator<SavedState>() {
537 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 public SavedState createFromParcel(Parcel in) {
539 return new SavedState(in);
540 }
541
Felipe Leme6d553872016-12-08 17:13:25 -0800542 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 public SavedState[] newArray(int size) {
544 return new SavedState[size];
545 }
546 };
547 }
548
549 @Override
550 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 Parcelable superState = super.onSaveInstanceState();
552
553 SavedState ss = new SavedState(superState);
554
555 ss.checked = isChecked();
556 return ss;
557 }
558
559 @Override
560 public void onRestoreInstanceState(Parcelable state) {
561 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 super.onRestoreInstanceState(ss.getSuperState());
564 setChecked(ss.checked);
565 requestLayout();
566 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700567
568 /** @hide */
569 @Override
570 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
571 super.encodeProperties(stream);
572 stream.addProperty("checked", isChecked());
573 }
Felipe Leme6d553872016-12-08 17:13:25 -0800574
Felipe Leme640f30a2017-03-06 15:44:06 -0800575 // TODO(b/33197203): add unit/CTS tests for autofill methods (and make sure they handle enable)
Felipe Leme6d553872016-12-08 17:13:25 -0800576
Felipe Leme0200d9e2017-01-24 15:10:26 -0800577 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800578 public void onProvideAutofillStructure(ViewStructure structure, int flags) {
579 super.onProvideAutofillStructure(structure, flags);
Felipe Lemec01a8732017-02-22 17:26:06 -0800580
Felipe Lemec9a19b12017-03-13 18:05:22 -0700581 structure.setDataIsSensitive(!mCheckedFromResource);
Felipe Lemec01a8732017-02-22 17:26:06 -0800582 }
583
584 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800585 public void autofill(AutofillValue value) {
Felipe Lemebab851c2017-02-03 18:45:08 -0800586 if (!isEnabled()) return;
587
Felipe Leme6d553872016-12-08 17:13:25 -0800588 setChecked(value.getToggleValue());
589 }
590
591 @Override
Felipe Leme8931e302017-03-06 13:44:35 -0800592 public @AutofillType int getAutofillType() {
593 return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE;
Felipe Leme6d553872016-12-08 17:13:25 -0800594 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800595
596 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800597 public AutofillValue getAutofillValue() {
598 return isEnabled() ? AutofillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600}