blob: 4fcb358830dbe0e41f716cb0c8c074466490fe07 [file] [log] [blame]
Adam Powell12190b32010-11-28 19:07:53 -08001/*
2 * Copyright (C) 2010 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
Adam Powell12190b32010-11-28 19:07:53 -080019import android.content.Context;
20import android.content.res.ColorStateList;
21import android.content.res.Resources;
22import android.content.res.TypedArray;
23import android.graphics.Canvas;
24import android.graphics.Paint;
25import android.graphics.Rect;
26import android.graphics.Typeface;
27import android.graphics.drawable.Drawable;
28import android.text.Layout;
29import android.text.StaticLayout;
30import android.text.TextPaint;
31import android.text.TextUtils;
32import android.util.AttributeSet;
33import android.view.Gravity;
34import android.view.MotionEvent;
35import android.view.VelocityTracker;
36import android.view.ViewConfiguration;
Svetoslav Ganov63bce032011-07-23 19:52:17 -070037import android.view.accessibility.AccessibilityEvent;
Adam Powell12190b32010-11-28 19:07:53 -080038
Adam Powellbe0a4532010-11-29 17:47:48 -080039import com.android.internal.R;
40
Adam Powell12190b32010-11-28 19:07:53 -080041/**
42 * A Switch is a two-state toggle switch widget that can select between two
43 * options. The user may drag the "thumb" back and forth to choose the selected option,
Chet Haase150176d2011-08-26 09:54:06 -070044 * or simply tap to toggle as if it were a checkbox. The {@link #setText(CharSequence) text}
45 * property controls the text displayed in the label for the switch, whereas the
46 * {@link #setTextOff(CharSequence) off} and {@link #setTextOn(CharSequence) on} text
47 * controls the text on the thumb. Similarly, the
48 * {@link #setTextAppearance(android.content.Context, int) textAppearance} and the related
49 * setTypeface() methods control the typeface and style of label text, whereas the
50 * {@link #setSwitchTextAppearance(android.content.Context, int) switchTextAppearance} and
51 * the related seSwitchTypeface() methods control that of the thumb.
Adam Powell12190b32010-11-28 19:07:53 -080052 *
Adam Powell12190b32010-11-28 19:07:53 -080053 */
54public class Switch extends CompoundButton {
55 private static final int TOUCH_MODE_IDLE = 0;
56 private static final int TOUCH_MODE_DOWN = 1;
57 private static final int TOUCH_MODE_DRAGGING = 2;
58
59 // Enum for the "typeface" XML parameter.
60 private static final int SANS = 1;
61 private static final int SERIF = 2;
62 private static final int MONOSPACE = 3;
63
64 private Drawable mThumbDrawable;
65 private Drawable mTrackDrawable;
66 private int mThumbTextPadding;
67 private int mSwitchMinWidth;
68 private int mSwitchPadding;
69 private CharSequence mTextOn;
70 private CharSequence mTextOff;
71
72 private int mTouchMode;
73 private int mTouchSlop;
74 private float mTouchX;
75 private float mTouchY;
76 private VelocityTracker mVelocityTracker = VelocityTracker.obtain();
77 private int mMinFlingVelocity;
78
79 private float mThumbPosition;
80 private int mSwitchWidth;
81 private int mSwitchHeight;
82 private int mThumbWidth; // Does not include padding
83
84 private int mSwitchLeft;
85 private int mSwitchTop;
86 private int mSwitchRight;
87 private int mSwitchBottom;
88
89 private TextPaint mTextPaint;
90 private ColorStateList mTextColors;
91 private Layout mOnLayout;
92 private Layout mOffLayout;
93
Adam Powellbe0a4532010-11-29 17:47:48 -080094 @SuppressWarnings("hiding")
Adam Powell12190b32010-11-28 19:07:53 -080095 private final Rect mTempRect = new Rect();
96
97 private static final int[] CHECKED_STATE_SET = {
98 R.attr.state_checked
99 };
100
101 /**
102 * Construct a new Switch with default styling.
103 *
104 * @param context The Context that will determine this widget's theming.
105 */
106 public Switch(Context context) {
107 this(context, null);
108 }
109
110 /**
111 * Construct a new Switch with default styling, overriding specific style
112 * attributes as requested.
113 *
114 * @param context The Context that will determine this widget's theming.
115 * @param attrs Specification of attributes that should deviate from default styling.
116 */
117 public Switch(Context context, AttributeSet attrs) {
118 this(context, attrs, com.android.internal.R.attr.switchStyle);
119 }
120
121 /**
122 * Construct a new Switch with a default style determined by the given theme attribute,
123 * overriding specific style attributes as requested.
124 *
125 * @param context The Context that will determine this widget's theming.
126 * @param attrs Specification of attributes that should deviate from the default styling.
127 * @param defStyle An attribute ID within the active theme containing a reference to the
128 * default style for this widget. e.g. android.R.attr.switchStyle.
129 */
130 public Switch(Context context, AttributeSet attrs, int defStyle) {
131 super(context, attrs, defStyle);
132
133 mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
134 Resources res = getResources();
135 mTextPaint.density = res.getDisplayMetrics().density;
136 mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);
137
138 TypedArray a = context.obtainStyledAttributes(attrs,
139 com.android.internal.R.styleable.Switch, defStyle, 0);
140
Chet Haase150176d2011-08-26 09:54:06 -0700141 mThumbDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_thumb);
142 mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_track);
Adam Powell12190b32010-11-28 19:07:53 -0800143 mTextOn = a.getText(com.android.internal.R.styleable.Switch_textOn);
144 mTextOff = a.getText(com.android.internal.R.styleable.Switch_textOff);
145 mThumbTextPadding = a.getDimensionPixelSize(
146 com.android.internal.R.styleable.Switch_thumbTextPadding, 0);
147 mSwitchMinWidth = a.getDimensionPixelSize(
148 com.android.internal.R.styleable.Switch_switchMinWidth, 0);
149 mSwitchPadding = a.getDimensionPixelSize(
150 com.android.internal.R.styleable.Switch_switchPadding, 0);
151
152 int appearance = a.getResourceId(
153 com.android.internal.R.styleable.Switch_switchTextAppearance, 0);
154 if (appearance != 0) {
Chet Haase150176d2011-08-26 09:54:06 -0700155 setSwitchTextAppearance(context, appearance);
Adam Powell12190b32010-11-28 19:07:53 -0800156 }
157 a.recycle();
158
159 ViewConfiguration config = ViewConfiguration.get(context);
160 mTouchSlop = config.getScaledTouchSlop();
161 mMinFlingVelocity = config.getScaledMinimumFlingVelocity();
162
163 // Refresh display with current params
164 setChecked(isChecked());
165 }
166
167 /**
168 * Sets the switch text color, size, style, hint color, and highlight color
169 * from the specified TextAppearance resource.
170 */
Chet Haase150176d2011-08-26 09:54:06 -0700171 public void setSwitchTextAppearance(Context context, int resid) {
Adam Powell12190b32010-11-28 19:07:53 -0800172 TypedArray appearance =
Chet Haase150176d2011-08-26 09:54:06 -0700173 context.obtainStyledAttributes(resid,
Adam Powell12190b32010-11-28 19:07:53 -0800174 com.android.internal.R.styleable.TextAppearance);
175
176 ColorStateList colors;
177 int ts;
178
179 colors = appearance.getColorStateList(com.android.internal.R.styleable.
180 TextAppearance_textColor);
181 if (colors != null) {
182 mTextColors = colors;
Chet Haase150176d2011-08-26 09:54:06 -0700183 } else {
184 // If no color set in TextAppearance, default to the view's textColor
185 mTextColors = getTextColors();
Adam Powell12190b32010-11-28 19:07:53 -0800186 }
187
188 ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
189 TextAppearance_textSize, 0);
190 if (ts != 0) {
191 if (ts != mTextPaint.getTextSize()) {
192 mTextPaint.setTextSize(ts);
193 requestLayout();
194 }
195 }
196
197 int typefaceIndex, styleIndex;
198
199 typefaceIndex = appearance.getInt(com.android.internal.R.styleable.
200 TextAppearance_typeface, -1);
201 styleIndex = appearance.getInt(com.android.internal.R.styleable.
202 TextAppearance_textStyle, -1);
203
204 setSwitchTypefaceByIndex(typefaceIndex, styleIndex);
205
Adam Powell12190b32010-11-28 19:07:53 -0800206 appearance.recycle();
207 }
208
209 private void setSwitchTypefaceByIndex(int typefaceIndex, int styleIndex) {
210 Typeface tf = null;
211 switch (typefaceIndex) {
212 case SANS:
213 tf = Typeface.SANS_SERIF;
214 break;
215
216 case SERIF:
217 tf = Typeface.SERIF;
218 break;
219
220 case MONOSPACE:
221 tf = Typeface.MONOSPACE;
222 break;
223 }
224
225 setSwitchTypeface(tf, styleIndex);
226 }
227
228 /**
229 * Sets the typeface and style in which the text should be displayed on the
230 * switch, and turns on the fake bold and italic bits in the Paint if the
231 * Typeface that you provided does not have all the bits in the
232 * style that you specified.
233 */
234 public void setSwitchTypeface(Typeface tf, int style) {
235 if (style > 0) {
236 if (tf == null) {
237 tf = Typeface.defaultFromStyle(style);
238 } else {
239 tf = Typeface.create(tf, style);
240 }
241
242 setSwitchTypeface(tf);
243 // now compute what (if any) algorithmic styling is needed
244 int typefaceStyle = tf != null ? tf.getStyle() : 0;
245 int need = style & ~typefaceStyle;
246 mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
247 mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
248 } else {
249 mTextPaint.setFakeBoldText(false);
250 mTextPaint.setTextSkewX(0);
251 setSwitchTypeface(tf);
252 }
253 }
254
255 /**
Chet Haase150176d2011-08-26 09:54:06 -0700256 * Sets the typeface in which the text should be displayed on the switch.
Adam Powell12190b32010-11-28 19:07:53 -0800257 * Note that not all Typeface families actually have bold and italic
258 * variants, so you may need to use
259 * {@link #setSwitchTypeface(Typeface, int)} to get the appearance
260 * that you actually want.
261 *
262 * @attr ref android.R.styleable#TextView_typeface
263 * @attr ref android.R.styleable#TextView_textStyle
264 */
265 public void setSwitchTypeface(Typeface tf) {
266 if (mTextPaint.getTypeface() != tf) {
267 mTextPaint.setTypeface(tf);
268
269 requestLayout();
270 invalidate();
271 }
272 }
273
274 /**
Chet Haase150176d2011-08-26 09:54:06 -0700275 * Returns the text displayed when the button is in the checked state.
Adam Powell12190b32010-11-28 19:07:53 -0800276 */
277 public CharSequence getTextOn() {
278 return mTextOn;
279 }
280
281 /**
Chet Haase150176d2011-08-26 09:54:06 -0700282 * Sets the text displayed when the button is in the checked state.
Adam Powell12190b32010-11-28 19:07:53 -0800283 */
284 public void setTextOn(CharSequence textOn) {
285 mTextOn = textOn;
286 requestLayout();
287 }
288
289 /**
Chet Haase150176d2011-08-26 09:54:06 -0700290 * Returns the text displayed when the button is not in the checked state.
Adam Powell12190b32010-11-28 19:07:53 -0800291 */
292 public CharSequence getTextOff() {
293 return mTextOff;
294 }
295
296 /**
Chet Haase150176d2011-08-26 09:54:06 -0700297 * Sets the text displayed when the button is not in the checked state.
Adam Powell12190b32010-11-28 19:07:53 -0800298 */
299 public void setTextOff(CharSequence textOff) {
300 mTextOff = textOff;
301 requestLayout();
302 }
303
304 @Override
305 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
306 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
307 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
308 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
309 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
310
311
312 if (mOnLayout == null) {
313 mOnLayout = makeLayout(mTextOn);
314 }
315 if (mOffLayout == null) {
316 mOffLayout = makeLayout(mTextOff);
317 }
318
319 mTrackDrawable.getPadding(mTempRect);
320 final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth());
321 final int switchWidth = Math.max(mSwitchMinWidth,
322 maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right);
323 final int switchHeight = mTrackDrawable.getIntrinsicHeight();
324
325 mThumbWidth = maxTextWidth + mThumbTextPadding * 2;
326
327 switch (widthMode) {
328 case MeasureSpec.AT_MOST:
329 widthSize = Math.min(widthSize, switchWidth);
330 break;
331
332 case MeasureSpec.UNSPECIFIED:
333 widthSize = switchWidth;
334 break;
335
336 case MeasureSpec.EXACTLY:
337 // Just use what we were given
338 break;
339 }
340
341 switch (heightMode) {
342 case MeasureSpec.AT_MOST:
343 heightSize = Math.min(heightSize, switchHeight);
344 break;
345
346 case MeasureSpec.UNSPECIFIED:
347 heightSize = switchHeight;
348 break;
349
350 case MeasureSpec.EXACTLY:
351 // Just use what we were given
352 break;
353 }
354
355 mSwitchWidth = switchWidth;
356 mSwitchHeight = switchHeight;
357
358 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Adam Powell12190b32010-11-28 19:07:53 -0800359 final int measuredHeight = getMeasuredHeight();
360 if (measuredHeight < switchHeight) {
Dianne Hackborn189ee182010-12-02 21:48:53 -0800361 setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);
Adam Powell12190b32010-11-28 19:07:53 -0800362 }
363 }
364
Svetoslav Ganov63bce032011-07-23 19:52:17 -0700365 @Override
366 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
367 super.onPopulateAccessibilityEvent(event);
Svetoslav Ganov76502592011-07-29 10:44:59 -0700368 if (isChecked()) {
369 CharSequence text = mOnLayout.getText();
370 if (TextUtils.isEmpty(text)) {
371 text = mContext.getString(R.string.switch_on);
372 }
373 event.getText().add(text);
374 } else {
375 CharSequence text = mOffLayout.getText();
376 if (TextUtils.isEmpty(text)) {
377 text = mContext.getString(R.string.switch_off);
378 }
379 event.getText().add(text);
380 }
Svetoslav Ganov63bce032011-07-23 19:52:17 -0700381 }
382
Adam Powell12190b32010-11-28 19:07:53 -0800383 private Layout makeLayout(CharSequence text) {
384 return new StaticLayout(text, mTextPaint,
385 (int) Math.ceil(Layout.getDesiredWidth(text, mTextPaint)),
386 Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
387 }
388
389 /**
390 * @return true if (x, y) is within the target area of the switch thumb
391 */
392 private boolean hitThumb(float x, float y) {
393 mThumbDrawable.getPadding(mTempRect);
394 final int thumbTop = mSwitchTop - mTouchSlop;
395 final int thumbLeft = mSwitchLeft + (int) (mThumbPosition + 0.5f) - mTouchSlop;
396 final int thumbRight = thumbLeft + mThumbWidth +
397 mTempRect.left + mTempRect.right + mTouchSlop;
398 final int thumbBottom = mSwitchBottom + mTouchSlop;
399 return x > thumbLeft && x < thumbRight && y > thumbTop && y < thumbBottom;
400 }
401
402 @Override
403 public boolean onTouchEvent(MotionEvent ev) {
404 mVelocityTracker.addMovement(ev);
405 final int action = ev.getActionMasked();
406 switch (action) {
407 case MotionEvent.ACTION_DOWN: {
408 final float x = ev.getX();
409 final float y = ev.getY();
Gilles Debunnec2ab0d62011-06-13 12:52:48 -0700410 if (isEnabled() && hitThumb(x, y)) {
Adam Powell12190b32010-11-28 19:07:53 -0800411 mTouchMode = TOUCH_MODE_DOWN;
412 mTouchX = x;
413 mTouchY = y;
414 }
415 break;
416 }
417
418 case MotionEvent.ACTION_MOVE: {
419 switch (mTouchMode) {
420 case TOUCH_MODE_IDLE:
421 // Didn't target the thumb, treat normally.
422 break;
423
424 case TOUCH_MODE_DOWN: {
425 final float x = ev.getX();
426 final float y = ev.getY();
427 if (Math.abs(x - mTouchX) > mTouchSlop ||
428 Math.abs(y - mTouchY) > mTouchSlop) {
429 mTouchMode = TOUCH_MODE_DRAGGING;
430 getParent().requestDisallowInterceptTouchEvent(true);
431 mTouchX = x;
432 mTouchY = y;
433 return true;
434 }
435 break;
436 }
437
438 case TOUCH_MODE_DRAGGING: {
439 final float x = ev.getX();
440 final float dx = x - mTouchX;
441 float newPos = Math.max(0,
442 Math.min(mThumbPosition + dx, getThumbScrollRange()));
443 if (newPos != mThumbPosition) {
444 mThumbPosition = newPos;
445 mTouchX = x;
446 invalidate();
447 }
448 return true;
449 }
450 }
451 break;
452 }
453
454 case MotionEvent.ACTION_UP:
455 case MotionEvent.ACTION_CANCEL: {
456 if (mTouchMode == TOUCH_MODE_DRAGGING) {
457 stopDrag(ev);
458 return true;
459 }
460 mTouchMode = TOUCH_MODE_IDLE;
461 mVelocityTracker.clear();
462 break;
463 }
464 }
465
466 return super.onTouchEvent(ev);
467 }
468
469 private void cancelSuperTouch(MotionEvent ev) {
470 MotionEvent cancel = MotionEvent.obtain(ev);
471 cancel.setAction(MotionEvent.ACTION_CANCEL);
472 super.onTouchEvent(cancel);
473 cancel.recycle();
474 }
475
476 /**
477 * Called from onTouchEvent to end a drag operation.
478 *
479 * @param ev Event that triggered the end of drag mode - ACTION_UP or ACTION_CANCEL
480 */
481 private void stopDrag(MotionEvent ev) {
482 mTouchMode = TOUCH_MODE_IDLE;
Gilles Debunnec2ab0d62011-06-13 12:52:48 -0700483 // Up and not canceled, also checks the switch has not been disabled during the drag
484 boolean commitChange = ev.getAction() == MotionEvent.ACTION_UP && isEnabled();
Adam Powell12190b32010-11-28 19:07:53 -0800485
486 cancelSuperTouch(ev);
487
488 if (commitChange) {
489 boolean newState;
490 mVelocityTracker.computeCurrentVelocity(1000);
491 float xvel = mVelocityTracker.getXVelocity();
492 if (Math.abs(xvel) > mMinFlingVelocity) {
Adam Powell01d11ed2011-08-09 16:32:12 -0700493 newState = xvel > 0;
Adam Powell12190b32010-11-28 19:07:53 -0800494 } else {
495 newState = getTargetCheckedState();
496 }
497 animateThumbToCheckedState(newState);
498 } else {
499 animateThumbToCheckedState(isChecked());
500 }
501 }
502
503 private void animateThumbToCheckedState(boolean newCheckedState) {
Adam Powell12190b32010-11-28 19:07:53 -0800504 // TODO animate!
Joe Onoratoc3eabb92011-01-07 15:58:44 -0800505 //float targetPos = newCheckedState ? 0 : getThumbScrollRange();
506 //mThumbPosition = targetPos;
Adam Powell12190b32010-11-28 19:07:53 -0800507 setChecked(newCheckedState);
508 }
509
510 private boolean getTargetCheckedState() {
Adam Powellec1d6032011-08-07 18:03:39 -0700511 return mThumbPosition >= getThumbScrollRange() / 2;
Adam Powell12190b32010-11-28 19:07:53 -0800512 }
513
514 @Override
515 public void setChecked(boolean checked) {
516 super.setChecked(checked);
Adam Powellec1d6032011-08-07 18:03:39 -0700517 mThumbPosition = checked ? getThumbScrollRange() : 0;
Joe Onoratoc3eabb92011-01-07 15:58:44 -0800518 invalidate();
Adam Powell12190b32010-11-28 19:07:53 -0800519 }
520
521 @Override
522 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
523 super.onLayout(changed, left, top, right, bottom);
524
Adam Powellec1d6032011-08-07 18:03:39 -0700525 mThumbPosition = isChecked() ? getThumbScrollRange() : 0;
Joe Onoratoc3eabb92011-01-07 15:58:44 -0800526
Adam Powell12190b32010-11-28 19:07:53 -0800527 int switchRight = getWidth() - getPaddingRight();
528 int switchLeft = switchRight - mSwitchWidth;
529 int switchTop = 0;
530 int switchBottom = 0;
531 switch (getGravity() & Gravity.VERTICAL_GRAVITY_MASK) {
532 default:
533 case Gravity.TOP:
534 switchTop = getPaddingTop();
535 switchBottom = switchTop + mSwitchHeight;
536 break;
537
538 case Gravity.CENTER_VERTICAL:
539 switchTop = (getPaddingTop() + getHeight() - getPaddingBottom()) / 2 -
540 mSwitchHeight / 2;
541 switchBottom = switchTop + mSwitchHeight;
542 break;
543
544 case Gravity.BOTTOM:
545 switchBottom = getHeight() - getPaddingBottom();
546 switchTop = switchBottom - mSwitchHeight;
547 break;
548 }
549
550 mSwitchLeft = switchLeft;
551 mSwitchTop = switchTop;
552 mSwitchBottom = switchBottom;
553 mSwitchRight = switchRight;
554 }
555
556 @Override
557 protected void onDraw(Canvas canvas) {
558 super.onDraw(canvas);
559
560 // Draw the switch
561 int switchLeft = mSwitchLeft;
562 int switchTop = mSwitchTop;
563 int switchRight = mSwitchRight;
564 int switchBottom = mSwitchBottom;
565
566 mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);
567 mTrackDrawable.draw(canvas);
568
569 canvas.save();
570
571 mTrackDrawable.getPadding(mTempRect);
572 int switchInnerLeft = switchLeft + mTempRect.left;
573 int switchInnerTop = switchTop + mTempRect.top;
574 int switchInnerRight = switchRight - mTempRect.right;
575 int switchInnerBottom = switchBottom - mTempRect.bottom;
576 canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);
577
578 mThumbDrawable.getPadding(mTempRect);
579 final int thumbPos = (int) (mThumbPosition + 0.5f);
580 int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
581 int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;
582
583 mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
584 mThumbDrawable.draw(canvas);
585
Chet Haase150176d2011-08-26 09:54:06 -0700586 // mTextColors should not be null, but just in case
587 if (mTextColors != null) {
588 mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(),
589 mTextColors.getDefaultColor()));
590 }
Adam Powell12190b32010-11-28 19:07:53 -0800591 mTextPaint.drawableState = getDrawableState();
592
593 Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
594
595 canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,
596 (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);
597 switchText.draw(canvas);
598
599 canvas.restore();
600 }
601
602 @Override
603 public int getCompoundPaddingRight() {
604 int padding = super.getCompoundPaddingRight() + mSwitchWidth;
605 if (!TextUtils.isEmpty(getText())) {
606 padding += mSwitchPadding;
607 }
608 return padding;
609 }
610
611 private int getThumbScrollRange() {
612 if (mTrackDrawable == null) {
613 return 0;
614 }
615 mTrackDrawable.getPadding(mTempRect);
616 return mSwitchWidth - mThumbWidth - mTempRect.left - mTempRect.right;
617 }
618
619 @Override
620 protected int[] onCreateDrawableState(int extraSpace) {
621 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
622 if (isChecked()) {
623 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
624 }
625 return drawableState;
626 }
627
628 @Override
629 protected void drawableStateChanged() {
630 super.drawableStateChanged();
631
632 int[] myDrawableState = getDrawableState();
633
634 // Set the state of the Drawable
635 mThumbDrawable.setState(myDrawableState);
636 mTrackDrawable.setState(myDrawableState);
637
638 invalidate();
639 }
640
641 @Override
642 protected boolean verifyDrawable(Drawable who) {
643 return super.verifyDrawable(who) || who == mThumbDrawable || who == mTrackDrawable;
644 }
645
646 @Override
647 public void jumpDrawablesToCurrentState() {
648 super.jumpDrawablesToCurrentState();
649 mThumbDrawable.jumpToCurrentState();
650 mTrackDrawable.jumpToCurrentState();
651 }
652}