blob: 80ec1c65293b59dbbc7b5ba32f48e719e4139991 [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;
Mathew Inwood978c6e22018-08-21 15:58:55 +010022import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Alan Viverette91174362014-06-17 14:51:45 -070024import android.content.res.ColorStateList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.res.TypedArray;
26import android.graphics.Canvas;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070027import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.drawable.Drawable;
29import android.os.Parcel;
30import android.os.Parcelable;
31import android.util.AttributeSet;
Philip P. Moltmann96689032017-03-09 13:19:55 -080032import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.view.Gravity;
Alan Viveretted4e77902014-10-27 17:50:51 -070034import android.view.SoundEffectConstants;
Steve Zeigler7a367882010-02-23 16:39:08 -080035import android.view.ViewDebug;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070036import android.view.ViewHierarchyEncoder;
Felipe Lemec01a8732017-02-22 17:26:06 -080037import android.view.ViewStructure;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070038import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070039import android.view.accessibility.AccessibilityNodeInfo;
Felipe Leme640f30a2017-03-06 15:44:06 -080040import android.view.autofill.AutofillManager;
41import android.view.autofill.AutofillValue;
Ashley Rose55f9f922019-01-28 19:29:36 -050042import android.view.inspector.InspectableProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Aurimas Liutikas99441c52016-10-11 16:48:32 -070044import com.android.internal.R;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/**
47 * <p>
48 * A button with two states, checked and unchecked. When the button is pressed
49 * or clicked, the state changes automatically.
50 * </p>
51 *
52 * <p><strong>XML attributes</strong></p>
53 * <p>
54 * See {@link android.R.styleable#CompoundButton
55 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
56 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
57 * android.R.styleable#View View Attributes}
58 * </p>
59 */
60public abstract class CompoundButton extends Button implements Checkable {
Philip P. Moltmann96689032017-03-09 13:19:55 -080061 private static final String LOG_TAG = CompoundButton.class.getSimpleName();
Felipe Leme6d553872016-12-08 17:13:25 -080062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 private boolean mChecked;
Mathew Inwood978c6e22018-08-21 15:58:55 +010064 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070066
Mathew Inwood978c6e22018-08-21 15:58:55 +010067 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070069 private ColorStateList mButtonTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070070 private PorterDuff.Mode mButtonTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070071 private boolean mHasButtonTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070072 private boolean mHasButtonTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070073
Mathew Inwood978c6e22018-08-21 15:58:55 +010074 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private OnCheckedChangeListener mOnCheckedChangeListener;
76 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
77
Felipe Lemec01a8732017-02-22 17:26:06 -080078 // Indicates whether the toggle state was set from resources or dynamically, so it can be used
Felipe Leme640f30a2017-03-06 15:44:06 -080079 // to sanitize autofill requests.
Felipe Lemec01a8732017-02-22 17:26:06 -080080 private boolean mCheckedFromResource = false;
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private static final int[] CHECKED_STATE_SET = {
83 R.attr.state_checked
84 };
85
86 public CompoundButton(Context context) {
87 this(context, null);
88 }
89
90 public CompoundButton(Context context, AttributeSet attrs) {
91 this(context, attrs, 0);
92 }
93
Alan Viverette617feb92013-09-09 18:09:13 -070094 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
95 this(context, attrs, defStyleAttr, 0);
96 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Alan Viverette617feb92013-09-09 18:09:13 -070098 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
99 super(context, attrs, defStyleAttr, defStyleRes);
100
101 final TypedArray a = context.obtainStyledAttributes(
102 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
Aurimas Liutikasab324cf2019-02-07 16:46:38 -0800103 saveAttributeDataForStyleable(context, com.android.internal.R.styleable.CompoundButton,
104 attrs, a, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Alan Viverette91174362014-06-17 14:51:45 -0700106 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 if (d != null) {
108 setButtonDrawable(d);
109 }
110
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700111 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
112 mButtonTintMode = Drawable.parseTintMode(a.getInt(
113 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
114 mHasButtonTintMode = true;
115 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700116
Alan Viverette91174362014-06-17 14:51:45 -0700117 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700118 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700119 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700120 }
121
122 final boolean checked = a.getBoolean(
123 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 setChecked(checked);
Felipe Lemec01a8732017-02-22 17:26:06 -0800125 mCheckedFromResource = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700128
129 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 }
131
Felipe Leme6d553872016-12-08 17:13:25 -0800132 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public void toggle() {
134 setChecked(!mChecked);
135 }
136
137 @Override
138 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700140
141 final boolean handled = super.performClick();
142 if (!handled) {
143 // View only makes a sound effect if the onClickListener was
144 // called, so we'll need to make one here instead.
145 playSoundEffect(SoundEffectConstants.CLICK);
146 }
147
148 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 }
150
Ashley Rose55f9f922019-01-28 19:29:36 -0500151 @InspectableProperty
Steve Zeigler7a367882010-02-23 16:39:08 -0800152 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800153 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public boolean isChecked() {
155 return mChecked;
156 }
157
158 /**
159 * <p>Changes the checked state of this button.</p>
160 *
161 * @param checked true to check the button, false to uncheck it
162 */
Felipe Leme6d553872016-12-08 17:13:25 -0800163 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public void setChecked(boolean checked) {
165 if (mChecked != checked) {
Felipe Lemec01a8732017-02-22 17:26:06 -0800166 mCheckedFromResource = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 mChecked = checked;
168 refreshDrawableState();
Eugene Susla72c510f2018-01-23 21:12:11 +0000169 notifyViewAccessibilityStateChangedIfNeeded(
Alan Viverette77e9a282013-09-12 17:16:09 -0700170 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
172 // Avoid infinite recursions if setChecked() is called from a listener
173 if (mBroadcasting) {
174 return;
175 }
176
177 mBroadcasting = true;
178 if (mOnCheckedChangeListener != null) {
179 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
180 }
181 if (mOnCheckedChangeWidgetListener != null) {
182 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
183 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800184 final AutofillManager afm = mContext.getSystemService(AutofillManager.class);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800185 if (afm != null) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700186 afm.notifyValueChanged(this);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800187 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700188
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700189 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191 }
192
193 /**
194 * Register a callback to be invoked when the checked state of this button
195 * changes.
196 *
197 * @param listener the callback to call on checked state change
198 */
Jason Longacdaaea502016-12-01 22:54:37 -0800199 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mOnCheckedChangeListener = listener;
201 }
202
203 /**
204 * Register a callback to be invoked when the checked state of this button
205 * changes. This callback is used for internal purpose only.
206 *
207 * @param listener the callback to call on checked state change
208 * @hide
209 */
210 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
211 mOnCheckedChangeWidgetListener = listener;
212 }
213
214 /**
215 * Interface definition for a callback to be invoked when the checked state
216 * of a compound button changed.
217 */
218 public static interface OnCheckedChangeListener {
219 /**
220 * Called when the checked state of a compound button has changed.
221 *
222 * @param buttonView The compound button view whose state has changed.
223 * @param isChecked The new checked state of buttonView.
224 */
225 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
226 }
227
228 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800229 * Sets a drawable as the compound button image given its resource
230 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800232 * @param resId the resource identifier of the drawable
233 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800235 public void setButtonDrawable(@DrawableRes int resId) {
236 final Drawable d;
237 if (resId != 0) {
238 d = getContext().getDrawable(resId);
239 } else {
240 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242 setButtonDrawable(d);
243 }
244
245 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800246 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800248 * @param drawable the drawable to set
249 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800251 public void setButtonDrawable(@Nullable Drawable drawable) {
252 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 if (mButtonDrawable != null) {
254 mButtonDrawable.setCallback(null);
255 unscheduleDrawable(mButtonDrawable);
256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Alan Viverette6a394f42015-02-12 15:03:22 -0800258 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700259
Alan Viverette6a394f42015-02-12 15:03:22 -0800260 if (drawable != null) {
261 drawable.setCallback(this);
262 drawable.setLayoutDirection(getLayoutDirection());
263 if (drawable.isStateful()) {
264 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700265 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800266 drawable.setVisible(getVisibility() == VISIBLE, false);
267 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700268 applyButtonTint();
269 }
270 }
271 }
272
273 /**
Doris Liu3380e692015-06-30 11:26:47 -0700274 * @hide
275 */
276 @Override
277 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
278 super.onResolveDrawables(layoutDirection);
279 if (mButtonDrawable != null) {
280 mButtonDrawable.setLayoutDirection(layoutDirection);
281 }
282 }
283
284 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800285 * @return the drawable used as the compound button image
286 * @see #setButtonDrawable(Drawable)
287 * @see #setButtonDrawable(int)
288 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500289 @InspectableProperty(name = "button")
Alan Viverette6a394f42015-02-12 15:03:22 -0800290 @Nullable
291 public Drawable getButtonDrawable() {
292 return mButtonDrawable;
293 }
294
295 /**
Alan Viverette91174362014-06-17 14:51:45 -0700296 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700297 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700298 * <p>
299 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
300 * automatically mutate the drawable and apply the specified tint and tint
301 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700302 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700303 *
304 * @param tint the tint to apply, may be {@code null} to clear tint
305 *
306 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700307 * @see #setButtonTintList(ColorStateList)
308 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700309 */
Alan Viverettea4264452014-07-28 16:02:55 -0700310 public void setButtonTintList(@Nullable ColorStateList tint) {
311 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700312 mHasButtonTint = true;
313
314 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700315 }
316
317 /**
318 * @return the tint applied to the button drawable
319 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700320 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700321 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500322 @InspectableProperty(name = "buttonTint")
Alan Viverette91174362014-06-17 14:51:45 -0700323 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700324 public ColorStateList getButtonTintList() {
325 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700326 }
327
328 /**
329 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700330 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700331 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700332 *
333 * @param tintMode the blending mode used to apply the tint, may be
334 * {@code null} to clear tint
335 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700336 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700337 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700338 */
339 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700340 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700341 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700342
343 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700344 }
345
346 /**
347 * @return the blending mode used to apply the tint to the button drawable
348 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700349 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700350 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500351 @InspectableProperty
Alan Viverette91174362014-06-17 14:51:45 -0700352 @Nullable
353 public PorterDuff.Mode getButtonTintMode() {
354 return mButtonTintMode;
355 }
356
357 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700358 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700359 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700360
361 if (mHasButtonTint) {
362 mButtonDrawable.setTintList(mButtonTintList);
363 }
364
365 if (mHasButtonTintMode) {
366 mButtonDrawable.setTintMode(mButtonTintMode);
367 }
Alan Viveretted5133792014-10-28 14:41:36 -0700368
369 // The drawable (or one of its children) may not have been
370 // stateful before applying the tint, so let's try again.
371 if (mButtonDrawable.isStateful()) {
372 mButtonDrawable.setState(getDrawableState());
373 }
Alan Viverette91174362014-06-17 14:51:45 -0700374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 }
376
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800377 @Override
378 public CharSequence getAccessibilityClassName() {
379 return CompoundButton.class.getName();
380 }
381
Alan Viverettea54956a2015-01-07 16:05:02 -0800382 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800384 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
385 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700386 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700387 }
388
Alan Viverettea54956a2015-01-07 16:05:02 -0800389 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700390 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800391 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
392 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700393 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700394 info.setChecked(mChecked);
395 }
396
397 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700398 public int getCompoundPaddingLeft() {
399 int padding = super.getCompoundPaddingLeft();
400 if (!isLayoutRtl()) {
401 final Drawable buttonDrawable = mButtonDrawable;
402 if (buttonDrawable != null) {
403 padding += buttonDrawable.getIntrinsicWidth();
404 }
405 }
406 return padding;
407 }
408
409 @Override
410 public int getCompoundPaddingRight() {
411 int padding = super.getCompoundPaddingRight();
412 if (isLayoutRtl()) {
413 final Drawable buttonDrawable = mButtonDrawable;
414 if (buttonDrawable != null) {
415 padding += buttonDrawable.getIntrinsicWidth();
416 }
417 }
418 return padding;
419 }
420
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800421 /**
422 * @hide
423 */
424 @Override
425 public int getHorizontalOffsetForDrawables() {
426 final Drawable buttonDrawable = mButtonDrawable;
427 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
428 }
429
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 final Drawable buttonDrawable = mButtonDrawable;
433 if (buttonDrawable != null) {
434 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700435 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
436 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
Alan Viverette61956602014-04-22 19:07:06 -0700438 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 switch (verticalGravity) {
440 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700441 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 break;
443 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700444 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 break;
Alan Viverette61956602014-04-22 19:07:06 -0700446 default:
447 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
Alan Viverette61956602014-04-22 19:07:06 -0700449 final int bottom = top + drawableHeight;
450 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
451 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700453 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700454
455 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700456 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700457 background.setHotspotBounds(left, top, right, bottom);
458 }
459 }
460
461 super.onDraw(canvas);
462
463 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700464 final int scrollX = mScrollX;
465 final int scrollY = mScrollY;
466 if (scrollX == 0 && scrollY == 0) {
467 buttonDrawable.draw(canvas);
468 } else {
469 canvas.translate(scrollX, scrollY);
470 buttonDrawable.draw(canvas);
471 canvas.translate(-scrollX, -scrollY);
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474 }
475
476 @Override
477 protected int[] onCreateDrawableState(int extraSpace) {
478 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
479 if (isChecked()) {
480 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
481 }
482 return drawableState;
483 }
484
485 @Override
486 protected void drawableStateChanged() {
487 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400488
489 final Drawable buttonDrawable = mButtonDrawable;
490 if (buttonDrawable != null && buttonDrawable.isStateful()
491 && buttonDrawable.setState(getDrawableState())) {
492 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494 }
495
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700496 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700497 public void drawableHotspotChanged(float x, float y) {
498 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700499
500 if (mButtonDrawable != null) {
501 mButtonDrawable.setHotspot(x, y);
502 }
503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500506 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 return super.verifyDrawable(who) || who == mButtonDrawable;
508 }
509
Dianne Hackborne2136772010-11-04 15:08:59 -0700510 @Override
511 public void jumpDrawablesToCurrentState() {
512 super.jumpDrawablesToCurrentState();
513 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
514 }
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 static class SavedState extends BaseSavedState {
517 boolean checked;
518
519 /**
520 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
521 */
522 SavedState(Parcelable superState) {
523 super(superState);
524 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 /**
527 * Constructor called from {@link #CREATOR}
528 */
529 private SavedState(Parcel in) {
530 super(in);
531 checked = (Boolean)in.readValue(null);
532 }
533
534 @Override
535 public void writeToParcel(Parcel out, int flags) {
536 super.writeToParcel(out, flags);
537 out.writeValue(checked);
538 }
539
540 @Override
541 public String toString() {
542 return "CompoundButton.SavedState{"
543 + Integer.toHexString(System.identityHashCode(this))
544 + " checked=" + checked + "}";
545 }
546
Felipe Leme6d553872016-12-08 17:13:25 -0800547 @SuppressWarnings("hiding")
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700548 public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
Felipe Leme6d553872016-12-08 17:13:25 -0800549 new Parcelable.Creator<SavedState>() {
550 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 public SavedState createFromParcel(Parcel in) {
552 return new SavedState(in);
553 }
554
Felipe Leme6d553872016-12-08 17:13:25 -0800555 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 public SavedState[] newArray(int size) {
557 return new SavedState[size];
558 }
559 };
560 }
561
562 @Override
563 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 Parcelable superState = super.onSaveInstanceState();
565
566 SavedState ss = new SavedState(superState);
567
568 ss.checked = isChecked();
569 return ss;
570 }
571
572 @Override
573 public void onRestoreInstanceState(Parcelable state) {
574 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 super.onRestoreInstanceState(ss.getSuperState());
577 setChecked(ss.checked);
578 requestLayout();
579 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700580
581 /** @hide */
582 @Override
583 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
584 super.encodeProperties(stream);
585 stream.addProperty("checked", isChecked());
586 }
Felipe Leme6d553872016-12-08 17:13:25 -0800587
Felipe Lemec01a8732017-02-22 17:26:06 -0800588
Felipe Leme92736c12018-11-13 12:00:59 -0800589 /** @hide */
590 @Override
591 protected void onProvideStructure(@NonNull ViewStructure structure,
592 @ViewStructureType int viewFor, int flags) {
593 super.onProvideStructure(structure, viewFor, flags);
594
595 if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) {
596 structure.setDataIsSensitive(!mCheckedFromResource);
597 }
Felipe Lemec01a8732017-02-22 17:26:06 -0800598 }
599
600 @Override
Felipe Leme955e2522017-03-29 17:47:58 -0700601 public void autofill(AutofillValue value) {
602 if (!isEnabled()) return;
Felipe Lemebab851c2017-02-03 18:45:08 -0800603
Felipe Leme955e2522017-03-29 17:47:58 -0700604 if (!value.isToggle()) {
Philip P. Moltmann96689032017-03-09 13:19:55 -0800605 Log.w(LOG_TAG, value + " could not be autofilled into " + this);
Felipe Leme955e2522017-03-29 17:47:58 -0700606 return;
Philip P. Moltmann96689032017-03-09 13:19:55 -0800607 }
Philip P. Moltmann7b771162017-03-03 17:22:57 -0800608
Felipe Leme955e2522017-03-29 17:47:58 -0700609 setChecked(value.getToggleValue());
Felipe Leme6d553872016-12-08 17:13:25 -0800610 }
611
612 @Override
Felipe Leme8931e302017-03-06 13:44:35 -0800613 public @AutofillType int getAutofillType() {
614 return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE;
Felipe Leme6d553872016-12-08 17:13:25 -0800615 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800616
617 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800618 public AutofillValue getAutofillValue() {
619 return isEnabled() ? AutofillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621}