Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 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 | |
| 17 | package android.widget; |
| 18 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.ColorStateList; |
| 21 | import android.content.res.Resources; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.Paint; |
| 25 | import android.graphics.Rect; |
| 26 | import android.graphics.Typeface; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.text.Layout; |
| 29 | import android.text.StaticLayout; |
| 30 | import android.text.TextPaint; |
| 31 | import android.text.TextUtils; |
| 32 | import android.util.AttributeSet; |
| 33 | import android.view.Gravity; |
| 34 | import android.view.MotionEvent; |
| 35 | import android.view.VelocityTracker; |
| 36 | import android.view.ViewConfiguration; |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 37 | import android.view.accessibility.AccessibilityEvent; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 38 | |
Adam Powell | be0a453 | 2010-11-29 17:47:48 -0800 | [diff] [blame] | 39 | import com.android.internal.R; |
| 40 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 41 | /** |
| 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 44 | * 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 Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 52 | * |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 53 | */ |
| 54 | public 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 Powell | be0a453 | 2010-11-29 17:47:48 -0800 | [diff] [blame] | 94 | @SuppressWarnings("hiding") |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 95 | 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 141 | mThumbDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_thumb); |
| 142 | mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_track); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 143 | 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 155 | setSwitchTextAppearance(context, appearance); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 156 | } |
| 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 171 | public void setSwitchTextAppearance(Context context, int resid) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 172 | TypedArray appearance = |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 173 | context.obtainStyledAttributes(resid, |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 174 | 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 183 | } else { |
| 184 | // If no color set in TextAppearance, default to the view's textColor |
| 185 | mTextColors = getTextColors(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 186 | } |
| 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 Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 206 | 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 256 | * Sets the typeface in which the text should be displayed on the switch. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 257 | * 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 275 | * Returns the text displayed when the button is in the checked state. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 276 | */ |
| 277 | public CharSequence getTextOn() { |
| 278 | return mTextOn; |
| 279 | } |
| 280 | |
| 281 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 282 | * Sets the text displayed when the button is in the checked state. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 283 | */ |
| 284 | public void setTextOn(CharSequence textOn) { |
| 285 | mTextOn = textOn; |
| 286 | requestLayout(); |
| 287 | } |
| 288 | |
| 289 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 290 | * Returns the text displayed when the button is not in the checked state. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 291 | */ |
| 292 | public CharSequence getTextOff() { |
| 293 | return mTextOff; |
| 294 | } |
| 295 | |
| 296 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 297 | * Sets the text displayed when the button is not in the checked state. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 298 | */ |
| 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 Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 359 | final int measuredHeight = getMeasuredHeight(); |
| 360 | if (measuredHeight < switchHeight) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 361 | setMeasuredDimension(getMeasuredWidthAndState(), switchHeight); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 365 | @Override |
| 366 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 367 | super.onPopulateAccessibilityEvent(event); |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 368 | 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 Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 383 | 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 Debunne | c2ab0d6 | 2011-06-13 12:52:48 -0700 | [diff] [blame] | 410 | if (isEnabled() && hitThumb(x, y)) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 411 | 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 Debunne | c2ab0d6 | 2011-06-13 12:52:48 -0700 | [diff] [blame] | 483 | // 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 Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 485 | |
| 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 Powell | 01d11ed | 2011-08-09 16:32:12 -0700 | [diff] [blame] | 493 | newState = xvel > 0; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 494 | } else { |
| 495 | newState = getTargetCheckedState(); |
| 496 | } |
| 497 | animateThumbToCheckedState(newState); |
| 498 | } else { |
| 499 | animateThumbToCheckedState(isChecked()); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | private void animateThumbToCheckedState(boolean newCheckedState) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 504 | // TODO animate! |
Joe Onorato | c3eabb9 | 2011-01-07 15:58:44 -0800 | [diff] [blame] | 505 | //float targetPos = newCheckedState ? 0 : getThumbScrollRange(); |
| 506 | //mThumbPosition = targetPos; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 507 | setChecked(newCheckedState); |
| 508 | } |
| 509 | |
| 510 | private boolean getTargetCheckedState() { |
Adam Powell | ec1d603 | 2011-08-07 18:03:39 -0700 | [diff] [blame] | 511 | return mThumbPosition >= getThumbScrollRange() / 2; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void setChecked(boolean checked) { |
| 516 | super.setChecked(checked); |
Adam Powell | ec1d603 | 2011-08-07 18:03:39 -0700 | [diff] [blame] | 517 | mThumbPosition = checked ? getThumbScrollRange() : 0; |
Joe Onorato | c3eabb9 | 2011-01-07 15:58:44 -0800 | [diff] [blame] | 518 | invalidate(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 519 | } |
| 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 Powell | ec1d603 | 2011-08-07 18:03:39 -0700 | [diff] [blame] | 525 | mThumbPosition = isChecked() ? getThumbScrollRange() : 0; |
Joe Onorato | c3eabb9 | 2011-01-07 15:58:44 -0800 | [diff] [blame] | 526 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 527 | 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 Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame^] | 586 | // mTextColors should not be null, but just in case |
| 587 | if (mTextColors != null) { |
| 588 | mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(), |
| 589 | mTextColors.getDefaultColor())); |
| 590 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 591 | 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 | } |