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