From 09cf5a89303ccb824906c68d17d205a9da6a2e6c Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sat, 27 Aug 2011 15:37:04 -0500 Subject: [PATCH] Handle CoverView long-presses better. This treats them the same way as normal presses: they can be activated with up to a small amount of movement. Also executes scroll-reset code even when a press is detected. Before small movements were not being reset. --- src/org/kreed/vanilla/CoverView.java | 79 +++++++++++++++------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/src/org/kreed/vanilla/CoverView.java b/src/org/kreed/vanilla/CoverView.java index 29d087f1..da652dfa 100644 --- a/src/org/kreed/vanilla/CoverView.java +++ b/src/org/kreed/vanilla/CoverView.java @@ -220,26 +220,25 @@ public final class CoverView extends View implements Handler.Callback { { if (mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain(); - mVelocityTracker.addMovement(ev); + mVelocityTracker.addMovement(ev); - mHandler.removeMessages(MSG_LONG_CLICK); + float x = ev.getX(); + float y = ev.getY(); + int scrollX = getScrollX(); + int width = getWidth(); - float x = ev.getX(); - int scrollX = getScrollX(); - int width = getWidth(); - - switch (ev.getAction()) { - case MotionEvent.ACTION_DOWN: - if (!mScroller.isFinished()) + switch (ev.getAction()) { + case MotionEvent.ACTION_DOWN: + if (!mScroller.isFinished()) mScroller.abortAnimation(); - mStartX = x; - mStartY = ev.getY(); + mStartX = x; + mStartY = y; mLastMotionX = x; mHandler.sendEmptyMessageDelayed(MSG_LONG_CLICK, ViewConfiguration.getLongPressTimeout()); break; - case MotionEvent.ACTION_MOVE: + case MotionEvent.ACTION_MOVE: { int deltaX = (int) (mLastMotionX - x); mLastMotionX = x; @@ -252,46 +251,52 @@ public final class CoverView extends View implements Handler.Callback { if (availableToScroll > 0) scrollBy(Math.min(availableToScroll, deltaX), 0); } + + if (Math.abs(mStartX - x) + Math.abs(mStartY - y) < 10) + mHandler.removeMessages(MSG_LONG_CLICK); break; - case MotionEvent.ACTION_UP: - if (Math.abs(mStartX - x) + Math.abs(mStartY - ev.getY()) < 10) { + } + case MotionEvent.ACTION_UP: { + mHandler.removeMessages(MSG_LONG_CLICK); + if (Math.abs(mStartX - x) + Math.abs(mStartY - y) < 10) { // A long press was performed and thus the normal action should // not be executed. if (mIgnoreNextUp) mIgnoreNextUp = false; else performClick(); - } else { - VelocityTracker velocityTracker = mVelocityTracker; - velocityTracker.computeCurrentVelocity(250); - int velocity = (int) velocityTracker.getXVelocity(); - - int min = mTimelinePos == 0 ? 1 : 0; - int max = 2; - int nearestCover = (scrollX + width / 2) / width; - int whichCover = Math.max(min, Math.min(nearestCover, max)); - - if (velocity > SNAP_VELOCITY && whichCover != min) - --whichCover; - else if (velocity < -SNAP_VELOCITY && whichCover != max) - ++whichCover; - - int newX = whichCover * width; - int delta = newX - scrollX; - mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 2); - if (whichCover != 1) - mTentativeCover = whichCover; - - postInvalidate(); } + VelocityTracker velocityTracker = mVelocityTracker; + velocityTracker.computeCurrentVelocity(250); + int velocity = (int) velocityTracker.getXVelocity(); + + int min = mTimelinePos == 0 ? 1 : 0; + int max = 2; + int nearestCover = (scrollX + width / 2) / width; + int whichCover = Math.max(min, Math.min(nearestCover, max)); + + if (velocity > SNAP_VELOCITY && whichCover != min) + --whichCover; + else if (velocity < -SNAP_VELOCITY && whichCover != max) + ++whichCover; + + int newX = whichCover * width; + int delta = newX - scrollX; + mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 2); + if (whichCover != 1) + mTentativeCover = whichCover; + + postInvalidate(); + if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; - } + } + } return true; }