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; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.DrawableRes; |
Alan Viverette | f6d87ec | 2016-03-11 10:09:14 -0500 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 23 | import android.annotation.StyleRes; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 24 | import android.compat.annotation.UnsupportedAppUsage; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.res.ColorStateList; |
| 27 | import android.content.res.Resources; |
| 28 | import android.content.res.TypedArray; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 29 | import android.graphics.BlendMode; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 30 | import android.graphics.Canvas; |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 31 | import android.graphics.Insets; |
Chris Craik | 6a49dde | 2015-05-12 10:28:14 -0700 | [diff] [blame] | 32 | import android.graphics.Paint; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 33 | import android.graphics.PorterDuff; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 34 | import android.graphics.Rect; |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 35 | import android.graphics.Region.Op; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 36 | import android.graphics.Typeface; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 37 | import android.graphics.drawable.Drawable; |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 38 | import android.os.Build.VERSION_CODES; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 39 | import android.text.Layout; |
| 40 | import android.text.StaticLayout; |
| 41 | import android.text.TextPaint; |
| 42 | import android.text.TextUtils; |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 43 | import android.text.method.AllCapsTransformationMethod; |
| 44 | import android.text.method.TransformationMethod2; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 45 | import android.util.AttributeSet; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 46 | import android.util.FloatProperty; |
| 47 | import android.util.MathUtils; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 48 | import android.view.Gravity; |
| 49 | import android.view.MotionEvent; |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 50 | import android.view.SoundEffectConstants; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 51 | import android.view.VelocityTracker; |
| 52 | import android.view.ViewConfiguration; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 53 | import android.view.ViewStructure; |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 54 | import android.view.accessibility.AccessibilityEvent; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 55 | import android.view.inspector.InspectableProperty; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 56 | |
Adam Powell | be0a453 | 2010-11-29 17:47:48 -0800 | [diff] [blame] | 57 | import com.android.internal.R; |
| 58 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 59 | /** |
| 60 | * A Switch is a two-state toggle switch widget that can select between two |
| 61 | * 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] | 62 | * or simply tap to toggle as if it were a checkbox. The {@link #setText(CharSequence) text} |
| 63 | * property controls the text displayed in the label for the switch, whereas the |
| 64 | * {@link #setTextOff(CharSequence) off} and {@link #setTextOn(CharSequence) on} text |
| 65 | * controls the text on the thumb. Similarly, the |
| 66 | * {@link #setTextAppearance(android.content.Context, int) textAppearance} and the related |
| 67 | * setTypeface() methods control the typeface and style of label text, whereas the |
| 68 | * {@link #setSwitchTextAppearance(android.content.Context, int) switchTextAppearance} and |
Andrew Solovay | 07e7021 | 2015-07-08 12:49:21 -0700 | [diff] [blame] | 69 | * the related setSwitchTypeface() methods control that of the thumb. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 70 | * |
Mark Lu | 3404032 | 2016-08-30 18:12:30 -0700 | [diff] [blame] | 71 | * <p>{@link android.support.v7.widget.SwitchCompat} is a version of |
| 72 | * the Switch widget which runs on devices back to API 7.</p> |
| 73 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 74 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a> |
| 75 | * guide.</p> |
| 76 | * |
| 77 | * @attr ref android.R.styleable#Switch_textOn |
| 78 | * @attr ref android.R.styleable#Switch_textOff |
| 79 | * @attr ref android.R.styleable#Switch_switchMinWidth |
| 80 | * @attr ref android.R.styleable#Switch_switchPadding |
| 81 | * @attr ref android.R.styleable#Switch_switchTextAppearance |
| 82 | * @attr ref android.R.styleable#Switch_thumb |
| 83 | * @attr ref android.R.styleable#Switch_thumbTextPadding |
| 84 | * @attr ref android.R.styleable#Switch_track |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 85 | */ |
| 86 | public class Switch extends CompoundButton { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 87 | private static final int THUMB_ANIMATION_DURATION = 250; |
| 88 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 89 | private static final int TOUCH_MODE_IDLE = 0; |
| 90 | private static final int TOUCH_MODE_DOWN = 1; |
| 91 | private static final int TOUCH_MODE_DRAGGING = 2; |
| 92 | |
| 93 | // Enum for the "typeface" XML parameter. |
| 94 | private static final int SANS = 1; |
| 95 | private static final int SERIF = 2; |
| 96 | private static final int MONOSPACE = 3; |
| 97 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 98 | @UnsupportedAppUsage |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 99 | private Drawable mThumbDrawable; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 100 | private ColorStateList mThumbTintList = null; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 101 | private BlendMode mThumbBlendMode = null; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 102 | private boolean mHasThumbTint = false; |
| 103 | private boolean mHasThumbTintMode = false; |
| 104 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 105 | @UnsupportedAppUsage |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 106 | private Drawable mTrackDrawable; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 107 | private ColorStateList mTrackTintList = null; |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 108 | private BlendMode mTrackBlendMode = null; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 109 | private boolean mHasTrackTint = false; |
| 110 | private boolean mHasTrackTintMode = false; |
| 111 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 112 | private int mThumbTextPadding; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 113 | @UnsupportedAppUsage |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 114 | private int mSwitchMinWidth; |
| 115 | private int mSwitchPadding; |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 116 | private boolean mSplitTrack; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 117 | private CharSequence mTextOn; |
| 118 | private CharSequence mTextOff; |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 119 | private boolean mShowText; |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 120 | private boolean mUseFallbackLineSpacing; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 121 | |
| 122 | private int mTouchMode; |
| 123 | private int mTouchSlop; |
| 124 | private float mTouchX; |
| 125 | private float mTouchY; |
| 126 | private VelocityTracker mVelocityTracker = VelocityTracker.obtain(); |
| 127 | private int mMinFlingVelocity; |
| 128 | |
| 129 | private float mThumbPosition; |
Alan Viverette | 8bb3990 | 2014-07-29 17:22:30 -0700 | [diff] [blame] | 130 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Width required to draw the switch track and thumb. Includes padding and |
| 133 | * optical bounds for both the track and thumb. |
| 134 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 135 | @UnsupportedAppUsage |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 136 | private int mSwitchWidth; |
| 137 | |
| 138 | /** |
| 139 | * Height required to draw the switch track and thumb. Includes padding and |
| 140 | * optical bounds for both the track and thumb. |
| 141 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 142 | @UnsupportedAppUsage |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 143 | private int mSwitchHeight; |
| 144 | |
| 145 | /** |
| 146 | * Width of the thumb's content region. Does not include padding or |
| 147 | * optical bounds. |
| 148 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 149 | @UnsupportedAppUsage |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 150 | private int mThumbWidth; |
| 151 | |
| 152 | /** Left bound for drawing the switch track and thumb. */ |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 153 | private int mSwitchLeft; |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 154 | |
| 155 | /** Top bound for drawing the switch track and thumb. */ |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 156 | private int mSwitchTop; |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 157 | |
| 158 | /** Right bound for drawing the switch track and thumb. */ |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 159 | private int mSwitchRight; |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 160 | |
| 161 | /** Bottom bound for drawing the switch track and thumb. */ |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 162 | private int mSwitchBottom; |
| 163 | |
| 164 | private TextPaint mTextPaint; |
| 165 | private ColorStateList mTextColors; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 166 | @UnsupportedAppUsage |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 167 | private Layout mOnLayout; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 168 | @UnsupportedAppUsage |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 169 | private Layout mOffLayout; |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 170 | private TransformationMethod2 mSwitchTransformationMethod; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 171 | private ObjectAnimator mPositionAnimator; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 172 | |
Adam Powell | be0a453 | 2010-11-29 17:47:48 -0800 | [diff] [blame] | 173 | @SuppressWarnings("hiding") |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 174 | private final Rect mTempRect = new Rect(); |
| 175 | |
| 176 | private static final int[] CHECKED_STATE_SET = { |
| 177 | R.attr.state_checked |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * Construct a new Switch with default styling. |
| 182 | * |
| 183 | * @param context The Context that will determine this widget's theming. |
| 184 | */ |
| 185 | public Switch(Context context) { |
| 186 | this(context, null); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Construct a new Switch with default styling, overriding specific style |
| 191 | * attributes as requested. |
| 192 | * |
| 193 | * @param context The Context that will determine this widget's theming. |
| 194 | * @param attrs Specification of attributes that should deviate from default styling. |
| 195 | */ |
| 196 | public Switch(Context context, AttributeSet attrs) { |
| 197 | this(context, attrs, com.android.internal.R.attr.switchStyle); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Construct a new Switch with a default style determined by the given theme attribute, |
| 202 | * overriding specific style attributes as requested. |
| 203 | * |
| 204 | * @param context The Context that will determine this widget's theming. |
| 205 | * @param attrs Specification of attributes that should deviate from the default styling. |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 206 | * @param defStyleAttr An attribute in the current theme that contains a |
| 207 | * reference to a style resource that supplies default values for |
| 208 | * the view. Can be 0 to not look for defaults. |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 209 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 210 | public Switch(Context context, AttributeSet attrs, int defStyleAttr) { |
| 211 | this(context, attrs, defStyleAttr, 0); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * Construct a new Switch with a default style determined by the given theme |
| 217 | * attribute or style resource, overriding specific style attributes as |
| 218 | * requested. |
| 219 | * |
| 220 | * @param context The Context that will determine this widget's theming. |
| 221 | * @param attrs Specification of attributes that should deviate from the |
| 222 | * default styling. |
| 223 | * @param defStyleAttr An attribute in the current theme that contains a |
| 224 | * reference to a style resource that supplies default values for |
| 225 | * the view. Can be 0 to not look for defaults. |
| 226 | * @param defStyleRes A resource identifier of a style resource that |
| 227 | * supplies default values for the view, used only if |
| 228 | * defStyleAttr is 0 or can not be found in the theme. Can be 0 |
| 229 | * to not look for defaults. |
| 230 | */ |
| 231 | public Switch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 232 | super(context, attrs, defStyleAttr, defStyleRes); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 233 | |
Chris Craik | 6a49dde | 2015-05-12 10:28:14 -0700 | [diff] [blame] | 234 | mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 235 | |
| 236 | final Resources res = getResources(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 237 | mTextPaint.density = res.getDisplayMetrics().density; |
| 238 | mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale); |
| 239 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 240 | final TypedArray a = context.obtainStyledAttributes( |
| 241 | attrs, com.android.internal.R.styleable.Switch, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 242 | saveAttributeDataForStyleable(context, com.android.internal.R.styleable.Switch, |
| 243 | attrs, a, defStyleAttr, defStyleRes); |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 244 | mThumbDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_thumb); |
Alan Viverette | b067405 | 2014-09-26 16:12:16 -0700 | [diff] [blame] | 245 | if (mThumbDrawable != null) { |
| 246 | mThumbDrawable.setCallback(this); |
| 247 | } |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 248 | mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_track); |
Alan Viverette | b067405 | 2014-09-26 16:12:16 -0700 | [diff] [blame] | 249 | if (mTrackDrawable != null) { |
| 250 | mTrackDrawable.setCallback(this); |
| 251 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 252 | mTextOn = a.getText(com.android.internal.R.styleable.Switch_textOn); |
| 253 | mTextOff = a.getText(com.android.internal.R.styleable.Switch_textOff); |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 254 | mShowText = a.getBoolean(com.android.internal.R.styleable.Switch_showText, true); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 255 | mThumbTextPadding = a.getDimensionPixelSize( |
| 256 | com.android.internal.R.styleable.Switch_thumbTextPadding, 0); |
| 257 | mSwitchMinWidth = a.getDimensionPixelSize( |
| 258 | com.android.internal.R.styleable.Switch_switchMinWidth, 0); |
| 259 | mSwitchPadding = a.getDimensionPixelSize( |
| 260 | com.android.internal.R.styleable.Switch_switchPadding, 0); |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 261 | mSplitTrack = a.getBoolean(com.android.internal.R.styleable.Switch_splitTrack, false); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 262 | |
Roozbeh Pournader | 60f555c | 2017-09-21 12:54:55 -0700 | [diff] [blame] | 263 | mUseFallbackLineSpacing = context.getApplicationInfo().targetSdkVersion >= VERSION_CODES.P; |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 264 | |
Jun Mukai | 9fb302c | 2015-06-24 18:31:06 -0700 | [diff] [blame] | 265 | ColorStateList thumbTintList = a.getColorStateList( |
| 266 | com.android.internal.R.styleable.Switch_thumbTint); |
| 267 | if (thumbTintList != null) { |
| 268 | mThumbTintList = thumbTintList; |
| 269 | mHasThumbTint = true; |
| 270 | } |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 271 | BlendMode thumbTintMode = Drawable.parseBlendMode( |
| 272 | a.getInt(com.android.internal.R.styleable.Switch_thumbTintMode, -1), |
| 273 | null); |
| 274 | if (mThumbBlendMode != thumbTintMode) { |
| 275 | mThumbBlendMode = thumbTintMode; |
Jun Mukai | 9fb302c | 2015-06-24 18:31:06 -0700 | [diff] [blame] | 276 | mHasThumbTintMode = true; |
| 277 | } |
| 278 | if (mHasThumbTint || mHasThumbTintMode) { |
| 279 | applyThumbTint(); |
| 280 | } |
| 281 | |
| 282 | ColorStateList trackTintList = a.getColorStateList( |
| 283 | com.android.internal.R.styleable.Switch_trackTint); |
| 284 | if (trackTintList != null) { |
| 285 | mTrackTintList = trackTintList; |
| 286 | mHasTrackTint = true; |
| 287 | } |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 288 | BlendMode trackTintMode = Drawable.parseBlendMode( |
| 289 | a.getInt(com.android.internal.R.styleable.Switch_trackTintMode, -1), |
| 290 | null); |
| 291 | if (mTrackBlendMode != trackTintMode) { |
| 292 | mTrackBlendMode = trackTintMode; |
Jun Mukai | 9fb302c | 2015-06-24 18:31:06 -0700 | [diff] [blame] | 293 | mHasTrackTintMode = true; |
| 294 | } |
| 295 | if (mHasTrackTint || mHasTrackTintMode) { |
| 296 | applyTrackTint(); |
| 297 | } |
| 298 | |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 299 | final int appearance = a.getResourceId( |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 300 | com.android.internal.R.styleable.Switch_switchTextAppearance, 0); |
| 301 | if (appearance != 0) { |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 302 | setSwitchTextAppearance(context, appearance); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 303 | } |
| 304 | a.recycle(); |
| 305 | |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 306 | final ViewConfiguration config = ViewConfiguration.get(context); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 307 | mTouchSlop = config.getScaledTouchSlop(); |
| 308 | mMinFlingVelocity = config.getScaledMinimumFlingVelocity(); |
| 309 | |
| 310 | // Refresh display with current params |
Gilles Debunne | e724ee4 | 2011-08-31 11:20:27 -0700 | [diff] [blame] | 311 | refreshDrawableState(); |
yingleiw | 6a55e06 | 2019-12-16 15:11:26 -0800 | [diff] [blame] | 312 | // Default state is derived from on/off-text, so state has to be updated when on/off-text |
| 313 | // are updated. |
| 314 | setDefaultStateDescritption(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 315 | setChecked(isChecked()); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Sets the switch text color, size, style, hint color, and highlight color |
| 320 | * from the specified TextAppearance resource. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 321 | * |
| 322 | * @attr ref android.R.styleable#Switch_switchTextAppearance |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 323 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 324 | public void setSwitchTextAppearance(Context context, @StyleRes int resid) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 325 | TypedArray appearance = |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 326 | context.obtainStyledAttributes(resid, |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 327 | com.android.internal.R.styleable.TextAppearance); |
| 328 | |
| 329 | ColorStateList colors; |
| 330 | int ts; |
| 331 | |
| 332 | colors = appearance.getColorStateList(com.android.internal.R.styleable. |
| 333 | TextAppearance_textColor); |
| 334 | if (colors != null) { |
| 335 | mTextColors = colors; |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 336 | } else { |
| 337 | // If no color set in TextAppearance, default to the view's textColor |
| 338 | mTextColors = getTextColors(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable. |
| 342 | TextAppearance_textSize, 0); |
| 343 | if (ts != 0) { |
| 344 | if (ts != mTextPaint.getTextSize()) { |
| 345 | mTextPaint.setTextSize(ts); |
| 346 | requestLayout(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | int typefaceIndex, styleIndex; |
| 351 | |
| 352 | typefaceIndex = appearance.getInt(com.android.internal.R.styleable. |
| 353 | TextAppearance_typeface, -1); |
| 354 | styleIndex = appearance.getInt(com.android.internal.R.styleable. |
| 355 | TextAppearance_textStyle, -1); |
| 356 | |
| 357 | setSwitchTypefaceByIndex(typefaceIndex, styleIndex); |
| 358 | |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 359 | boolean allCaps = appearance.getBoolean(com.android.internal.R.styleable. |
| 360 | TextAppearance_textAllCaps, false); |
| 361 | if (allCaps) { |
| 362 | mSwitchTransformationMethod = new AllCapsTransformationMethod(getContext()); |
| 363 | mSwitchTransformationMethod.setLengthChangesAllowed(true); |
| 364 | } else { |
| 365 | mSwitchTransformationMethod = null; |
| 366 | } |
| 367 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 368 | appearance.recycle(); |
| 369 | } |
| 370 | |
| 371 | private void setSwitchTypefaceByIndex(int typefaceIndex, int styleIndex) { |
| 372 | Typeface tf = null; |
| 373 | switch (typefaceIndex) { |
| 374 | case SANS: |
| 375 | tf = Typeface.SANS_SERIF; |
| 376 | break; |
| 377 | |
| 378 | case SERIF: |
| 379 | tf = Typeface.SERIF; |
| 380 | break; |
| 381 | |
| 382 | case MONOSPACE: |
| 383 | tf = Typeface.MONOSPACE; |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | setSwitchTypeface(tf, styleIndex); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Sets the typeface and style in which the text should be displayed on the |
| 392 | * switch, and turns on the fake bold and italic bits in the Paint if the |
| 393 | * Typeface that you provided does not have all the bits in the |
| 394 | * style that you specified. |
| 395 | */ |
| 396 | public void setSwitchTypeface(Typeface tf, int style) { |
| 397 | if (style > 0) { |
| 398 | if (tf == null) { |
| 399 | tf = Typeface.defaultFromStyle(style); |
| 400 | } else { |
| 401 | tf = Typeface.create(tf, style); |
| 402 | } |
| 403 | |
| 404 | setSwitchTypeface(tf); |
| 405 | // now compute what (if any) algorithmic styling is needed |
| 406 | int typefaceStyle = tf != null ? tf.getStyle() : 0; |
| 407 | int need = style & ~typefaceStyle; |
| 408 | mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0); |
| 409 | mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0); |
| 410 | } else { |
Victoria Lease | aa0980a | 2012-06-11 14:46:04 -0700 | [diff] [blame] | 411 | mTextPaint.setFakeBoldText(false); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 412 | mTextPaint.setTextSkewX(0); |
| 413 | setSwitchTypeface(tf); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 418 | * 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] | 419 | * Note that not all Typeface families actually have bold and italic |
| 420 | * variants, so you may need to use |
| 421 | * {@link #setSwitchTypeface(Typeface, int)} to get the appearance |
| 422 | * that you actually want. |
| 423 | * |
| 424 | * @attr ref android.R.styleable#TextView_typeface |
| 425 | * @attr ref android.R.styleable#TextView_textStyle |
| 426 | */ |
| 427 | public void setSwitchTypeface(Typeface tf) { |
| 428 | if (mTextPaint.getTypeface() != tf) { |
| 429 | mTextPaint.setTypeface(tf); |
| 430 | |
| 431 | requestLayout(); |
| 432 | invalidate(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 437 | * Set the amount of horizontal padding between the switch and the associated text. |
| 438 | * |
| 439 | * @param pixels Amount of padding in pixels |
| 440 | * |
| 441 | * @attr ref android.R.styleable#Switch_switchPadding |
| 442 | */ |
| 443 | public void setSwitchPadding(int pixels) { |
| 444 | mSwitchPadding = pixels; |
| 445 | requestLayout(); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Get the amount of horizontal padding between the switch and the associated text. |
| 450 | * |
| 451 | * @return Amount of padding in pixels |
| 452 | * |
| 453 | * @attr ref android.R.styleable#Switch_switchPadding |
| 454 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 455 | @InspectableProperty |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 456 | public int getSwitchPadding() { |
| 457 | return mSwitchPadding; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Set the minimum width of the switch in pixels. The switch's width will be the maximum |
| 462 | * of this value and its measured width as determined by the switch drawables and text used. |
| 463 | * |
| 464 | * @param pixels Minimum width of the switch in pixels |
| 465 | * |
| 466 | * @attr ref android.R.styleable#Switch_switchMinWidth |
| 467 | */ |
| 468 | public void setSwitchMinWidth(int pixels) { |
| 469 | mSwitchMinWidth = pixels; |
| 470 | requestLayout(); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Get the minimum width of the switch in pixels. The switch's width will be the maximum |
| 475 | * of this value and its measured width as determined by the switch drawables and text used. |
| 476 | * |
| 477 | * @return Minimum width of the switch in pixels |
| 478 | * |
| 479 | * @attr ref android.R.styleable#Switch_switchMinWidth |
| 480 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 481 | @InspectableProperty |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 482 | public int getSwitchMinWidth() { |
| 483 | return mSwitchMinWidth; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Set the horizontal padding around the text drawn on the switch itself. |
| 488 | * |
| 489 | * @param pixels Horizontal padding for switch thumb text in pixels |
| 490 | * |
| 491 | * @attr ref android.R.styleable#Switch_thumbTextPadding |
| 492 | */ |
| 493 | public void setThumbTextPadding(int pixels) { |
| 494 | mThumbTextPadding = pixels; |
| 495 | requestLayout(); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Get the horizontal padding around the text drawn on the switch itself. |
| 500 | * |
| 501 | * @return Horizontal padding for switch thumb text in pixels |
| 502 | * |
| 503 | * @attr ref android.R.styleable#Switch_thumbTextPadding |
| 504 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 505 | @InspectableProperty |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 506 | public int getThumbTextPadding() { |
| 507 | return mThumbTextPadding; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Set the drawable used for the track that the switch slides within. |
| 512 | * |
| 513 | * @param track Track drawable |
| 514 | * |
| 515 | * @attr ref android.R.styleable#Switch_track |
| 516 | */ |
| 517 | public void setTrackDrawable(Drawable track) { |
Alan Viverette | b067405 | 2014-09-26 16:12:16 -0700 | [diff] [blame] | 518 | if (mTrackDrawable != null) { |
| 519 | mTrackDrawable.setCallback(null); |
| 520 | } |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 521 | mTrackDrawable = track; |
Alan Viverette | b067405 | 2014-09-26 16:12:16 -0700 | [diff] [blame] | 522 | if (track != null) { |
| 523 | track.setCallback(this); |
| 524 | } |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 525 | requestLayout(); |
| 526 | } |
| 527 | |
| 528 | /** |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 529 | * Set the drawable used for the track that the switch slides within. |
| 530 | * |
Adam Powell | dca510e | 2012-03-08 20:06:39 -0800 | [diff] [blame] | 531 | * @param resId Resource ID of a track drawable |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 532 | * |
| 533 | * @attr ref android.R.styleable#Switch_track |
| 534 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 535 | public void setTrackResource(@DrawableRes int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 536 | setTrackDrawable(getContext().getDrawable(resId)); |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 540 | * Get the drawable used for the track that the switch slides within. |
| 541 | * |
| 542 | * @return Track drawable |
| 543 | * |
| 544 | * @attr ref android.R.styleable#Switch_track |
| 545 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 546 | @InspectableProperty(name = "track") |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 547 | public Drawable getTrackDrawable() { |
| 548 | return mTrackDrawable; |
| 549 | } |
| 550 | |
| 551 | /** |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 552 | * Applies a tint to the track drawable. Does not modify the current |
| 553 | * tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
| 554 | * <p> |
| 555 | * Subsequent calls to {@link #setTrackDrawable(Drawable)} will |
| 556 | * automatically mutate the drawable and apply the specified tint and tint |
| 557 | * mode using {@link Drawable#setTintList(ColorStateList)}. |
| 558 | * |
| 559 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 560 | * |
| 561 | * @attr ref android.R.styleable#Switch_trackTint |
| 562 | * @see #getTrackTintList() |
| 563 | * @see Drawable#setTintList(ColorStateList) |
| 564 | */ |
| 565 | public void setTrackTintList(@Nullable ColorStateList tint) { |
| 566 | mTrackTintList = tint; |
| 567 | mHasTrackTint = true; |
| 568 | |
| 569 | applyTrackTint(); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @return the tint applied to the track drawable |
| 574 | * @attr ref android.R.styleable#Switch_trackTint |
| 575 | * @see #setTrackTintList(ColorStateList) |
| 576 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 577 | @InspectableProperty(name = "trackTint") |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 578 | @Nullable |
| 579 | public ColorStateList getTrackTintList() { |
| 580 | return mTrackTintList; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Specifies the blending mode used to apply the tint specified by |
| 585 | * {@link #setTrackTintList(ColorStateList)}} to the track drawable. |
| 586 | * The default mode is {@link PorterDuff.Mode#SRC_IN}. |
| 587 | * |
| 588 | * @param tintMode the blending mode used to apply the tint, may be |
| 589 | * {@code null} to clear tint |
| 590 | * @attr ref android.R.styleable#Switch_trackTintMode |
| 591 | * @see #getTrackTintMode() |
| 592 | * @see Drawable#setTintMode(PorterDuff.Mode) |
| 593 | */ |
| 594 | public void setTrackTintMode(@Nullable PorterDuff.Mode tintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 595 | setTrackTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Specifies the blending mode used to apply the tint specified by |
| 600 | * {@link #setTrackTintList(ColorStateList)}} to the track drawable. |
| 601 | * The default mode is {@link BlendMode#SRC_IN}. |
| 602 | * |
| 603 | * @param blendMode the blending mode used to apply the tint, may be |
| 604 | * {@code null} to clear tint |
| 605 | * @attr ref android.R.styleable#Switch_trackTintMode |
| 606 | * @see #getTrackTintMode() |
| 607 | * @see Drawable#setTintBlendMode(BlendMode) |
| 608 | */ |
| 609 | public void setTrackTintBlendMode(@Nullable BlendMode blendMode) { |
| 610 | mTrackBlendMode = blendMode; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 611 | mHasTrackTintMode = true; |
| 612 | |
| 613 | applyTrackTint(); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * @return the blending mode used to apply the tint to the track |
| 618 | * drawable |
| 619 | * @attr ref android.R.styleable#Switch_trackTintMode |
| 620 | * @see #setTrackTintMode(PorterDuff.Mode) |
| 621 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 622 | @InspectableProperty |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 623 | @Nullable |
| 624 | public PorterDuff.Mode getTrackTintMode() { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 625 | BlendMode mode = getTrackTintBlendMode(); |
| 626 | return mode != null ? BlendMode.blendModeToPorterDuffMode(mode) : null; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * @return the blending mode used to apply the tint to the track |
| 631 | * drawable |
| 632 | * @attr ref android.R.styleable#Switch_trackTintMode |
| 633 | * @see #setTrackTintBlendMode(BlendMode) |
| 634 | */ |
| 635 | @InspectableProperty(attributeId = com.android.internal.R.styleable.Switch_trackTintMode) |
| 636 | @Nullable |
| 637 | public BlendMode getTrackTintBlendMode() { |
| 638 | return mTrackBlendMode; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | private void applyTrackTint() { |
| 642 | if (mTrackDrawable != null && (mHasTrackTint || mHasTrackTintMode)) { |
| 643 | mTrackDrawable = mTrackDrawable.mutate(); |
| 644 | |
| 645 | if (mHasTrackTint) { |
| 646 | mTrackDrawable.setTintList(mTrackTintList); |
| 647 | } |
| 648 | |
| 649 | if (mHasTrackTintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 650 | mTrackDrawable.setTintBlendMode(mTrackBlendMode); |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | // The drawable (or one of its children) may not have been |
| 654 | // stateful before applying the tint, so let's try again. |
| 655 | if (mTrackDrawable.isStateful()) { |
| 656 | mTrackDrawable.setState(getDrawableState()); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 662 | * Set the drawable used for the switch "thumb" - the piece that the user |
| 663 | * can physically touch and drag along the track. |
| 664 | * |
| 665 | * @param thumb Thumb drawable |
| 666 | * |
| 667 | * @attr ref android.R.styleable#Switch_thumb |
| 668 | */ |
| 669 | public void setThumbDrawable(Drawable thumb) { |
Alan Viverette | b067405 | 2014-09-26 16:12:16 -0700 | [diff] [blame] | 670 | if (mThumbDrawable != null) { |
| 671 | mThumbDrawable.setCallback(null); |
| 672 | } |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 673 | mThumbDrawable = thumb; |
Alan Viverette | b067405 | 2014-09-26 16:12:16 -0700 | [diff] [blame] | 674 | if (thumb != null) { |
| 675 | thumb.setCallback(this); |
| 676 | } |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 677 | requestLayout(); |
| 678 | } |
| 679 | |
| 680 | /** |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 681 | * Set the drawable used for the switch "thumb" - the piece that the user |
| 682 | * can physically touch and drag along the track. |
| 683 | * |
Adam Powell | dca510e | 2012-03-08 20:06:39 -0800 | [diff] [blame] | 684 | * @param resId Resource ID of a thumb drawable |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 685 | * |
| 686 | * @attr ref android.R.styleable#Switch_thumb |
| 687 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 688 | public void setThumbResource(@DrawableRes int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 689 | setThumbDrawable(getContext().getDrawable(resId)); |
Adam Powell | d9c7be6 | 2012-03-08 19:43:43 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /** |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 693 | * Get the drawable used for the switch "thumb" - the piece that the user |
| 694 | * can physically touch and drag along the track. |
| 695 | * |
| 696 | * @return Thumb drawable |
| 697 | * |
| 698 | * @attr ref android.R.styleable#Switch_thumb |
| 699 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 700 | @InspectableProperty(name = "thumb") |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 701 | public Drawable getThumbDrawable() { |
| 702 | return mThumbDrawable; |
| 703 | } |
| 704 | |
| 705 | /** |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 706 | * Applies a tint to the thumb drawable. Does not modify the current |
| 707 | * tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
| 708 | * <p> |
| 709 | * Subsequent calls to {@link #setThumbDrawable(Drawable)} will |
| 710 | * automatically mutate the drawable and apply the specified tint and tint |
| 711 | * mode using {@link Drawable#setTintList(ColorStateList)}. |
| 712 | * |
| 713 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 714 | * |
| 715 | * @attr ref android.R.styleable#Switch_thumbTint |
| 716 | * @see #getThumbTintList() |
| 717 | * @see Drawable#setTintList(ColorStateList) |
| 718 | */ |
| 719 | public void setThumbTintList(@Nullable ColorStateList tint) { |
| 720 | mThumbTintList = tint; |
| 721 | mHasThumbTint = true; |
| 722 | |
| 723 | applyThumbTint(); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * @return the tint applied to the thumb drawable |
| 728 | * @attr ref android.R.styleable#Switch_thumbTint |
| 729 | * @see #setThumbTintList(ColorStateList) |
| 730 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 731 | @InspectableProperty(name = "thumbTint") |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 732 | @Nullable |
| 733 | public ColorStateList getThumbTintList() { |
| 734 | return mThumbTintList; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Specifies the blending mode used to apply the tint specified by |
| 739 | * {@link #setThumbTintList(ColorStateList)}} to the thumb drawable. |
| 740 | * The default mode is {@link PorterDuff.Mode#SRC_IN}. |
| 741 | * |
| 742 | * @param tintMode the blending mode used to apply the tint, may be |
| 743 | * {@code null} to clear tint |
| 744 | * @attr ref android.R.styleable#Switch_thumbTintMode |
| 745 | * @see #getThumbTintMode() |
| 746 | * @see Drawable#setTintMode(PorterDuff.Mode) |
| 747 | */ |
| 748 | public void setThumbTintMode(@Nullable PorterDuff.Mode tintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 749 | setThumbTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Specifies the blending mode used to apply the tint specified by |
| 754 | * {@link #setThumbTintList(ColorStateList)}} to the thumb drawable. |
| 755 | * The default mode is {@link PorterDuff.Mode#SRC_IN}. |
| 756 | * |
| 757 | * @param blendMode the blending mode used to apply the tint, may be |
| 758 | * {@code null} to clear tint |
| 759 | * @attr ref android.R.styleable#Switch_thumbTintMode |
| 760 | * @see #getThumbTintMode() |
| 761 | * @see Drawable#setTintBlendMode(BlendMode) |
| 762 | */ |
| 763 | public void setThumbTintBlendMode(@Nullable BlendMode blendMode) { |
| 764 | mThumbBlendMode = blendMode; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 765 | mHasThumbTintMode = true; |
| 766 | |
| 767 | applyThumbTint(); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * @return the blending mode used to apply the tint to the thumb |
| 772 | * drawable |
| 773 | * @attr ref android.R.styleable#Switch_thumbTintMode |
| 774 | * @see #setThumbTintMode(PorterDuff.Mode) |
| 775 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 776 | @InspectableProperty |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 777 | @Nullable |
| 778 | public PorterDuff.Mode getThumbTintMode() { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 779 | BlendMode mode = getThumbTintBlendMode(); |
| 780 | return mode != null ? BlendMode.blendModeToPorterDuffMode(mode) : null; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * @return the blending mode used to apply the tint to the thumb |
| 785 | * drawable |
| 786 | * @attr ref android.R.styleable#Switch_thumbTintMode |
| 787 | * @see #setThumbTintBlendMode(BlendMode) |
| 788 | */ |
| 789 | @InspectableProperty(attributeId = com.android.internal.R.styleable.Switch_thumbTintMode) |
| 790 | @Nullable |
| 791 | public BlendMode getThumbTintBlendMode() { |
| 792 | return mThumbBlendMode; |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | private void applyThumbTint() { |
| 796 | if (mThumbDrawable != null && (mHasThumbTint || mHasThumbTintMode)) { |
| 797 | mThumbDrawable = mThumbDrawable.mutate(); |
| 798 | |
| 799 | if (mHasThumbTint) { |
| 800 | mThumbDrawable.setTintList(mThumbTintList); |
| 801 | } |
| 802 | |
| 803 | if (mHasThumbTintMode) { |
Nader Jawad | 8e31c3e | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 804 | mThumbDrawable.setTintBlendMode(mThumbBlendMode); |
Alan Viverette | e7eee64 | 2015-01-29 14:27:49 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // The drawable (or one of its children) may not have been |
| 808 | // stateful before applying the tint, so let's try again. |
| 809 | if (mThumbDrawable.isStateful()) { |
| 810 | mThumbDrawable.setState(getDrawableState()); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /** |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 816 | * Specifies whether the track should be split by the thumb. When true, |
| 817 | * the thumb's optical bounds will be clipped out of the track drawable, |
| 818 | * then the thumb will be drawn into the resulting gap. |
| 819 | * |
| 820 | * @param splitTrack Whether the track should be split by the thumb |
| 821 | * |
| 822 | * @attr ref android.R.styleable#Switch_splitTrack |
| 823 | */ |
| 824 | public void setSplitTrack(boolean splitTrack) { |
| 825 | mSplitTrack = splitTrack; |
| 826 | invalidate(); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Returns whether the track should be split by the thumb. |
| 831 | * |
| 832 | * @attr ref android.R.styleable#Switch_splitTrack |
| 833 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 834 | @InspectableProperty |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 835 | public boolean getSplitTrack() { |
| 836 | return mSplitTrack; |
| 837 | } |
| 838 | |
| 839 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 840 | * Returns the text displayed when the button is in the checked state. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 841 | * |
| 842 | * @attr ref android.R.styleable#Switch_textOn |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 843 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 844 | @InspectableProperty |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 845 | public CharSequence getTextOn() { |
| 846 | return mTextOn; |
| 847 | } |
| 848 | |
| 849 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 850 | * Sets the text displayed when the button is in the checked state. |
Adam Powell | 6c86e1b | 2012-03-08 15:11:46 -0800 | [diff] [blame] | 851 | * |
| 852 | * @attr ref android.R.styleable#Switch_textOn |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 853 | */ |
| 854 | public void setTextOn(CharSequence textOn) { |
| 855 | mTextOn = textOn; |
| 856 | requestLayout(); |
yingleiw | 473fc12 | 2019-10-11 15:10:55 -0700 | [diff] [blame] | 857 | // Default state is derived from on/off-text, so state has to be updated when on/off-text |
| 858 | // are updated. |
| 859 | setDefaultStateDescritption(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 863 | * 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] | 864 | * |
| 865 | * @attr ref android.R.styleable#Switch_textOff |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 866 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 867 | @InspectableProperty |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 868 | public CharSequence getTextOff() { |
| 869 | return mTextOff; |
| 870 | } |
| 871 | |
| 872 | /** |
Chet Haase | 150176d | 2011-08-26 09:54:06 -0700 | [diff] [blame] | 873 | * 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] | 874 | * |
| 875 | * @attr ref android.R.styleable#Switch_textOff |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 876 | */ |
| 877 | public void setTextOff(CharSequence textOff) { |
| 878 | mTextOff = textOff; |
| 879 | requestLayout(); |
yingleiw | 473fc12 | 2019-10-11 15:10:55 -0700 | [diff] [blame] | 880 | // Default state is derived from on/off-text, so state has to be updated when on/off-text |
| 881 | // are updated. |
| 882 | setDefaultStateDescritption(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 883 | } |
| 884 | |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 885 | /** |
| 886 | * Sets whether the on/off text should be displayed. |
| 887 | * |
| 888 | * @param showText {@code true} to display on/off text |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 889 | * @attr ref android.R.styleable#Switch_showText |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 890 | */ |
| 891 | public void setShowText(boolean showText) { |
| 892 | if (mShowText != showText) { |
| 893 | mShowText = showText; |
| 894 | requestLayout(); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * @return whether the on/off text should be displayed |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 900 | * @attr ref android.R.styleable#Switch_showText |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 901 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 902 | @InspectableProperty |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 903 | public boolean getShowText() { |
| 904 | return mShowText; |
| 905 | } |
| 906 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 907 | @Override |
| 908 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 909 | if (mShowText) { |
| 910 | if (mOnLayout == null) { |
| 911 | mOnLayout = makeLayout(mTextOn); |
| 912 | } |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 913 | |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 914 | if (mOffLayout == null) { |
| 915 | mOffLayout = makeLayout(mTextOff); |
| 916 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Alan Viverette | 9b38f6c | 2014-07-30 02:39:07 +0000 | [diff] [blame] | 919 | final Rect padding = mTempRect; |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 920 | final int thumbWidth; |
| 921 | final int thumbHeight; |
| 922 | if (mThumbDrawable != null) { |
| 923 | // Cached thumb width does not include padding. |
| 924 | mThumbDrawable.getPadding(padding); |
| 925 | thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right; |
| 926 | thumbHeight = mThumbDrawable.getIntrinsicHeight(); |
| 927 | } else { |
| 928 | thumbWidth = 0; |
| 929 | thumbHeight = 0; |
| 930 | } |
| 931 | |
| 932 | final int maxTextWidth; |
| 933 | if (mShowText) { |
| 934 | maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()) |
| 935 | + mThumbTextPadding * 2; |
| 936 | } else { |
| 937 | maxTextWidth = 0; |
| 938 | } |
| 939 | |
| 940 | mThumbWidth = Math.max(maxTextWidth, thumbWidth); |
| 941 | |
| 942 | final int trackHeight; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 943 | if (mTrackDrawable != null) { |
| 944 | mTrackDrawable.getPadding(padding); |
| 945 | trackHeight = mTrackDrawable.getIntrinsicHeight(); |
| 946 | } else { |
| 947 | padding.setEmpty(); |
| 948 | trackHeight = 0; |
| 949 | } |
| 950 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 951 | // Adjust left and right padding to ensure there's enough room for the |
| 952 | // thumb's padding (when present). |
| 953 | int paddingLeft = padding.left; |
| 954 | int paddingRight = padding.right; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 955 | if (mThumbDrawable != null) { |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 956 | final Insets inset = mThumbDrawable.getOpticalInsets(); |
| 957 | paddingLeft = Math.max(paddingLeft, inset.left); |
| 958 | paddingRight = Math.max(paddingRight, inset.right); |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 959 | } |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 960 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 961 | final int switchWidth = Math.max(mSwitchMinWidth, |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 962 | 2 * mThumbWidth + paddingLeft + paddingRight); |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 963 | final int switchHeight = Math.max(trackHeight, thumbHeight); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 964 | mSwitchWidth = switchWidth; |
| 965 | mSwitchHeight = switchHeight; |
| 966 | |
| 967 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 968 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 969 | final int measuredHeight = getMeasuredHeight(); |
| 970 | if (measuredHeight < switchHeight) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 971 | setMeasuredDimension(getMeasuredWidthAndState(), switchHeight); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 975 | /** @hide */ |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 976 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 977 | public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 978 | super.onPopulateAccessibilityEventInternal(event); |
Alan Viverette | 2a37cf8d | 2014-06-19 17:09:10 -0700 | [diff] [blame] | 979 | |
| 980 | final CharSequence text = isChecked() ? mTextOn : mTextOff; |
| 981 | if (text != null) { |
| 982 | event.getText().add(text); |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 983 | } |
Svetoslav Ganov | 63bce03 | 2011-07-23 19:52:17 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 986 | private Layout makeLayout(CharSequence text) { |
Daniel Sandler | 4c3308d | 2012-04-19 11:04:39 -0400 | [diff] [blame] | 987 | final CharSequence transformed = (mSwitchTransformationMethod != null) |
| 988 | ? mSwitchTransformationMethod.getTransformation(text, this) |
| 989 | : text; |
| 990 | |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 991 | int width = (int) Math.ceil(Layout.getDesiredWidth(transformed, 0, |
| 992 | transformed.length(), mTextPaint, getTextDirectionHeuristic())); |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 993 | return StaticLayout.Builder.obtain(transformed, 0, transformed.length(), mTextPaint, width) |
| 994 | .setUseLineSpacingFromFallbacks(mUseFallbackLineSpacing) |
| 995 | .build(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /** |
| 999 | * @return true if (x, y) is within the target area of the switch thumb |
| 1000 | */ |
| 1001 | private boolean hitThumb(float x, float y) { |
Alan Viverette | 01a0963 | 2014-12-08 13:02:06 -0800 | [diff] [blame] | 1002 | if (mThumbDrawable == null) { |
| 1003 | return false; |
| 1004 | } |
| 1005 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1006 | // Relies on mTempRect, MUST be called first! |
| 1007 | final int thumbOffset = getThumbOffset(); |
| 1008 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1009 | mThumbDrawable.getPadding(mTempRect); |
| 1010 | final int thumbTop = mSwitchTop - mTouchSlop; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1011 | final int thumbLeft = mSwitchLeft + thumbOffset - mTouchSlop; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1012 | final int thumbRight = thumbLeft + mThumbWidth + |
| 1013 | mTempRect.left + mTempRect.right + mTouchSlop; |
| 1014 | final int thumbBottom = mSwitchBottom + mTouchSlop; |
| 1015 | return x > thumbLeft && x < thumbRight && y > thumbTop && y < thumbBottom; |
| 1016 | } |
| 1017 | |
| 1018 | @Override |
| 1019 | public boolean onTouchEvent(MotionEvent ev) { |
| 1020 | mVelocityTracker.addMovement(ev); |
| 1021 | final int action = ev.getActionMasked(); |
| 1022 | switch (action) { |
| 1023 | case MotionEvent.ACTION_DOWN: { |
| 1024 | final float x = ev.getX(); |
| 1025 | final float y = ev.getY(); |
Gilles Debunne | c2ab0d6 | 2011-06-13 12:52:48 -0700 | [diff] [blame] | 1026 | if (isEnabled() && hitThumb(x, y)) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1027 | mTouchMode = TOUCH_MODE_DOWN; |
| 1028 | mTouchX = x; |
| 1029 | mTouchY = y; |
| 1030 | } |
| 1031 | break; |
| 1032 | } |
| 1033 | |
| 1034 | case MotionEvent.ACTION_MOVE: { |
| 1035 | switch (mTouchMode) { |
| 1036 | case TOUCH_MODE_IDLE: |
| 1037 | // Didn't target the thumb, treat normally. |
| 1038 | break; |
| 1039 | |
| 1040 | case TOUCH_MODE_DOWN: { |
| 1041 | final float x = ev.getX(); |
| 1042 | final float y = ev.getY(); |
| 1043 | if (Math.abs(x - mTouchX) > mTouchSlop || |
| 1044 | Math.abs(y - mTouchY) > mTouchSlop) { |
| 1045 | mTouchMode = TOUCH_MODE_DRAGGING; |
| 1046 | getParent().requestDisallowInterceptTouchEvent(true); |
| 1047 | mTouchX = x; |
| 1048 | mTouchY = y; |
| 1049 | return true; |
| 1050 | } |
| 1051 | break; |
| 1052 | } |
| 1053 | |
| 1054 | case TOUCH_MODE_DRAGGING: { |
| 1055 | final float x = ev.getX(); |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1056 | final int thumbScrollRange = getThumbScrollRange(); |
| 1057 | final float thumbScrollOffset = x - mTouchX; |
| 1058 | float dPos; |
| 1059 | if (thumbScrollRange != 0) { |
| 1060 | dPos = thumbScrollOffset / thumbScrollRange; |
| 1061 | } else { |
| 1062 | // If the thumb scroll range is empty, just use the |
| 1063 | // movement direction to snap on or off. |
| 1064 | dPos = thumbScrollOffset > 0 ? 1 : -1; |
| 1065 | } |
| 1066 | if (isLayoutRtl()) { |
| 1067 | dPos = -dPos; |
| 1068 | } |
| 1069 | final float newPos = MathUtils.constrain(mThumbPosition + dPos, 0, 1); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1070 | if (newPos != mThumbPosition) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1071 | mTouchX = x; |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1072 | setThumbPosition(newPos); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1073 | } |
| 1074 | return true; |
| 1075 | } |
| 1076 | } |
| 1077 | break; |
| 1078 | } |
| 1079 | |
| 1080 | case MotionEvent.ACTION_UP: |
| 1081 | case MotionEvent.ACTION_CANCEL: { |
| 1082 | if (mTouchMode == TOUCH_MODE_DRAGGING) { |
| 1083 | stopDrag(ev); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 1084 | // Allow super class to handle pressed state, etc. |
| 1085 | super.onTouchEvent(ev); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1086 | return true; |
| 1087 | } |
| 1088 | mTouchMode = TOUCH_MODE_IDLE; |
| 1089 | mVelocityTracker.clear(); |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | return super.onTouchEvent(ev); |
| 1095 | } |
| 1096 | |
| 1097 | private void cancelSuperTouch(MotionEvent ev) { |
| 1098 | MotionEvent cancel = MotionEvent.obtain(ev); |
| 1099 | cancel.setAction(MotionEvent.ACTION_CANCEL); |
| 1100 | super.onTouchEvent(cancel); |
| 1101 | cancel.recycle(); |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * Called from onTouchEvent to end a drag operation. |
| 1106 | * |
| 1107 | * @param ev Event that triggered the end of drag mode - ACTION_UP or ACTION_CANCEL |
| 1108 | */ |
| 1109 | private void stopDrag(MotionEvent ev) { |
| 1110 | mTouchMode = TOUCH_MODE_IDLE; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1111 | |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1112 | // Commit the change if the event is up and not canceled and the switch |
| 1113 | // has not been disabled during the drag. |
| 1114 | final boolean commitChange = ev.getAction() == MotionEvent.ACTION_UP && isEnabled(); |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 1115 | final boolean oldState = isChecked(); |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1116 | final boolean newState; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1117 | if (commitChange) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1118 | mVelocityTracker.computeCurrentVelocity(1000); |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1119 | final float xvel = mVelocityTracker.getXVelocity(); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1120 | if (Math.abs(xvel) > mMinFlingVelocity) { |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 1121 | newState = isLayoutRtl() ? (xvel < 0) : (xvel > 0); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1122 | } else { |
| 1123 | newState = getTargetCheckedState(); |
| 1124 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1125 | } else { |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 1126 | newState = oldState; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1127 | } |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1128 | |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 1129 | if (newState != oldState) { |
| 1130 | playSoundEffect(SoundEffectConstants.CLICK); |
Alan Viverette | d4e7790 | 2014-10-27 17:50:51 -0700 | [diff] [blame] | 1131 | } |
Alan Viverette | 03306a0 | 2015-08-07 11:34:25 -0400 | [diff] [blame] | 1132 | // Always call setChecked so that the thumb is moved back to the correct edge |
| 1133 | setChecked(newState); |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1134 | cancelSuperTouch(ev); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | private void animateThumbToCheckedState(boolean newCheckedState) { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1138 | final float targetPosition = newCheckedState ? 1 : 0; |
| 1139 | mPositionAnimator = ObjectAnimator.ofFloat(this, THUMB_POS, targetPosition); |
| 1140 | mPositionAnimator.setDuration(THUMB_ANIMATION_DURATION); |
| 1141 | mPositionAnimator.setAutoCancel(true); |
| 1142 | mPositionAnimator.start(); |
| 1143 | } |
| 1144 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1145 | @UnsupportedAppUsage |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1146 | private void cancelPositionAnimator() { |
| 1147 | if (mPositionAnimator != null) { |
| 1148 | mPositionAnimator.cancel(); |
| 1149 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | private boolean getTargetCheckedState() { |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1153 | return mThumbPosition > 0.5f; |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1156 | /** |
| 1157 | * Sets the thumb position as a decimal value between 0 (off) and 1 (on). |
| 1158 | * |
| 1159 | * @param position new position between [0,1] |
| 1160 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1161 | @UnsupportedAppUsage |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1162 | private void setThumbPosition(float position) { |
| 1163 | mThumbPosition = position; |
| 1164 | invalidate(); |
| 1165 | } |
| 1166 | |
| 1167 | @Override |
| 1168 | public void toggle() { |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1169 | setChecked(!isChecked()); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
yingleiw | 473fc12 | 2019-10-11 15:10:55 -0700 | [diff] [blame] | 1172 | /** @hide **/ |
| 1173 | @Override |
| 1174 | @NonNull |
| 1175 | protected CharSequence getButtonStateDescription() { |
| 1176 | if (isChecked()) { |
| 1177 | return mTextOn == null ? getResources().getString(R.string.capital_on) : mTextOn; |
| 1178 | } else { |
| 1179 | return mTextOff == null ? getResources().getString(R.string.capital_off) : mTextOff; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1183 | @Override |
| 1184 | public void setChecked(boolean checked) { |
| 1185 | super.setChecked(checked); |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1186 | |
Alan Viverette | 467d629 | 2014-08-12 15:13:19 -0700 | [diff] [blame] | 1187 | // Calling the super method may result in setChecked() getting called |
| 1188 | // recursively with a different value, so load the REAL value... |
| 1189 | checked = isChecked(); |
| 1190 | |
Alan Viverette | 86453ff | 2013-09-26 14:46:08 -0700 | [diff] [blame] | 1191 | if (isAttachedToWindow() && isLaidOut()) { |
| 1192 | animateThumbToCheckedState(checked); |
| 1193 | } else { |
| 1194 | // Immediately move the thumb to the new position. |
| 1195 | cancelPositionAnimator(); |
| 1196 | setThumbPosition(checked ? 1 : 0); |
| 1197 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | @Override |
| 1201 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 1202 | super.onLayout(changed, left, top, right, bottom); |
| 1203 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1204 | int opticalInsetLeft = 0; |
| 1205 | int opticalInsetRight = 0; |
| 1206 | if (mThumbDrawable != null) { |
| 1207 | final Rect trackPadding = mTempRect; |
| 1208 | if (mTrackDrawable != null) { |
| 1209 | mTrackDrawable.getPadding(trackPadding); |
| 1210 | } else { |
| 1211 | trackPadding.setEmpty(); |
| 1212 | } |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 1213 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1214 | final Insets insets = mThumbDrawable.getOpticalInsets(); |
| 1215 | opticalInsetLeft = Math.max(0, insets.left - trackPadding.left); |
| 1216 | opticalInsetRight = Math.max(0, insets.right - trackPadding.right); |
Alan Viverette | 8bb3990 | 2014-07-29 17:22:30 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1219 | final int switchRight; |
| 1220 | final int switchLeft; |
| 1221 | if (isLayoutRtl()) { |
| 1222 | switchLeft = getPaddingLeft() + opticalInsetLeft; |
| 1223 | switchRight = switchLeft + mSwitchWidth - opticalInsetLeft - opticalInsetRight; |
| 1224 | } else { |
| 1225 | switchRight = getWidth() - getPaddingRight() - opticalInsetRight; |
| 1226 | switchLeft = switchRight - mSwitchWidth + opticalInsetLeft + opticalInsetRight; |
| 1227 | } |
| 1228 | |
| 1229 | final int switchTop; |
| 1230 | final int switchBottom; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1231 | switch (getGravity() & Gravity.VERTICAL_GRAVITY_MASK) { |
| 1232 | default: |
| 1233 | case Gravity.TOP: |
| 1234 | switchTop = getPaddingTop(); |
| 1235 | switchBottom = switchTop + mSwitchHeight; |
| 1236 | break; |
| 1237 | |
| 1238 | case Gravity.CENTER_VERTICAL: |
| 1239 | switchTop = (getPaddingTop() + getHeight() - getPaddingBottom()) / 2 - |
| 1240 | mSwitchHeight / 2; |
| 1241 | switchBottom = switchTop + mSwitchHeight; |
| 1242 | break; |
| 1243 | |
| 1244 | case Gravity.BOTTOM: |
| 1245 | switchBottom = getHeight() - getPaddingBottom(); |
| 1246 | switchTop = switchBottom - mSwitchHeight; |
| 1247 | break; |
| 1248 | } |
| 1249 | |
| 1250 | mSwitchLeft = switchLeft; |
| 1251 | mSwitchTop = switchTop; |
| 1252 | mSwitchBottom = switchBottom; |
| 1253 | mSwitchRight = switchRight; |
| 1254 | } |
| 1255 | |
| 1256 | @Override |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 1257 | public void draw(Canvas c) { |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1258 | final Rect padding = mTempRect; |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 1259 | final int switchLeft = mSwitchLeft; |
| 1260 | final int switchTop = mSwitchTop; |
| 1261 | final int switchRight = mSwitchRight; |
| 1262 | final int switchBottom = mSwitchBottom; |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1263 | |
| 1264 | int thumbInitialLeft = switchLeft + getThumbOffset(); |
| 1265 | |
| 1266 | final Insets thumbInsets; |
| 1267 | if (mThumbDrawable != null) { |
| 1268 | thumbInsets = mThumbDrawable.getOpticalInsets(); |
| 1269 | } else { |
| 1270 | thumbInsets = Insets.NONE; |
Alan Viverette | 8bb3990 | 2014-07-29 17:22:30 -0700 | [diff] [blame] | 1271 | } |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1272 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1273 | // Layout the track. |
| 1274 | if (mTrackDrawable != null) { |
| 1275 | mTrackDrawable.getPadding(padding); |
Alan Viverette | 9b38f6c | 2014-07-30 02:39:07 +0000 | [diff] [blame] | 1276 | |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1277 | // Adjust thumb position for track padding. |
| 1278 | thumbInitialLeft += padding.left; |
| 1279 | |
| 1280 | // If necessary, offset by the optical insets of the thumb asset. |
| 1281 | int trackLeft = switchLeft; |
| 1282 | int trackTop = switchTop; |
| 1283 | int trackRight = switchRight; |
| 1284 | int trackBottom = switchBottom; |
| 1285 | if (thumbInsets != Insets.NONE) { |
| 1286 | if (thumbInsets.left > padding.left) { |
| 1287 | trackLeft += thumbInsets.left - padding.left; |
| 1288 | } |
| 1289 | if (thumbInsets.top > padding.top) { |
| 1290 | trackTop += thumbInsets.top - padding.top; |
| 1291 | } |
| 1292 | if (thumbInsets.right > padding.right) { |
| 1293 | trackRight -= thumbInsets.right - padding.right; |
| 1294 | } |
| 1295 | if (thumbInsets.bottom > padding.bottom) { |
| 1296 | trackBottom -= thumbInsets.bottom - padding.bottom; |
| 1297 | } |
| 1298 | } |
| 1299 | mTrackDrawable.setBounds(trackLeft, trackTop, trackRight, trackBottom); |
| 1300 | } |
Alan Viverette | 9b38f6c | 2014-07-30 02:39:07 +0000 | [diff] [blame] | 1301 | |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1302 | // Layout the thumb. |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1303 | if (mThumbDrawable != null) { |
| 1304 | mThumbDrawable.getPadding(padding); |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1305 | |
| 1306 | final int thumbLeft = thumbInitialLeft - padding.left; |
| 1307 | final int thumbRight = thumbInitialLeft + mThumbWidth + padding.right; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1308 | mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom); |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 1309 | |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1310 | final Drawable background = getBackground(); |
| 1311 | if (background != null) { |
| 1312 | background.setHotspotBounds(thumbLeft, switchTop, thumbRight, switchBottom); |
| 1313 | } |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 1316 | // Draw the background. |
| 1317 | super.draw(c); |
| 1318 | } |
| 1319 | |
| 1320 | @Override |
| 1321 | protected void onDraw(Canvas canvas) { |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 1322 | super.onDraw(canvas); |
| 1323 | |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1324 | final Rect padding = mTempRect; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 1325 | final Drawable trackDrawable = mTrackDrawable; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1326 | if (trackDrawable != null) { |
| 1327 | trackDrawable.getPadding(padding); |
| 1328 | } else { |
| 1329 | padding.setEmpty(); |
| 1330 | } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 1331 | |
| 1332 | final int switchTop = mSwitchTop; |
| 1333 | final int switchBottom = mSwitchBottom; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1334 | final int switchInnerTop = switchTop + padding.top; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1335 | final int switchInnerBottom = switchBottom - padding.bottom; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 1336 | |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1337 | final Drawable thumbDrawable = mThumbDrawable; |
| 1338 | if (trackDrawable != null) { |
| 1339 | if (mSplitTrack && thumbDrawable != null) { |
| 1340 | final Insets insets = thumbDrawable.getOpticalInsets(); |
| 1341 | thumbDrawable.copyBounds(padding); |
| 1342 | padding.left += insets.left; |
| 1343 | padding.right -= insets.right; |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1344 | |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1345 | final int saveCount = canvas.save(); |
| 1346 | canvas.clipRect(padding, Op.DIFFERENCE); |
| 1347 | trackDrawable.draw(canvas); |
| 1348 | canvas.restoreToCount(saveCount); |
| 1349 | } else { |
| 1350 | trackDrawable.draw(canvas); |
| 1351 | } |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1352 | } |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 1353 | |
| 1354 | final int saveCount = canvas.save(); |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1355 | |
| 1356 | if (thumbDrawable != null) { |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1357 | thumbDrawable.draw(canvas); |
| 1358 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1359 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 1360 | final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout; |
Fabrice Di Meglio | be06e32 | 2012-09-11 17:42:45 -0700 | [diff] [blame] | 1361 | if (switchText != null) { |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1362 | final int drawableState[] = getDrawableState(); |
| 1363 | if (mTextColors != null) { |
| 1364 | mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0)); |
| 1365 | } |
| 1366 | mTextPaint.drawableState = drawableState; |
| 1367 | |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1368 | final int cX; |
| 1369 | if (thumbDrawable != null) { |
| 1370 | final Rect bounds = thumbDrawable.getBounds(); |
| 1371 | cX = bounds.left + bounds.right; |
| 1372 | } else { |
Alan Viverette | dec1729 | 2014-07-12 00:26:36 -0700 | [diff] [blame] | 1373 | cX = getWidth(); |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | final int left = cX / 2 - switchText.getWidth() / 2; |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 1377 | final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2; |
| 1378 | canvas.translate(left, top); |
Fabrice Di Meglio | be06e32 | 2012-09-11 17:42:45 -0700 | [diff] [blame] | 1379 | switchText.draw(canvas); |
| 1380 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1381 | |
Alan Viverette | 5876ff4 | 2014-03-03 17:40:46 -0800 | [diff] [blame] | 1382 | canvas.restoreToCount(saveCount); |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | @Override |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 1386 | public int getCompoundPaddingLeft() { |
| 1387 | if (!isLayoutRtl()) { |
| 1388 | return super.getCompoundPaddingLeft(); |
| 1389 | } |
| 1390 | int padding = super.getCompoundPaddingLeft() + mSwitchWidth; |
| 1391 | if (!TextUtils.isEmpty(getText())) { |
| 1392 | padding += mSwitchPadding; |
| 1393 | } |
| 1394 | return padding; |
| 1395 | } |
| 1396 | |
| 1397 | @Override |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1398 | public int getCompoundPaddingRight() { |
Fabrice Di Meglio | 28efba3 | 2012-06-01 16:52:31 -0700 | [diff] [blame] | 1399 | if (isLayoutRtl()) { |
| 1400 | return super.getCompoundPaddingRight(); |
| 1401 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1402 | int padding = super.getCompoundPaddingRight() + mSwitchWidth; |
| 1403 | if (!TextUtils.isEmpty(getText())) { |
| 1404 | padding += mSwitchPadding; |
| 1405 | } |
| 1406 | return padding; |
| 1407 | } |
| 1408 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1409 | /** |
| 1410 | * Translates thumb position to offset according to current RTL setting and |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1411 | * thumb scroll range. Accounts for both track and thumb padding. |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1412 | * |
| 1413 | * @return thumb offset |
| 1414 | */ |
| 1415 | private int getThumbOffset() { |
| 1416 | final float thumbPosition; |
| 1417 | if (isLayoutRtl()) { |
| 1418 | thumbPosition = 1 - mThumbPosition; |
| 1419 | } else { |
| 1420 | thumbPosition = mThumbPosition; |
| 1421 | } |
| 1422 | return (int) (thumbPosition * getThumbScrollRange() + 0.5f); |
| 1423 | } |
| 1424 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1425 | private int getThumbScrollRange() { |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1426 | if (mTrackDrawable != null) { |
Alan Viverette | 0c0dde7 | 2014-07-30 13:29:39 -0700 | [diff] [blame] | 1427 | final Rect padding = mTempRect; |
| 1428 | mTrackDrawable.getPadding(padding); |
| 1429 | |
| 1430 | final Insets insets; |
| 1431 | if (mThumbDrawable != null) { |
| 1432 | insets = mThumbDrawable.getOpticalInsets(); |
| 1433 | } else { |
| 1434 | insets = Insets.NONE; |
| 1435 | } |
| 1436 | |
| 1437 | return mSwitchWidth - mThumbWidth - padding.left - padding.right |
| 1438 | - insets.left - insets.right; |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1439 | } else { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1440 | return 0; |
| 1441 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | @Override |
| 1445 | protected int[] onCreateDrawableState(int extraSpace) { |
| 1446 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 1447 | if (isChecked()) { |
| 1448 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 1449 | } |
| 1450 | return drawableState; |
| 1451 | } |
| 1452 | |
| 1453 | @Override |
| 1454 | protected void drawableStateChanged() { |
| 1455 | super.drawableStateChanged(); |
| 1456 | |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 1457 | final int[] state = getDrawableState(); |
| 1458 | boolean changed = false; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1459 | |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 1460 | final Drawable thumbDrawable = mThumbDrawable; |
| 1461 | if (thumbDrawable != null && thumbDrawable.isStateful()) { |
| 1462 | changed |= thumbDrawable.setState(state); |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 1465 | final Drawable trackDrawable = mTrackDrawable; |
| 1466 | if (trackDrawable != null && trackDrawable.isStateful()) { |
| 1467 | changed |= trackDrawable.setState(state); |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1468 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1469 | |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 1470 | if (changed) { |
| 1471 | invalidate(); |
| 1472 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1475 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 1476 | public void drawableHotspotChanged(float x, float y) { |
| 1477 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1478 | |
| 1479 | if (mThumbDrawable != null) { |
| 1480 | mThumbDrawable.setHotspot(x, y); |
| 1481 | } |
| 1482 | |
| 1483 | if (mTrackDrawable != null) { |
| 1484 | mTrackDrawable.setHotspot(x, y); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1488 | @Override |
Alan Viverette | f6d87ec | 2016-03-11 10:09:14 -0500 | [diff] [blame] | 1489 | protected boolean verifyDrawable(@NonNull Drawable who) { |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1490 | return super.verifyDrawable(who) || who == mThumbDrawable || who == mTrackDrawable; |
| 1491 | } |
| 1492 | |
| 1493 | @Override |
| 1494 | public void jumpDrawablesToCurrentState() { |
| 1495 | super.jumpDrawablesToCurrentState(); |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1496 | |
| 1497 | if (mThumbDrawable != null) { |
| 1498 | mThumbDrawable.jumpToCurrentState(); |
| 1499 | } |
| 1500 | |
| 1501 | if (mTrackDrawable != null) { |
| 1502 | mTrackDrawable.jumpToCurrentState(); |
| 1503 | } |
| 1504 | |
Alan Viverette | ee20d77 | 2015-09-23 14:23:21 -0400 | [diff] [blame] | 1505 | if (mPositionAnimator != null && mPositionAnimator.isStarted()) { |
Alan Viverette | 4d065a0 | 2014-07-11 15:28:38 -0700 | [diff] [blame] | 1506 | mPositionAnimator.end(); |
| 1507 | mPositionAnimator = null; |
| 1508 | } |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1509 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1510 | |
| 1511 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1512 | public CharSequence getAccessibilityClassName() { |
| 1513 | return Switch.class.getName(); |
| 1514 | } |
| 1515 | |
Felipe Leme | 92ab385 | 2018-11-15 11:30:40 -0800 | [diff] [blame] | 1516 | /** @hide */ |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1517 | @Override |
Felipe Leme | 92ab385 | 2018-11-15 11:30:40 -0800 | [diff] [blame] | 1518 | protected void onProvideStructure(@NonNull ViewStructure structure, |
| 1519 | @ViewStructureType int viewFor, int flags) { |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1520 | CharSequence switchText = isChecked() ? mTextOn : mTextOff; |
| 1521 | if (!TextUtils.isEmpty(switchText)) { |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1522 | CharSequence oldText = structure.getText(); |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1523 | if (TextUtils.isEmpty(oldText)) { |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1524 | structure.setText(switchText); |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1525 | } else { |
| 1526 | StringBuilder newText = new StringBuilder(); |
| 1527 | newText.append(oldText).append(' ').append(switchText); |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1528 | structure.setText(newText); |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1529 | } |
James Cook | 5cfaae4 | 2015-05-28 15:52:44 -0700 | [diff] [blame] | 1530 | // The style of the label text is provided via the base TextView class. This is more |
| 1531 | // relevant than the style of the (optional) on/off text on the switch button itself, |
| 1532 | // so ignore the size/color/style stored this.mTextPaint. |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1533 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Alan Viverette | cc2688d | 2013-09-17 17:00:12 -0700 | [diff] [blame] | 1536 | private static final FloatProperty<Switch> THUMB_POS = new FloatProperty<Switch>("thumbPos") { |
| 1537 | @Override |
| 1538 | public Float get(Switch object) { |
| 1539 | return object.mThumbPosition; |
| 1540 | } |
| 1541 | |
| 1542 | @Override |
| 1543 | public void setValue(Switch object, float value) { |
| 1544 | object.setThumbPosition(value); |
| 1545 | } |
| 1546 | }; |
Adam Powell | 12190b3 | 2010-11-28 19:07:53 -0800 | [diff] [blame] | 1547 | } |