blob: 8d094898d9098eac03cb98c5942e16962d82491c [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Aurimas Liutikas99441c52016-10-11 16:48:32 -070043import com.android.internal.R;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/**
46 * <p>
47 * A button with two states, checked and unchecked. When the button is pressed
48 * or clicked, the state changes automatically.
49 * </p>
50 *
51 * <p><strong>XML attributes</strong></p>
52 * <p>
53 * See {@link android.R.styleable#CompoundButton
54 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
55 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
56 * android.R.styleable#View View Attributes}
57 * </p>
58 */
59public abstract class CompoundButton extends Button implements Checkable {
Philip P. Moltmann96689032017-03-09 13:19:55 -080060 private static final String LOG_TAG = CompoundButton.class.getSimpleName();
Felipe Leme6d553872016-12-08 17:13:25 -080061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 private boolean mChecked;
Mathew Inwood978c6e22018-08-21 15:58:55 +010063 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070065
Mathew Inwood978c6e22018-08-21 15:58:55 +010066 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070068 private ColorStateList mButtonTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070069 private PorterDuff.Mode mButtonTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070070 private boolean mHasButtonTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070071 private boolean mHasButtonTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070072
Mathew Inwood978c6e22018-08-21 15:58:55 +010073 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private OnCheckedChangeListener mOnCheckedChangeListener;
75 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
76
Felipe Lemec01a8732017-02-22 17:26:06 -080077 // Indicates whether the toggle state was set from resources or dynamically, so it can be used
Felipe Leme640f30a2017-03-06 15:44:06 -080078 // to sanitize autofill requests.
Felipe Lemec01a8732017-02-22 17:26:06 -080079 private boolean mCheckedFromResource = false;
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private static final int[] CHECKED_STATE_SET = {
82 R.attr.state_checked
83 };
84
85 public CompoundButton(Context context) {
86 this(context, null);
87 }
88
89 public CompoundButton(Context context, AttributeSet attrs) {
90 this(context, attrs, 0);
91 }
92
Alan Viverette617feb92013-09-09 18:09:13 -070093 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
94 this(context, attrs, defStyleAttr, 0);
95 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Alan Viverette617feb92013-09-09 18:09:13 -070097 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
98 super(context, attrs, defStyleAttr, defStyleRes);
99
100 final TypedArray a = context.obtainStyledAttributes(
101 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Alan Viverette91174362014-06-17 14:51:45 -0700103 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 if (d != null) {
105 setButtonDrawable(d);
106 }
107
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700108 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
109 mButtonTintMode = Drawable.parseTintMode(a.getInt(
110 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
111 mHasButtonTintMode = true;
112 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700113
Alan Viverette91174362014-06-17 14:51:45 -0700114 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700115 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700116 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700117 }
118
119 final boolean checked = a.getBoolean(
120 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 setChecked(checked);
Felipe Lemec01a8732017-02-22 17:26:06 -0800122 mCheckedFromResource = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700125
126 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
128
Felipe Leme6d553872016-12-08 17:13:25 -0800129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public void toggle() {
131 setChecked(!mChecked);
132 }
133
134 @Override
135 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700137
138 final boolean handled = super.performClick();
139 if (!handled) {
140 // View only makes a sound effect if the onClickListener was
141 // called, so we'll need to make one here instead.
142 playSoundEffect(SoundEffectConstants.CLICK);
143 }
144
145 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147
Steve Zeigler7a367882010-02-23 16:39:08 -0800148 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800149 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 public boolean isChecked() {
151 return mChecked;
152 }
153
154 /**
155 * <p>Changes the checked state of this button.</p>
156 *
157 * @param checked true to check the button, false to uncheck it
158 */
Felipe Leme6d553872016-12-08 17:13:25 -0800159 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public void setChecked(boolean checked) {
161 if (mChecked != checked) {
Felipe Lemec01a8732017-02-22 17:26:06 -0800162 mCheckedFromResource = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 mChecked = checked;
164 refreshDrawableState();
Eugene Susla72c510f2018-01-23 21:12:11 +0000165 notifyViewAccessibilityStateChangedIfNeeded(
Alan Viverette77e9a282013-09-12 17:16:09 -0700166 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 // Avoid infinite recursions if setChecked() is called from a listener
169 if (mBroadcasting) {
170 return;
171 }
172
173 mBroadcasting = true;
174 if (mOnCheckedChangeListener != null) {
175 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
176 }
177 if (mOnCheckedChangeWidgetListener != null) {
178 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
179 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800180 final AutofillManager afm = mContext.getSystemService(AutofillManager.class);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800181 if (afm != null) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700182 afm.notifyValueChanged(this);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800183 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700184
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700185 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
187 }
188
189 /**
190 * Register a callback to be invoked when the checked state of this button
191 * changes.
192 *
193 * @param listener the callback to call on checked state change
194 */
Jason Longacdaaea502016-12-01 22:54:37 -0800195 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 mOnCheckedChangeListener = listener;
197 }
198
199 /**
200 * Register a callback to be invoked when the checked state of this button
201 * changes. This callback is used for internal purpose only.
202 *
203 * @param listener the callback to call on checked state change
204 * @hide
205 */
206 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
207 mOnCheckedChangeWidgetListener = listener;
208 }
209
210 /**
211 * Interface definition for a callback to be invoked when the checked state
212 * of a compound button changed.
213 */
214 public static interface OnCheckedChangeListener {
215 /**
216 * Called when the checked state of a compound button has changed.
217 *
218 * @param buttonView The compound button view whose state has changed.
219 * @param isChecked The new checked state of buttonView.
220 */
221 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
222 }
223
224 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800225 * Sets a drawable as the compound button image given its resource
226 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800228 * @param resId the resource identifier of the drawable
229 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800231 public void setButtonDrawable(@DrawableRes int resId) {
232 final Drawable d;
233 if (resId != 0) {
234 d = getContext().getDrawable(resId);
235 } else {
236 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238 setButtonDrawable(d);
239 }
240
241 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800242 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800244 * @param drawable the drawable to set
245 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800247 public void setButtonDrawable(@Nullable Drawable drawable) {
248 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 if (mButtonDrawable != null) {
250 mButtonDrawable.setCallback(null);
251 unscheduleDrawable(mButtonDrawable);
252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Alan Viverette6a394f42015-02-12 15:03:22 -0800254 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700255
Alan Viverette6a394f42015-02-12 15:03:22 -0800256 if (drawable != null) {
257 drawable.setCallback(this);
258 drawable.setLayoutDirection(getLayoutDirection());
259 if (drawable.isStateful()) {
260 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700261 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800262 drawable.setVisible(getVisibility() == VISIBLE, false);
263 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700264 applyButtonTint();
265 }
266 }
267 }
268
269 /**
Doris Liu3380e692015-06-30 11:26:47 -0700270 * @hide
271 */
272 @Override
273 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
274 super.onResolveDrawables(layoutDirection);
275 if (mButtonDrawable != null) {
276 mButtonDrawable.setLayoutDirection(layoutDirection);
277 }
278 }
279
280 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800281 * @return the drawable used as the compound button image
282 * @see #setButtonDrawable(Drawable)
283 * @see #setButtonDrawable(int)
284 */
285 @Nullable
286 public Drawable getButtonDrawable() {
287 return mButtonDrawable;
288 }
289
290 /**
Alan Viverette91174362014-06-17 14:51:45 -0700291 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700292 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700293 * <p>
294 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
295 * automatically mutate the drawable and apply the specified tint and tint
296 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700297 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700298 *
299 * @param tint the tint to apply, may be {@code null} to clear tint
300 *
301 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700302 * @see #setButtonTintList(ColorStateList)
303 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700304 */
Alan Viverettea4264452014-07-28 16:02:55 -0700305 public void setButtonTintList(@Nullable ColorStateList tint) {
306 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700307 mHasButtonTint = true;
308
309 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700310 }
311
312 /**
313 * @return the tint applied to the button drawable
314 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700315 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700316 */
317 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700318 public ColorStateList getButtonTintList() {
319 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700320 }
321
322 /**
323 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700324 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700325 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700326 *
327 * @param tintMode the blending mode used to apply the tint, may be
328 * {@code null} to clear tint
329 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700330 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700331 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700332 */
333 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700334 mButtonTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700335 mHasButtonTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700336
337 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700338 }
339
340 /**
341 * @return the blending mode used to apply the tint to the button drawable
342 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700343 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700344 */
345 @Nullable
346 public PorterDuff.Mode getButtonTintMode() {
347 return mButtonTintMode;
348 }
349
350 private void applyButtonTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700351 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700352 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700353
354 if (mHasButtonTint) {
355 mButtonDrawable.setTintList(mButtonTintList);
356 }
357
358 if (mHasButtonTintMode) {
359 mButtonDrawable.setTintMode(mButtonTintMode);
360 }
Alan Viveretted5133792014-10-28 14:41:36 -0700361
362 // The drawable (or one of its children) may not have been
363 // stateful before applying the tint, so let's try again.
364 if (mButtonDrawable.isStateful()) {
365 mButtonDrawable.setState(getDrawableState());
366 }
Alan Viverette91174362014-06-17 14:51:45 -0700367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
369
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800370 @Override
371 public CharSequence getAccessibilityClassName() {
372 return CompoundButton.class.getName();
373 }
374
Alan Viverettea54956a2015-01-07 16:05:02 -0800375 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800377 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
378 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700379 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700380 }
381
Alan Viverettea54956a2015-01-07 16:05:02 -0800382 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700383 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800384 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
385 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700386 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700387 info.setChecked(mChecked);
388 }
389
390 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700391 public int getCompoundPaddingLeft() {
392 int padding = super.getCompoundPaddingLeft();
393 if (!isLayoutRtl()) {
394 final Drawable buttonDrawable = mButtonDrawable;
395 if (buttonDrawable != null) {
396 padding += buttonDrawable.getIntrinsicWidth();
397 }
398 }
399 return padding;
400 }
401
402 @Override
403 public int getCompoundPaddingRight() {
404 int padding = super.getCompoundPaddingRight();
405 if (isLayoutRtl()) {
406 final Drawable buttonDrawable = mButtonDrawable;
407 if (buttonDrawable != null) {
408 padding += buttonDrawable.getIntrinsicWidth();
409 }
410 }
411 return padding;
412 }
413
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800414 /**
415 * @hide
416 */
417 @Override
418 public int getHorizontalOffsetForDrawables() {
419 final Drawable buttonDrawable = mButtonDrawable;
420 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
421 }
422
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700423 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 final Drawable buttonDrawable = mButtonDrawable;
426 if (buttonDrawable != null) {
427 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700428 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
429 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
Alan Viverette61956602014-04-22 19:07:06 -0700431 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 switch (verticalGravity) {
433 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700434 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 break;
436 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700437 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 break;
Alan Viverette61956602014-04-22 19:07:06 -0700439 default:
440 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Alan Viverette61956602014-04-22 19:07:06 -0700442 final int bottom = top + drawableHeight;
443 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
444 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700446 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700447
448 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700449 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700450 background.setHotspotBounds(left, top, right, bottom);
451 }
452 }
453
454 super.onDraw(canvas);
455
456 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700457 final int scrollX = mScrollX;
458 final int scrollY = mScrollY;
459 if (scrollX == 0 && scrollY == 0) {
460 buttonDrawable.draw(canvas);
461 } else {
462 canvas.translate(scrollX, scrollY);
463 buttonDrawable.draw(canvas);
464 canvas.translate(-scrollX, -scrollY);
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468
469 @Override
470 protected int[] onCreateDrawableState(int extraSpace) {
471 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
472 if (isChecked()) {
473 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
474 }
475 return drawableState;
476 }
477
478 @Override
479 protected void drawableStateChanged() {
480 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400481
482 final Drawable buttonDrawable = mButtonDrawable;
483 if (buttonDrawable != null && buttonDrawable.isStateful()
484 && buttonDrawable.setState(getDrawableState())) {
485 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
488
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700489 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700490 public void drawableHotspotChanged(float x, float y) {
491 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700492
493 if (mButtonDrawable != null) {
494 mButtonDrawable.setHotspot(x, y);
495 }
496 }
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500499 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 return super.verifyDrawable(who) || who == mButtonDrawable;
501 }
502
Dianne Hackborne2136772010-11-04 15:08:59 -0700503 @Override
504 public void jumpDrawablesToCurrentState() {
505 super.jumpDrawablesToCurrentState();
506 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
507 }
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 static class SavedState extends BaseSavedState {
510 boolean checked;
511
512 /**
513 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
514 */
515 SavedState(Parcelable superState) {
516 super(superState);
517 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 /**
520 * Constructor called from {@link #CREATOR}
521 */
522 private SavedState(Parcel in) {
523 super(in);
524 checked = (Boolean)in.readValue(null);
525 }
526
527 @Override
528 public void writeToParcel(Parcel out, int flags) {
529 super.writeToParcel(out, flags);
530 out.writeValue(checked);
531 }
532
533 @Override
534 public String toString() {
535 return "CompoundButton.SavedState{"
536 + Integer.toHexString(System.identityHashCode(this))
537 + " checked=" + checked + "}";
538 }
539
Felipe Leme6d553872016-12-08 17:13:25 -0800540 @SuppressWarnings("hiding")
541 public static final Parcelable.Creator<SavedState> CREATOR =
542 new Parcelable.Creator<SavedState>() {
543 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 public SavedState createFromParcel(Parcel in) {
545 return new SavedState(in);
546 }
547
Felipe Leme6d553872016-12-08 17:13:25 -0800548 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public SavedState[] newArray(int size) {
550 return new SavedState[size];
551 }
552 };
553 }
554
555 @Override
556 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 Parcelable superState = super.onSaveInstanceState();
558
559 SavedState ss = new SavedState(superState);
560
561 ss.checked = isChecked();
562 return ss;
563 }
564
565 @Override
566 public void onRestoreInstanceState(Parcelable state) {
567 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 super.onRestoreInstanceState(ss.getSuperState());
570 setChecked(ss.checked);
571 requestLayout();
572 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700573
574 /** @hide */
575 @Override
576 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
577 super.encodeProperties(stream);
578 stream.addProperty("checked", isChecked());
579 }
Felipe Leme6d553872016-12-08 17:13:25 -0800580
Felipe Leme0200d9e2017-01-24 15:10:26 -0800581 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800582 public void onProvideAutofillStructure(ViewStructure structure, int flags) {
583 super.onProvideAutofillStructure(structure, flags);
Felipe Lemec01a8732017-02-22 17:26:06 -0800584
Felipe Lemec9a19b12017-03-13 18:05:22 -0700585 structure.setDataIsSensitive(!mCheckedFromResource);
Felipe Lemec01a8732017-02-22 17:26:06 -0800586 }
587
588 @Override
Felipe Leme955e2522017-03-29 17:47:58 -0700589 public void autofill(AutofillValue value) {
590 if (!isEnabled()) return;
Felipe Lemebab851c2017-02-03 18:45:08 -0800591
Felipe Leme955e2522017-03-29 17:47:58 -0700592 if (!value.isToggle()) {
Philip P. Moltmann96689032017-03-09 13:19:55 -0800593 Log.w(LOG_TAG, value + " could not be autofilled into " + this);
Felipe Leme955e2522017-03-29 17:47:58 -0700594 return;
Philip P. Moltmann96689032017-03-09 13:19:55 -0800595 }
Philip P. Moltmann7b771162017-03-03 17:22:57 -0800596
Felipe Leme955e2522017-03-29 17:47:58 -0700597 setChecked(value.getToggleValue());
Felipe Leme6d553872016-12-08 17:13:25 -0800598 }
599
600 @Override
Felipe Leme8931e302017-03-06 13:44:35 -0800601 public @AutofillType int getAutofillType() {
602 return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE;
Felipe Leme6d553872016-12-08 17:13:25 -0800603 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800604
605 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800606 public AutofillValue getAutofillValue() {
607 return isEnabled() ? AutofillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609}