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 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.ColorStateList; |
| 22 | import android.content.res.Resources; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.Rect; |
| 27 | import android.graphics.Typeface; |
| 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.text.Layout; |
| 30 | import android.text.StaticLayout; |
| 31 | import android.text.TextPaint; |
| 32 | import android.text.TextUtils; |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 33 | import android.text.method.AllCapsTransformationMethod; |
| 34 | import android.text.method.TransformationMethod2; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 36 | import android.util.FloatProperty; |
| 37 | import android.util.MathUtils; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 38 | import android.view.Gravity; |
| 39 | import android.view.MotionEvent; |
| 40 | import android.view.VelocityTracker; |
| 41 | import android.view.ViewConfiguration; |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 43 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 44 | |
Adam Powell | be0a453 | 2010-11-29 17:47:48 -0800 | [diff] [blame] | 45 | import com.android.internal.R; |
| 46 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 47 | /** |
| 48 | * A Switch is a two-state toggle switch widget that can select between two |
| 49 | * 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] | 50 | * or simply tap to toggle as if it were a checkbox. The {@link #setText(CharSequence) text} |
| 51 | * property controls the text displayed in the label for the switch, whereas the |
| 52 | * {@link #setTextOff(CharSequence) off} and {@link #setTextOn(CharSequence) on} text |
| 53 | * controls the text on the thumb. Similarly, the |
| 54 | * {@link #setTextAppearance(android.content.Context, int) textAppearance} and the related |
| 55 | * setTypeface() methods control the typeface and style of label text, whereas the |
| 56 | * {@link #setSwitchTextAppearance(android.content.Context, int) switchTextAppearance} and |
| 57 | * the related seSwitchTypeface() methods control that of the thumb. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 58 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 59 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a> |
| 60 | * guide.</p> |
| 61 | * |
| 62 | * @attr ref android.R.styleable#Switch_textOn |
| 63 | * @attr ref android.R.styleable#Switch_textOff |
| 64 | * @attr ref android.R.styleable#Switch_switchMinWidth |
| 65 | * @attr ref android.R.styleable#Switch_switchPadding |
| 66 | * @attr ref android.R.styleable#Switch_switchTextAppearance |
| 67 | * @attr ref android.R.styleable#Switch_thumb |
| 68 | * @attr ref android.R.styleable#Switch_thumbTextPadding |
| 69 | * @attr ref android.R.styleable#Switch_track |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 70 | */ |
| 71 | public class Switch extends CompoundButton { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 72 | private static final int THUMB_ANIMATION_DURATION = 250; |
| 73 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 74 | private static final int TOUCH_MODE_IDLE = 0; |
| 75 | private static final int TOUCH_MODE_DOWN = 1; |
| 76 | private static final int TOUCH_MODE_DRAGGING = 2; |
| 77 | |
| 78 | // Enum for the "typeface" XML parameter. |
| 79 | private static final int SANS = 1; |
| 80 | private static final int SERIF = 2; |
| 81 | private static final int MONOSPACE = 3; |
| 82 | |
| 83 | private Drawable mThumbDrawable; |
| 84 | private Drawable mTrackDrawable; |
| 85 | private int mThumbTextPadding; |
| 86 | private int mSwitchMinWidth; |
| 87 | private int mSwitchPadding; |
| 88 | private CharSequence mTextOn; |
| 89 | private CharSequence mTextOff; |
| 90 | |
| 91 | private int mTouchMode; |
| 92 | private int mTouchSlop; |
| 93 | private float mTouchX; |
| 94 | private float mTouchY; |
| 95 | private VelocityTracker mVelocityTracker = VelocityTracker.obtain(); |
| 96 | private int mMinFlingVelocity; |
| 97 | |
| 98 | private float mThumbPosition; |
| 99 | private int mSwitchWidth; |
| 100 | private int mSwitchHeight; |
| 101 | private int mThumbWidth; // Does not include padding |
| 102 | |
| 103 | private int mSwitchLeft; |
| 104 | private int mSwitchTop; |
| 105 | private int mSwitchRight; |
| 106 | private int mSwitchBottom; |
| 107 | |
| 108 | private TextPaint mTextPaint; |
| 109 | private ColorStateList mTextColors; |
| 110 | private Layout mOnLayout; |
| 111 | private Layout mOffLayout; |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 112 | private TransformationMethod2 mSwitchTransformationMethod; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 113 | private ObjectAnimator mPositionAnimator; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 114 | |
Adam Powell | be0a453 | 2010-11-29 17:47:48 -0800 | [diff] [blame] | 115 | @SuppressWarnings("hiding") |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 116 | private final Rect mTempRect = new Rect(); |
| 117 | |
| 118 | private static final int[] CHECKED_STATE_SET = { |
| 119 | R.attr.state_checked |
| 120 | }; |
| 121 | |
| 122 | /** |
| 123 | * Construct a new Switch with default styling. |
| 124 | * |
| 125 | * @param context The Context that will determine this widget's theming. |
| 126 | */ |
| 127 | public Switch(Context context) { |
| 128 | this(context, null); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Construct a new Switch with default styling, overriding specific style |
| 133 | * attributes as requested. |
| 134 | * |
| 135 | * @param context The Context that will determine this widget's theming. |
| 136 | * @param attrs Specification of attributes that should deviate from default styling. |
| 137 | */ |
| 138 | public Switch(Context context, AttributeSet attrs) { |
| 139 | this(context, attrs, com.android.internal.R.attr.switchStyle); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Construct a new Switch with a default style determined by the given theme attribute, |
| 144 | * overriding specific style attributes as requested. |
| 145 | * |
| 146 | * @param context The Context that will determine this widget's theming. |
| 147 | * @param attrs Specification of attributes that should deviate from the default styling. |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 148 | * @param defStyleAttr An attribute in the current theme that contains a |
| 149 | * reference to a style resource that supplies default values for |
| 150 | * the view. Can be 0 to not look for defaults. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 151 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 152 | public Switch(Context context, AttributeSet attrs, int defStyleAttr) { |
| 153 | this(context, attrs, defStyleAttr, 0); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Construct a new Switch with a default style determined by the given theme |
| 159 | * attribute or style resource, overriding specific style attributes as |
| 160 | * requested. |
| 161 | * |
| 162 | * @param context The Context that will determine this widget's theming. |
| 163 | * @param attrs Specification of attributes that should deviate from the |
| 164 | * default styling. |
| 165 | * @param defStyleAttr An attribute in the current theme that contains a |
| 166 | * reference to a style resource that supplies default values for |
| 167 | * the view. Can be 0 to not look for defaults. |
| 168 | * @param defStyleRes A resource identifier of a style resource that |
| 169 | * supplies default values for the view, used only if |
| 170 | * defStyleAttr is 0 or can not be found in the theme. Can be 0 |
| 171 | * to not look for defaults. |
| 172 | */ |
| 173 | public Switch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 174 | super(context, attrs, defStyleAttr, defStyleRes); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 175 | |
| 176 | mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); |
| 177 | Resources res = getResources(); |
| 178 | mTextPaint.density = res.getDisplayMetrics().density; |
| 179 | mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale); |
| 180 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 181 | final TypedArray a = context.obtainStyledAttributes( |
| 182 | attrs, com.android.internal.R.styleable.Switch, defStyleAttr, defStyleRes); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 183 | |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 184 | mThumbDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_thumb); |
| 185 | mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_track); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 186 | mTextOn = a.getText(com.android.internal.R.styleable.Switch_textOn); |
| 187 | mTextOff = a.getText(com.android.internal.R.styleable.Switch_textOff); |
| 188 | mThumbTextPadding = a.getDimensionPixelSize( |
| 189 | com.android.internal.R.styleable.Switch_thumbTextPadding, 0); |
| 190 | mSwitchMinWidth = a.getDimensionPixelSize( |
| 191 | com.android.internal.R.styleable.Switch_switchMinWidth, 0); |
| 192 | mSwitchPadding = a.getDimensionPixelSize( |
| 193 | com.android.internal.R.styleable.Switch_switchPadding, 0); |
| 194 | |
| 195 | int appearance = a.getResourceId( |
| 196 | com.android.internal.R.styleable.Switch_switchTextAppearance, 0); |
| 197 | if (appearance != 0) { |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 198 | setSwitchTextAppearance(context, appearance); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 199 | } |
| 200 | a.recycle(); |
| 201 | |
| 202 | ViewConfiguration config = ViewConfiguration.get(context); |
| 203 | mTouchSlop = config.getScaledTouchSlop(); |
| 204 | mMinFlingVelocity = config.getScaledMinimumFlingVelocity(); |
| 205 | |
| 206 | // Refresh display with current params |
Gilles Debunne | e724ee4 | 2011-08-31 11:20:27 -0700 | [diff] [blame] | 207 | refreshDrawableState(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 208 | setChecked(isChecked()); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Sets the switch text color, size, style, hint color, and highlight color |
| 213 | * from the specified TextAppearance resource. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 214 | * |
| 215 | * @attr ref android.R.styleable#Switch_switchTextAppearance |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 216 | */ |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 217 | public void setSwitchTextAppearance(Context context, int resid) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 218 | TypedArray appearance = |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 219 | context.obtainStyledAttributes(resid, |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 220 | com.android.internal.R.styleable.TextAppearance); |
| 221 | |
| 222 | ColorStateList colors; |
| 223 | int ts; |
| 224 | |
| 225 | colors = appearance.getColorStateList(com.android.internal.R.styleable. |
| 226 | TextAppearance_textColor); |
| 227 | if (colors != null) { |
| 228 | mTextColors = colors; |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 229 | } else { |
| 230 | // If no color set in TextAppearance, default to the view's textColor |
| 231 | mTextColors = getTextColors(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable. |
| 235 | TextAppearance_textSize, 0); |
| 236 | if (ts != 0) { |
| 237 | if (ts != mTextPaint.getTextSize()) { |
| 238 | mTextPaint.setTextSize(ts); |
| 239 | requestLayout(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | int typefaceIndex, styleIndex; |
| 244 | |
| 245 | typefaceIndex = appearance.getInt(com.android.internal.R.styleable. |
| 246 | TextAppearance_typeface, -1); |
| 247 | styleIndex = appearance.getInt(com.android.internal.R.styleable. |
| 248 | TextAppearance_textStyle, -1); |
| 249 | |
| 250 | setSwitchTypefaceByIndex(typefaceIndex, styleIndex); |
| 251 | |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 252 | boolean allCaps = appearance.getBoolean(com.android.internal.R.styleable. |
| 253 | TextAppearance_textAllCaps, false); |
| 254 | if (allCaps) { |
| 255 | mSwitchTransformationMethod = new AllCapsTransformationMethod(getContext()); |
| 256 | mSwitchTransformationMethod.setLengthChangesAllowed(true); |
| 257 | } else { |
| 258 | mSwitchTransformationMethod = null; |
| 259 | } |
| 260 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 261 | appearance.recycle(); |
| 262 | } |
| 263 | |
| 264 | private void setSwitchTypefaceByIndex(int typefaceIndex, int styleIndex) { |
| 265 | Typeface tf = null; |
| 266 | switch (typefaceIndex) { |
| 267 | case SANS: |
| 268 | tf = Typeface.SANS_SERIF; |
| 269 | break; |
| 270 | |
| 271 | case SERIF: |
| 272 | tf = Typeface.SERIF; |
| 273 | break; |
| 274 | |
| 275 | case MONOSPACE: |
| 276 | tf = Typeface.MONOSPACE; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | setSwitchTypeface(tf, styleIndex); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Sets the typeface and style in which the text should be displayed on the |
| 285 | * switch, and turns on the fake bold and italic bits in the Paint if the |
| 286 | * Typeface that you provided does not have all the bits in the |
| 287 | * style that you specified. |
| 288 | */ |
| 289 | public void setSwitchTypeface(Typeface tf, int style) { |
| 290 | if (style > 0) { |
| 291 | if (tf == null) { |
| 292 | tf = Typeface.defaultFromStyle(style); |
| 293 | } else { |
| 294 | tf = Typeface.create(tf, style); |
| 295 | } |
| 296 | |
| 297 | setSwitchTypeface(tf); |
| 298 | // now compute what (if any) algorithmic styling is needed |
| 299 | int typefaceStyle = tf != null ? tf.getStyle() : 0; |
| 300 | int need = style & ~typefaceStyle; |
| 301 | mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0); |
| 302 | mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0); |
| 303 | } else { |
Victoria Lease | aa0980a | 2012-06-11 14:46:04 -0700 | [diff] [blame] | 304 | mTextPaint.setFakeBoldText(false); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 305 | mTextPaint.setTextSkewX(0); |
| 306 | setSwitchTypeface(tf); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 311 | * 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] | 312 | * Note that not all Typeface families actually have bold and italic |
| 313 | * variants, so you may need to use |
| 314 | * {@link #setSwitchTypeface(Typeface, int)} to get the appearance |
| 315 | * that you actually want. |
| 316 | * |
| 317 | * @attr ref android.R.styleable#TextView_typeface |
| 318 | * @attr ref android.R.styleable#TextView_textStyle |
| 319 | */ |
| 320 | public void setSwitchTypeface(Typeface tf) { |
| 321 | if (mTextPaint.getTypeface() != tf) { |
| 322 | mTextPaint.setTypeface(tf); |
| 323 | |
| 324 | requestLayout(); |
| 325 | invalidate(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 330 | * Set the amount of horizontal padding between the switch and the associated text. |
| 331 | * |
| 332 | * @param pixels Amount of padding in pixels |
| 333 | * |
| 334 | * @attr ref android.R.styleable#Switch_switchPadding |
| 335 | */ |
| 336 | public void setSwitchPadding(int pixels) { |
| 337 | mSwitchPadding = pixels; |
| 338 | requestLayout(); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get the amount of horizontal padding between the switch and the associated text. |
| 343 | * |
| 344 | * @return Amount of padding in pixels |
| 345 | * |
| 346 | * @attr ref android.R.styleable#Switch_switchPadding |
| 347 | */ |
| 348 | public int getSwitchPadding() { |
| 349 | return mSwitchPadding; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Set the minimum width of the switch in pixels. The switch's width will be the maximum |
| 354 | * of this value and its measured width as determined by the switch drawables and text used. |
| 355 | * |
| 356 | * @param pixels Minimum width of the switch in pixels |
| 357 | * |
| 358 | * @attr ref android.R.styleable#Switch_switchMinWidth |
| 359 | */ |
| 360 | public void setSwitchMinWidth(int pixels) { |
| 361 | mSwitchMinWidth = pixels; |
| 362 | requestLayout(); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Get the minimum width of the switch in pixels. The switch's width will be the maximum |
| 367 | * of this value and its measured width as determined by the switch drawables and text used. |
| 368 | * |
| 369 | * @return Minimum width of the switch in pixels |
| 370 | * |
| 371 | * @attr ref android.R.styleable#Switch_switchMinWidth |
| 372 | */ |
| 373 | public int getSwitchMinWidth() { |
| 374 | return mSwitchMinWidth; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Set the horizontal padding around the text drawn on the switch itself. |
| 379 | * |
| 380 | * @param pixels Horizontal padding for switch thumb text in pixels |
| 381 | * |
| 382 | * @attr ref android.R.styleable#Switch_thumbTextPadding |
| 383 | */ |
| 384 | public void setThumbTextPadding(int pixels) { |
| 385 | mThumbTextPadding = pixels; |
| 386 | requestLayout(); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Get the horizontal padding around the text drawn on the switch itself. |
| 391 | * |
| 392 | * @return Horizontal padding for switch thumb text in pixels |
| 393 | * |
| 394 | * @attr ref android.R.styleable#Switch_thumbTextPadding |
| 395 | */ |
| 396 | public int getThumbTextPadding() { |
| 397 | return mThumbTextPadding; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Set the drawable used for the track that the switch slides within. |
| 402 | * |
| 403 | * @param track Track drawable |
| 404 | * |
| 405 | * @attr ref android.R.styleable#Switch_track |
| 406 | */ |
| 407 | public void setTrackDrawable(Drawable track) { |
| 408 | mTrackDrawable = track; |
| 409 | requestLayout(); |
| 410 | } |
| 411 | |
| 412 | /** |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 413 | * Set the drawable used for the track that the switch slides within. |
| 414 | * |
Adam Powell | dca510e | 2012-03-08 20:06:39 -0800 | [diff] [blame] | 415 | * @param resId Resource ID of a track drawable |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 416 | * |
| 417 | * @attr ref android.R.styleable#Switch_track |
| 418 | */ |
| 419 | public void setTrackResource(int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 420 | setTrackDrawable(getContext().getDrawable(resId)); |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 424 | * Get the drawable used for the track that the switch slides within. |
| 425 | * |
| 426 | * @return Track drawable |
| 427 | * |
| 428 | * @attr ref android.R.styleable#Switch_track |
| 429 | */ |
| 430 | public Drawable getTrackDrawable() { |
| 431 | return mTrackDrawable; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Set the drawable used for the switch "thumb" - the piece that the user |
| 436 | * can physically touch and drag along the track. |
| 437 | * |
| 438 | * @param thumb Thumb drawable |
| 439 | * |
| 440 | * @attr ref android.R.styleable#Switch_thumb |
| 441 | */ |
| 442 | public void setThumbDrawable(Drawable thumb) { |
| 443 | mThumbDrawable = thumb; |
| 444 | requestLayout(); |
| 445 | } |
| 446 | |
| 447 | /** |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 448 | * Set the drawable used for the switch "thumb" - the piece that the user |
| 449 | * can physically touch and drag along the track. |
| 450 | * |
Adam Powell | dca510e | 2012-03-08 20:06:39 -0800 | [diff] [blame] | 451 | * @param resId Resource ID of a thumb drawable |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 452 | * |
| 453 | * @attr ref android.R.styleable#Switch_thumb |
| 454 | */ |
| 455 | public void setThumbResource(int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 456 | setThumbDrawable(getContext().getDrawable(resId)); |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 460 | * Get the drawable used for the switch "thumb" - the piece that the user |
| 461 | * can physically touch and drag along the track. |
| 462 | * |
| 463 | * @return Thumb drawable |
| 464 | * |
| 465 | * @attr ref android.R.styleable#Switch_thumb |
| 466 | */ |
| 467 | public Drawable getThumbDrawable() { |
| 468 | return mThumbDrawable; |
| 469 | } |
| 470 | |
| 471 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 472 | * Returns the text displayed when the button is in the checked state. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 473 | * |
| 474 | * @attr ref android.R.styleable#Switch_textOn |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 475 | */ |
| 476 | public CharSequence getTextOn() { |
| 477 | return mTextOn; |
| 478 | } |
| 479 | |
| 480 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 481 | * Sets the text displayed when the button is in the checked state. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 482 | * |
| 483 | * @attr ref android.R.styleable#Switch_textOn |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 484 | */ |
| 485 | public void setTextOn(CharSequence textOn) { |
| 486 | mTextOn = textOn; |
| 487 | requestLayout(); |
| 488 | } |
| 489 | |
| 490 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 491 | * Returns the text displayed when the button is not in the checked state. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 492 | * |
| 493 | * @attr ref android.R.styleable#Switch_textOff |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 494 | */ |
| 495 | public CharSequence getTextOff() { |
| 496 | return mTextOff; |
| 497 | } |
| 498 | |
| 499 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 500 | * Sets the text displayed when the button is not in the checked state. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 501 | * |
| 502 | * @attr ref android.R.styleable#Switch_textOff |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 503 | */ |
| 504 | public void setTextOff(CharSequence textOff) { |
| 505 | mTextOff = textOff; |
| 506 | requestLayout(); |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 511 | if (mOnLayout == null) { |
| 512 | mOnLayout = makeLayout(mTextOn); |
| 513 | } |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 514 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 515 | if (mOffLayout == null) { |
| 516 | mOffLayout = makeLayout(mTextOff); |
| 517 | } |
| 518 | |
| 519 | mTrackDrawable.getPadding(mTempRect); |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 520 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 521 | final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()); |
| 522 | final int switchWidth = Math.max(mSwitchMinWidth, |
| 523 | maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right); |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 524 | final int switchHeight = Math.max(mTrackDrawable.getIntrinsicHeight(), |
| 525 | mThumbDrawable.getIntrinsicHeight()); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 526 | |
| 527 | mThumbWidth = maxTextWidth + mThumbTextPadding * 2; |
| 528 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 529 | mSwitchWidth = switchWidth; |
| 530 | mSwitchHeight = switchHeight; |
| 531 | |
| 532 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 533 | final int measuredHeight = getMeasuredHeight(); |
| 534 | if (measuredHeight < switchHeight) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 535 | setMeasuredDimension(getMeasuredWidthAndState(), switchHeight); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 539 | @Override |
| 540 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 541 | super.onPopulateAccessibilityEvent(event); |
Svetoslav Ganov | b8c50e8 | 2012-09-14 11:33:39 -0700 | [diff] [blame] | 542 | Layout layout = isChecked() ? mOnLayout : mOffLayout; |
| 543 | if (layout != null && !TextUtils.isEmpty(layout.getText())) { |
| 544 | event.getText().add(layout.getText()); |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 545 | } |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 548 | private Layout makeLayout(CharSequence text) { |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 549 | final CharSequence transformed = (mSwitchTransformationMethod != null) |
| 550 | ? mSwitchTransformationMethod.getTransformation(text, this) |
| 551 | : text; |
| 552 | |
| 553 | return new StaticLayout(transformed, mTextPaint, |
| 554 | (int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)), |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 555 | Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @return true if (x, y) is within the target area of the switch thumb |
| 560 | */ |
| 561 | private boolean hitThumb(float x, float y) { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 562 | // Relies on mTempRect, MUST be called first! |
| 563 | final int thumbOffset = getThumbOffset(); |
| 564 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 565 | mThumbDrawable.getPadding(mTempRect); |
| 566 | final int thumbTop = mSwitchTop - mTouchSlop; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 567 | final int thumbLeft = mSwitchLeft + thumbOffset - mTouchSlop; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 568 | final int thumbRight = thumbLeft + mThumbWidth + |
| 569 | mTempRect.left + mTempRect.right + mTouchSlop; |
| 570 | final int thumbBottom = mSwitchBottom + mTouchSlop; |
| 571 | return x > thumbLeft && x < thumbRight && y > thumbTop && y < thumbBottom; |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public boolean onTouchEvent(MotionEvent ev) { |
| 576 | mVelocityTracker.addMovement(ev); |
| 577 | final int action = ev.getActionMasked(); |
| 578 | switch (action) { |
| 579 | case MotionEvent.ACTION_DOWN: { |
| 580 | final float x = ev.getX(); |
| 581 | final float y = ev.getY(); |
Gilles Debunne | c2ab0d6 | 2011-06-13 12:52:48 -0700 | [diff] [blame] | 582 | if (isEnabled() && hitThumb(x, y)) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 583 | mTouchMode = TOUCH_MODE_DOWN; |
| 584 | mTouchX = x; |
| 585 | mTouchY = y; |
| 586 | } |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | case MotionEvent.ACTION_MOVE: { |
| 591 | switch (mTouchMode) { |
| 592 | case TOUCH_MODE_IDLE: |
| 593 | // Didn't target the thumb, treat normally. |
| 594 | break; |
| 595 | |
| 596 | case TOUCH_MODE_DOWN: { |
| 597 | final float x = ev.getX(); |
| 598 | final float y = ev.getY(); |
| 599 | if (Math.abs(x - mTouchX) > mTouchSlop || |
| 600 | Math.abs(y - mTouchY) > mTouchSlop) { |
| 601 | mTouchMode = TOUCH_MODE_DRAGGING; |
| 602 | getParent().requestDisallowInterceptTouchEvent(true); |
| 603 | mTouchX = x; |
| 604 | mTouchY = y; |
| 605 | return true; |
| 606 | } |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | case TOUCH_MODE_DRAGGING: { |
| 611 | final float x = ev.getX(); |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 612 | final int thumbScrollRange = getThumbScrollRange(); |
| 613 | final float thumbScrollOffset = x - mTouchX; |
| 614 | float dPos; |
| 615 | if (thumbScrollRange != 0) { |
| 616 | dPos = thumbScrollOffset / thumbScrollRange; |
| 617 | } else { |
| 618 | // If the thumb scroll range is empty, just use the |
| 619 | // movement direction to snap on or off. |
| 620 | dPos = thumbScrollOffset > 0 ? 1 : -1; |
| 621 | } |
| 622 | if (isLayoutRtl()) { |
| 623 | dPos = -dPos; |
| 624 | } |
| 625 | final float newPos = MathUtils.constrain(mThumbPosition + dPos, 0, 1); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 626 | if (newPos != mThumbPosition) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 627 | mTouchX = x; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 628 | setThumbPosition(newPos); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 629 | } |
| 630 | return true; |
| 631 | } |
| 632 | } |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | case MotionEvent.ACTION_UP: |
| 637 | case MotionEvent.ACTION_CANCEL: { |
| 638 | if (mTouchMode == TOUCH_MODE_DRAGGING) { |
| 639 | stopDrag(ev); |
| 640 | return true; |
| 641 | } |
| 642 | mTouchMode = TOUCH_MODE_IDLE; |
| 643 | mVelocityTracker.clear(); |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return super.onTouchEvent(ev); |
| 649 | } |
| 650 | |
| 651 | private void cancelSuperTouch(MotionEvent ev) { |
| 652 | MotionEvent cancel = MotionEvent.obtain(ev); |
| 653 | cancel.setAction(MotionEvent.ACTION_CANCEL); |
| 654 | super.onTouchEvent(cancel); |
| 655 | cancel.recycle(); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Called from onTouchEvent to end a drag operation. |
| 660 | * |
| 661 | * @param ev Event that triggered the end of drag mode - ACTION_UP or ACTION_CANCEL |
| 662 | */ |
| 663 | private void stopDrag(MotionEvent ev) { |
| 664 | mTouchMode = TOUCH_MODE_IDLE; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 665 | |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 666 | // Commit the change if the event is up and not canceled and the switch |
| 667 | // has not been disabled during the drag. |
| 668 | final boolean commitChange = ev.getAction() == MotionEvent.ACTION_UP && isEnabled(); |
| 669 | final boolean newState; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 670 | if (commitChange) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 671 | mVelocityTracker.computeCurrentVelocity(1000); |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 672 | final float xvel = mVelocityTracker.getXVelocity(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 673 | if (Math.abs(xvel) > mMinFlingVelocity) { |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 674 | newState = isLayoutRtl() ? (xvel < 0) : (xvel > 0); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 675 | } else { |
| 676 | newState = getTargetCheckedState(); |
| 677 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 678 | } else { |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 679 | newState = isChecked(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 680 | } |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 681 | |
| 682 | setChecked(newState); |
| 683 | cancelSuperTouch(ev); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | private void animateThumbToCheckedState(boolean newCheckedState) { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 687 | final float targetPosition = newCheckedState ? 1 : 0; |
| 688 | mPositionAnimator = ObjectAnimator.ofFloat(this, THUMB_POS, targetPosition); |
| 689 | mPositionAnimator.setDuration(THUMB_ANIMATION_DURATION); |
| 690 | mPositionAnimator.setAutoCancel(true); |
| 691 | mPositionAnimator.start(); |
| 692 | } |
| 693 | |
| 694 | private void cancelPositionAnimator() { |
| 695 | if (mPositionAnimator != null) { |
| 696 | mPositionAnimator.cancel(); |
| 697 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | private boolean getTargetCheckedState() { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 701 | return mThumbPosition > 0.5f; |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 704 | /** |
| 705 | * Sets the thumb position as a decimal value between 0 (off) and 1 (on). |
| 706 | * |
| 707 | * @param position new position between [0,1] |
| 708 | */ |
| 709 | private void setThumbPosition(float position) { |
| 710 | mThumbPosition = position; |
| 711 | invalidate(); |
| 712 | } |
| 713 | |
| 714 | @Override |
| 715 | public void toggle() { |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 716 | setChecked(!isChecked()); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | @Override |
| 720 | public void setChecked(boolean checked) { |
| 721 | super.setChecked(checked); |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 722 | |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 723 | if (isAttachedToWindow() && isLaidOut()) { |
| 724 | animateThumbToCheckedState(checked); |
| 725 | } else { |
| 726 | // Immediately move the thumb to the new position. |
| 727 | cancelPositionAnimator(); |
| 728 | setThumbPosition(checked ? 1 : 0); |
| 729 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | @Override |
| 733 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 734 | super.onLayout(changed, left, top, right, bottom); |
| 735 | |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 736 | int switchRight; |
| 737 | int switchLeft; |
| 738 | |
| 739 | if (isLayoutRtl()) { |
| 740 | switchLeft = getPaddingLeft(); |
| 741 | switchRight = switchLeft + mSwitchWidth; |
| 742 | } else { |
| 743 | switchRight = getWidth() - getPaddingRight(); |
| 744 | switchLeft = switchRight - mSwitchWidth; |
| 745 | } |
| 746 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 747 | int switchTop = 0; |
| 748 | int switchBottom = 0; |
| 749 | switch (getGravity() & Gravity.VERTICAL_GRAVITY_MASK) { |
| 750 | default: |
| 751 | case Gravity.TOP: |
| 752 | switchTop = getPaddingTop(); |
| 753 | switchBottom = switchTop + mSwitchHeight; |
| 754 | break; |
| 755 | |
| 756 | case Gravity.CENTER_VERTICAL: |
| 757 | switchTop = (getPaddingTop() + getHeight() - getPaddingBottom()) / 2 - |
| 758 | mSwitchHeight / 2; |
| 759 | switchBottom = switchTop + mSwitchHeight; |
| 760 | break; |
| 761 | |
| 762 | case Gravity.BOTTOM: |
| 763 | switchBottom = getHeight() - getPaddingBottom(); |
| 764 | switchTop = switchBottom - mSwitchHeight; |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | mSwitchLeft = switchLeft; |
| 769 | mSwitchTop = switchTop; |
| 770 | mSwitchBottom = switchBottom; |
| 771 | mSwitchRight = switchRight; |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | protected void onDraw(Canvas canvas) { |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 776 | final Rect tempRect = mTempRect; |
| 777 | final Drawable trackDrawable = mTrackDrawable; |
| 778 | final Drawable thumbDrawable = mThumbDrawable; |
| 779 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 780 | // Draw the switch |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 781 | final int switchLeft = mSwitchLeft; |
| 782 | final int switchTop = mSwitchTop; |
| 783 | final int switchRight = mSwitchRight; |
| 784 | final int switchBottom = mSwitchBottom; |
| 785 | trackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom); |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 786 | trackDrawable.getPadding(tempRect); |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame^] | 787 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 788 | final int switchInnerLeft = switchLeft + tempRect.left; |
| 789 | final int switchInnerTop = switchTop + tempRect.top; |
| 790 | final int switchInnerRight = switchRight - tempRect.right; |
| 791 | final int switchInnerBottom = switchBottom - tempRect.bottom; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 792 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 793 | // Relies on mTempRect, MUST be called first! |
| 794 | final int thumbPos = getThumbOffset(); |
| 795 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 796 | thumbDrawable.getPadding(tempRect); |
| 797 | int thumbLeft = switchInnerLeft - tempRect.left + thumbPos; |
| 798 | int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + tempRect.right; |
| 799 | thumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom); |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame^] | 800 | |
| 801 | final Drawable background = getBackground(); |
| 802 | if (background.supportsHotspots()) { |
| 803 | background.setHotspotBounds(thumbLeft, switchTop, thumbRight, switchBottom); |
| 804 | } |
| 805 | |
| 806 | super.onDraw(canvas); |
| 807 | |
| 808 | trackDrawable.draw(canvas); |
| 809 | |
| 810 | final int saveCount = canvas.save(); |
| 811 | canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom); |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 812 | thumbDrawable.draw(canvas); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 813 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 814 | final int drawableState[] = getDrawableState(); |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 815 | if (mTextColors != null) { |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 816 | mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0)); |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 817 | } |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 818 | mTextPaint.drawableState = drawableState; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 819 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 820 | final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout; |
Fabrice Di Meglio | be06e32 | 2012-09-11 17:42:45 -0700 | [diff] [blame] | 821 | if (switchText != null) { |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 822 | final int left = (thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2; |
| 823 | final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2; |
| 824 | canvas.translate(left, top); |
Fabrice Di Meglio | be06e32 | 2012-09-11 17:42:45 -0700 | [diff] [blame] | 825 | switchText.draw(canvas); |
| 826 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 827 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 828 | canvas.restoreToCount(saveCount); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | @Override |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 832 | public int getCompoundPaddingLeft() { |
| 833 | if (!isLayoutRtl()) { |
| 834 | return super.getCompoundPaddingLeft(); |
| 835 | } |
| 836 | int padding = super.getCompoundPaddingLeft() + mSwitchWidth; |
| 837 | if (!TextUtils.isEmpty(getText())) { |
| 838 | padding += mSwitchPadding; |
| 839 | } |
| 840 | return padding; |
| 841 | } |
| 842 | |
| 843 | @Override |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 844 | public int getCompoundPaddingRight() { |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 845 | if (isLayoutRtl()) { |
| 846 | return super.getCompoundPaddingRight(); |
| 847 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 848 | int padding = super.getCompoundPaddingRight() + mSwitchWidth; |
| 849 | if (!TextUtils.isEmpty(getText())) { |
| 850 | padding += mSwitchPadding; |
| 851 | } |
| 852 | return padding; |
| 853 | } |
| 854 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 855 | /** |
| 856 | * Translates thumb position to offset according to current RTL setting and |
| 857 | * thumb scroll range. |
| 858 | * |
| 859 | * @return thumb offset |
| 860 | */ |
| 861 | private int getThumbOffset() { |
| 862 | final float thumbPosition; |
| 863 | if (isLayoutRtl()) { |
| 864 | thumbPosition = 1 - mThumbPosition; |
| 865 | } else { |
| 866 | thumbPosition = mThumbPosition; |
| 867 | } |
| 868 | return (int) (thumbPosition * getThumbScrollRange() + 0.5f); |
| 869 | } |
| 870 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 871 | private int getThumbScrollRange() { |
| 872 | if (mTrackDrawable == null) { |
| 873 | return 0; |
| 874 | } |
| 875 | mTrackDrawable.getPadding(mTempRect); |
| 876 | return mSwitchWidth - mThumbWidth - mTempRect.left - mTempRect.right; |
| 877 | } |
| 878 | |
| 879 | @Override |
| 880 | protected int[] onCreateDrawableState(int extraSpace) { |
| 881 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 882 | if (isChecked()) { |
| 883 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 884 | } |
| 885 | return drawableState; |
| 886 | } |
| 887 | |
| 888 | @Override |
| 889 | protected void drawableStateChanged() { |
| 890 | super.drawableStateChanged(); |
| 891 | |
| 892 | int[] myDrawableState = getDrawableState(); |
| 893 | |
| 894 | // Set the state of the Drawable |
Gilles Debunne | e724ee4 | 2011-08-31 11:20:27 -0700 | [diff] [blame] | 895 | // Drawable may be null when checked state is set from XML, from super constructor |
| 896 | if (mThumbDrawable != null) mThumbDrawable.setState(myDrawableState); |
| 897 | if (mTrackDrawable != null) mTrackDrawable.setState(myDrawableState); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 898 | |
| 899 | invalidate(); |
| 900 | } |
| 901 | |
| 902 | @Override |
| 903 | protected boolean verifyDrawable(Drawable who) { |
| 904 | return super.verifyDrawable(who) || who == mThumbDrawable || who == mTrackDrawable; |
| 905 | } |
| 906 | |
| 907 | @Override |
| 908 | public void jumpDrawablesToCurrentState() { |
| 909 | super.jumpDrawablesToCurrentState(); |
| 910 | mThumbDrawable.jumpToCurrentState(); |
| 911 | mTrackDrawable.jumpToCurrentState(); |
| 912 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 913 | |
| 914 | @Override |
| 915 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 916 | super.onInitializeAccessibilityEvent(event); |
| 917 | event.setClassName(Switch.class.getName()); |
| 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 922 | super.onInitializeAccessibilityNodeInfo(info); |
| 923 | info.setClassName(Switch.class.getName()); |
Svetoslav Ganov | 78bcc15 | 2012-04-12 17:17:19 -0700 | [diff] [blame] | 924 | CharSequence switchText = isChecked() ? mTextOn : mTextOff; |
| 925 | if (!TextUtils.isEmpty(switchText)) { |
| 926 | CharSequence oldText = info.getText(); |
| 927 | if (TextUtils.isEmpty(oldText)) { |
| 928 | info.setText(switchText); |
| 929 | } else { |
| 930 | StringBuilder newText = new StringBuilder(); |
| 931 | newText.append(oldText).append(' ').append(switchText); |
| 932 | info.setText(newText); |
| 933 | } |
| 934 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 935 | } |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 936 | |
| 937 | private static final FloatProperty<Switch> THUMB_POS = new FloatProperty<Switch>("thumbPos") { |
| 938 | @Override |
| 939 | public Float get(Switch object) { |
| 940 | return object.mThumbPosition; |
| 941 | } |
| 942 | |
| 943 | @Override |
| 944 | public void setValue(Switch object, float value) { |
| 945 | object.setThumbPosition(value); |
| 946 | } |
| 947 | }; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 948 | } |