Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 com.android.internal.widget; |
| 18 | |
Artur Satayev | 2a9f3b8 | 2019-12-10 17:47:55 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Rect; |
Jim Miller | 753401a | 2009-11-11 17:53:53 -0800 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 25 | import android.media.AudioAttributes; |
Jeff Sharkey | 723a725 | 2012-10-12 14:26:31 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 27 | import android.os.Vibrator; |
Jeff Sharkey | 723a725 | 2012-10-12 14:26:31 -0700 | [diff] [blame] | 28 | import android.provider.Settings; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
| 30 | import android.util.Log; |
| 31 | import android.view.Gravity; |
| 32 | import android.view.MotionEvent; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 35 | import android.view.animation.AlphaAnimation; |
| 36 | import android.view.animation.Animation; |
Artur Satayev | 2a9f3b8 | 2019-12-10 17:47:55 +0000 | [diff] [blame] | 37 | import android.view.animation.Animation.AnimationListener; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 38 | import android.view.animation.LinearInterpolator; |
| 39 | import android.view.animation.TranslateAnimation; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 40 | import android.widget.ImageView; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 41 | import android.widget.ImageView.ScaleType; |
Artur Satayev | 2a9f3b8 | 2019-12-10 17:47:55 +0000 | [diff] [blame] | 42 | import android.widget.TextView; |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 43 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 44 | import com.android.internal.R; |
| 45 | |
| 46 | /** |
| 47 | * A special widget containing two Sliders and a threshold for each. Moving either slider beyond |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 48 | * the threshold will cause the registered OnTriggerListener.onTrigger() to be called with |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 49 | * whichHandle being {@link OnTriggerListener#LEFT_HANDLE} or {@link OnTriggerListener#RIGHT_HANDLE} |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 50 | * Equivalently, selecting a tab will result in a call to |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 51 | * {@link OnTriggerListener#onGrabbedStateChange(View, int)} with one of these two states. Releasing |
| 52 | * the tab will result in whichHandle being {@link OnTriggerListener#NO_HANDLE}. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 53 | * |
| 54 | */ |
| 55 | public class SlidingTab extends ViewGroup { |
| 56 | private static final String LOG_TAG = "SlidingTab"; |
| 57 | private static final boolean DBG = false; |
| 58 | private static final int HORIZONTAL = 0; // as defined in attrs.xml |
| 59 | private static final int VERTICAL = 1; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 60 | |
| 61 | // TODO: Make these configurable |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 62 | private static final float THRESHOLD = 2.0f / 3.0f; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 63 | private static final long VIBRATE_SHORT = 30; |
| 64 | private static final long VIBRATE_LONG = 40; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 65 | private static final int TRACKING_MARGIN = 50; |
| 66 | private static final int ANIM_DURATION = 250; // Time for most animations (in ms) |
| 67 | private static final int ANIM_TARGET_TIME = 500; // Time to show targets (in ms) |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 68 | private boolean mHoldLeftOnTransition = true; |
| 69 | private boolean mHoldRightOnTransition = true; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 70 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 71 | private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() |
| 72 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 73 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 74 | .build(); |
| 75 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 76 | private OnTriggerListener mOnTriggerListener; |
| 77 | private int mGrabbedState = OnTriggerListener.NO_HANDLE; |
| 78 | private boolean mTriggered = false; |
| 79 | private Vibrator mVibrator; |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 80 | private final float mDensity; // used to scale dimensions for bitmaps. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 81 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Either {@link #HORIZONTAL} or {@link #VERTICAL}. |
| 84 | */ |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 85 | private final int mOrientation; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 86 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 87 | @UnsupportedAppUsage |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 88 | private final Slider mLeftSlider; |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 89 | @UnsupportedAppUsage |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 90 | private final Slider mRightSlider; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 91 | private Slider mCurrentSlider; |
| 92 | private boolean mTracking; |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 93 | private float mThreshold; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 94 | private Slider mOtherSlider; |
| 95 | private boolean mAnimating; |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 96 | private final Rect mTmpRect; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 99 | * Listener used to reset the view when the current animation completes. |
| 100 | */ |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 101 | @UnsupportedAppUsage |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 102 | private final AnimationListener mAnimationDoneListener = new AnimationListener() { |
| 103 | public void onAnimationStart(Animation animation) { |
| 104 | |
| 105 | } |
| 106 | |
| 107 | public void onAnimationRepeat(Animation animation) { |
| 108 | |
| 109 | } |
| 110 | |
| 111 | public void onAnimationEnd(Animation animation) { |
| 112 | onAnimationDone(); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | /** |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 117 | * Interface definition for a callback to be invoked when a tab is triggered |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 118 | * by moving it beyond a threshold. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 119 | */ |
| 120 | public interface OnTriggerListener { |
| 121 | /** |
| 122 | * The interface was triggered because the user let go of the handle without reaching the |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 123 | * threshold. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 124 | */ |
| 125 | public static final int NO_HANDLE = 0; |
| 126 | |
| 127 | /** |
| 128 | * The interface was triggered because the user grabbed the left handle and moved it past |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 129 | * the threshold. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 130 | */ |
| 131 | public static final int LEFT_HANDLE = 1; |
| 132 | |
| 133 | /** |
| 134 | * The interface was triggered because the user grabbed the right handle and moved it past |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 135 | * the threshold. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 136 | */ |
| 137 | public static final int RIGHT_HANDLE = 2; |
| 138 | |
| 139 | /** |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 140 | * Called when the user moves a handle beyond the threshold. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 141 | * |
| 142 | * @param v The view that was triggered. |
| 143 | * @param whichHandle Which "dial handle" the user grabbed, |
| 144 | * either {@link #LEFT_HANDLE}, {@link #RIGHT_HANDLE}. |
| 145 | */ |
| 146 | void onTrigger(View v, int whichHandle); |
| 147 | |
| 148 | /** |
| 149 | * Called when the "grabbed state" changes (i.e. when the user either grabs or releases |
| 150 | * one of the handles.) |
| 151 | * |
| 152 | * @param v the view that was triggered |
| 153 | * @param grabbedState the new state: {@link #NO_HANDLE}, {@link #LEFT_HANDLE}, |
| 154 | * or {@link #RIGHT_HANDLE}. |
| 155 | */ |
| 156 | void onGrabbedStateChange(View v, int grabbedState); |
| 157 | } |
| 158 | |
| 159 | /** |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 160 | * Simple container class for all things pertinent to a slider. |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 161 | * A slider consists of 3 Views: |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 162 | * |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 163 | * {@link #tab} is the tab shown on the screen in the default state. |
| 164 | * {@link #text} is the view revealed as the user slides the tab out. |
| 165 | * {@link #target} is the target the user must drag the slider past to trigger the slider. |
| 166 | * |
| 167 | */ |
| 168 | private static class Slider { |
| 169 | /** |
| 170 | * Tab alignment - determines which side the tab should be drawn on |
| 171 | */ |
| 172 | public static final int ALIGN_LEFT = 0; |
| 173 | public static final int ALIGN_RIGHT = 1; |
| 174 | public static final int ALIGN_TOP = 2; |
| 175 | public static final int ALIGN_BOTTOM = 3; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 176 | public static final int ALIGN_UNKNOWN = 4; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * States for the view. |
| 180 | */ |
| 181 | private static final int STATE_NORMAL = 0; |
| 182 | private static final int STATE_PRESSED = 1; |
| 183 | private static final int STATE_ACTIVE = 2; |
| 184 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 185 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 186 | private final ImageView tab; |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 187 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 188 | private final TextView text; |
| 189 | private final ImageView target; |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 190 | private int currentState = STATE_NORMAL; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 191 | private int alignment = ALIGN_UNKNOWN; |
| 192 | private int alignment_value; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Constructor |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 196 | * |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 197 | * @param parent the container view of this one |
| 198 | * @param tabId drawable for the tab |
| 199 | * @param barId drawable for the bar |
| 200 | * @param targetId drawable for the target |
| 201 | */ |
| 202 | Slider(ViewGroup parent, int tabId, int barId, int targetId) { |
| 203 | // Create tab |
| 204 | tab = new ImageView(parent.getContext()); |
| 205 | tab.setBackgroundResource(tabId); |
| 206 | tab.setScaleType(ScaleType.CENTER); |
| 207 | tab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 208 | LayoutParams.WRAP_CONTENT)); |
| 209 | |
| 210 | // Create hint TextView |
| 211 | text = new TextView(parent.getContext()); |
| 212 | text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 213 | LayoutParams.MATCH_PARENT)); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 214 | text.setBackgroundResource(barId); |
| 215 | text.setTextAppearance(parent.getContext(), R.style.TextAppearance_SlidingTabNormal); |
| 216 | // hint.setSingleLine(); // Hmm.. this causes the text to disappear off-screen |
| 217 | |
| 218 | // Create target |
| 219 | target = new ImageView(parent.getContext()); |
| 220 | target.setImageResource(targetId); |
| 221 | target.setScaleType(ScaleType.CENTER); |
| 222 | target.setLayoutParams( |
| 223 | new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
| 224 | target.setVisibility(View.INVISIBLE); |
| 225 | |
| 226 | parent.addView(target); // this needs to be first - relies on painter's algorithm |
| 227 | parent.addView(tab); |
| 228 | parent.addView(text); |
| 229 | } |
| 230 | |
| 231 | void setIcon(int iconId) { |
| 232 | tab.setImageResource(iconId); |
| 233 | } |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 234 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 235 | void setTabBackgroundResource(int tabId) { |
| 236 | tab.setBackgroundResource(tabId); |
| 237 | } |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 238 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 239 | void setBarBackgroundResource(int barId) { |
| 240 | text.setBackgroundResource(barId); |
| 241 | } |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 242 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 243 | void setHintText(int resId) { |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 244 | text.setText(resId); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void hide() { |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 248 | boolean horiz = alignment == ALIGN_LEFT || alignment == ALIGN_RIGHT; |
| 249 | int dx = horiz ? (alignment == ALIGN_LEFT ? alignment_value - tab.getRight() |
| 250 | : alignment_value - tab.getLeft()) : 0; |
| 251 | int dy = horiz ? 0 : (alignment == ALIGN_TOP ? alignment_value - tab.getBottom() |
| 252 | : alignment_value - tab.getTop()); |
| 253 | |
| 254 | Animation trans = new TranslateAnimation(0, dx, 0, dy); |
| 255 | trans.setDuration(ANIM_DURATION); |
| 256 | trans.setFillAfter(true); |
| 257 | tab.startAnimation(trans); |
| 258 | text.startAnimation(trans); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 259 | target.setVisibility(View.INVISIBLE); |
| 260 | } |
| 261 | |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 262 | void show(boolean animate) { |
| 263 | text.setVisibility(View.VISIBLE); |
| 264 | tab.setVisibility(View.VISIBLE); |
| 265 | //target.setVisibility(View.INVISIBLE); |
| 266 | if (animate) { |
| 267 | boolean horiz = alignment == ALIGN_LEFT || alignment == ALIGN_RIGHT; |
| 268 | int dx = horiz ? (alignment == ALIGN_LEFT ? tab.getWidth() : -tab.getWidth()) : 0; |
| 269 | int dy = horiz ? 0: (alignment == ALIGN_TOP ? tab.getHeight() : -tab.getHeight()); |
| 270 | |
| 271 | Animation trans = new TranslateAnimation(-dx, 0, -dy, 0); |
| 272 | trans.setDuration(ANIM_DURATION); |
| 273 | tab.startAnimation(trans); |
| 274 | text.startAnimation(trans); |
| 275 | } |
| 276 | } |
| 277 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 278 | void setState(int state) { |
| 279 | text.setPressed(state == STATE_PRESSED); |
| 280 | tab.setPressed(state == STATE_PRESSED); |
| 281 | if (state == STATE_ACTIVE) { |
| 282 | final int[] activeState = new int[] {com.android.internal.R.attr.state_active}; |
| 283 | if (text.getBackground().isStateful()) { |
| 284 | text.getBackground().setState(activeState); |
| 285 | } |
| 286 | if (tab.getBackground().isStateful()) { |
| 287 | tab.getBackground().setState(activeState); |
| 288 | } |
| 289 | text.setTextAppearance(text.getContext(), R.style.TextAppearance_SlidingTabActive); |
| 290 | } else { |
| 291 | text.setTextAppearance(text.getContext(), R.style.TextAppearance_SlidingTabNormal); |
| 292 | } |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 293 | currentState = state; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void showTarget() { |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 297 | AlphaAnimation alphaAnim = new AlphaAnimation(0.0f, 1.0f); |
| 298 | alphaAnim.setDuration(ANIM_TARGET_TIME); |
| 299 | target.startAnimation(alphaAnim); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 300 | target.setVisibility(View.VISIBLE); |
| 301 | } |
| 302 | |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 303 | void reset(boolean animate) { |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 304 | setState(STATE_NORMAL); |
| 305 | text.setVisibility(View.VISIBLE); |
| 306 | text.setTextAppearance(text.getContext(), R.style.TextAppearance_SlidingTabNormal); |
| 307 | tab.setVisibility(View.VISIBLE); |
| 308 | target.setVisibility(View.INVISIBLE); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 309 | final boolean horiz = alignment == ALIGN_LEFT || alignment == ALIGN_RIGHT; |
| 310 | int dx = horiz ? (alignment == ALIGN_LEFT ? alignment_value - tab.getLeft() |
| 311 | : alignment_value - tab.getRight()) : 0; |
| 312 | int dy = horiz ? 0 : (alignment == ALIGN_TOP ? alignment_value - tab.getTop() |
| 313 | : alignment_value - tab.getBottom()); |
| 314 | if (animate) { |
| 315 | TranslateAnimation trans = new TranslateAnimation(0, dx, 0, dy); |
| 316 | trans.setDuration(ANIM_DURATION); |
| 317 | trans.setFillAfter(false); |
| 318 | text.startAnimation(trans); |
| 319 | tab.startAnimation(trans); |
| 320 | } else { |
| 321 | if (horiz) { |
| 322 | text.offsetLeftAndRight(dx); |
| 323 | tab.offsetLeftAndRight(dx); |
| 324 | } else { |
| 325 | text.offsetTopAndBottom(dy); |
| 326 | tab.offsetTopAndBottom(dy); |
| 327 | } |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 328 | text.clearAnimation(); |
| 329 | tab.clearAnimation(); |
| 330 | target.clearAnimation(); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 331 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void setTarget(int targetId) { |
| 335 | target.setImageResource(targetId); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Layout the given widgets within the parent. |
| 340 | * |
| 341 | * @param l the parent's left border |
| 342 | * @param t the parent's top border |
| 343 | * @param r the parent's right border |
| 344 | * @param b the parent's bottom border |
| 345 | * @param alignment which side to align the widget to |
| 346 | */ |
| 347 | void layout(int l, int t, int r, int b, int alignment) { |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 348 | this.alignment = alignment; |
Jim Miller | 753401a | 2009-11-11 17:53:53 -0800 | [diff] [blame] | 349 | final Drawable tabBackground = tab.getBackground(); |
| 350 | final int handleWidth = tabBackground.getIntrinsicWidth(); |
| 351 | final int handleHeight = tabBackground.getIntrinsicHeight(); |
| 352 | final Drawable targetDrawable = target.getDrawable(); |
| 353 | final int targetWidth = targetDrawable.getIntrinsicWidth(); |
| 354 | final int targetHeight = targetDrawable.getIntrinsicHeight(); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 355 | final int parentWidth = r - l; |
| 356 | final int parentHeight = b - t; |
| 357 | |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 358 | final int leftTarget = (int) (THRESHOLD * parentWidth) - targetWidth + handleWidth / 2; |
| 359 | final int rightTarget = (int) ((1.0f - THRESHOLD) * parentWidth) - handleWidth / 2; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 360 | final int left = (parentWidth - handleWidth) / 2; |
| 361 | final int right = left + handleWidth; |
| 362 | |
| 363 | if (alignment == ALIGN_LEFT || alignment == ALIGN_RIGHT) { |
| 364 | // horizontal |
| 365 | final int targetTop = (parentHeight - targetHeight) / 2; |
| 366 | final int targetBottom = targetTop + targetHeight; |
| 367 | final int top = (parentHeight - handleHeight) / 2; |
| 368 | final int bottom = (parentHeight + handleHeight) / 2; |
| 369 | if (alignment == ALIGN_LEFT) { |
| 370 | tab.layout(0, top, handleWidth, bottom); |
| 371 | text.layout(0 - parentWidth, top, 0, bottom); |
| 372 | text.setGravity(Gravity.RIGHT); |
| 373 | target.layout(leftTarget, targetTop, leftTarget + targetWidth, targetBottom); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 374 | alignment_value = l; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 375 | } else { |
| 376 | tab.layout(parentWidth - handleWidth, top, parentWidth, bottom); |
| 377 | text.layout(parentWidth, top, parentWidth + parentWidth, bottom); |
| 378 | target.layout(rightTarget, targetTop, rightTarget + targetWidth, targetBottom); |
| 379 | text.setGravity(Gravity.TOP); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 380 | alignment_value = r; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 381 | } |
| 382 | } else { |
| 383 | // vertical |
| 384 | final int targetLeft = (parentWidth - targetWidth) / 2; |
| 385 | final int targetRight = (parentWidth + targetWidth) / 2; |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 386 | final int top = (int) (THRESHOLD * parentHeight) + handleHeight / 2 - targetHeight; |
| 387 | final int bottom = (int) ((1.0f - THRESHOLD) * parentHeight) - handleHeight / 2; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 388 | if (alignment == ALIGN_TOP) { |
| 389 | tab.layout(left, 0, right, handleHeight); |
| 390 | text.layout(left, 0 - parentHeight, right, 0); |
| 391 | target.layout(targetLeft, top, targetRight, top + targetHeight); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 392 | alignment_value = t; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 393 | } else { |
| 394 | tab.layout(left, parentHeight - handleHeight, right, parentHeight); |
| 395 | text.layout(left, parentHeight, right, parentHeight + parentHeight); |
| 396 | target.layout(targetLeft, bottom, targetRight, bottom + targetHeight); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 397 | alignment_value = b; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 402 | public void updateDrawableStates() { |
| 403 | setState(currentState); |
| 404 | } |
| 405 | |
Jim Miller | 4811d62 | 2009-11-12 17:45:07 -0800 | [diff] [blame] | 406 | /** |
| 407 | * Ensure all the dependent widgets are measured. |
| 408 | */ |
Filip Gruszczynski | b6824bf | 2015-04-13 09:16:25 -0700 | [diff] [blame] | 409 | public void measure(int widthMeasureSpec, int heightMeasureSpec) { |
| 410 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 411 | int height = MeasureSpec.getSize(heightMeasureSpec); |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 412 | tab.measure(View.MeasureSpec.makeSafeMeasureSpec(width, View.MeasureSpec.UNSPECIFIED), |
| 413 | View.MeasureSpec.makeSafeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED)); |
| 414 | text.measure(View.MeasureSpec.makeSafeMeasureSpec(width, View.MeasureSpec.UNSPECIFIED), |
| 415 | View.MeasureSpec.makeSafeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED)); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Jim Miller | 4811d62 | 2009-11-12 17:45:07 -0800 | [diff] [blame] | 418 | /** |
| 419 | * Get the measured tab width. Must be called after {@link Slider#measure()}. |
| 420 | * @return |
| 421 | */ |
| 422 | public int getTabWidth() { |
| 423 | return tab.getMeasuredWidth(); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Get the measured tab width. Must be called after {@link Slider#measure()}. |
| 428 | * @return |
| 429 | */ |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 430 | public int getTabHeight() { |
Jim Miller | 4811d62 | 2009-11-12 17:45:07 -0800 | [diff] [blame] | 431 | return tab.getMeasuredHeight(); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 432 | } |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 433 | |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 434 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 435 | * Start animating the slider. Note we need two animations since a ValueAnimator |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 436 | * keeps internal state of the invalidation region which is just the view being animated. |
Jim Miller | 2cd1e6e | 2010-01-15 16:20:36 -0800 | [diff] [blame] | 437 | * |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 438 | * @param anim1 |
| 439 | * @param anim2 |
| 440 | */ |
| 441 | public void startAnimation(Animation anim1, Animation anim2) { |
| 442 | tab.startAnimation(anim1); |
| 443 | text.startAnimation(anim2); |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | public void hideTarget() { |
| 447 | target.clearAnimation(); |
| 448 | target.setVisibility(View.INVISIBLE); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 449 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | public SlidingTab(Context context) { |
| 453 | this(context, null); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Constructor used when this widget is created from a layout file. |
| 458 | */ |
| 459 | public SlidingTab(Context context, AttributeSet attrs) { |
| 460 | super(context, attrs); |
| 461 | |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 462 | // Allocate a temporary once that can be used everywhere. |
| 463 | mTmpRect = new Rect(); |
| 464 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 465 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingTab); |
| 466 | mOrientation = a.getInt(R.styleable.SlidingTab_orientation, HORIZONTAL); |
| 467 | a.recycle(); |
| 468 | |
| 469 | Resources r = getResources(); |
| 470 | mDensity = r.getDisplayMetrics().density; |
| 471 | if (DBG) log("- Density: " + mDensity); |
| 472 | |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 473 | mLeftSlider = new Slider(this, |
| 474 | R.drawable.jog_tab_left_generic, |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 475 | R.drawable.jog_tab_bar_left_generic, |
| 476 | R.drawable.jog_tab_target_gray); |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 477 | mRightSlider = new Slider(this, |
| 478 | R.drawable.jog_tab_right_generic, |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 479 | R.drawable.jog_tab_bar_right_generic, |
| 480 | R.drawable.jog_tab_target_gray); |
| 481 | |
| 482 | // setBackgroundColor(0x80808080); |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 487 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 488 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 489 | |
| 490 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 491 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 492 | |
Jim Miller | 3747f3f | 2010-08-17 13:31:46 -0700 | [diff] [blame] | 493 | if (DBG) { |
| 494 | if (widthSpecMode == MeasureSpec.UNSPECIFIED |
| 495 | || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 496 | Log.e("SlidingTab", "SlidingTab cannot have UNSPECIFIED MeasureSpec" |
| 497 | +"(wspec=" + widthSpecMode + ", hspec=" + heightSpecMode + ")", |
| 498 | new RuntimeException(LOG_TAG + "stack:")); |
| 499 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Filip Gruszczynski | b6824bf | 2015-04-13 09:16:25 -0700 | [diff] [blame] | 502 | mLeftSlider.measure(widthMeasureSpec, heightMeasureSpec); |
| 503 | mRightSlider.measure(widthMeasureSpec, heightMeasureSpec); |
Jim Miller | 4811d62 | 2009-11-12 17:45:07 -0800 | [diff] [blame] | 504 | final int leftTabWidth = mLeftSlider.getTabWidth(); |
| 505 | final int rightTabWidth = mRightSlider.getTabWidth(); |
| 506 | final int leftTabHeight = mLeftSlider.getTabHeight(); |
| 507 | final int rightTabHeight = mRightSlider.getTabHeight(); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 508 | final int width; |
| 509 | final int height; |
| 510 | if (isHorizontal()) { |
| 511 | width = Math.max(widthSpecSize, leftTabWidth + rightTabWidth); |
| 512 | height = Math.max(leftTabHeight, rightTabHeight); |
| 513 | } else { |
| 514 | width = Math.max(leftTabWidth, rightTabHeight); |
| 515 | height = Math.max(heightSpecSize, leftTabHeight + rightTabHeight); |
| 516 | } |
| 517 | setMeasuredDimension(width, height); |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 522 | final int action = event.getAction(); |
| 523 | final float x = event.getX(); |
| 524 | final float y = event.getY(); |
| 525 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 526 | if (mAnimating) { |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | View leftHandle = mLeftSlider.tab; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 531 | leftHandle.getHitRect(mTmpRect); |
| 532 | boolean leftHit = mTmpRect.contains((int) x, (int) y); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 533 | |
| 534 | View rightHandle = mRightSlider.tab; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 535 | rightHandle.getHitRect(mTmpRect); |
| 536 | boolean rightHit = mTmpRect.contains((int)x, (int) y); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 537 | |
| 538 | if (!mTracking && !(leftHit || rightHit)) { |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | switch (action) { |
| 543 | case MotionEvent.ACTION_DOWN: { |
| 544 | mTracking = true; |
| 545 | mTriggered = false; |
| 546 | vibrate(VIBRATE_SHORT); |
| 547 | if (leftHit) { |
| 548 | mCurrentSlider = mLeftSlider; |
| 549 | mOtherSlider = mRightSlider; |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 550 | mThreshold = isHorizontal() ? THRESHOLD : 1.0f - THRESHOLD; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 551 | setGrabbedState(OnTriggerListener.LEFT_HANDLE); |
| 552 | } else { |
| 553 | mCurrentSlider = mRightSlider; |
| 554 | mOtherSlider = mLeftSlider; |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 555 | mThreshold = isHorizontal() ? 1.0f - THRESHOLD : THRESHOLD; |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 556 | setGrabbedState(OnTriggerListener.RIGHT_HANDLE); |
| 557 | } |
| 558 | mCurrentSlider.setState(Slider.STATE_PRESSED); |
| 559 | mCurrentSlider.showTarget(); |
| 560 | mOtherSlider.hide(); |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return true; |
| 566 | } |
| 567 | |
Jim Miller | d8a3a89 | 2010-01-29 17:25:26 -0800 | [diff] [blame] | 568 | /** |
| 569 | * Reset the tabs to their original state and stop any existing animation. |
| 570 | * Animate them back into place if animate is true. |
| 571 | * |
| 572 | * @param animate |
| 573 | */ |
| 574 | public void reset(boolean animate) { |
| 575 | mLeftSlider.reset(animate); |
| 576 | mRightSlider.reset(animate); |
Jim Miller | 966a8c7 | 2010-03-26 15:54:09 -0700 | [diff] [blame] | 577 | if (!animate) { |
| 578 | mAnimating = false; |
| 579 | } |
Jim Miller | d8a3a89 | 2010-01-29 17:25:26 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 582 | @Override |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 583 | public void setVisibility(int visibility) { |
| 584 | // Clear animations so sliders don't continue to animate when we show the widget again. |
| 585 | if (visibility != getVisibility() && visibility == View.INVISIBLE) { |
Jim Miller | d8a3a89 | 2010-01-29 17:25:26 -0800 | [diff] [blame] | 586 | reset(false); |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 587 | } |
| 588 | super.setVisibility(visibility); |
| 589 | } |
| 590 | |
| 591 | @Override |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 592 | public boolean onTouchEvent(MotionEvent event) { |
| 593 | if (mTracking) { |
| 594 | final int action = event.getAction(); |
| 595 | final float x = event.getX(); |
| 596 | final float y = event.getY(); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 597 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 598 | switch (action) { |
| 599 | case MotionEvent.ACTION_MOVE: |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 600 | if (withinView(x, y, this) ) { |
| 601 | moveHandle(x, y); |
| 602 | float position = isHorizontal() ? x : y; |
| 603 | float target = mThreshold * (isHorizontal() ? getWidth() : getHeight()); |
| 604 | boolean thresholdReached; |
| 605 | if (isHorizontal()) { |
| 606 | thresholdReached = mCurrentSlider == mLeftSlider ? |
| 607 | position > target : position < target; |
| 608 | } else { |
| 609 | thresholdReached = mCurrentSlider == mLeftSlider ? |
| 610 | position < target : position > target; |
| 611 | } |
| 612 | if (!mTriggered && thresholdReached) { |
| 613 | mTriggered = true; |
| 614 | mTracking = false; |
| 615 | mCurrentSlider.setState(Slider.STATE_ACTIVE); |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 616 | boolean isLeft = mCurrentSlider == mLeftSlider; |
| 617 | dispatchTriggerEvent(isLeft ? |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 618 | OnTriggerListener.LEFT_HANDLE : OnTriggerListener.RIGHT_HANDLE); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 619 | |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 620 | startAnimating(isLeft ? mHoldLeftOnTransition : mHoldRightOnTransition); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 621 | setGrabbedState(OnTriggerListener.NO_HANDLE); |
| 622 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | // Intentionally fall through - we're outside tracking rectangle |
| 626 | |
| 627 | case MotionEvent.ACTION_UP: |
| 628 | case MotionEvent.ACTION_CANCEL: |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 629 | cancelGrab(); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 630 | break; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return mTracking || super.onTouchEvent(event); |
| 635 | } |
| 636 | |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 637 | private void cancelGrab() { |
| 638 | mTracking = false; |
| 639 | mTriggered = false; |
| 640 | mOtherSlider.show(true); |
| 641 | mCurrentSlider.reset(false); |
| 642 | mCurrentSlider.hideTarget(); |
| 643 | mCurrentSlider = null; |
| 644 | mOtherSlider = null; |
| 645 | setGrabbedState(OnTriggerListener.NO_HANDLE); |
| 646 | } |
| 647 | |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 648 | void startAnimating(final boolean holdAfter) { |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 649 | mAnimating = true; |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 650 | final Animation trans1; |
| 651 | final Animation trans2; |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 652 | final Slider slider = mCurrentSlider; |
| 653 | final Slider other = mOtherSlider; |
| 654 | final int dx; |
| 655 | final int dy; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 656 | if (isHorizontal()) { |
| 657 | int right = slider.tab.getRight(); |
| 658 | int width = slider.tab.getWidth(); |
| 659 | int left = slider.tab.getLeft(); |
| 660 | int viewWidth = getWidth(); |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 661 | int holdOffset = holdAfter ? 0 : width; // how much of tab to show at the end of anim |
| 662 | dx = slider == mRightSlider ? - (right + viewWidth - holdOffset) |
| 663 | : (viewWidth - left) + viewWidth - holdOffset; |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 664 | dy = 0; |
| 665 | } else { |
| 666 | int top = slider.tab.getTop(); |
| 667 | int bottom = slider.tab.getBottom(); |
| 668 | int height = slider.tab.getHeight(); |
| 669 | int viewHeight = getHeight(); |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 670 | int holdOffset = holdAfter ? 0 : height; // how much of tab to show at end of anim |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 671 | dx = 0; |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 672 | dy = slider == mRightSlider ? (top + viewHeight - holdOffset) |
| 673 | : - ((viewHeight - bottom) + viewHeight - holdOffset); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 674 | } |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 675 | trans1 = new TranslateAnimation(0, dx, 0, dy); |
| 676 | trans1.setDuration(ANIM_DURATION); |
| 677 | trans1.setInterpolator(new LinearInterpolator()); |
| 678 | trans1.setFillAfter(true); |
| 679 | trans2 = new TranslateAnimation(0, dx, 0, dy); |
| 680 | trans2.setDuration(ANIM_DURATION); |
| 681 | trans2.setInterpolator(new LinearInterpolator()); |
| 682 | trans2.setFillAfter(true); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 683 | |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 684 | trans1.setAnimationListener(new AnimationListener() { |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 685 | public void onAnimationEnd(Animation animation) { |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 686 | Animation anim; |
| 687 | if (holdAfter) { |
| 688 | anim = new TranslateAnimation(dx, dx, dy, dy); |
| 689 | anim.setDuration(1000); // plenty of time for transitions |
| 690 | mAnimating = false; |
| 691 | } else { |
| 692 | anim = new AlphaAnimation(0.5f, 1.0f); |
| 693 | anim.setDuration(ANIM_DURATION); |
| 694 | resetView(); |
| 695 | } |
| 696 | anim.setAnimationListener(mAnimationDoneListener); |
Jim Miller | 2cd1e6e | 2010-01-15 16:20:36 -0800 | [diff] [blame] | 697 | |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 698 | /* Animation can be the same for these since the animation just holds */ |
| 699 | mLeftSlider.startAnimation(anim, anim); |
| 700 | mRightSlider.startAnimation(anim, anim); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | public void onAnimationRepeat(Animation animation) { |
| 704 | |
| 705 | } |
| 706 | |
| 707 | public void onAnimationStart(Animation animation) { |
| 708 | |
| 709 | } |
| 710 | |
| 711 | }); |
| 712 | |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 713 | slider.hideTarget(); |
Jim Miller | 8b63ab6 | 2009-12-02 13:42:48 -0800 | [diff] [blame] | 714 | slider.startAnimation(trans1, trans2); |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 717 | @UnsupportedAppUsage |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 718 | private void onAnimationDone() { |
| 719 | resetView(); |
| 720 | mAnimating = false; |
| 721 | } |
| 722 | |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 723 | private boolean withinView(final float x, final float y, final View view) { |
| 724 | return isHorizontal() && y > - TRACKING_MARGIN && y < TRACKING_MARGIN + view.getHeight() |
| 725 | || !isHorizontal() && x > -TRACKING_MARGIN && x < TRACKING_MARGIN + view.getWidth(); |
| 726 | } |
| 727 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 728 | private boolean isHorizontal() { |
| 729 | return mOrientation == HORIZONTAL; |
| 730 | } |
| 731 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 732 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 733 | private void resetView() { |
Jim Miller | 521d400 | 2009-11-15 16:19:24 -0800 | [diff] [blame] | 734 | mLeftSlider.reset(false); |
| 735 | mRightSlider.reset(false); |
| 736 | // onLayout(true, getLeft(), getTop(), getLeft() + getWidth(), getTop() + getHeight()); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | @Override |
| 740 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 741 | if (!changed) return; |
| 742 | |
| 743 | // Center the widgets in the view |
| 744 | mLeftSlider.layout(l, t, r, b, isHorizontal() ? Slider.ALIGN_LEFT : Slider.ALIGN_BOTTOM); |
| 745 | mRightSlider.layout(l, t, r, b, isHorizontal() ? Slider.ALIGN_RIGHT : Slider.ALIGN_TOP); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | private void moveHandle(float x, float y) { |
| 749 | final View handle = mCurrentSlider.tab; |
| 750 | final View content = mCurrentSlider.text; |
| 751 | if (isHorizontal()) { |
| 752 | int deltaX = (int) x - handle.getLeft() - (handle.getWidth() / 2); |
| 753 | handle.offsetLeftAndRight(deltaX); |
| 754 | content.offsetLeftAndRight(deltaX); |
| 755 | } else { |
| 756 | int deltaY = (int) y - handle.getTop() - (handle.getHeight() / 2); |
| 757 | handle.offsetTopAndBottom(deltaY); |
| 758 | content.offsetTopAndBottom(deltaY); |
| 759 | } |
| 760 | invalidate(); // TODO: be more conservative about what we're invalidating |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Sets the left handle icon to a given resource. |
| 765 | * |
| 766 | * The resource should refer to a Drawable object, or use 0 to remove |
| 767 | * the icon. |
| 768 | * |
| 769 | * @param iconId the resource ID of the icon drawable |
| 770 | * @param targetId the resource of the target drawable |
| 771 | * @param barId the resource of the bar drawable (stateful) |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 772 | * @param tabId the resource of the |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 773 | */ |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 774 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 775 | public void setLeftTabResources(int iconId, int targetId, int barId, int tabId) { |
| 776 | mLeftSlider.setIcon(iconId); |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 777 | mLeftSlider.setTarget(targetId); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 778 | mLeftSlider.setBarBackgroundResource(barId); |
| 779 | mLeftSlider.setTabBackgroundResource(tabId); |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 780 | mLeftSlider.updateDrawableStates(); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Sets the left handle hint text to a given resource string. |
| 785 | * |
| 786 | * @param resId |
| 787 | */ |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 788 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 789 | public void setLeftHintText(int resId) { |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 790 | if (isHorizontal()) { |
| 791 | mLeftSlider.setHintText(resId); |
| 792 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Sets the right handle icon to a given resource. |
| 797 | * |
| 798 | * The resource should refer to a Drawable object, or use 0 to remove |
| 799 | * the icon. |
| 800 | * |
| 801 | * @param iconId the resource ID of the icon drawable |
| 802 | * @param targetId the resource of the target drawable |
| 803 | * @param barId the resource of the bar drawable (stateful) |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 804 | * @param tabId the resource of the |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 805 | */ |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 806 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 807 | public void setRightTabResources(int iconId, int targetId, int barId, int tabId) { |
| 808 | mRightSlider.setIcon(iconId); |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 809 | mRightSlider.setTarget(targetId); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 810 | mRightSlider.setBarBackgroundResource(barId); |
| 811 | mRightSlider.setTabBackgroundResource(tabId); |
Jim Miller | 4df2c54 | 2009-11-12 03:39:14 -0800 | [diff] [blame] | 812 | mRightSlider.updateDrawableStates(); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Sets the left handle hint text to a given resource string. |
| 817 | * |
| 818 | * @param resId |
| 819 | */ |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 820 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 821 | public void setRightHintText(int resId) { |
Jim Miller | 425ca59 | 2009-11-13 19:20:28 -0800 | [diff] [blame] | 822 | if (isHorizontal()) { |
| 823 | mRightSlider.setHintText(resId); |
| 824 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 825 | } |
| 826 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 827 | @UnsupportedAppUsage |
Jim Miller | 4f01d4a | 2009-11-16 23:08:35 -0800 | [diff] [blame] | 828 | public void setHoldAfterTrigger(boolean holdLeft, boolean holdRight) { |
| 829 | mHoldLeftOnTransition = holdLeft; |
| 830 | mHoldRightOnTransition = holdRight; |
| 831 | } |
| 832 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 833 | /** |
| 834 | * Triggers haptic feedback. |
| 835 | */ |
| 836 | private synchronized void vibrate(long duration) { |
Jeff Sharkey | 723a725 | 2012-10-12 14:26:31 -0700 | [diff] [blame] | 837 | final boolean hapticEnabled = Settings.System.getIntForUser( |
| 838 | mContext.getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 1, |
| 839 | UserHandle.USER_CURRENT) != 0; |
| 840 | if (hapticEnabled) { |
| 841 | if (mVibrator == null) { |
| 842 | mVibrator = (android.os.Vibrator) getContext() |
| 843 | .getSystemService(Context.VIBRATOR_SERVICE); |
| 844 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 845 | mVibrator.vibrate(duration, VIBRATION_ATTRIBUTES); |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 846 | } |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Registers a callback to be invoked when the user triggers an event. |
| 851 | * |
| 852 | * @param listener the OnDialTriggerListener to attach to this view |
| 853 | */ |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 854 | @UnsupportedAppUsage |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 855 | public void setOnTriggerListener(OnTriggerListener listener) { |
| 856 | mOnTriggerListener = listener; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Dispatches a trigger event to listener. Ignored if a listener is not set. |
| 861 | * @param whichHandle the handle that triggered the event. |
| 862 | */ |
| 863 | private void dispatchTriggerEvent(int whichHandle) { |
| 864 | vibrate(VIBRATE_LONG); |
| 865 | if (mOnTriggerListener != null) { |
| 866 | mOnTriggerListener.onTrigger(this, whichHandle); |
| 867 | } |
| 868 | } |
| 869 | |
Jim Miller | 92c1571 | 2010-05-25 19:09:44 -0700 | [diff] [blame] | 870 | @Override |
| 871 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 872 | super.onVisibilityChanged(changedView, visibility); |
| 873 | // When visibility changes and the user has a tab selected, unselect it and |
| 874 | // make sure their callback gets called. |
| 875 | if (changedView == this && visibility != VISIBLE |
| 876 | && mGrabbedState != OnTriggerListener.NO_HANDLE) { |
| 877 | cancelGrab(); |
| 878 | } |
| 879 | } |
| 880 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 881 | /** |
| 882 | * Sets the current grabbed state, and dispatches a grabbed state change |
| 883 | * event to our listener. |
| 884 | */ |
| 885 | private void setGrabbedState(int newState) { |
| 886 | if (newState != mGrabbedState) { |
| 887 | mGrabbedState = newState; |
| 888 | if (mOnTriggerListener != null) { |
| 889 | mOnTriggerListener.onGrabbedStateChange(this, mGrabbedState); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
Jim Miller | 24ccf3c | 2009-11-10 21:37:45 -0800 | [diff] [blame] | 894 | private void log(String msg) { |
| 895 | Log.d(LOG_TAG, msg); |
| 896 | } |
| 897 | } |