blob: 33b3ff4fef59ed16b6aead4a12371cf22de3b8cf [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package android.view;
18
Mathew Inwooda570dee2018-08-17 14:56:00 +010019import android.annotation.UnsupportedAppUsage;
Adam Powell216bccf2010-02-01 15:03:17 -080020import android.content.Context;
Jackal Guo198a2622018-11-19 11:51:31 +080021import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.os.Handler;
23import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25/**
26 * Detects various gestures and events using the supplied {@link MotionEvent}s.
27 * The {@link OnGestureListener} callback will notify users when a particular
28 * motion event has occurred. This class should only be used with {@link MotionEvent}s
29 * reported via touch (don't use for trackball events).
30 *
31 * To use this class:
32 * <ul>
33 * <li>Create an instance of the {@code GestureDetector} for your {@link View}
34 * <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call
35 * {@link #onTouchEvent(MotionEvent)}. The methods defined in your callback
36 * will be executed when the events occur.
Mady Mellor015020e2015-06-05 14:41:22 -070037 * <li>If listening for {@link OnContextClickListener#onContextClick(MotionEvent)}
Mady Mellorb0933442015-05-27 14:15:57 -070038 * you must call {@link #onGenericMotionEvent(MotionEvent)}
39 * in {@link View#onGenericMotionEvent(MotionEvent)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 * </ul>
41 */
42public class GestureDetector {
43 /**
44 * The listener that is used to notify when gestures occur.
45 * If you want to listen for all the different gestures then implement
46 * this interface. If you only want to listen for a subset it might
47 * be easier to extend {@link SimpleOnGestureListener}.
48 */
49 public interface OnGestureListener {
50
51 /**
52 * Notified when a tap occurs with the down {@link MotionEvent}
53 * that triggered it. This will be triggered immediately for
54 * every down event. All other events should be preceded by this.
55 *
56 * @param e The down motion event.
57 */
58 boolean onDown(MotionEvent e);
59
60 /**
61 * The user has performed a down {@link MotionEvent} and not performed
62 * a move or up yet. This event is commonly used to provide visual
63 * feedback to the user to let them know that their action has been
64 * recognized i.e. highlight an element.
65 *
66 * @param e The down motion event
67 */
68 void onShowPress(MotionEvent e);
69
70 /**
71 * Notified when a tap occurs with the up {@link MotionEvent}
72 * that triggered it.
73 *
74 * @param e The up motion event that completed the first tap
75 * @return true if the event is consumed, else false
76 */
77 boolean onSingleTapUp(MotionEvent e);
78
79 /**
80 * Notified when a scroll occurs with the initial on down {@link MotionEvent} and the
81 * current move {@link MotionEvent}. The distance in x and y is also supplied for
82 * convenience.
83 *
84 * @param e1 The first down motion event that started the scrolling.
85 * @param e2 The move motion event that triggered the current onScroll.
86 * @param distanceX The distance along the X axis that has been scrolled since the last
87 * call to onScroll. This is NOT the distance between {@code e1}
88 * and {@code e2}.
89 * @param distanceY The distance along the Y axis that has been scrolled since the last
90 * call to onScroll. This is NOT the distance between {@code e1}
91 * and {@code e2}.
92 * @return true if the event is consumed, else false
93 */
94 boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
95
96 /**
97 * Notified when a long press occurs with the initial on down {@link MotionEvent}
98 * that trigged it.
99 *
100 * @param e The initial on down motion event that started the longpress.
101 */
102 void onLongPress(MotionEvent e);
103
104 /**
105 * Notified of a fling event when it occurs with the initial on down {@link MotionEvent}
106 * and the matching up {@link MotionEvent}. The calculated velocity is supplied along
107 * the x and y axis in pixels per second.
108 *
109 * @param e1 The first down motion event that started the fling.
110 * @param e2 The move motion event that triggered the current onFling.
111 * @param velocityX The velocity of this fling measured in pixels per second
112 * along the x axis.
113 * @param velocityY The velocity of this fling measured in pixels per second
114 * along the y axis.
115 * @return true if the event is consumed, else false
116 */
117 boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
118 }
119
120 /**
121 * The listener that is used to notify when a double-tap or a confirmed
122 * single-tap occur.
123 */
124 public interface OnDoubleTapListener {
125 /**
126 * Notified when a single-tap occurs.
127 * <p>
128 * Unlike {@link OnGestureListener#onSingleTapUp(MotionEvent)}, this
129 * will only be called after the detector is confident that the user's
130 * first tap is not followed by a second tap leading to a double-tap
131 * gesture.
132 *
133 * @param e The down motion event of the single-tap.
134 * @return true if the event is consumed, else false
135 */
136 boolean onSingleTapConfirmed(MotionEvent e);
137
138 /**
139 * Notified when a double-tap occurs.
140 *
141 * @param e The down motion event of the first tap of the double-tap.
142 * @return true if the event is consumed, else false
143 */
144 boolean onDoubleTap(MotionEvent e);
145
146 /**
147 * Notified when an event within a double-tap gesture occurs, including
148 * the down, move, and up events.
149 *
150 * @param e The motion event that occurred during the double-tap gesture.
151 * @return true if the event is consumed, else false
152 */
153 boolean onDoubleTapEvent(MotionEvent e);
154 }
155
156 /**
Mady Mellor015020e2015-06-05 14:41:22 -0700157 * The listener that is used to notify when a context click occurs. When listening for a
158 * context click ensure that you call {@link #onGenericMotionEvent(MotionEvent)} in
Mady Mellorb0933442015-05-27 14:15:57 -0700159 * {@link View#onGenericMotionEvent(MotionEvent)}.
Mady Mellora6b16452015-04-14 18:03:34 -0700160 */
Mady Mellor015020e2015-06-05 14:41:22 -0700161 public interface OnContextClickListener {
Mady Mellora6b16452015-04-14 18:03:34 -0700162 /**
Mady Mellor015020e2015-06-05 14:41:22 -0700163 * Notified when a context click occurs.
Mady Mellora6b16452015-04-14 18:03:34 -0700164 *
Mady Mellor015020e2015-06-05 14:41:22 -0700165 * @param e The motion event that occurred during the context click.
Mady Mellora6b16452015-04-14 18:03:34 -0700166 * @return true if the event is consumed, else false
167 */
Mady Mellor015020e2015-06-05 14:41:22 -0700168 boolean onContextClick(MotionEvent e);
Mady Mellora6b16452015-04-14 18:03:34 -0700169 }
170
171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 * A convenience class to extend when you only want to listen for a subset
173 * of all the gestures. This implements all methods in the
Mady Mellor015020e2015-06-05 14:41:22 -0700174 * {@link OnGestureListener}, {@link OnDoubleTapListener}, and {@link OnContextClickListener}
Mady Mellora6b16452015-04-14 18:03:34 -0700175 * but does nothing and return {@code false} for all applicable methods.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 */
Mady Mellora6b16452015-04-14 18:03:34 -0700177 public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener,
Mady Mellor015020e2015-06-05 14:41:22 -0700178 OnContextClickListener {
Mady Mellora6b16452015-04-14 18:03:34 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public boolean onSingleTapUp(MotionEvent e) {
181 return false;
182 }
183
184 public void onLongPress(MotionEvent e) {
185 }
186
187 public boolean onScroll(MotionEvent e1, MotionEvent e2,
188 float distanceX, float distanceY) {
189 return false;
190 }
191
192 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
193 float velocityY) {
194 return false;
195 }
196
197 public void onShowPress(MotionEvent e) {
198 }
199
200 public boolean onDown(MotionEvent e) {
201 return false;
202 }
203
204 public boolean onDoubleTap(MotionEvent e) {
205 return false;
206 }
207
208 public boolean onDoubleTapEvent(MotionEvent e) {
209 return false;
210 }
211
212 public boolean onSingleTapConfirmed(MotionEvent e) {
213 return false;
214 }
Mady Mellora6b16452015-04-14 18:03:34 -0700215
Mady Mellor015020e2015-06-05 14:41:22 -0700216 public boolean onContextClick(MotionEvent e) {
Mady Mellora6b16452015-04-14 18:03:34 -0700217 return false;
218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
220
Mathew Inwooda570dee2018-08-17 14:56:00 +0100221 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 private int mTouchSlopSquare;
Gilles Debunne006fa482011-10-27 17:23:36 -0700223 private int mDoubleTapTouchSlopSquare;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 private int mDoubleTapSlopSquare;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100225 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 private int mMinimumFlingVelocity;
Romain Guy4296fc42009-07-06 11:48:52 -0700227 private int mMaximumFlingVelocity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Jackal Guo198a2622018-11-19 11:51:31 +0800229 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout();
231 private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout();
Adam Powellaf1785f2013-09-05 13:44:45 -0700233 private static final int DOUBLE_TAP_MIN_TIME = ViewConfiguration.getDoubleTapMinTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
235 // constants for Message.what used by GestureHandler below
236 private static final int SHOW_PRESS = 1;
237 private static final int LONG_PRESS = 2;
238 private static final int TAP = 3;
239
240 private final Handler mHandler;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100241 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 private final OnGestureListener mListener;
243 private OnDoubleTapListener mDoubleTapListener;
Mady Mellor015020e2015-06-05 14:41:22 -0700244 private OnContextClickListener mContextClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 private boolean mStillDown;
Adam Powelleca3e602013-02-15 11:26:45 -0800247 private boolean mDeferConfirmSingleTap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 private boolean mInLongPress;
Mady Mellor015020e2015-06-05 14:41:22 -0700249 private boolean mInContextClick;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100250 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 private boolean mAlwaysInTapRegion;
252 private boolean mAlwaysInBiggerTapRegion;
Mady Mellorb0933442015-05-27 14:15:57 -0700253 private boolean mIgnoreNextUpEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
255 private MotionEvent mCurrentDownEvent;
256 private MotionEvent mPreviousUpEvent;
257
258 /**
259 * True when the user is still touching for the second tap (down, move, and
260 * up events). Can only be true if there is a double tap listener attached.
261 */
262 private boolean mIsDoubleTapping;
263
Adam Powell05a1e1f2012-08-29 13:54:44 -0700264 private float mLastFocusX;
265 private float mLastFocusY;
266 private float mDownFocusX;
267 private float mDownFocusY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
269 private boolean mIsLongpressEnabled;
270
271 /**
272 * Determines speed during touch scrolling
273 */
274 private VelocityTracker mVelocityTracker;
275
Jeff Brown21bc5c92011-02-28 18:27:14 -0800276 /**
277 * Consistency verifier for debugging purposes.
278 */
279 private final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
280 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
281 new InputEventConsistencyVerifier(this, 0) : null;
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 private class GestureHandler extends Handler {
284 GestureHandler() {
285 super();
286 }
287
288 GestureHandler(Handler handler) {
289 super(handler.getLooper());
290 }
291
292 @Override
293 public void handleMessage(Message msg) {
294 switch (msg.what) {
295 case SHOW_PRESS:
296 mListener.onShowPress(mCurrentDownEvent);
297 break;
298
299 case LONG_PRESS:
300 dispatchLongPress();
301 break;
302
303 case TAP:
304 // If the user's finger is still down, do not count it as a tap
Adam Powelleca3e602013-02-15 11:26:45 -0800305 if (mDoubleTapListener != null) {
306 if (!mStillDown) {
307 mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent);
308 } else {
309 mDeferConfirmSingleTap = true;
310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312 break;
313
314 default:
315 throw new RuntimeException("Unknown message " + msg); //never
316 }
317 }
318 }
319
320 /**
321 * Creates a GestureDetector with the supplied listener.
322 * This variant of the constructor should be used from a non-UI thread
323 * (as it allows specifying the Handler).
324 *
325 * @param listener the listener invoked for all the callbacks, this must
326 * not be null.
327 * @param handler the handler to use
328 *
329 * @throws NullPointerException if either {@code listener} or
330 * {@code handler} is null.
331 *
332 * @deprecated Use {@link #GestureDetector(android.content.Context,
333 * android.view.GestureDetector.OnGestureListener, android.os.Handler)} instead.
334 */
335 @Deprecated
336 public GestureDetector(OnGestureListener listener, Handler handler) {
337 this(null, listener, handler);
338 }
339
340 /**
341 * Creates a GestureDetector with the supplied listener.
342 * You may only use this constructor from a UI thread (this is the usual situation).
343 * @see android.os.Handler#Handler()
344 *
345 * @param listener the listener invoked for all the callbacks, this must
346 * not be null.
347 *
348 * @throws NullPointerException if {@code listener} is null.
349 *
350 * @deprecated Use {@link #GestureDetector(android.content.Context,
351 * android.view.GestureDetector.OnGestureListener)} instead.
352 */
353 @Deprecated
354 public GestureDetector(OnGestureListener listener) {
355 this(null, listener, null);
356 }
357
358 /**
359 * Creates a GestureDetector with the supplied listener.
Scott Mainc27ea252013-08-30 13:56:05 -0700360 * You may only use this constructor from a {@link android.os.Looper} thread.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 * @see android.os.Handler#Handler()
362 *
363 * @param context the application's context
364 * @param listener the listener invoked for all the callbacks, this must
365 * not be null.
366 *
367 * @throws NullPointerException if {@code listener} is null.
368 */
369 public GestureDetector(Context context, OnGestureListener listener) {
370 this(context, listener, null);
371 }
372
373 /**
Scott Mainc27ea252013-08-30 13:56:05 -0700374 * Creates a GestureDetector with the supplied listener that runs deferred events on the
375 * thread associated with the supplied {@link android.os.Handler}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * @see android.os.Handler#Handler()
377 *
378 * @param context the application's context
379 * @param listener the listener invoked for all the callbacks, this must
380 * not be null.
Scott Mainc27ea252013-08-30 13:56:05 -0700381 * @param handler the handler to use for running deferred listener events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 *
383 * @throws NullPointerException if {@code listener} is null.
384 */
385 public GestureDetector(Context context, OnGestureListener listener, Handler handler) {
Adam Powell05a1e1f2012-08-29 13:54:44 -0700386 if (handler != null) {
387 mHandler = new GestureHandler(handler);
388 } else {
389 mHandler = new GestureHandler();
390 }
391 mListener = listener;
392 if (listener instanceof OnDoubleTapListener) {
393 setOnDoubleTapListener((OnDoubleTapListener) listener);
394 }
Mady Mellor015020e2015-06-05 14:41:22 -0700395 if (listener instanceof OnContextClickListener) {
396 setContextClickListener((OnContextClickListener) listener);
Mady Mellora6b16452015-04-14 18:03:34 -0700397 }
Adam Powell05a1e1f2012-08-29 13:54:44 -0700398 init(context);
Adam Powell216bccf2010-02-01 15:03:17 -0800399 }
400
401 /**
Scott Mainc27ea252013-08-30 13:56:05 -0700402 * Creates a GestureDetector with the supplied listener that runs deferred events on the
403 * thread associated with the supplied {@link android.os.Handler}.
Adam Powell216bccf2010-02-01 15:03:17 -0800404 * @see android.os.Handler#Handler()
405 *
406 * @param context the application's context
407 * @param listener the listener invoked for all the callbacks, this must
408 * not be null.
Scott Mainc27ea252013-08-30 13:56:05 -0700409 * @param handler the handler to use for running deferred listener events.
410 * @param unused currently not used.
Adam Powell216bccf2010-02-01 15:03:17 -0800411 *
412 * @throws NullPointerException if {@code listener} is null.
413 */
414 public GestureDetector(Context context, OnGestureListener listener, Handler handler,
Adam Powell05a1e1f2012-08-29 13:54:44 -0700415 boolean unused) {
416 this(context, listener, handler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418
Adam Powell05a1e1f2012-08-29 13:54:44 -0700419 private void init(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 if (mListener == null) {
421 throw new NullPointerException("OnGestureListener must not be null");
422 }
423 mIsLongpressEnabled = true;
424
425 // Fallback to support pre-donuts releases
Gilles Debunne006fa482011-10-27 17:23:36 -0700426 int touchSlop, doubleTapSlop, doubleTapTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 if (context == null) {
428 //noinspection deprecation
429 touchSlop = ViewConfiguration.getTouchSlop();
Gilles Debunne006fa482011-10-27 17:23:36 -0700430 doubleTapTouchSlop = touchSlop; // Hack rather than adding a hiden method for this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 doubleTapSlop = ViewConfiguration.getDoubleTapSlop();
432 //noinspection deprecation
433 mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();
Romain Guy4296fc42009-07-06 11:48:52 -0700434 mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 } else {
436 final ViewConfiguration configuration = ViewConfiguration.get(context);
437 touchSlop = configuration.getScaledTouchSlop();
Gilles Debunne006fa482011-10-27 17:23:36 -0700438 doubleTapTouchSlop = configuration.getScaledDoubleTapTouchSlop();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 doubleTapSlop = configuration.getScaledDoubleTapSlop();
440 mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();
Romain Guy4296fc42009-07-06 11:48:52 -0700441 mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 mTouchSlopSquare = touchSlop * touchSlop;
Gilles Debunne006fa482011-10-27 17:23:36 -0700444 mDoubleTapTouchSlopSquare = doubleTapTouchSlop * doubleTapTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop;
446 }
447
448 /**
449 * Sets the listener which will be called for double-tap and related
450 * gestures.
451 *
452 * @param onDoubleTapListener the listener invoked for all the callbacks, or
453 * null to stop listening for double-tap gestures.
454 */
455 public void setOnDoubleTapListener(OnDoubleTapListener onDoubleTapListener) {
456 mDoubleTapListener = onDoubleTapListener;
457 }
458
459 /**
Mady Mellor015020e2015-06-05 14:41:22 -0700460 * Sets the listener which will be called for context clicks.
Mady Mellora6b16452015-04-14 18:03:34 -0700461 *
Mady Mellor015020e2015-06-05 14:41:22 -0700462 * @param onContextClickListener the listener invoked for all the callbacks, or null to stop
463 * listening for context clicks.
Mady Mellora6b16452015-04-14 18:03:34 -0700464 */
Mady Mellor015020e2015-06-05 14:41:22 -0700465 public void setContextClickListener(OnContextClickListener onContextClickListener) {
466 mContextClickListener = onContextClickListener;
Mady Mellora6b16452015-04-14 18:03:34 -0700467 }
468
469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 * Set whether longpress is enabled, if this is enabled when a user
471 * presses and holds down you get a longpress event and nothing further.
472 * If it's disabled the user can press and hold down and then later
473 * moved their finger and you will get scroll events. By default
474 * longpress is enabled.
475 *
476 * @param isLongpressEnabled whether longpress should be enabled.
477 */
478 public void setIsLongpressEnabled(boolean isLongpressEnabled) {
479 mIsLongpressEnabled = isLongpressEnabled;
480 }
481
482 /**
483 * @return true if longpress is enabled, else false.
484 */
485 public boolean isLongpressEnabled() {
486 return mIsLongpressEnabled;
487 }
488
489 /**
490 * Analyzes the given motion event and if applicable triggers the
491 * appropriate callbacks on the {@link OnGestureListener} supplied.
492 *
493 * @param ev The current motion event.
494 * @return true if the {@link OnGestureListener} consumed the event,
495 * else false.
496 */
497 public boolean onTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -0800498 if (mInputEventConsistencyVerifier != null) {
499 mInputEventConsistencyVerifier.onTouchEvent(ev, 0);
500 }
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 final int action = ev.getAction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
504 if (mVelocityTracker == null) {
505 mVelocityTracker = VelocityTracker.obtain();
506 }
507 mVelocityTracker.addMovement(ev);
508
Adam Powellf90165a2012-08-31 11:11:39 -0700509 final boolean pointerUp =
510 (action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP;
Adam Powell05a1e1f2012-08-29 13:54:44 -0700511 final int skipIndex = pointerUp ? ev.getActionIndex() : -1;
Dennis Kempinac1b31d2016-11-02 17:02:25 -0700512 final boolean isGeneratedGesture =
513 (ev.getFlags() & MotionEvent.FLAG_IS_GENERATED_GESTURE) != 0;
Adam Powell05a1e1f2012-08-29 13:54:44 -0700514
515 // Determine focal point
516 float sumX = 0, sumY = 0;
517 final int count = ev.getPointerCount();
518 for (int i = 0; i < count; i++) {
519 if (skipIndex == i) continue;
520 sumX += ev.getX(i);
521 sumY += ev.getY(i);
522 }
523 final int div = pointerUp ? count - 1 : count;
524 final float focusX = sumX / div;
525 final float focusY = sumY / div;
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 boolean handled = false;
528
Adam Powell216bccf2010-02-01 15:03:17 -0800529 switch (action & MotionEvent.ACTION_MASK) {
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800530 case MotionEvent.ACTION_POINTER_DOWN:
531 mDownFocusX = mLastFocusX = focusX;
532 mDownFocusY = mLastFocusY = focusY;
533 // Cancel long press and taps
534 cancelTaps();
535 break;
Adam Powell216bccf2010-02-01 15:03:17 -0800536
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800537 case MotionEvent.ACTION_POINTER_UP:
538 mDownFocusX = mLastFocusX = focusX;
539 mDownFocusY = mLastFocusY = focusY;
Adam Powellcd663592012-09-11 09:52:37 -0700540
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800541 // Check the dot product of current velocities.
542 // If the pointer that left was opposing another velocity vector, clear.
543 mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
544 final int upIndex = ev.getActionIndex();
545 final int id1 = ev.getPointerId(upIndex);
546 final float x1 = mVelocityTracker.getXVelocity(id1);
547 final float y1 = mVelocityTracker.getYVelocity(id1);
548 for (int i = 0; i < count; i++) {
549 if (i == upIndex) continue;
Adam Powellcd663592012-09-11 09:52:37 -0700550
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800551 final int id2 = ev.getPointerId(i);
552 final float x = x1 * mVelocityTracker.getXVelocity(id2);
553 final float y = y1 * mVelocityTracker.getYVelocity(id2);
Adam Powellcd663592012-09-11 09:52:37 -0700554
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800555 final float dot = x + y;
556 if (dot < 0) {
557 mVelocityTracker.clear();
558 break;
559 }
560 }
561 break;
562
563 case MotionEvent.ACTION_DOWN:
564 if (mDoubleTapListener != null) {
565 boolean hadTapMessage = mHandler.hasMessages(TAP);
566 if (hadTapMessage) mHandler.removeMessages(TAP);
567 if ((mCurrentDownEvent != null) && (mPreviousUpEvent != null)
568 && hadTapMessage
569 && isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, ev)) {
570 // This is a second tap
571 mIsDoubleTapping = true;
572 // Give a callback with the first tap of the double-tap
573 handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
574 // Give a callback with down event of the double-tap
575 handled |= mDoubleTapListener.onDoubleTapEvent(ev);
576 } else {
577 // This is a first tap
578 mHandler.sendEmptyMessageDelayed(TAP, DOUBLE_TAP_TIMEOUT);
579 }
580 }
581
582 mDownFocusX = mLastFocusX = focusX;
583 mDownFocusY = mLastFocusY = focusY;
584 if (mCurrentDownEvent != null) {
585 mCurrentDownEvent.recycle();
586 }
587 mCurrentDownEvent = MotionEvent.obtain(ev);
588 mAlwaysInTapRegion = true;
589 mAlwaysInBiggerTapRegion = true;
590 mStillDown = true;
591 mInLongPress = false;
592 mDeferConfirmSingleTap = false;
593
594 if (mIsLongpressEnabled) {
595 mHandler.removeMessages(LONG_PRESS);
Jackal Guo198a2622018-11-19 11:51:31 +0800596 mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime()
597 + ViewConfiguration.getLongPressTimeout());
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800598 }
599 mHandler.sendEmptyMessageAtTime(SHOW_PRESS,
600 mCurrentDownEvent.getDownTime() + TAP_TIMEOUT);
601 handled |= mListener.onDown(ev);
602 break;
603
604 case MotionEvent.ACTION_MOVE:
605 if (mInLongPress || mInContextClick) {
Adam Powellcd663592012-09-11 09:52:37 -0700606 break;
607 }
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800608 final float scrollX = mLastFocusX - focusX;
609 final float scrollY = mLastFocusY - focusY;
610 if (mIsDoubleTapping) {
611 // Give the move events of the double-tap
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 handled |= mDoubleTapListener.onDoubleTapEvent(ev);
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800613 } else if (mAlwaysInTapRegion) {
614 final int deltaX = (int) (focusX - mDownFocusX);
615 final int deltaY = (int) (focusY - mDownFocusY);
616 int distance = (deltaX * deltaX) + (deltaY * deltaY);
617 int slopSquare = isGeneratedGesture ? 0 : mTouchSlopSquare;
618 if (distance > slopSquare) {
619 handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY);
620 mLastFocusX = focusX;
621 mLastFocusY = focusY;
622 mAlwaysInTapRegion = false;
623 mHandler.removeMessages(TAP);
624 mHandler.removeMessages(SHOW_PRESS);
625 mHandler.removeMessages(LONG_PRESS);
626 }
627 int doubleTapSlopSquare = isGeneratedGesture ? 0 : mDoubleTapTouchSlopSquare;
628 if (distance > doubleTapSlopSquare) {
629 mAlwaysInBiggerTapRegion = false;
630 }
631 } else if ((Math.abs(scrollX) >= 1) || (Math.abs(scrollY) >= 1)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY);
Adam Powell05a1e1f2012-08-29 13:54:44 -0700633 mLastFocusX = focusX;
634 mLastFocusY = focusY;
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800635 }
636 break;
637
638 case MotionEvent.ACTION_UP:
639 mStillDown = false;
640 MotionEvent currentUpEvent = MotionEvent.obtain(ev);
641 if (mIsDoubleTapping) {
642 // Finally, give the up event of the double-tap
643 handled |= mDoubleTapListener.onDoubleTapEvent(ev);
644 } else if (mInLongPress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 mHandler.removeMessages(TAP);
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800646 mInLongPress = false;
647 } else if (mAlwaysInTapRegion && !mIgnoreNextUpEvent) {
648 handled = mListener.onSingleTapUp(ev);
649 if (mDeferConfirmSingleTap && mDoubleTapListener != null) {
650 mDoubleTapListener.onSingleTapConfirmed(ev);
651 }
652 } else if (!mIgnoreNextUpEvent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800654 // A fling must travel the minimum tap distance
655 final VelocityTracker velocityTracker = mVelocityTracker;
656 final int pointerId = ev.getPointerId(0);
657 velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
658 final float velocityY = velocityTracker.getYVelocity(pointerId);
659 final float velocityX = velocityTracker.getXVelocity(pointerId);
660
661 if ((Math.abs(velocityY) > mMinimumFlingVelocity)
662 || (Math.abs(velocityX) > mMinimumFlingVelocity)) {
663 handled = mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY);
664 }
Adam Powelleca3e602013-02-15 11:26:45 -0800665 }
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800666 if (mPreviousUpEvent != null) {
667 mPreviousUpEvent.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800669 // Hold the event we obtained above - listeners may have changed the original.
670 mPreviousUpEvent = currentUpEvent;
671 if (mVelocityTracker != null) {
672 // This may have been cleared when we called out to the
673 // application above.
674 mVelocityTracker.recycle();
675 mVelocityTracker = null;
676 }
677 mIsDoubleTapping = false;
678 mDeferConfirmSingleTap = false;
679 mIgnoreNextUpEvent = false;
680 mHandler.removeMessages(SHOW_PRESS);
681 mHandler.removeMessages(LONG_PRESS);
682 break;
Jeff Brownbbdc50b2011-04-19 23:46:52 -0700683
Siarhei Vishniakoud60090d2017-12-12 16:24:26 -0800684 case MotionEvent.ACTION_CANCEL:
685 cancel();
686 break;
Jeff Brownbbdc50b2011-04-19 23:46:52 -0700687 }
688
689 if (!handled && mInputEventConsistencyVerifier != null) {
690 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692 return handled;
693 }
694
Mady Mellorb0933442015-05-27 14:15:57 -0700695 /**
696 * Analyzes the given generic motion event and if applicable triggers the
697 * appropriate callbacks on the {@link OnGestureListener} supplied.
698 *
699 * @param ev The current motion event.
700 * @return true if the {@link OnGestureListener} consumed the event,
701 * else false.
702 */
703 public boolean onGenericMotionEvent(MotionEvent ev) {
704 if (mInputEventConsistencyVerifier != null) {
705 mInputEventConsistencyVerifier.onGenericMotionEvent(ev, 0);
706 }
707
Mady Mellor015020e2015-06-05 14:41:22 -0700708 final int actionButton = ev.getActionButton();
Mady Mellorb0933442015-05-27 14:15:57 -0700709 switch (ev.getActionMasked()) {
710 case MotionEvent.ACTION_BUTTON_PRESS:
Mady Mellor015020e2015-06-05 14:41:22 -0700711 if (mContextClickListener != null && !mInContextClick && !mInLongPress
712 && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY
713 || actionButton == MotionEvent.BUTTON_SECONDARY)) {
714 if (mContextClickListener.onContextClick(ev)) {
715 mInContextClick = true;
Mady Mellorb0933442015-05-27 14:15:57 -0700716 mHandler.removeMessages(LONG_PRESS);
717 mHandler.removeMessages(TAP);
718 return true;
719 }
720 }
721 break;
722
723 case MotionEvent.ACTION_BUTTON_RELEASE:
Mady Mellor015020e2015-06-05 14:41:22 -0700724 if (mInContextClick && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY
725 || actionButton == MotionEvent.BUTTON_SECONDARY)) {
726 mInContextClick = false;
Mady Mellorb0933442015-05-27 14:15:57 -0700727 mIgnoreNextUpEvent = true;
728 }
729 break;
730 }
731 return false;
732 }
733
Adam Powell216bccf2010-02-01 15:03:17 -0800734 private void cancel() {
735 mHandler.removeMessages(SHOW_PRESS);
736 mHandler.removeMessages(LONG_PRESS);
737 mHandler.removeMessages(TAP);
738 mVelocityTracker.recycle();
739 mVelocityTracker = null;
740 mIsDoubleTapping = false;
741 mStillDown = false;
Adam Powell17921ee2011-06-09 11:39:21 -0700742 mAlwaysInTapRegion = false;
743 mAlwaysInBiggerTapRegion = false;
Adam Powelleca3e602013-02-15 11:26:45 -0800744 mDeferConfirmSingleTap = false;
Mady Mellora6b16452015-04-14 18:03:34 -0700745 mInLongPress = false;
Mady Mellor015020e2015-06-05 14:41:22 -0700746 mInContextClick = false;
Mady Mellorb0933442015-05-27 14:15:57 -0700747 mIgnoreNextUpEvent = false;
Adam Powell216bccf2010-02-01 15:03:17 -0800748 }
749
Adam Powell05a1e1f2012-08-29 13:54:44 -0700750 private void cancelTaps() {
751 mHandler.removeMessages(SHOW_PRESS);
752 mHandler.removeMessages(LONG_PRESS);
753 mHandler.removeMessages(TAP);
754 mIsDoubleTapping = false;
755 mAlwaysInTapRegion = false;
756 mAlwaysInBiggerTapRegion = false;
Adam Powelleca3e602013-02-15 11:26:45 -0800757 mDeferConfirmSingleTap = false;
Mady Mellora6b16452015-04-14 18:03:34 -0700758 mInLongPress = false;
Mady Mellor015020e2015-06-05 14:41:22 -0700759 mInContextClick = false;
Mady Mellorb0933442015-05-27 14:15:57 -0700760 mIgnoreNextUpEvent = false;
Adam Powell05a1e1f2012-08-29 13:54:44 -0700761 }
762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp,
764 MotionEvent secondDown) {
765 if (!mAlwaysInBiggerTapRegion) {
766 return false;
767 }
768
Adam Powellaf1785f2013-09-05 13:44:45 -0700769 final long deltaTime = secondDown.getEventTime() - firstUp.getEventTime();
770 if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 return false;
772 }
773
774 int deltaX = (int) firstDown.getX() - (int) secondDown.getX();
775 int deltaY = (int) firstDown.getY() - (int) secondDown.getY();
Dennis Kempinac1b31d2016-11-02 17:02:25 -0700776 final boolean isGeneratedGesture =
777 (firstDown.getFlags() & MotionEvent.FLAG_IS_GENERATED_GESTURE) != 0;
778 int slopSquare = isGeneratedGesture ? 0 : mDoubleTapSlopSquare;
779 return (deltaX * deltaX + deltaY * deltaY < slopSquare);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781
782 private void dispatchLongPress() {
783 mHandler.removeMessages(TAP);
Adam Powelleca3e602013-02-15 11:26:45 -0800784 mDeferConfirmSingleTap = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 mInLongPress = true;
786 mListener.onLongPress(mCurrentDownEvent);
787 }
788}