diff options
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetector.java')
-rw-r--r-- | library/main/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetector.java | 147 |
1 files changed, 69 insertions, 78 deletions
diff --git a/library/main/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetector.java b/library/main/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetector.java index f438691..2ea5288 100644 --- a/library/main/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetector.java +++ b/library/main/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetector.java @@ -24,95 +24,86 @@ import android.view.ViewConfiguration; /** * Helper class to detect the consective-tap gestures on a view. * - * <p/>This class is instantiated and used similar to a GestureDetector, where onTouchEvent should - * be called when there are MotionEvents this detector should know about. + * <p>This class is instantiated and used similar to a GestureDetector, where onTouchEvent should be + * called when there are MotionEvents this detector should know about. */ public final class ConsecutiveTapsGestureDetector { - public interface OnConsecutiveTapsListener { - /** - * Callback method when the user tapped on the target view X number of times. - */ - void onConsecutiveTaps(int numOfConsecutiveTaps); - } - - private final View mView; - private final OnConsecutiveTapsListener mListener; - private final int mConsecutiveTapTouchSlopSquare; - private final int mConsecutiveTapTimeout; + public interface OnConsecutiveTapsListener { + /** Callback method when the user tapped on the target view X number of times. */ + void onConsecutiveTaps(int numOfConsecutiveTaps); + } - private int mConsecutiveTapsCounter = 0; - private MotionEvent mPreviousTapEvent; + private final View view; + private final OnConsecutiveTapsListener listener; + private final int consecutiveTapTouchSlopSquare; + private final int consecutiveTapTimeout; - /** - * @param listener The listener that responds to the gesture. - * @param view The target view that associated with consecutive-tap gesture. - */ - public ConsecutiveTapsGestureDetector( - OnConsecutiveTapsListener listener, - View view) { - mListener = listener; - mView = view; - int doubleTapSlop = ViewConfiguration.get(mView.getContext()).getScaledDoubleTapSlop(); - mConsecutiveTapTouchSlopSquare = doubleTapSlop * doubleTapSlop; - mConsecutiveTapTimeout = ViewConfiguration.getDoubleTapTimeout(); - } + private int consecutiveTapsCounter = 0; + private MotionEvent previousTapEvent; - /** - * This method should be called from the relevant activity or view, typically in - * onTouchEvent, onInterceptTouchEvent or dispatchTouchEvent. - * - * @param ev The motion event - */ - public void onTouchEvent(MotionEvent ev) { - if (ev.getAction() == MotionEvent.ACTION_UP) { - Rect viewRect = new Rect(); - int[] leftTop = new int[2]; - mView.getLocationOnScreen(leftTop); - viewRect.set( - leftTop[0], - leftTop[1], - leftTop[0] + mView.getWidth(), - leftTop[1] + mView.getHeight()); - if (viewRect.contains((int) ev.getX(), (int) ev.getY())) { - if (isConsecutiveTap(ev)) { - mConsecutiveTapsCounter++; - } else { - mConsecutiveTapsCounter = 1; - } - mListener.onConsecutiveTaps(mConsecutiveTapsCounter); - } else { - // Touch outside the target view. Reset counter. - mConsecutiveTapsCounter = 0; - } + /** + * @param listener The listener that responds to the gesture. + * @param view The target view that associated with consecutive-tap gesture. + */ + public ConsecutiveTapsGestureDetector(OnConsecutiveTapsListener listener, View view) { + this.listener = listener; + this.view = view; + int doubleTapSlop = ViewConfiguration.get(this.view.getContext()).getScaledDoubleTapSlop(); + consecutiveTapTouchSlopSquare = doubleTapSlop * doubleTapSlop; + consecutiveTapTimeout = ViewConfiguration.getDoubleTapTimeout(); + } - if (mPreviousTapEvent != null) { - mPreviousTapEvent.recycle(); - } - mPreviousTapEvent = MotionEvent.obtain(ev); + /** + * This method should be called from the relevant activity or view, typically in onTouchEvent, + * onInterceptTouchEvent or dispatchTouchEvent. + * + * @param ev The motion event + */ + public void onTouchEvent(MotionEvent ev) { + if (ev.getAction() == MotionEvent.ACTION_UP) { + Rect viewRect = new Rect(); + int[] leftTop = new int[2]; + view.getLocationOnScreen(leftTop); + viewRect.set( + leftTop[0], leftTop[1], leftTop[0] + view.getWidth(), leftTop[1] + view.getHeight()); + if (viewRect.contains((int) ev.getX(), (int) ev.getY())) { + if (isConsecutiveTap(ev)) { + consecutiveTapsCounter++; + } else { + consecutiveTapsCounter = 1; } - } + listener.onConsecutiveTaps(consecutiveTapsCounter); + } else { + // Touch outside the target view. Reset counter. + consecutiveTapsCounter = 0; + } - /** - * Resets the consecutive-tap counter to zero. - */ - public void resetCounter() { - mConsecutiveTapsCounter = 0; + if (previousTapEvent != null) { + previousTapEvent.recycle(); + } + previousTapEvent = MotionEvent.obtain(ev); } + } - /** - * Returns true if the distance between consecutive tap is within - * {@link #mConsecutiveTapTouchSlopSquare}. False, otherwise. - */ - private boolean isConsecutiveTap(MotionEvent currentTapEvent) { - if (mPreviousTapEvent == null) { - return false; - } + /** Resets the consecutive-tap counter to zero. */ + public void resetCounter() { + consecutiveTapsCounter = 0; + } - double deltaX = mPreviousTapEvent.getX() - currentTapEvent.getX(); - double deltaY = mPreviousTapEvent.getY() - currentTapEvent.getY(); - long deltaTime = currentTapEvent.getEventTime() - mPreviousTapEvent.getEventTime(); - return (deltaX * deltaX + deltaY * deltaY <= mConsecutiveTapTouchSlopSquare) - && deltaTime < mConsecutiveTapTimeout; + /** + * Returns true if the distance between consecutive tap is within {@link + * #consecutiveTapTouchSlopSquare}. False, otherwise. + */ + private boolean isConsecutiveTap(MotionEvent currentTapEvent) { + if (previousTapEvent == null) { + return false; } + + double deltaX = previousTapEvent.getX() - currentTapEvent.getX(); + double deltaY = previousTapEvent.getY() - currentTapEvent.getY(); + long deltaTime = currentTapEvent.getEventTime() - previousTapEvent.getEventTime(); + return (deltaX * deltaX + deltaY * deltaY <= consecutiveTapTouchSlopSquare) + && deltaTime < consecutiveTapTimeout; + } } |