blob: 5820f4bdafad755589c1047279de8b7d402176aa [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;
Artur Satayeved5a6ae2019-12-10 17:47:54 +000022import android.compat.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;
Nader Jawad8e31c3e2019-04-14 21:58:04 -070026import android.graphics.BlendMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.graphics.Canvas;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070028import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.drawable.Drawable;
30import android.os.Parcel;
31import android.os.Parcelable;
32import android.util.AttributeSet;
Philip P. Moltmann96689032017-03-09 13:19:55 -080033import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.view.Gravity;
Alan Viveretted4e77902014-10-27 17:50:51 -070035import android.view.SoundEffectConstants;
Steve Zeigler7a367882010-02-23 16:39:08 -080036import android.view.ViewDebug;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070037import android.view.ViewHierarchyEncoder;
Felipe Lemec01a8732017-02-22 17:26:06 -080038import android.view.ViewStructure;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070039import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070040import android.view.accessibility.AccessibilityNodeInfo;
Felipe Leme640f30a2017-03-06 15:44:06 -080041import android.view.autofill.AutofillManager;
42import android.view.autofill.AutofillValue;
Ashley Rose55f9f922019-01-28 19:29:36 -050043import android.view.inspector.InspectableProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Aurimas Liutikas99441c52016-10-11 16:48:32 -070045import com.android.internal.R;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047/**
48 * <p>
49 * A button with two states, checked and unchecked. When the button is pressed
50 * or clicked, the state changes automatically.
51 * </p>
52 *
53 * <p><strong>XML attributes</strong></p>
54 * <p>
55 * See {@link android.R.styleable#CompoundButton
56 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
57 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
58 * android.R.styleable#View View Attributes}
59 * </p>
60 */
61public abstract class CompoundButton extends Button implements Checkable {
Philip P. Moltmann96689032017-03-09 13:19:55 -080062 private static final String LOG_TAG = CompoundButton.class.getSimpleName();
Felipe Leme6d553872016-12-08 17:13:25 -080063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 private boolean mChecked;
Mathew Inwood978c6e22018-08-21 15:58:55 +010065 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070067
Mathew Inwood978c6e22018-08-21 15:58:55 +010068 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070070 private ColorStateList mButtonTintList = null;
Nader Jawad8e31c3e2019-04-14 21:58:04 -070071 private BlendMode mButtonBlendMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070072 private boolean mHasButtonTint = false;
Nader Jawad8e31c3e2019-04-14 21:58:04 -070073 private boolean mHasButtonBlendMode = false;
Alan Viverette91174362014-06-17 14:51:45 -070074
Mathew Inwood978c6e22018-08-21 15:58:55 +010075 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 private OnCheckedChangeListener mOnCheckedChangeListener;
77 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
78
Felipe Lemec01a8732017-02-22 17:26:06 -080079 // Indicates whether the toggle state was set from resources or dynamically, so it can be used
Felipe Leme640f30a2017-03-06 15:44:06 -080080 // to sanitize autofill requests.
Felipe Lemec01a8732017-02-22 17:26:06 -080081 private boolean mCheckedFromResource = false;
82
yingleiw473fc122019-10-11 15:10:55 -070083 private CharSequence mCustomStateDescription = null;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final int[] CHECKED_STATE_SET = {
86 R.attr.state_checked
87 };
88
89 public CompoundButton(Context context) {
90 this(context, null);
91 }
92
93 public CompoundButton(Context context, AttributeSet attrs) {
94 this(context, attrs, 0);
95 }
96
Alan Viverette617feb92013-09-09 18:09:13 -070097 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
98 this(context, attrs, defStyleAttr, 0);
99 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Alan Viverette617feb92013-09-09 18:09:13 -0700101 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
102 super(context, attrs, defStyleAttr, defStyleRes);
103
104 final TypedArray a = context.obtainStyledAttributes(
105 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
Aurimas Liutikasab324cf2019-02-07 16:46:38 -0800106 saveAttributeDataForStyleable(context, com.android.internal.R.styleable.CompoundButton,
107 attrs, a, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Alan Viverette91174362014-06-17 14:51:45 -0700109 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 if (d != null) {
111 setButtonDrawable(d);
112 }
113
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700114 if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
Nader Jawad8e31c3e2019-04-14 21:58:04 -0700115 mButtonBlendMode = Drawable.parseBlendMode(a.getInt(
116 R.styleable.CompoundButton_buttonTintMode, -1), mButtonBlendMode);
117 mHasButtonBlendMode = true;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700118 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700119
Alan Viverette91174362014-06-17 14:51:45 -0700120 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -0700121 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -0700122 mHasButtonTint = true;
Alan Viverette91174362014-06-17 14:51:45 -0700123 }
124
125 final boolean checked = a.getBoolean(
126 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 setChecked(checked);
Felipe Lemec01a8732017-02-22 17:26:06 -0800128 mCheckedFromResource = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
130 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700131
132 applyButtonTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 }
134
Felipe Leme6d553872016-12-08 17:13:25 -0800135 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public void toggle() {
137 setChecked(!mChecked);
138 }
139
140 @Override
141 public boolean performClick() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 toggle();
Alan Viveretted4e77902014-10-27 17:50:51 -0700143
144 final boolean handled = super.performClick();
145 if (!handled) {
146 // View only makes a sound effect if the onClickListener was
147 // called, so we'll need to make one here instead.
148 playSoundEffect(SoundEffectConstants.CLICK);
149 }
150
151 return handled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
153
Ashley Rose55f9f922019-01-28 19:29:36 -0500154 @InspectableProperty
Steve Zeigler7a367882010-02-23 16:39:08 -0800155 @ViewDebug.ExportedProperty
Felipe Leme6d553872016-12-08 17:13:25 -0800156 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public boolean isChecked() {
158 return mChecked;
159 }
160
yingleiw473fc122019-10-11 15:10:55 -0700161 /** @hide */
162 @NonNull
163 protected CharSequence getButtonStateDescription() {
164 if (isChecked()) {
165 return getResources().getString(R.string.checked);
166 } else {
167 return getResources().getString(R.string.not_checked);
168 }
169 }
170
171 /**
172 * This function is called when an instance or subclass sets the state description. Once this
173 * is called and the argument is not null, the app developer will be responsible for updating
174 * state description when checked state changes and we will not set state description
175 * in {@link #setChecked}. App developers can restore the default behavior by setting the
176 * argument to null. If {@link #setChecked} is called first and then setStateDescription is
177 * called, two state change events will be merged by event throttling and we can still get
178 * the correct state description.
179 *
180 * @param stateDescription The state description.
181 */
182 @Override
183 public void setStateDescription(@Nullable CharSequence stateDescription) {
184 mCustomStateDescription = stateDescription;
185 if (stateDescription == null) {
186 setDefaultStateDescritption();
187 } else {
188 super.setStateDescription(stateDescription);
189 }
190 }
191
192 /** @hide **/
193 protected void setDefaultStateDescritption() {
194 if (mCustomStateDescription == null) {
195 super.setStateDescription(getButtonStateDescription());
196 }
197 }
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 /**
200 * <p>Changes the checked state of this button.</p>
201 *
202 * @param checked true to check the button, false to uncheck it
203 */
Felipe Leme6d553872016-12-08 17:13:25 -0800204 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public void setChecked(boolean checked) {
206 if (mChecked != checked) {
Felipe Lemec01a8732017-02-22 17:26:06 -0800207 mCheckedFromResource = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 mChecked = checked;
209 refreshDrawableState();
210
211 // Avoid infinite recursions if setChecked() is called from a listener
212 if (mBroadcasting) {
213 return;
214 }
215
216 mBroadcasting = true;
217 if (mOnCheckedChangeListener != null) {
218 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
219 }
220 if (mOnCheckedChangeWidgetListener != null) {
221 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
222 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800223 final AutofillManager afm = mContext.getSystemService(AutofillManager.class);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800224 if (afm != null) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700225 afm.notifyValueChanged(this);
Felipe Leme5882c4f2017-02-16 21:46:46 -0800226 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700227
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700228 mBroadcasting = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
yingleiw473fc122019-10-11 15:10:55 -0700230 // setStateDescription will not send out event if the description is unchanged.
231 setDefaultStateDescritption();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233
234 /**
235 * Register a callback to be invoked when the checked state of this button
236 * changes.
237 *
238 * @param listener the callback to call on checked state change
239 */
Jason Longacdaaea502016-12-01 22:54:37 -0800240 public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 mOnCheckedChangeListener = listener;
242 }
243
244 /**
245 * Register a callback to be invoked when the checked state of this button
246 * changes. This callback is used for internal purpose only.
247 *
248 * @param listener the callback to call on checked state change
249 * @hide
250 */
251 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
252 mOnCheckedChangeWidgetListener = listener;
253 }
254
255 /**
256 * Interface definition for a callback to be invoked when the checked state
257 * of a compound button changed.
258 */
259 public static interface OnCheckedChangeListener {
260 /**
261 * Called when the checked state of a compound button has changed.
262 *
263 * @param buttonView The compound button view whose state has changed.
264 * @param isChecked The new checked state of buttonView.
265 */
266 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
267 }
268
269 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800270 * Sets a drawable as the compound button image given its resource
271 * identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800273 * @param resId the resource identifier of the drawable
274 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800276 public void setButtonDrawable(@DrawableRes int resId) {
277 final Drawable d;
278 if (resId != 0) {
279 d = getContext().getDrawable(resId);
280 } else {
281 d = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283 setButtonDrawable(d);
284 }
285
286 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800287 * Sets a drawable as the compound button image.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 *
Alan Viverette6a394f42015-02-12 15:03:22 -0800289 * @param drawable the drawable to set
290 * @attr ref android.R.styleable#CompoundButton_button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
Alan Viverette6a394f42015-02-12 15:03:22 -0800292 public void setButtonDrawable(@Nullable Drawable drawable) {
293 if (mButtonDrawable != drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 if (mButtonDrawable != null) {
295 mButtonDrawable.setCallback(null);
296 unscheduleDrawable(mButtonDrawable);
297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Alan Viverette6a394f42015-02-12 15:03:22 -0800299 mButtonDrawable = drawable;
Alan Viverette91174362014-06-17 14:51:45 -0700300
Alan Viverette6a394f42015-02-12 15:03:22 -0800301 if (drawable != null) {
302 drawable.setCallback(this);
303 drawable.setLayoutDirection(getLayoutDirection());
304 if (drawable.isStateful()) {
305 drawable.setState(getDrawableState());
Alan Viverette91174362014-06-17 14:51:45 -0700306 }
Alan Viverette6a394f42015-02-12 15:03:22 -0800307 drawable.setVisible(getVisibility() == VISIBLE, false);
308 setMinHeight(drawable.getIntrinsicHeight());
Alan Viverette91174362014-06-17 14:51:45 -0700309 applyButtonTint();
310 }
311 }
312 }
313
314 /**
Doris Liu3380e692015-06-30 11:26:47 -0700315 * @hide
316 */
317 @Override
318 public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) {
319 super.onResolveDrawables(layoutDirection);
320 if (mButtonDrawable != null) {
321 mButtonDrawable.setLayoutDirection(layoutDirection);
322 }
323 }
324
325 /**
Alan Viverette6a394f42015-02-12 15:03:22 -0800326 * @return the drawable used as the compound button image
327 * @see #setButtonDrawable(Drawable)
328 * @see #setButtonDrawable(int)
329 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500330 @InspectableProperty(name = "button")
Alan Viverette6a394f42015-02-12 15:03:22 -0800331 @Nullable
332 public Drawable getButtonDrawable() {
333 return mButtonDrawable;
334 }
335
336 /**
Alan Viverette91174362014-06-17 14:51:45 -0700337 * Applies a tint to the button drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700338 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700339 * <p>
340 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
341 * automatically mutate the drawable and apply the specified tint and tint
342 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700343 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700344 *
345 * @param tint the tint to apply, may be {@code null} to clear tint
346 *
347 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700348 * @see #setButtonTintList(ColorStateList)
349 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700350 */
Alan Viverettea4264452014-07-28 16:02:55 -0700351 public void setButtonTintList(@Nullable ColorStateList tint) {
352 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700353 mHasButtonTint = true;
354
355 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700356 }
357
358 /**
359 * @return the tint applied to the button drawable
360 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700361 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700362 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500363 @InspectableProperty(name = "buttonTint")
Alan Viverette91174362014-06-17 14:51:45 -0700364 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700365 public ColorStateList getButtonTintList() {
366 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700367 }
368
369 /**
370 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700371 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700372 * default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700373 *
374 * @param tintMode the blending mode used to apply the tint, may be
375 * {@code null} to clear tint
376 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700377 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700378 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700379 */
380 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Nader Jawad8e31c3e2019-04-14 21:58:04 -0700381 setButtonTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null);
382 }
383
384 /**
385 * Specifies the blending mode used to apply the tint specified by
386 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
387 * default mode is {@link PorterDuff.Mode#SRC_IN}.
388 *
389 * @param tintMode the blending mode used to apply the tint, may be
390 * {@code null} to clear tint
391 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
392 * @see #getButtonTintMode()
393 * @see Drawable#setTintBlendMode(BlendMode)
394 */
395 public void setButtonTintBlendMode(@Nullable BlendMode tintMode) {
396 mButtonBlendMode = tintMode;
397 mHasButtonBlendMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700398
399 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700400 }
401
402 /**
403 * @return the blending mode used to apply the tint to the button drawable
404 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700405 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700406 */
Nader Jawad8e31c3e2019-04-14 21:58:04 -0700407 @InspectableProperty(name = "buttonTintMode")
Alan Viverette91174362014-06-17 14:51:45 -0700408 @Nullable
409 public PorterDuff.Mode getButtonTintMode() {
Nader Jawad8e31c3e2019-04-14 21:58:04 -0700410 return mButtonBlendMode != null ? BlendMode.blendModeToPorterDuffMode(mButtonBlendMode) :
411 null;
412 }
413
414 /**
415 * @return the blending mode used to apply the tint to the button drawable
416 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
417 * @see #setButtonTintBlendMode(BlendMode)
418 */
419 @InspectableProperty(name = "buttonBlendMode",
420 attributeId = R.styleable.CompoundButton_buttonTintMode)
421 @Nullable
422 public BlendMode getButtonTintBlendMode() {
423 return mButtonBlendMode;
Alan Viverette91174362014-06-17 14:51:45 -0700424 }
425
426 private void applyButtonTint() {
Nader Jawad8e31c3e2019-04-14 21:58:04 -0700427 if (mButtonDrawable != null && (mHasButtonTint || mHasButtonBlendMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700428 mButtonDrawable = mButtonDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700429
430 if (mHasButtonTint) {
431 mButtonDrawable.setTintList(mButtonTintList);
432 }
433
Nader Jawad8e31c3e2019-04-14 21:58:04 -0700434 if (mHasButtonBlendMode) {
435 mButtonDrawable.setTintBlendMode(mButtonBlendMode);
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700436 }
Alan Viveretted5133792014-10-28 14:41:36 -0700437
438 // The drawable (or one of its children) may not have been
439 // stateful before applying the tint, so let's try again.
440 if (mButtonDrawable.isStateful()) {
441 mButtonDrawable.setState(getDrawableState());
442 }
Alan Viverette91174362014-06-17 14:51:45 -0700443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800446 @Override
447 public CharSequence getAccessibilityClassName() {
448 return CompoundButton.class.getName();
449 }
450
Alan Viverettea54956a2015-01-07 16:05:02 -0800451 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800453 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
454 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700455 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700456 }
457
Alan Viverettea54956a2015-01-07 16:05:02 -0800458 /** @hide */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700459 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800460 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
461 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700462 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700463 info.setChecked(mChecked);
464 }
465
466 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700467 public int getCompoundPaddingLeft() {
468 int padding = super.getCompoundPaddingLeft();
469 if (!isLayoutRtl()) {
470 final Drawable buttonDrawable = mButtonDrawable;
471 if (buttonDrawable != null) {
472 padding += buttonDrawable.getIntrinsicWidth();
473 }
474 }
475 return padding;
476 }
477
478 @Override
479 public int getCompoundPaddingRight() {
480 int padding = super.getCompoundPaddingRight();
481 if (isLayoutRtl()) {
482 final Drawable buttonDrawable = mButtonDrawable;
483 if (buttonDrawable != null) {
484 padding += buttonDrawable.getIntrinsicWidth();
485 }
486 }
487 return padding;
488 }
489
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800490 /**
491 * @hide
492 */
493 @Override
494 public int getHorizontalOffsetForDrawables() {
495 final Drawable buttonDrawable = mButtonDrawable;
496 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
497 }
498
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700499 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 final Drawable buttonDrawable = mButtonDrawable;
502 if (buttonDrawable != null) {
503 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700504 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
505 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
Alan Viverette61956602014-04-22 19:07:06 -0700507 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 switch (verticalGravity) {
509 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700510 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 break;
512 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700513 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 break;
Alan Viverette61956602014-04-22 19:07:06 -0700515 default:
516 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
Alan Viverette61956602014-04-22 19:07:06 -0700518 final int bottom = top + drawableHeight;
519 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
520 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700522 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700523
524 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700525 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700526 background.setHotspotBounds(left, top, right, bottom);
527 }
528 }
529
530 super.onDraw(canvas);
531
532 if (buttonDrawable != null) {
Alan Viveretteb95c3362014-10-17 17:19:12 -0700533 final int scrollX = mScrollX;
534 final int scrollY = mScrollY;
535 if (scrollX == 0 && scrollY == 0) {
536 buttonDrawable.draw(canvas);
537 } else {
538 canvas.translate(scrollX, scrollY);
539 buttonDrawable.draw(canvas);
540 canvas.translate(-scrollX, -scrollY);
541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543 }
544
545 @Override
546 protected int[] onCreateDrawableState(int extraSpace) {
547 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
548 if (isChecked()) {
549 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
550 }
551 return drawableState;
552 }
553
554 @Override
555 protected void drawableStateChanged() {
556 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400557
558 final Drawable buttonDrawable = mButtonDrawable;
559 if (buttonDrawable != null && buttonDrawable.isStateful()
560 && buttonDrawable.setState(getDrawableState())) {
561 invalidateDrawable(buttonDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563 }
564
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700565 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700566 public void drawableHotspotChanged(float x, float y) {
567 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700568
569 if (mButtonDrawable != null) {
570 mButtonDrawable.setHotspot(x, y);
571 }
572 }
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500575 protected boolean verifyDrawable(@NonNull Drawable who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 return super.verifyDrawable(who) || who == mButtonDrawable;
577 }
578
Dianne Hackborne2136772010-11-04 15:08:59 -0700579 @Override
580 public void jumpDrawablesToCurrentState() {
581 super.jumpDrawablesToCurrentState();
582 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
583 }
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 static class SavedState extends BaseSavedState {
586 boolean checked;
587
588 /**
589 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
590 */
591 SavedState(Parcelable superState) {
592 super(superState);
593 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 /**
596 * Constructor called from {@link #CREATOR}
597 */
598 private SavedState(Parcel in) {
599 super(in);
600 checked = (Boolean)in.readValue(null);
601 }
602
603 @Override
604 public void writeToParcel(Parcel out, int flags) {
605 super.writeToParcel(out, flags);
606 out.writeValue(checked);
607 }
608
609 @Override
610 public String toString() {
611 return "CompoundButton.SavedState{"
612 + Integer.toHexString(System.identityHashCode(this))
613 + " checked=" + checked + "}";
614 }
615
Felipe Leme6d553872016-12-08 17:13:25 -0800616 @SuppressWarnings("hiding")
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700617 public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
Felipe Leme6d553872016-12-08 17:13:25 -0800618 new Parcelable.Creator<SavedState>() {
619 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public SavedState createFromParcel(Parcel in) {
621 return new SavedState(in);
622 }
623
Felipe Leme6d553872016-12-08 17:13:25 -0800624 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public SavedState[] newArray(int size) {
626 return new SavedState[size];
627 }
628 };
629 }
630
631 @Override
632 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 Parcelable superState = super.onSaveInstanceState();
634
635 SavedState ss = new SavedState(superState);
636
637 ss.checked = isChecked();
638 return ss;
639 }
640
641 @Override
642 public void onRestoreInstanceState(Parcelable state) {
643 SavedState ss = (SavedState) state;
Siva Velusamy94a6d152015-05-05 15:07:00 -0700644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 super.onRestoreInstanceState(ss.getSuperState());
646 setChecked(ss.checked);
647 requestLayout();
648 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700649
650 /** @hide */
651 @Override
652 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
653 super.encodeProperties(stream);
654 stream.addProperty("checked", isChecked());
655 }
Felipe Leme6d553872016-12-08 17:13:25 -0800656
Felipe Lemec01a8732017-02-22 17:26:06 -0800657
Felipe Leme92736c12018-11-13 12:00:59 -0800658 /** @hide */
659 @Override
660 protected void onProvideStructure(@NonNull ViewStructure structure,
661 @ViewStructureType int viewFor, int flags) {
662 super.onProvideStructure(structure, viewFor, flags);
663
664 if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) {
665 structure.setDataIsSensitive(!mCheckedFromResource);
666 }
Felipe Lemec01a8732017-02-22 17:26:06 -0800667 }
668
669 @Override
Felipe Leme955e2522017-03-29 17:47:58 -0700670 public void autofill(AutofillValue value) {
671 if (!isEnabled()) return;
Felipe Lemebab851c2017-02-03 18:45:08 -0800672
Felipe Leme955e2522017-03-29 17:47:58 -0700673 if (!value.isToggle()) {
Philip P. Moltmann96689032017-03-09 13:19:55 -0800674 Log.w(LOG_TAG, value + " could not be autofilled into " + this);
Felipe Leme955e2522017-03-29 17:47:58 -0700675 return;
Philip P. Moltmann96689032017-03-09 13:19:55 -0800676 }
Philip P. Moltmann7b771162017-03-03 17:22:57 -0800677
Felipe Leme955e2522017-03-29 17:47:58 -0700678 setChecked(value.getToggleValue());
Felipe Leme6d553872016-12-08 17:13:25 -0800679 }
680
681 @Override
Felipe Leme8931e302017-03-06 13:44:35 -0800682 public @AutofillType int getAutofillType() {
683 return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE;
Felipe Leme6d553872016-12-08 17:13:25 -0800684 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800685
686 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800687 public AutofillValue getAutofillValue() {
688 return isEnabled() ? AutofillValue.forToggle(isChecked()) : null;
Felipe Lemebab851c2017-02-03 18:45:08 -0800689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690}