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