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