blob: 99440f862871d213f67df69c3fcb8f3c8be3333e [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
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070020import android.annotation.NonNull;
Alan Viverette4f64c042014-07-21 17:49:13 -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 Viverette4f64c042014-07-21 17:49:13 -070024import android.content.res.ColorStateList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.res.TypedArray;
26import android.graphics.Canvas;
Alan Viverette4f64c042014-07-21 17:49:13 -070027import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.drawable.Drawable;
Aurimas Liutikase3fa1762015-11-17 14:28:04 -080029import android.os.Parcel;
30import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.AttributeSet;
32import android.view.Gravity;
Alan Viverette2356c5e2014-05-22 22:43:59 -070033import android.view.RemotableViewMethod;
Steve Zeigler7a367882010-02-23 16:39:08 -080034import android.view.ViewDebug;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070035import android.view.ViewHierarchyEncoder;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -070037import android.view.accessibility.AccessibilityNodeInfo;
Ashley Rose55f9f922019-01-28 19:29:36 -050038import android.view.inspector.InspectableProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Aurimas Liutikas99441c52016-10-11 16:48:32 -070040import com.android.internal.R;
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
Alan Viverette49f118e2015-02-23 17:15:27 -080043 * An extension to {@link TextView} that supports the {@link Checkable}
44 * interface and displays.
45 * <p>
46 * This is useful when used in a {@link android.widget.ListView ListView} where
47 * the {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has
48 * been set to something other than
49 * {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 *
Gilles Debunne2fb40282012-05-01 12:07:06 -070051 * @attr ref android.R.styleable#CheckedTextView_checked
52 * @attr ref android.R.styleable#CheckedTextView_checkMark
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 */
Romain Guy8b5e7c02009-04-29 11:48:22 -070054public class CheckedTextView extends TextView implements Checkable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 private boolean mChecked;
Alan Viverette4f64c042014-07-21 17:49:13 -070056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 private int mCheckMarkResource;
Mathew Inwood978c6e22018-08-21 15:58:55 +010058 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private Drawable mCheckMarkDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070060 private ColorStateList mCheckMarkTintList = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070061 private PorterDuff.Mode mCheckMarkTintMode = null;
Alan Viverette4f64c042014-07-21 17:49:13 -070062 private boolean mHasCheckMarkTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070063 private boolean mHasCheckMarkTintMode = false;
Alan Viverette4f64c042014-07-21 17:49:13 -070064
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070065 private int mBasePadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 private int mCheckMarkWidth;
Mathew Inwood978c6e22018-08-21 15:58:55 +010067 @UnsupportedAppUsage
Adam Powell16f2b902014-08-11 17:30:19 -070068 private int mCheckMarkGravity = Gravity.END;
69
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070070 private boolean mNeedRequestlayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72 private static final int[] CHECKED_STATE_SET = {
73 R.attr.state_checked
74 };
75
76 public CheckedTextView(Context context) {
77 this(context, null);
78 }
79
80 public CheckedTextView(Context context, AttributeSet attrs) {
Fabrice Di Megliob023a582012-09-30 15:31:06 -070081 this(context, attrs, R.attr.checkedTextViewStyle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
83
Alan Viverette617feb92013-09-09 18:09:13 -070084 public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
85 this(context, attrs, defStyleAttr, 0);
86 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Alan Viverette617feb92013-09-09 18:09:13 -070088 public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
89 super(context, attrs, defStyleAttr, defStyleRes);
90
91 final TypedArray a = context.obtainStyledAttributes(
92 attrs, R.styleable.CheckedTextView, defStyleAttr, defStyleRes);
Aurimas Liutikasab324cf2019-02-07 16:46:38 -080093 saveAttributeDataForStyleable(context, R.styleable.CheckedTextView,
94 attrs, a, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Alan Viveretteb56f5d22014-09-14 15:48:50 -070096 final Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 if (d != null) {
98 setCheckMarkDrawable(d);
99 }
100
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700101 if (a.hasValue(R.styleable.CheckedTextView_checkMarkTintMode)) {
102 mCheckMarkTintMode = Drawable.parseTintMode(a.getInt(
103 R.styleable.CheckedTextView_checkMarkTintMode, -1), mCheckMarkTintMode);
104 mHasCheckMarkTintMode = true;
105 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700106
Adam Powell16f2b902014-08-11 17:30:19 -0700107 if (a.hasValue(R.styleable.CheckedTextView_checkMarkTint)) {
108 mCheckMarkTintList = a.getColorStateList(R.styleable.CheckedTextView_checkMarkTint);
Alan Viverette4f64c042014-07-21 17:49:13 -0700109 mHasCheckMarkTint = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700110 }
111
Adam Powell16f2b902014-08-11 17:30:19 -0700112 mCheckMarkGravity = a.getInt(R.styleable.CheckedTextView_checkMarkGravity, Gravity.END);
113
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700114 final boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 setChecked(checked);
116
117 a.recycle();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700118
119 applyCheckMarkTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 }
121
122 public void toggle() {
123 setChecked(!mChecked);
124 }
Steve Zeigler7a367882010-02-23 16:39:08 -0800125
126 @ViewDebug.ExportedProperty
Ashley Rose55f9f922019-01-28 19:29:36 -0500127 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public boolean isChecked() {
129 return mChecked;
130 }
131
132 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800133 * Sets the checked state of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800135 * @param checked {@code true} set the state to checked, {@code false} to
136 * uncheck
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
138 public void setChecked(boolean checked) {
139 if (mChecked != checked) {
140 mChecked = checked;
141 refreshDrawableState();
Eugene Susla72c510f2018-01-23 21:12:11 +0000142 notifyViewAccessibilityStateChangedIfNeeded(
Alan Viverette77e9a282013-09-12 17:16:09 -0700143 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 }
145 }
146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800148 * Sets the check mark to the drawable with the specified resource ID.
149 * <p>
150 * When this view is checked, the drawable's state set will include
151 * {@link android.R.attr#state_checked}.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700152 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800153 * @param resId the resource identifier of drawable to use as the check
154 * mark
155 * @attr ref android.R.styleable#CheckedTextView_checkMark
Gilles Debunne2fb40282012-05-01 12:07:06 -0700156 * @see #setCheckMarkDrawable(Drawable)
157 * @see #getCheckMarkDrawable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800159 public void setCheckMarkDrawable(@DrawableRes int resId) {
160 if (resId != 0 && resId == mCheckMarkResource) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 return;
162 }
163
Alan Viverettec60feca2015-08-12 16:26:37 -0400164 final Drawable d = resId != 0 ? getContext().getDrawable(resId) : null;
165 setCheckMarkDrawableInternal(d, resId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167
168 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800169 * Set the check mark to the specified drawable.
170 * <p>
171 * When this view is checked, the drawable's state set will include
172 * {@link android.R.attr#state_checked}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800174 * @param d the drawable to use for the check mark
175 * @attr ref android.R.styleable#CheckedTextView_checkMark
Gilles Debunne2fb40282012-05-01 12:07:06 -0700176 * @see #setCheckMarkDrawable(int)
177 * @see #getCheckMarkDrawable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 */
Alan Viverettec60feca2015-08-12 16:26:37 -0400179 public void setCheckMarkDrawable(@Nullable Drawable d) {
180 setCheckMarkDrawableInternal(d, 0);
181 }
182
183 private void setCheckMarkDrawableInternal(@Nullable Drawable d, @DrawableRes int resId) {
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400184 if (mCheckMarkDrawable != null) {
185 mCheckMarkDrawable.setCallback(null);
186 unscheduleDrawable(mCheckMarkDrawable);
187 }
Alan Viverettec60feca2015-08-12 16:26:37 -0400188
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700189 mNeedRequestlayout = (d != mCheckMarkDrawable);
Alan Viverettec60feca2015-08-12 16:26:37 -0400190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 if (d != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 d.setCallback(this);
193 d.setVisible(getVisibility() == VISIBLE, false);
194 d.setState(CHECKED_STATE_SET);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700195
Alan Viverette28fabe52016-04-25 13:15:39 -0400196 // Record the intrinsic dimensions when in "checked" state.
197 setMinHeight(d.getIntrinsicHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 mCheckMarkWidth = d.getIntrinsicWidth();
Alan Viverette28fabe52016-04-25 13:15:39 -0400199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 d.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 } else {
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700202 mCheckMarkWidth = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
Alan Viverettec60feca2015-08-12 16:26:37 -0400204
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400205 mCheckMarkDrawable = d;
Alan Viverettec60feca2015-08-12 16:26:37 -0400206 mCheckMarkResource = resId;
Alan Viverette2356c5e2014-05-22 22:43:59 -0700207
Alan Viverette28fabe52016-04-25 13:15:39 -0400208 applyCheckMarkTint();
209
Alan Viverette2356c5e2014-05-22 22:43:59 -0700210 // Do padding resolution. This will call internalSetPadding() and do a
211 // requestLayout() if needed.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700212 resolvePadding();
213 }
214
Alan Viverette4f64c042014-07-21 17:49:13 -0700215 /**
216 * Applies a tint to the check mark drawable. Does not modify the
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700217 * current tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette4f64c042014-07-21 17:49:13 -0700218 * <p>
219 * Subsequent calls to {@link #setCheckMarkDrawable(Drawable)} will
220 * automatically mutate the drawable and apply the specified tint and
221 * tint mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700222 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette4f64c042014-07-21 17:49:13 -0700223 *
224 * @param tint the tint to apply, may be {@code null} to clear tint
225 *
226 * @attr ref android.R.styleable#CheckedTextView_checkMarkTint
Alan Viverettea4264452014-07-28 16:02:55 -0700227 * @see #getCheckMarkTintList()
228 * @see Drawable#setTintList(ColorStateList)
Alan Viverette4f64c042014-07-21 17:49:13 -0700229 */
Alan Viverettea4264452014-07-28 16:02:55 -0700230 public void setCheckMarkTintList(@Nullable ColorStateList tint) {
231 mCheckMarkTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700232 mHasCheckMarkTint = true;
233
234 applyCheckMarkTint();
235 }
236
237 /**
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700238 * Returns the tint applied to the check mark drawable, if specified.
239 *
Alan Viverette4f64c042014-07-21 17:49:13 -0700240 * @return the tint applied to the check mark drawable
241 * @attr ref android.R.styleable#CheckedTextView_checkMarkTint
Alan Viverettea4264452014-07-28 16:02:55 -0700242 * @see #setCheckMarkTintList(ColorStateList)
Alan Viverette4f64c042014-07-21 17:49:13 -0700243 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500244 @InspectableProperty(name = "checkMarkTint")
Alan Viverette4f64c042014-07-21 17:49:13 -0700245 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700246 public ColorStateList getCheckMarkTintList() {
247 return mCheckMarkTintList;
Alan Viverette4f64c042014-07-21 17:49:13 -0700248 }
249
250 /**
251 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700252 * {@link #setCheckMarkTintList(ColorStateList)} to the check mark
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700253 * drawable. The default mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette4f64c042014-07-21 17:49:13 -0700254 *
255 * @param tintMode the blending mode used to apply the tint, may be
256 * {@code null} to clear tint
257 * @attr ref android.R.styleable#CheckedTextView_checkMarkTintMode
Alan Viverettea4264452014-07-28 16:02:55 -0700258 * @see #setCheckMarkTintList(ColorStateList)
259 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette4f64c042014-07-21 17:49:13 -0700260 */
261 public void setCheckMarkTintMode(@Nullable PorterDuff.Mode tintMode) {
262 mCheckMarkTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700263 mHasCheckMarkTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700264
265 applyCheckMarkTint();
266 }
267
268 /**
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700269 * Returns the blending mode used to apply the tint to the check mark
270 * drawable, if specified.
271 *
272 * @return the blending mode used to apply the tint to the check mark
273 * drawable
Alan Viverette4f64c042014-07-21 17:49:13 -0700274 * @attr ref android.R.styleable#CheckedTextView_checkMarkTintMode
275 * @see #setCheckMarkTintMode(PorterDuff.Mode)
276 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500277 @InspectableProperty
Alan Viverette4f64c042014-07-21 17:49:13 -0700278 @Nullable
279 public PorterDuff.Mode getCheckMarkTintMode() {
280 return mCheckMarkTintMode;
281 }
282
283 private void applyCheckMarkTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700284 if (mCheckMarkDrawable != null && (mHasCheckMarkTint || mHasCheckMarkTintMode)) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700285 mCheckMarkDrawable = mCheckMarkDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700286
287 if (mHasCheckMarkTint) {
288 mCheckMarkDrawable.setTintList(mCheckMarkTintList);
289 }
290
291 if (mHasCheckMarkTintMode) {
292 mCheckMarkDrawable.setTintMode(mCheckMarkTintMode);
293 }
Alan Viveretted5133792014-10-28 14:41:36 -0700294
295 // The drawable (or one of its children) may not have been
296 // stateful before applying the tint, so let's try again.
297 if (mCheckMarkDrawable.isStateful()) {
298 mCheckMarkDrawable.setState(getDrawableState());
299 }
Alan Viverette4f64c042014-07-21 17:49:13 -0700300 }
301 }
302
Alan Viverette2356c5e2014-05-22 22:43:59 -0700303 @RemotableViewMethod
304 @Override
305 public void setVisibility(int visibility) {
306 super.setVisibility(visibility);
307
308 if (mCheckMarkDrawable != null) {
309 mCheckMarkDrawable.setVisible(visibility == VISIBLE, false);
310 }
311 }
312
313 @Override
314 public void jumpDrawablesToCurrentState() {
315 super.jumpDrawablesToCurrentState();
316
317 if (mCheckMarkDrawable != null) {
318 mCheckMarkDrawable.jumpToCurrentState();
319 }
320 }
321
322 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500323 protected boolean verifyDrawable(@NonNull Drawable who) {
Alan Viverette2356c5e2014-05-22 22:43:59 -0700324 return who == mCheckMarkDrawable || super.verifyDrawable(who);
325 }
326
Gilles Debunne2fb40282012-05-01 12:07:06 -0700327 /**
328 * Gets the checkmark drawable
329 *
330 * @return The drawable use to represent the checkmark, if any.
331 *
332 * @see #setCheckMarkDrawable(Drawable)
333 * @see #setCheckMarkDrawable(int)
334 *
335 * @attr ref android.R.styleable#CheckedTextView_checkMark
336 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500337 @InspectableProperty(name = "checkMark")
Gilles Debunne2fb40282012-05-01 12:07:06 -0700338 public Drawable getCheckMarkDrawable() {
339 return mCheckMarkDrawable;
340 }
341
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700342 /**
343 * @hide
344 */
345 @Override
346 protected void internalSetPadding(int left, int top, int right, int bottom) {
347 super.internalSetPadding(left, top, right, bottom);
Adam Powell16f2b902014-08-11 17:30:19 -0700348 setBasePadding(isCheckMarkAtStart());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700349 }
350
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700351 @Override
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700352 public void onRtlPropertiesChanged(int layoutDirection) {
353 super.onRtlPropertiesChanged(layoutDirection);
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700354 updatePadding();
355 }
356
357 private void updatePadding() {
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -0700358 resetPaddingToInitialValues();
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700359 int newPadding = (mCheckMarkDrawable != null) ?
360 mCheckMarkWidth + mBasePadding : mBasePadding;
Adam Powell16f2b902014-08-11 17:30:19 -0700361 if (isCheckMarkAtStart()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -0700362 mNeedRequestlayout |= (mPaddingLeft != newPadding);
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700363 mPaddingLeft = newPadding;
364 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -0700365 mNeedRequestlayout |= (mPaddingRight != newPadding);
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700366 mPaddingRight = newPadding;
367 }
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700368 if (mNeedRequestlayout) {
369 requestLayout();
370 mNeedRequestlayout = false;
371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700373
Adam Powell16f2b902014-08-11 17:30:19 -0700374 private void setBasePadding(boolean checkmarkAtStart) {
375 if (checkmarkAtStart) {
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700376 mBasePadding = mPaddingLeft;
377 } else {
378 mBasePadding = mPaddingRight;
379 }
Fabrice Di Megliobf923eb2012-03-07 16:20:22 -0800380 }
381
Adam Powell16f2b902014-08-11 17:30:19 -0700382 private boolean isCheckMarkAtStart() {
383 final int gravity = Gravity.getAbsoluteGravity(mCheckMarkGravity, getLayoutDirection());
384 final int hgrav = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
385 return hgrav == Gravity.LEFT;
386 }
387
Fabrice Di Megliobf923eb2012-03-07 16:20:22 -0800388 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 protected void onDraw(Canvas canvas) {
390 super.onDraw(canvas);
391
392 final Drawable checkMarkDrawable = mCheckMarkDrawable;
393 if (checkMarkDrawable != null) {
394 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
395 final int height = checkMarkDrawable.getIntrinsicHeight();
396
397 int y = 0;
398
399 switch (verticalGravity) {
400 case Gravity.BOTTOM:
401 y = getHeight() - height;
402 break;
403 case Gravity.CENTER_VERTICAL:
404 y = (getHeight() - height) / 2;
405 break;
406 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700407
Adam Powell16f2b902014-08-11 17:30:19 -0700408 final boolean checkMarkAtStart = isCheckMarkAtStart();
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700409 final int width = getWidth();
410 final int top = y;
411 final int bottom = top + height;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700412 final int left;
413 final int right;
Adam Powell16f2b902014-08-11 17:30:19 -0700414 if (checkMarkAtStart) {
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700415 left = mBasePadding;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700416 right = left + mCheckMarkWidth;
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700417 } else {
418 right = width - mBasePadding;
419 left = right - mCheckMarkWidth;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700420 }
Jorn Jacobsson78cdc552013-01-31 17:52:20 +0100421 checkMarkDrawable.setBounds(mScrollX + left, top, mScrollX + right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 checkMarkDrawable.draw(canvas);
Alan Viverette2356c5e2014-05-22 22:43:59 -0700423
424 final Drawable background = getBackground();
425 if (background != null) {
426 background.setHotspotBounds(mScrollX + left, top, mScrollX + right, bottom);
427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 @Override
432 protected int[] onCreateDrawableState(int extraSpace) {
433 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
434 if (isChecked()) {
435 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
436 }
437 return drawableState;
438 }
439
440 @Override
441 protected void drawableStateChanged() {
442 super.drawableStateChanged();
Alan Viverettead0020f2015-09-04 10:10:42 -0400443
444 final Drawable checkMarkDrawable = mCheckMarkDrawable;
445 if (checkMarkDrawable != null && checkMarkDrawable.isStateful()
446 && checkMarkDrawable.setState(getDrawableState())) {
447 invalidateDrawable(checkMarkDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700450
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700451 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700452 public void drawableHotspotChanged(float x, float y) {
453 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700454
455 if (mCheckMarkDrawable != null) {
456 mCheckMarkDrawable.setHotspot(x, y);
457 }
458 }
459
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800460 @Override
461 public CharSequence getAccessibilityClassName() {
462 return CheckedTextView.class.getName();
463 }
464
Aurimas Liutikase3fa1762015-11-17 14:28:04 -0800465 static class SavedState extends BaseSavedState {
466 boolean checked;
467
468 /**
469 * Constructor called from {@link CheckedTextView#onSaveInstanceState()}
470 */
471 SavedState(Parcelable superState) {
472 super(superState);
473 }
474
475 /**
476 * Constructor called from {@link #CREATOR}
477 */
478 private SavedState(Parcel in) {
479 super(in);
480 checked = (Boolean)in.readValue(null);
481 }
482
483 @Override
484 public void writeToParcel(Parcel out, int flags) {
485 super.writeToParcel(out, flags);
486 out.writeValue(checked);
487 }
488
489 @Override
490 public String toString() {
491 return "CheckedTextView.SavedState{"
492 + Integer.toHexString(System.identityHashCode(this))
493 + " checked=" + checked + "}";
494 }
495
496 public static final Parcelable.Creator<SavedState> CREATOR
497 = new Parcelable.Creator<SavedState>() {
498 public SavedState createFromParcel(Parcel in) {
499 return new SavedState(in);
500 }
501
502 public SavedState[] newArray(int size) {
503 return new SavedState[size];
504 }
505 };
506 }
507
508 @Override
509 public Parcelable onSaveInstanceState() {
510 Parcelable superState = super.onSaveInstanceState();
511
512 SavedState ss = new SavedState(superState);
513
514 ss.checked = isChecked();
515 return ss;
516 }
517
518 @Override
519 public void onRestoreInstanceState(Parcelable state) {
520 SavedState ss = (SavedState) state;
521
522 super.onRestoreInstanceState(ss.getSuperState());
523 setChecked(ss.checked);
524 requestLayout();
525 }
526
Alan Viverettea54956a2015-01-07 16:05:02 -0800527 /** @hide */
svetoslavganov75986cf2009-05-14 22:28:01 -0700528 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800529 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
530 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700531 event.setChecked(mChecked);
svetoslavganov75986cf2009-05-14 22:28:01 -0700532 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700533
Alan Viverettea54956a2015-01-07 16:05:02 -0800534 /** @hide */
Svetoslav Ganov76502592011-07-29 10:44:59 -0700535 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800536 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
537 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganovc85015c2012-04-30 11:37:43 -0700538 info.setCheckable(true);
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -0700539 info.setChecked(mChecked);
540 }
Siva Velusamy94a6d152015-05-05 15:07:00 -0700541
542 /** @hide */
543 @Override
544 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
545 super.encodeProperties(stream);
546 stream.addProperty("text:checked", isChecked());
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548}