blob: ae58e2ae50e595a4232e43a52dd92c9cf440870f [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;
Philip P. Moltmann96689032017-03-09 13:19:55 -080031import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.view.Gravity;
Alan Viveretted4e77902014-10-27 17:50:51 -070033import android.view.SoundEffectConstants;
Steve Zeigler7a367882010-02-23 16:39:08 -080034import android.view.ViewDebug;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070035import android.view.ViewHierarchyEncoder;
Felipe Lemec01a8732017-02-22 17:26:06 -080036import android.view.ViewStructure;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070037import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070038import android.view.accessibility.AccessibilityNodeInfo;
Felipe Leme640f30a2017-03-06 15:44:06 -080039import android.view.autofill.AutofillManager;
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 {
Philip P. Moltmann96689032017-03-09 13:19:55 -080059 private static final String LOG_TAG = CompoundButton.class.getSimpleName();
Felipe Leme6d553872016-12-08 17:13:25 -080060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 private boolean mChecked;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070065 private ColorStateList mButtonTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070066 private PorterDuff.Mode mButtonTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070067 private boolean mHasButtonTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070068 private boolean mHasButtonTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 private OnCheckedChangeListener mOnCheckedChangeListener;
71 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
72
Felipe Lemec01a8732017-02-22 17:26:06 -080073 // Indicates whether the toggle state was set from resources or dynamically, so it can be used
Felipe Leme640f30a2017-03-06 15:44:06 -080074 // to sanitize autofill requests.
Felipe Lemec01a8732017-02-22 17:26:06 -080075 private boolean mCheckedFromResource = false;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 private static final int[] CHECKED_STATE_SET = {
78 R.attr.state_checked
79 };
80
81 public CompoundButton(Context context) {
82 this(context, null);
83 }
84
85 public CompoundButton(Context context, AttributeSet attrs) {
86 this(context, attrs, 0);
87 }
88
Alan Viverette617feb92013-09-09 18:09:13 -070089 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
90 this(context, attrs, defStyleAttr, 0);
91 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Alan Viverette617feb92013-09-09 18:09:13 -070093 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
94 super(context, attrs, defStyleAttr, defStyleRes);
95
96 final TypedArray a = context.obtainStyledAttributes(
97 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Alan Viverette91174362014-06-17 14:51:45 -070099 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 if (d != null) {
101 setButtonDrawable(d);
102 }
103
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700104 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
105 mButtonTintMode = Drawable.parseTintMode(a.getInt(
106 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
107 mHasButtonTintMode = true;
108 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700109
Alan Viverette91174362014-06-17 14:51:45 -0700110 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700111 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700112 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700113 }
114
115 final boolean checked = a.getBoolean(
116 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 setChecked(checked);
Felipe Lemec01a8732017-02-22 17:26:06 -0800118 mCheckedFromResource = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700121
122 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124
Felipe Leme6d553872016-12-08 17:13:25 -0800125 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public void toggle() {
127 setChecked(!mChecked);
128 }
129
130 @Override
131 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700133
134 final boolean handled = super.performClick();
135 if (!handled) {
136 // View only makes a sound effect if the onClickListener was
137 // called, so we'll need to make one here instead.
138 playSoundEffect(SoundEffectConstants.CLICK);
139 }
140
141 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 }
143
Steve Zeigler7a367882010-02-23 16:39:08 -0800144 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800145 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public boolean isChecked() {
147 return mChecked;
148 }
149
150 /**
151 * <p>Changes the checked state of this button.</p>
152 *
153 * @param checked true to check the button, false to uncheck it
154 */
Felipe Leme6d553872016-12-08 17:13:25 -0800155 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 public void setChecked(boolean checked) {
157 if (mChecked != checked) {
Felipe Lemec01a8732017-02-22 17:26:06 -0800158 mCheckedFromResource = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 mChecked = checked;
160 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700161 notifyViewAccessibilityStateChangedIfNeeded(
162 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 // Avoid infinite recursions if setChecked() is called from a listener
165 if (mBroadcasting) {
166 return;
167 }
168
169 mBroadcasting = true;
170 if (mOnCheckedChangeListener != null) {
171 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
172 }
173 if (mOnCheckedChangeWidgetListener != null) {
174 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
175 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800176 final AutofillManager afm = mContext.getSystemService(AutofillManager.class);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800177 if (afm != null) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700178 afm.notifyValueChanged(this);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800179 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700180
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700181 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
183 }
184
185 /**
186 * Register a callback to be invoked when the checked state of this button
187 * changes.
188 *
189 * @param listener the callback to call on checked state change
190 */
Jason Longacdaaea502016-12-01 22:54:37 -0800191 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 mOnCheckedChangeListener = listener;
193 }
194
195 /**
196 * Register a callback to be invoked when the checked state of this button
197 * changes. This callback is used for internal purpose only.
198 *
199 * @param listener the callback to call on checked state change
200 * @hide
201 */
202 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
203 mOnCheckedChangeWidgetListener = listener;
204 }
205
206 /**
207 * Interface definition for a callback to be invoked when the checked state
208 * of a compound button changed.
209 */
210 public static interface OnCheckedChangeListener {
211 /**
212 * Called when the checked state of a compound button has changed.
213 *
214 * @param buttonView The compound button view whose state has changed.
215 * @param isChecked The new checked state of buttonView.
216 */
217 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
218 }
219
220 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800221 * Sets a drawable as the compound button image given its resource
222 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800224 * @param resId the resource identifier of the drawable
225 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800227 public void setButtonDrawable(@DrawableRes int resId) {
228 final Drawable d;
229 if (resId != 0) {
230 d = getContext().getDrawable(resId);
231 } else {
232 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234 setButtonDrawable(d);
235 }
236
237 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800238 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800240 * @param drawable the drawable to set
241 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800243 public void setButtonDrawable(@Nullable Drawable drawable) {
244 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 if (mButtonDrawable != null) {
246 mButtonDrawable.setCallback(null);
247 unscheduleDrawable(mButtonDrawable);
248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
Alan Viverette6a394f42015-02-12 15:03:22 -0800250 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700251
Alan Viverette6a394f42015-02-12 15:03:22 -0800252 if (drawable != null) {
253 drawable.setCallback(this);
254 drawable.setLayoutDirection(getLayoutDirection());
255 if (drawable.isStateful()) {
256 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700257 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800258 drawable.setVisible(getVisibility() == VISIBLE, false);
259 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700260 applyButtonTint();
261 }
262 }
263 }
264
265 /**
Doris Liu3380e692015-06-30 11:26:47 -0700266 * @hide
267 */
268 @Override
269 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
270 super.onResolveDrawables(layoutDirection);
271 if (mButtonDrawable != null) {
272 mButtonDrawable.setLayoutDirection(layoutDirection);
273 }
274 }
275
276 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800277 * @return the drawable used as the compound button image
278 * @see #setButtonDrawable(Drawable)
279 * @see #setButtonDrawable(int)
280 */
281 @Nullable
282 public Drawable getButtonDrawable() {
283 return mButtonDrawable;
284 }
285
286 /**
Alan Viverette91174362014-06-17 14:51:45 -0700287 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700288 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700289 * <p>
290 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
291 * automatically mutate the drawable and apply the specified tint and tint
292 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700293 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700294 *
295 * @param tint the tint to apply, may be {@code null} to clear tint
296 *
297 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700298 * @see #setButtonTintList(ColorStateList)
299 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700300 */
Alan Viverettea4264452014-07-28 16:02:55 -0700301 public void setButtonTintList(@Nullable ColorStateList tint) {
302 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700303 mHasButtonTint = true;
304
305 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700306 }
307
308 /**
309 * @return the tint applied to the button drawable
310 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700311 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700312 */
313 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700314 public ColorStateList getButtonTintList() {
315 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700316 }
317
318 /**
319 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700320 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700321 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700322 *
323 * @param tintMode the blending mode used to apply the tint, may be
324 * {@code null} to clear tint
325 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700326 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700327 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700328 */
329 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700330 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700331 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700332
333 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700334 }
335
336 /**
337 * @return the blending mode used to apply the tint to the button drawable
338 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700339 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700340 */
341 @Nullable
342 public PorterDuff.Mode getButtonTintMode() {
343 return mButtonTintMode;
344 }
345
346 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700347 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700348 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700349
350 if (mHasButtonTint) {
351 mButtonDrawable.setTintList(mButtonTintList);
352 }
353
354 if (mHasButtonTintMode) {
355 mButtonDrawable.setTintMode(mButtonTintMode);
356 }
Alan Viveretted5133792014-10-28 14:41:36 -0700357
358 // The drawable (or one of its children) may not have been
359 // stateful before applying the tint, so let's try again.
360 if (mButtonDrawable.isStateful()) {
361 mButtonDrawable.setState(getDrawableState());
362 }
Alan Viverette91174362014-06-17 14:51:45 -0700363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800366 @Override
367 public CharSequence getAccessibilityClassName() {
368 return CompoundButton.class.getName();
369 }
370
Alan Viverettea54956a2015-01-07 16:05:02 -0800371 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800373 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
374 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700375 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700376 }
377
Alan Viverettea54956a2015-01-07 16:05:02 -0800378 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700379 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800380 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
381 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700382 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700383 info.setChecked(mChecked);
384 }
385
386 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700387 public int getCompoundPaddingLeft() {
388 int padding = super.getCompoundPaddingLeft();
389 if (!isLayoutRtl()) {
390 final Drawable buttonDrawable = mButtonDrawable;
391 if (buttonDrawable != null) {
392 padding += buttonDrawable.getIntrinsicWidth();
393 }
394 }
395 return padding;
396 }
397
398 @Override
399 public int getCompoundPaddingRight() {
400 int padding = super.getCompoundPaddingRight();
401 if (isLayoutRtl()) {
402 final Drawable buttonDrawable = mButtonDrawable;
403 if (buttonDrawable != null) {
404 padding += buttonDrawable.getIntrinsicWidth();
405 }
406 }
407 return padding;
408 }
409
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800410 /**
411 * @hide
412 */
413 @Override
414 public int getHorizontalOffsetForDrawables() {
415 final Drawable buttonDrawable = mButtonDrawable;
416 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
417 }
418
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700419 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 final Drawable buttonDrawable = mButtonDrawable;
422 if (buttonDrawable != null) {
423 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700424 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
425 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Alan Viverette61956602014-04-22 19:07:06 -0700427 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 switch (verticalGravity) {
429 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700430 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 break;
432 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700433 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 break;
Alan Viverette61956602014-04-22 19:07:06 -0700435 default:
436 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
Alan Viverette61956602014-04-22 19:07:06 -0700438 final int bottom = top + drawableHeight;
439 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
440 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700442 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700443
444 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700445 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700446 background.setHotspotBounds(left, top, right, bottom);
447 }
448 }
449
450 super.onDraw(canvas);
451
452 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700453 final int scrollX = mScrollX;
454 final int scrollY = mScrollY;
455 if (scrollX == 0 && scrollY == 0) {
456 buttonDrawable.draw(canvas);
457 } else {
458 canvas.translate(scrollX, scrollY);
459 buttonDrawable.draw(canvas);
460 canvas.translate(-scrollX, -scrollY);
461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463 }
464
465 @Override
466 protected int[] onCreateDrawableState(int extraSpace) {
467 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
468 if (isChecked()) {
469 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
470 }
471 return drawableState;
472 }
473
474 @Override
475 protected void drawableStateChanged() {
476 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400477
478 final Drawable buttonDrawable = mButtonDrawable;
479 if (buttonDrawable != null && buttonDrawable.isStateful()
480 && buttonDrawable.setState(getDrawableState())) {
481 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483 }
484
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700485 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700486 public void drawableHotspotChanged(float x, float y) {
487 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700488
489 if (mButtonDrawable != null) {
490 mButtonDrawable.setHotspot(x, y);
491 }
492 }
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500495 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 return super.verifyDrawable(who) || who == mButtonDrawable;
497 }
498
Dianne Hackborne2136772010-11-04 15:08:59 -0700499 @Override
500 public void jumpDrawablesToCurrentState() {
501 super.jumpDrawablesToCurrentState();
502 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 static class SavedState extends BaseSavedState {
506 boolean checked;
507
508 /**
509 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
510 */
511 SavedState(Parcelable superState) {
512 super(superState);
513 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 /**
516 * Constructor called from {@link #CREATOR}
517 */
518 private SavedState(Parcel in) {
519 super(in);
520 checked = (Boolean)in.readValue(null);
521 }
522
523 @Override
524 public void writeToParcel(Parcel out, int flags) {
525 super.writeToParcel(out, flags);
526 out.writeValue(checked);
527 }
528
529 @Override
530 public String toString() {
531 return "CompoundButton.SavedState{"
532 + Integer.toHexString(System.identityHashCode(this))
533 + " checked=" + checked + "}";
534 }
535
Felipe Leme6d553872016-12-08 17:13:25 -0800536 @SuppressWarnings("hiding")
537 public static final Parcelable.Creator<SavedState> CREATOR =
538 new Parcelable.Creator<SavedState>() {
539 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 public SavedState createFromParcel(Parcel in) {
541 return new SavedState(in);
542 }
543
Felipe Leme6d553872016-12-08 17:13:25 -0800544 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 public SavedState[] newArray(int size) {
546 return new SavedState[size];
547 }
548 };
549 }
550
551 @Override
552 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 Parcelable superState = super.onSaveInstanceState();
554
555 SavedState ss = new SavedState(superState);
556
557 ss.checked = isChecked();
558 return ss;
559 }
560
561 @Override
562 public void onRestoreInstanceState(Parcelable state) {
563 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 super.onRestoreInstanceState(ss.getSuperState());
566 setChecked(ss.checked);
567 requestLayout();
568 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700569
570 /** @hide */
571 @Override
572 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
573 super.encodeProperties(stream);
574 stream.addProperty("checked", isChecked());
575 }
Felipe Leme6d553872016-12-08 17:13:25 -0800576
Felipe Leme640f30a2017-03-06 15:44:06 -0800577 // TODO(b/33197203): add unit/CTS tests for autofill methods (and make sure they handle enable)
Felipe Leme6d553872016-12-08 17:13:25 -0800578
Felipe Leme0200d9e2017-01-24 15:10:26 -0800579 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800580 public void onProvideAutofillStructure(ViewStructure structure, int flags) {
581 super.onProvideAutofillStructure(structure, flags);
Felipe Lemec01a8732017-02-22 17:26:06 -0800582
583 structure.setSanitized(mCheckedFromResource);
584 }
585
586 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800587 public void autofill(AutofillValue value) {
Felipe Lemebab851c2017-02-03 18:45:08 -0800588 if (!isEnabled()) return;
589
Philip P. Moltmann96689032017-03-09 13:19:55 -0800590 if (value.isToggle()) {
591 setChecked(value.getToggleValue());
592 } else {
593 Log.w(LOG_TAG, value + " could not be autofilled into " + this);
594 }
Felipe Leme6d553872016-12-08 17:13:25 -0800595 }
596
597 @Override
Felipe Leme8931e302017-03-06 13:44:35 -0800598 public @AutofillType int getAutofillType() {
599 return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE;
Felipe Leme6d553872016-12-08 17:13:25 -0800600 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800601
602 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800603 public AutofillValue getAutofillValue() {
604 return isEnabled() ? AutofillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606}