Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.widget; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
| 21 | import android.animation.ValueAnimator; |
| 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.drawable.BitmapDrawable; |
| 28 | import android.os.Vibrator; |
| 29 | import android.util.AttributeSet; |
| 30 | import android.util.Log; |
| 31 | import android.view.MotionEvent; |
| 32 | import android.view.View; |
| 33 | |
| 34 | import com.android.internal.R; |
| 35 | |
| 36 | /** |
| 37 | * A special widget containing a center and outer ring. Moving the center ring to the outer ring |
| 38 | * causes an event that can be caught by implementing OnTriggerListener. |
| 39 | */ |
| 40 | public class WaveView extends View implements ValueAnimator.AnimatorUpdateListener { |
| 41 | private static final String TAG = "WaveView"; |
| 42 | private static final boolean DBG = false; |
| 43 | private static final int WAVE_COUNT = 5; // default wave count |
| 44 | private static final long VIBRATE_SHORT = 20; // msec |
| 45 | private static final long VIBRATE_LONG = 20; // msec |
| 46 | |
| 47 | // Lock state machine states |
| 48 | private static final int STATE_RESET_LOCK = 0; |
| 49 | private static final int STATE_READY = 1; |
| 50 | private static final int STATE_START_ATTEMPT = 2; |
| 51 | private static final int STATE_ATTEMPTING = 3; |
| 52 | private static final int STATE_UNLOCK_ATTEMPT = 4; |
| 53 | private static final int STATE_UNLOCK_SUCCESS = 5; |
| 54 | |
| 55 | // Animation properties. |
| 56 | private static final long DURATION = 500; // duration of transitional animations |
| 57 | private static final long FINAL_DELAY = 1300; // delay for final animations |
| 58 | private static final long SHORT_DELAY = 100; // for starting one animation after another. |
| 59 | private static final long WAVE_DURATION = 2000; // amount of time for way to expand/decay |
| 60 | private static final long RESET_TIMEOUT = 3000; // elapsed time of inactivity before we reset |
| 61 | private static final long DELAY_INCREMENT = 15; // increment per wave while tracking motion |
| 62 | private static final long DELAY_INCREMENT2 = 12; // increment per wave while not tracking |
| 63 | |
| 64 | private Vibrator mVibrator; |
| 65 | private OnTriggerListener mOnTriggerListener; |
| 66 | private ArrayList<DrawableHolder> mDrawables = new ArrayList<DrawableHolder>(3); |
| 67 | private ArrayList<DrawableHolder> mLightWaves = new ArrayList<DrawableHolder>(WAVE_COUNT); |
| 68 | private boolean mFingerDown = false; |
| 69 | private float mRingRadius = 182.0f; // Radius of bitmap ring. Used to snap halo to it |
| 70 | private int mSnapRadius = 136; // minimum threshold for drag unlock |
| 71 | private int mWaveDelay = 240; // time to delay |
| 72 | private int mWaveCount = WAVE_COUNT; // number of waves |
| 73 | private long mWaveTimerDelay = mWaveDelay; |
| 74 | private int mCurrentWave = 0; |
| 75 | private float mLockCenterX; // center of widget as dictated by widget size |
| 76 | private float mLockCenterY; |
| 77 | private float mMouseX; // current mouse position as of last touch event |
| 78 | private float mMouseY; |
| 79 | private DrawableHolder mUnlockRing; |
| 80 | private DrawableHolder mUnlockDefault; |
| 81 | private DrawableHolder mUnlockHalo; |
| 82 | private int mLockState = STATE_RESET_LOCK; |
| 83 | private int mGrabbedState = OnTriggerListener.NO_HANDLE; |
| 84 | |
| 85 | public WaveView(Context context) { |
| 86 | this(context, null); |
| 87 | } |
| 88 | |
| 89 | public WaveView(Context context, AttributeSet attrs) { |
| 90 | super(context, attrs); |
| 91 | |
| 92 | // TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WaveView); |
| 93 | // mOrientation = a.getInt(R.styleable.WaveView_orientation, HORIZONTAL); |
| 94 | // a.recycle(); |
| 95 | |
| 96 | initDrawables(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 101 | mLockCenterX = 0.5f * w; |
| 102 | mLockCenterY = 0.5f * h; |
| 103 | super.onSizeChanged(w, h, oldw, oldh); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected int getSuggestedMinimumWidth() { |
| 108 | // View should be large enough to contain the unlock ring + halo |
| 109 | return mUnlockRing.getWidth() + mUnlockHalo.getWidth(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected int getSuggestedMinimumHeight() { |
| 114 | // View should be large enough to contain the unlock ring + halo |
| 115 | return mUnlockRing.getHeight() + mUnlockHalo.getHeight(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 120 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 121 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 122 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 123 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 124 | int width; |
| 125 | int height; |
| 126 | |
| 127 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
| 128 | width = Math.min(widthSpecSize, getSuggestedMinimumWidth()); |
| 129 | } else if (widthSpecMode == MeasureSpec.EXACTLY) { |
| 130 | width = widthSpecSize; |
| 131 | } else { |
| 132 | width = getSuggestedMinimumWidth(); |
| 133 | } |
| 134 | |
| 135 | if (heightSpecMode == MeasureSpec.AT_MOST) { |
| 136 | height = Math.min(heightSpecSize, getSuggestedMinimumWidth()); |
| 137 | } else if (heightSpecMode == MeasureSpec.EXACTLY) { |
| 138 | height = heightSpecSize; |
| 139 | } else { |
| 140 | height = getSuggestedMinimumHeight(); |
| 141 | } |
| 142 | |
| 143 | setMeasuredDimension(width, height); |
| 144 | } |
| 145 | |
| 146 | private void initDrawables() { |
| 147 | mUnlockRing = new DrawableHolder(createDrawable(R.drawable.unlock_ring)) |
| 148 | .setX(mLockCenterX).setY(mLockCenterY).setScaleX(0.1f).setScaleY(0.1f).setAlpha(0.0f); |
| 149 | mDrawables.add(mUnlockRing); |
| 150 | |
| 151 | mUnlockDefault = new DrawableHolder(createDrawable(R.drawable.unlock_default)) |
| 152 | .setX(mLockCenterX).setY(mLockCenterY).setScaleX(0.1f).setScaleY(0.1f).setAlpha(0.0f); |
| 153 | mDrawables.add(mUnlockDefault); |
| 154 | |
| 155 | mUnlockHalo = new DrawableHolder(createDrawable(R.drawable.unlock_halo)) |
| 156 | .setX(mLockCenterX).setY(mLockCenterY).setScaleX(0.1f).setScaleY(0.1f).setAlpha(0.0f); |
| 157 | mDrawables.add(mUnlockHalo); |
| 158 | |
| 159 | BitmapDrawable wave = createDrawable(R.drawable.unlock_wave); |
| 160 | for (int i = 0; i < mWaveCount; i++) { |
| 161 | DrawableHolder holder = new DrawableHolder(wave); |
| 162 | mLightWaves.add(holder); |
| 163 | holder.setAlpha(0.0f); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private void waveUpdateFrame(float mouseX, float mouseY, boolean fingerDown) { |
| 168 | double distX = mouseX - mLockCenterX; |
| 169 | double distY = mouseY - mLockCenterY; |
| 170 | int dragDistance = (int) Math.ceil(Math.hypot(distX, distY)); |
| 171 | double touchA = Math.atan2(distX, distY); |
| 172 | float ringX = (float) (mLockCenterX + mRingRadius * Math.sin(touchA)); |
| 173 | float ringY = (float) (mLockCenterY + mRingRadius * Math.cos(touchA)); |
| 174 | |
| 175 | switch (mLockState) { |
| 176 | case STATE_RESET_LOCK: |
| 177 | if (DBG) Log.v(TAG, "State RESET_LOCK"); |
| 178 | mWaveTimerDelay = mWaveDelay; |
| 179 | for (int i = 0; i < mLightWaves.size(); i++) { |
| 180 | //TweenMax.to(mLightWave.get(i), .3, {alpha:0, ease:Quint.easeOut}); |
| 181 | DrawableHolder holder = mLightWaves.get(i); |
| 182 | holder.addAnimTo(300, 0, "alpha", 0.0f, false); |
| 183 | } |
| 184 | for (int i = 0; i < mLightWaves.size(); i++) { |
| 185 | mLightWaves.get(i).startAnimations(this); |
| 186 | } |
| 187 | |
| 188 | //TweenMax.to(unlockRing, .5, { x: lockX, y: lockY, scaleX: .1, scaleY: .1, |
| 189 | // alpha: 0, overwrite: true, ease:Quint.easeOut }); |
| 190 | mUnlockRing.addAnimTo(DURATION, 0, "x", mLockCenterX, true); |
| 191 | mUnlockRing.addAnimTo(DURATION, 0, "y", mLockCenterY, true); |
| 192 | mUnlockRing.addAnimTo(DURATION, 0, "scaleX", 0.1f, true); |
| 193 | mUnlockRing.addAnimTo(DURATION, 0, "scaleY", 0.1f, true); |
| 194 | mUnlockRing.addAnimTo(DURATION, 0, "alpha", 0.0f, true); |
| 195 | |
| 196 | //TweenMax.to(unlockDefault, 0, { x: lockX, y: lockY, scaleX: .1, scaleY: .1, |
| 197 | // alpha: 0 , overwrite: true }); |
| 198 | mUnlockDefault.removeAnimationFor("x"); |
| 199 | mUnlockDefault.removeAnimationFor("y"); |
| 200 | mUnlockDefault.removeAnimationFor("scaleX"); |
| 201 | mUnlockDefault.removeAnimationFor("scaleY"); |
| 202 | mUnlockDefault.removeAnimationFor("alpha"); |
| 203 | mUnlockDefault.setX(mLockCenterX).setY(mLockCenterY).setScaleX(0.1f).setScaleY(0.1f) |
| 204 | .setAlpha(0.0f); |
| 205 | |
| 206 | //TweenMax.to(unlockDefault, .5, { delay: .1, scaleX: 1, scaleY: 1, |
| 207 | // alpha: 1, overwrite: true, ease:Quint.easeOut }); |
| 208 | mUnlockDefault.addAnimTo(DURATION, SHORT_DELAY, "scaleX", 1.0f, true); |
| 209 | mUnlockDefault.addAnimTo(DURATION, SHORT_DELAY, "scaleY", 1.0f, true); |
| 210 | mUnlockDefault.addAnimTo(DURATION, SHORT_DELAY, "alpha", 1.0f, true); |
| 211 | |
| 212 | //TweenMax.to(unlockHalo, 0, { x: lockX, y: lockY, scaleX:.1, scaleY: .1, |
| 213 | // alpha: 0 , overwrite: true }); |
| 214 | mUnlockHalo.removeAnimationFor("x"); |
| 215 | mUnlockHalo.removeAnimationFor("y"); |
| 216 | mUnlockHalo.removeAnimationFor("scaleX"); |
| 217 | mUnlockHalo.removeAnimationFor("scaleY"); |
| 218 | mUnlockHalo.removeAnimationFor("alpha"); |
| 219 | mUnlockHalo.setX(mLockCenterX).setY(mLockCenterY).setScaleX(0.1f).setScaleY(0.1f) |
| 220 | .setAlpha(0.0f); |
| 221 | |
| 222 | //TweenMax.to(unlockHalo, .5, { x: lockX, y: lockY, scaleX: 1, scaleY: 1, |
| 223 | // alpha: 1 , overwrite: true, ease:Quint.easeOut }); |
| 224 | mUnlockHalo.addAnimTo(DURATION, SHORT_DELAY, "x", mLockCenterX, true); |
| 225 | mUnlockHalo.addAnimTo(DURATION, SHORT_DELAY, "y", mLockCenterY, true); |
| 226 | mUnlockHalo.addAnimTo(DURATION, SHORT_DELAY, "scaleX", 1.0f, true); |
| 227 | mUnlockHalo.addAnimTo(DURATION, SHORT_DELAY, "scaleY", 1.0f, true); |
| 228 | mUnlockHalo.addAnimTo(DURATION, SHORT_DELAY, "alpha", 1.0f, true); |
| 229 | |
| 230 | //lockTimer.stop(); |
| 231 | removeCallbacks(mLockTimerActions); |
| 232 | |
| 233 | mLockState = STATE_READY; |
| 234 | break; |
| 235 | |
| 236 | case STATE_READY: |
| 237 | if (DBG) Log.v(TAG, "State READY"); |
| 238 | break; |
| 239 | |
| 240 | case STATE_START_ATTEMPT: |
| 241 | if (DBG) Log.v(TAG, "State START_ATTEMPT"); |
| 242 | //TweenMax.to(unlockDefault, 0, {scaleX: .1, scaleY:.1, alpha: 0, |
| 243 | // x:lockX +182, y: lockY , overwrite: true }); |
| 244 | mUnlockDefault.removeAnimationFor("x"); |
| 245 | mUnlockDefault.removeAnimationFor("y"); |
| 246 | mUnlockDefault.removeAnimationFor("scaleX"); |
| 247 | mUnlockDefault.removeAnimationFor("scaleY"); |
| 248 | mUnlockDefault.removeAnimationFor("alpha"); |
| 249 | mUnlockDefault.setX(mLockCenterX + 182).setY(mLockCenterY).setScaleX(0.1f) |
| 250 | .setScaleY(0.1f).setAlpha(0.0f); |
| 251 | |
| 252 | //TweenMax.to(unlockDefault, 0.5, { delay: .1 , scaleX: 1, scaleY: 1, |
| 253 | // alpha: 1, ease:Quint.easeOut }); |
| 254 | mUnlockDefault.addAnimTo(DURATION, SHORT_DELAY, "scaleX", 1.0f, false); |
| 255 | mUnlockDefault.addAnimTo(DURATION, SHORT_DELAY, "scaleY", 1.0f, false); |
| 256 | mUnlockDefault.addAnimTo(DURATION, SHORT_DELAY, "alpha", 1.0f, false); |
| 257 | |
| 258 | //TweenMax.to(unlockRing, 0.5, {scaleX: 1, scaleY: 1, |
| 259 | // alpha: 1, ease:Quint.easeOut, overwrite: true }); |
| 260 | mUnlockRing.addAnimTo(DURATION, 0, "scaleX", 1.0f, true); |
| 261 | mUnlockRing.addAnimTo(DURATION, 0, "scaleY", 1.0f, true); |
| 262 | mUnlockRing.addAnimTo(DURATION, 0, "alpha", 1.0f, true); |
| 263 | |
| 264 | postDelayed(mAddWaveAction, mWaveTimerDelay); |
| 265 | |
| 266 | mLockState = STATE_ATTEMPTING; |
| 267 | break; |
| 268 | |
| 269 | case STATE_ATTEMPTING: |
| 270 | if (DBG) Log.v(TAG, "State ATTEMPTING"); |
| 271 | //TweenMax.to(unlockHalo, 0.4, { x:mouseX, y:mouseY, scaleX:1, scaleY:1, |
| 272 | // alpha: 1, ease:Quint.easeOut }); |
| 273 | if (dragDistance > mSnapRadius) { |
| 274 | if (fingerDown) { |
| 275 | //TweenMax.to(unlockHalo, 0.4, {x:ringX, y:ringY, scaleX:1, scaleY:1, |
| 276 | // alpha: 1 , ease:Quint.easeOut , overwrite: true }); |
| 277 | mUnlockHalo.addAnimTo(0, 0, "x", ringX, true); |
| 278 | mUnlockHalo.addAnimTo(0, 0, "y", ringY, true); |
| 279 | mUnlockHalo.addAnimTo(0, 0, "scaleX", 1.0f, true); |
| 280 | mUnlockHalo.addAnimTo(0, 0, "scaleY", 1.0f, true); |
| 281 | mUnlockHalo.addAnimTo(0, 0, "alpha", 1.0f, true); |
| 282 | } else { |
| 283 | mLockState = STATE_UNLOCK_ATTEMPT; |
| 284 | } |
| 285 | } else { |
| 286 | mUnlockHalo.addAnimTo(0, 0, "x", mouseX, true); |
| 287 | mUnlockHalo.addAnimTo(0, 0, "y", mouseY, true); |
| 288 | mUnlockHalo.addAnimTo(0, 0, "scaleX", 1.0f, true); |
| 289 | mUnlockHalo.addAnimTo(0, 0, "scaleY", 1.0f, true); |
| 290 | mUnlockHalo.addAnimTo(0, 0, "alpha", 1.0f, true); |
| 291 | } |
| 292 | break; |
| 293 | |
| 294 | case STATE_UNLOCK_ATTEMPT: |
| 295 | if (DBG) Log.v(TAG, "State UNLOCK_ATTEMPT"); |
| 296 | if (dragDistance > mSnapRadius) { |
| 297 | for (int n = 0; n < mLightWaves.size(); n++) { |
| 298 | //TweenMax.to(this["lightWave"+n], .5,{alpha:0, delay: (6+n-currentWave)*.1, |
| 299 | // x:ringX, y:ringY, scaleX: .1, scaleY: .1, ease:Quint.easeOut}); |
| 300 | DrawableHolder wave = mLightWaves.get(n); |
| 301 | long delay = 1000L*(6 + n - mCurrentWave)/10L; |
| 302 | wave.addAnimTo(DURATION, delay, "x", ringX, true); |
| 303 | wave.addAnimTo(DURATION, delay, "y", ringY, true); |
| 304 | wave.addAnimTo(DURATION, delay, "scaleX", 0.1f, true); |
| 305 | wave.addAnimTo(DURATION, delay, "scaleY", 0.1f, true); |
| 306 | wave.addAnimTo(DURATION, delay, "alpha", 0.0f, true); |
| 307 | } |
| 308 | for (int i = 0; i < mLightWaves.size(); i++) { |
| 309 | mLightWaves.get(i).startAnimations(this); |
| 310 | } |
| 311 | |
| 312 | //TweenMax.to(unlockRing, .5, {x:ringX, y: ringY, scaleX: .1, scaleY: .1, |
| 313 | // alpha: 0, ease: Quint.easeOut }); |
| 314 | mUnlockRing.addAnimTo(DURATION, 0, "x", ringX, false); |
| 315 | mUnlockRing.addAnimTo(DURATION, 0, "y", ringY, false); |
| 316 | mUnlockRing.addAnimTo(DURATION, 0, "scaleX", 0.1f, false); |
| 317 | mUnlockRing.addAnimTo(DURATION, 0, "scaleY", 0.1f, false); |
| 318 | mUnlockRing.addAnimTo(DURATION, 0, "alpha", 0.0f, false); |
| 319 | |
| 320 | //TweenMax.to(unlockRing, .5, { delay: 1.3, alpha: 0 , ease: Quint.easeOut }); |
| 321 | mUnlockRing.addAnimTo(DURATION, FINAL_DELAY, "alpha", 0.0f, false); |
| 322 | |
| 323 | //TweenMax.to(unlockDefault, 0, { x:ringX, y: ringY, scaleX: .1, scaleY: .1, |
| 324 | // alpha: 0 , overwrite: true }); |
| 325 | mUnlockDefault.removeAnimationFor("x"); |
| 326 | mUnlockDefault.removeAnimationFor("y"); |
| 327 | mUnlockDefault.removeAnimationFor("scaleX"); |
| 328 | mUnlockDefault.removeAnimationFor("scaleY"); |
| 329 | mUnlockDefault.removeAnimationFor("alpha"); |
| 330 | mUnlockDefault.setX(ringX).setY(ringY).setScaleX(0.1f).setScaleY(0.1f) |
| 331 | .setAlpha(0.0f); |
| 332 | |
| 333 | //TweenMax.to(unlockDefault, .5, { x:ringX, y: ringY, scaleX: 1, scaleY: 1, |
| 334 | // alpha: 1 , ease: Quint.easeOut , overwrite: true }); |
| 335 | mUnlockDefault.addAnimTo(DURATION, 0, "x", ringX, true); |
| 336 | mUnlockDefault.addAnimTo(DURATION, 0, "y", ringY, true); |
| 337 | mUnlockDefault.addAnimTo(DURATION, 0, "scaleX", 1.0f, true); |
| 338 | mUnlockDefault.addAnimTo(DURATION, 0, "scaleY", 1.0f, true); |
| 339 | mUnlockDefault.addAnimTo(DURATION, 0, "alpha", 1.0f, true); |
| 340 | |
| 341 | //TweenMax.to(unlockDefault, .5, { delay: 1.3, scaleX: 3, scaleY: 3, |
| 342 | // alpha: 1, ease: Quint.easeOut }); |
| 343 | mUnlockDefault.addAnimTo(DURATION, FINAL_DELAY, "scaleX", 3.0f, false); |
| 344 | mUnlockDefault.addAnimTo(DURATION, FINAL_DELAY, "scaleY", 3.0f, false); |
| 345 | mUnlockDefault.addAnimTo(DURATION, FINAL_DELAY, "alpha", 1.0f, false); |
| 346 | |
| 347 | //TweenMax.to(unlockHalo, .5, { x:ringX, y: ringY , ease: Back.easeOut }); |
| 348 | mUnlockHalo.addAnimTo(DURATION, 0, "x", ringX, false); |
| 349 | mUnlockHalo.addAnimTo(DURATION, 0, "y", ringY, false); |
| 350 | |
| 351 | //TweenMax.to(unlockHalo, .5, { delay: 1.3, scaleX: 3, scaleY: 3, |
| 352 | // alpha: 1, ease: Quint.easeOut }); |
| 353 | mUnlockHalo.addAnimTo(DURATION, FINAL_DELAY, "scaleX", 3.0f, false); |
| 354 | mUnlockHalo.addAnimTo(DURATION, FINAL_DELAY, "scaleY", 3.0f, false); |
| 355 | mUnlockHalo.addAnimTo(DURATION, FINAL_DELAY, "alpha", 1.0f, false); |
| 356 | |
| 357 | removeCallbacks(mLockTimerActions); |
| 358 | |
| 359 | postDelayed(mLockTimerActions, RESET_TIMEOUT); |
| 360 | |
| 361 | dispatchTriggerEvent(OnTriggerListener.CENTER_HANDLE); |
| 362 | mLockState = STATE_UNLOCK_SUCCESS; |
| 363 | } else { |
| 364 | mLockState = STATE_RESET_LOCK; |
| 365 | } |
| 366 | break; |
| 367 | |
| 368 | case STATE_UNLOCK_SUCCESS: |
| 369 | if (DBG) Log.v(TAG, "State UNLOCK_SUCCESS"); |
| 370 | removeCallbacks(mAddWaveAction); |
| 371 | break; |
| 372 | |
| 373 | default: |
| 374 | if (DBG) Log.v(TAG, "Unknown state " + mLockState); |
| 375 | break; |
| 376 | } |
| 377 | mUnlockDefault.startAnimations(this); |
| 378 | mUnlockHalo.startAnimations(this); |
| 379 | mUnlockRing.startAnimations(this); |
| 380 | } |
| 381 | |
| 382 | BitmapDrawable createDrawable(int resId) { |
| 383 | Resources res = getResources(); |
| 384 | Bitmap bitmap = BitmapFactory.decodeResource(res, resId); |
| 385 | return new BitmapDrawable(res, bitmap); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | protected void onDraw(Canvas canvas) { |
| 390 | waveUpdateFrame(mMouseX, mMouseY, mFingerDown); |
| 391 | for (int i = 0; i < mDrawables.size(); ++i) { |
| 392 | mDrawables.get(i).draw(canvas); |
| 393 | } |
| 394 | for (int i = 0; i < mLightWaves.size(); ++i) { |
| 395 | mLightWaves.get(i).draw(canvas); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | private final Runnable mLockTimerActions = new Runnable() { |
| 400 | public void run() { |
| 401 | if (DBG) Log.v(TAG, "LockTimerActions"); |
| 402 | // reset lock after inactivity |
| 403 | if (mLockState == STATE_ATTEMPTING) { |
| 404 | mLockState = STATE_RESET_LOCK; |
| 405 | } |
| 406 | // for prototype, reset after successful unlock |
| 407 | if (mLockState == STATE_UNLOCK_SUCCESS) { |
| 408 | mLockState = STATE_RESET_LOCK; |
| 409 | } |
| 410 | invalidate(); |
| 411 | } |
| 412 | }; |
| 413 | |
| 414 | private final Runnable mAddWaveAction = new Runnable() { |
| 415 | public void run() { |
| 416 | double distX = mMouseX - mLockCenterX; |
| 417 | double distY = mMouseY - mLockCenterY; |
| 418 | int dragDistance = (int) Math.ceil(Math.hypot(distX, distY)); |
| 419 | if (mLockState == STATE_ATTEMPTING && dragDistance < mSnapRadius |
| 420 | && mWaveTimerDelay >= mWaveDelay) { |
| 421 | mWaveTimerDelay = Math.min(WAVE_DURATION, mWaveTimerDelay + DELAY_INCREMENT); |
| 422 | |
| 423 | DrawableHolder wave = mLightWaves.get(mCurrentWave); |
| 424 | wave.setAlpha(0.0f); |
| 425 | wave.setScaleX(0.2f); |
| 426 | wave.setScaleY(0.2f); |
| 427 | wave.setX(mMouseX); |
| 428 | wave.setY(mMouseY); |
| 429 | |
| 430 | //TweenMax.to(this["lightWave"+currentWave], 2, { x:lockX , y:lockY, alpha: 1.5, |
| 431 | // scaleX: 1, scaleY:1, ease:Cubic.easeOut}); |
| 432 | wave.addAnimTo(WAVE_DURATION, 0, "x", mLockCenterX, true); |
| 433 | wave.addAnimTo(WAVE_DURATION, 0, "y", mLockCenterY, true); |
| 434 | wave.addAnimTo(WAVE_DURATION*2/3, 0, "alpha", 1.0f, true); |
| 435 | wave.addAnimTo(WAVE_DURATION, 0, "scaleX", 1.0f, true); |
| 436 | wave.addAnimTo(WAVE_DURATION, 0, "scaleY", 1.0f, true); |
| 437 | |
| 438 | //TweenMax.to(this["lightWave"+currentWave], 1, { delay: 1.3 |
| 439 | // , alpha: 0 , ease:Quint.easeOut}); |
| 440 | wave.addAnimTo(1000, FINAL_DELAY, "alpha", 0.0f, false); |
| 441 | wave.startAnimations(WaveView.this); |
| 442 | |
| 443 | mCurrentWave = (mCurrentWave+1) % mWaveCount; |
| 444 | if (DBG) Log.v(TAG, "WaveTimerDelay: start new wave in " + mWaveTimerDelay); |
| 445 | postDelayed(mAddWaveAction, mWaveTimerDelay); |
| 446 | } else { |
| 447 | mWaveTimerDelay += DELAY_INCREMENT2; |
| 448 | } |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | @Override |
| 453 | public boolean onTouchEvent(MotionEvent event) { |
| 454 | final int action = event.getAction(); |
| 455 | mMouseX = event.getX(); |
| 456 | mMouseY = event.getY(); |
| 457 | boolean handled = false; |
| 458 | switch (action) { |
| 459 | case MotionEvent.ACTION_DOWN: |
| 460 | removeCallbacks(mLockTimerActions); |
| 461 | mFingerDown = true; |
| 462 | setGrabbedState(OnTriggerListener.CENTER_HANDLE); |
| 463 | { |
| 464 | float x = mMouseX - mUnlockHalo.getX(); |
| 465 | float y = mMouseY - mUnlockHalo.getY(); |
| 466 | float dist = (float) Math.hypot(x, y); |
| 467 | if (dist < mUnlockHalo.getWidth()*0.5f) { |
| 468 | if (mLockState == STATE_READY) { |
| 469 | mLockState = STATE_START_ATTEMPT; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | handled = true; |
| 474 | break; |
| 475 | |
| 476 | case MotionEvent.ACTION_MOVE: |
| 477 | handled = true; |
| 478 | break; |
| 479 | |
| 480 | case MotionEvent.ACTION_UP: |
| 481 | mFingerDown = false; |
| 482 | postDelayed(mLockTimerActions, RESET_TIMEOUT); |
| 483 | setGrabbedState(OnTriggerListener.NO_HANDLE); |
| 484 | handled = true; |
| 485 | break; |
| 486 | |
| 487 | case MotionEvent.ACTION_CANCEL: |
| 488 | mFingerDown = false; |
| 489 | handled = true; |
| 490 | break; |
| 491 | } |
| 492 | invalidate(); |
| 493 | return handled ? true : super.onTouchEvent(event); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Triggers haptic feedback. |
| 498 | */ |
| 499 | private synchronized void vibrate(long duration) { |
| 500 | if (mVibrator == null) { |
| 501 | mVibrator = (android.os.Vibrator) |
| 502 | getContext().getSystemService(Context.VIBRATOR_SERVICE); |
| 503 | } |
| 504 | mVibrator.vibrate(duration); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Registers a callback to be invoked when the user triggers an event. |
| 509 | * |
| 510 | * @param listener the OnDialTriggerListener to attach to this view |
| 511 | */ |
| 512 | public void setOnTriggerListener(OnTriggerListener listener) { |
| 513 | mOnTriggerListener = listener; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Dispatches a trigger event to listener. Ignored if a listener is not set. |
| 518 | * @param whichHandle the handle that triggered the event. |
| 519 | */ |
| 520 | private void dispatchTriggerEvent(int whichHandle) { |
| 521 | vibrate(VIBRATE_LONG); |
| 522 | if (mOnTriggerListener != null) { |
| 523 | mOnTriggerListener.onTrigger(this, whichHandle); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Sets the current grabbed state, and dispatches a grabbed state change |
| 529 | * event to our listener. |
| 530 | */ |
| 531 | private void setGrabbedState(int newState) { |
| 532 | if (newState != mGrabbedState) { |
| 533 | mGrabbedState = newState; |
| 534 | if (mOnTriggerListener != null) { |
| 535 | mOnTriggerListener.onGrabbedStateChange(this, mGrabbedState); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | public interface OnTriggerListener { |
| 541 | /** |
| 542 | * Sent when the user releases the handle. |
| 543 | */ |
| 544 | public static final int NO_HANDLE = 0; |
| 545 | |
| 546 | /** |
| 547 | * Sent when the user grabs the center handle |
| 548 | */ |
| 549 | public static final int CENTER_HANDLE = 10; |
| 550 | |
| 551 | /** |
| 552 | * Called when the user drags the center ring beyond a threshold. |
| 553 | */ |
| 554 | void onTrigger(View v, int whichHandle); |
| 555 | |
| 556 | /** |
| 557 | * Called when the "grabbed state" changes (i.e. when the user either grabs or releases |
| 558 | * one of the handles.) |
| 559 | * |
| 560 | * @param v the view that was triggered |
| 561 | * @param grabbedState the new state: {@link #NO_HANDLE}, {@link #CENTER_HANDLE}, |
| 562 | */ |
| 563 | void onGrabbedStateChange(View v, int grabbedState); |
| 564 | } |
| 565 | |
| 566 | public void onAnimationUpdate(ValueAnimator animation) { |
| 567 | invalidate(); |
| 568 | } |
| 569 | |
| 570 | public void reset() { |
| 571 | mLockState = STATE_RESET_LOCK; |
| 572 | } |
| 573 | } |