The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Jackal Guo | 198a262 | 2018-11-19 11:51:31 +0800 | [diff] [blame] | 21 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 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 Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 37 | * <li>If listening for {@link OnContextClickListener#onContextClick(MotionEvent)} |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 38 | * you must call {@link #onGenericMotionEvent(MotionEvent)} |
| 39 | * in {@link View#onGenericMotionEvent(MotionEvent)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | * </ul> |
| 41 | */ |
| 42 | public 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 Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 157 | * 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 Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 159 | * {@link View#onGenericMotionEvent(MotionEvent)}. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 160 | */ |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 161 | public interface OnContextClickListener { |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 162 | /** |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 163 | * Notified when a context click occurs. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 164 | * |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 165 | * @param e The motion event that occurred during the context click. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 166 | * @return true if the event is consumed, else false |
| 167 | */ |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 168 | boolean onContextClick(MotionEvent e); |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | * 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 Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 174 | * {@link OnGestureListener}, {@link OnDoubleTapListener}, and {@link OnContextClickListener} |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 175 | * but does nothing and return {@code false} for all applicable methods. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | */ |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 177 | public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener, |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 178 | OnContextClickListener { |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | 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 Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 215 | |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 216 | public boolean onContextClick(MotionEvent e) { |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 217 | return false; |
| 218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 221 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | private int mTouchSlopSquare; |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 223 | private int mDoubleTapTouchSlopSquare; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | private int mDoubleTapSlopSquare; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 225 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | private int mMinimumFlingVelocity; |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 227 | private int mMaximumFlingVelocity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | |
Jackal Guo | 198a262 | 2018-11-19 11:51:31 +0800 | [diff] [blame] | 229 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout(); |
| 231 | private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout(); |
Adam Powell | af1785f | 2013-09-05 13:44:45 -0700 | [diff] [blame] | 233 | private static final int DOUBLE_TAP_MIN_TIME = ViewConfiguration.getDoubleTapMinTime(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | |
| 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 Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 241 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | private final OnGestureListener mListener; |
| 243 | private OnDoubleTapListener mDoubleTapListener; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 244 | private OnContextClickListener mContextClickListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | |
| 246 | private boolean mStillDown; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 247 | private boolean mDeferConfirmSingleTap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | private boolean mInLongPress; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 249 | private boolean mInContextClick; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 250 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | private boolean mAlwaysInTapRegion; |
| 252 | private boolean mAlwaysInBiggerTapRegion; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 253 | private boolean mIgnoreNextUpEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
| 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 Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 264 | private float mLastFocusX; |
| 265 | private float mLastFocusY; |
| 266 | private float mDownFocusX; |
| 267 | private float mDownFocusY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | |
| 269 | private boolean mIsLongpressEnabled; |
| 270 | |
| 271 | /** |
| 272 | * Determines speed during touch scrolling |
| 273 | */ |
| 274 | private VelocityTracker mVelocityTracker; |
| 275 | |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 276 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | 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) { |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 295 | case SHOW_PRESS: |
| 296 | mListener.onShowPress(mCurrentDownEvent); |
| 297 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 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 |
| 305 | if (mDoubleTapListener != null) { |
| 306 | if (!mStillDown) { |
| 307 | mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent); |
| 308 | } else { |
| 309 | mDeferConfirmSingleTap = true; |
| 310 | } |
| 311 | } |
| 312 | break; |
| 313 | |
| 314 | default: |
| 315 | throw new RuntimeException("Unknown message " + msg); //never |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 360 | * You may only use this constructor from a {@link android.os.Looper} thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | * @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 Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 374 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | * @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 Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 381 | * @param handler the handler to use for running deferred listener events. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | * |
| 383 | * @throws NullPointerException if {@code listener} is null. |
| 384 | */ |
| 385 | public GestureDetector(Context context, OnGestureListener listener, Handler handler) { |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 386 | 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 Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 395 | if (listener instanceof OnContextClickListener) { |
| 396 | setContextClickListener((OnContextClickListener) listener); |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 397 | } |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 398 | init(context); |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Scott Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 402 | * Creates a GestureDetector with the supplied listener that runs deferred events on the |
| 403 | * thread associated with the supplied {@link android.os.Handler}. |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 404 | * @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 Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 409 | * @param handler the handler to use for running deferred listener events. |
| 410 | * @param unused currently not used. |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 411 | * |
| 412 | * @throws NullPointerException if {@code listener} is null. |
| 413 | */ |
| 414 | public GestureDetector(Context context, OnGestureListener listener, Handler handler, |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 415 | boolean unused) { |
| 416 | this(context, listener, handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 419 | private void init(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | 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 Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 426 | int touchSlop, doubleTapSlop, doubleTapTouchSlop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | if (context == null) { |
| 428 | //noinspection deprecation |
| 429 | touchSlop = ViewConfiguration.getTouchSlop(); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 430 | doubleTapTouchSlop = touchSlop; // Hack rather than adding a hidden method for this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | doubleTapSlop = ViewConfiguration.getDoubleTapSlop(); |
| 432 | //noinspection deprecation |
| 433 | mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity(); |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 434 | mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } else { |
| 436 | final ViewConfiguration configuration = ViewConfiguration.get(context); |
| 437 | touchSlop = configuration.getScaledTouchSlop(); |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 438 | doubleTapTouchSlop = configuration.getScaledDoubleTapTouchSlop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | doubleTapSlop = configuration.getScaledDoubleTapSlop(); |
| 440 | mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity(); |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 441 | mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | } |
| 443 | mTouchSlopSquare = touchSlop * touchSlop; |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 444 | mDoubleTapTouchSlopSquare = doubleTapTouchSlop * doubleTapTouchSlop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | 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 Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 460 | * Sets the listener which will be called for context clicks. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 461 | * |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 462 | * @param onContextClickListener the listener invoked for all the callbacks, or null to stop |
| 463 | * listening for context clicks. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 464 | */ |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 465 | public void setContextClickListener(OnContextClickListener onContextClickListener) { |
| 466 | mContextClickListener = onContextClickListener; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | * 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 Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 498 | if (mInputEventConsistencyVerifier != null) { |
| 499 | mInputEventConsistencyVerifier.onTouchEvent(ev, 0); |
| 500 | } |
| 501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | final int action = ev.getAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | |
| 504 | if (mVelocityTracker == null) { |
| 505 | mVelocityTracker = VelocityTracker.obtain(); |
| 506 | } |
| 507 | mVelocityTracker.addMovement(ev); |
| 508 | |
Adam Powell | f90165a | 2012-08-31 11:11:39 -0700 | [diff] [blame] | 509 | final boolean pointerUp = |
| 510 | (action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 511 | final int skipIndex = pointerUp ? ev.getActionIndex() : -1; |
Dennis Kempin | ac1b31d | 2016-11-02 17:02:25 -0700 | [diff] [blame] | 512 | final boolean isGeneratedGesture = |
| 513 | (ev.getFlags() & MotionEvent.FLAG_IS_GENERATED_GESTURE) != 0; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 514 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | boolean handled = false; |
| 528 | |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 529 | switch (action & MotionEvent.ACTION_MASK) { |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 530 | case MotionEvent.ACTION_POINTER_DOWN: |
| 531 | mDownFocusX = mLastFocusX = focusX; |
| 532 | mDownFocusY = mLastFocusY = focusY; |
| 533 | // Cancel long press and taps |
| 534 | cancelTaps(); |
| 535 | break; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 536 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 537 | case MotionEvent.ACTION_POINTER_UP: |
| 538 | mDownFocusX = mLastFocusX = focusX; |
| 539 | mDownFocusY = mLastFocusY = focusY; |
Adam Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 540 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 541 | // 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 Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 550 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 551 | final int id2 = ev.getPointerId(i); |
| 552 | final float x = x1 * mVelocityTracker.getXVelocity(id2); |
| 553 | final float y = y1 * mVelocityTracker.getYVelocity(id2); |
Adam Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 554 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 555 | 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 Guo | 198a262 | 2018-11-19 11:51:31 +0800 | [diff] [blame] | 596 | mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime() |
| 597 | + ViewConfiguration.getLongPressTimeout()); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 598 | } |
| 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 Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 606 | break; |
| 607 | } |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 608 | |
| 609 | final int motionClassification = ev.getClassification(); |
| 610 | final boolean hasPendingLongPress = mHandler.hasMessages(LONG_PRESS); |
| 611 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 612 | final float scrollX = mLastFocusX - focusX; |
| 613 | final float scrollY = mLastFocusY - focusY; |
| 614 | if (mIsDoubleTapping) { |
| 615 | // Give the move events of the double-tap |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | handled |= mDoubleTapListener.onDoubleTapEvent(ev); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 617 | } else if (mAlwaysInTapRegion) { |
| 618 | final int deltaX = (int) (focusX - mDownFocusX); |
| 619 | final int deltaY = (int) (focusY - mDownFocusY); |
| 620 | int distance = (deltaX * deltaX) + (deltaY * deltaY); |
| 621 | int slopSquare = isGeneratedGesture ? 0 : mTouchSlopSquare; |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 622 | |
| 623 | final boolean ambiguousGesture = |
| 624 | motionClassification == MotionEvent.CLASSIFICATION_AMBIGUOUS_GESTURE; |
| 625 | final boolean shouldInhibitDefaultAction = |
| 626 | hasPendingLongPress && ambiguousGesture; |
| 627 | if (shouldInhibitDefaultAction) { |
| 628 | // Inhibit default long press |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 629 | final float multiplier = ViewConfiguration.getAmbiguousGestureMultiplier(); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 630 | if (distance > slopSquare) { |
| 631 | // The default action here is to remove long press. But if the touch |
| 632 | // slop below gets increased, and we never exceed the modified touch |
| 633 | // slop while still receiving AMBIGUOUS_GESTURE, we risk that *nothing* |
| 634 | // will happen in response to user input. To prevent this, |
| 635 | // reschedule long press with a modified timeout. |
| 636 | mHandler.removeMessages(LONG_PRESS); |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 637 | final long longPressTimeout = ViewConfiguration.getLongPressTimeout(); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 638 | mHandler.sendEmptyMessageAtTime(LONG_PRESS, ev.getDownTime() |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 639 | + (long) (longPressTimeout * multiplier)); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 640 | } |
| 641 | // Inhibit default scroll. If a gesture is ambiguous, we prevent scroll |
| 642 | // until the gesture is resolved. |
| 643 | // However, for safety, simply increase the touch slop in case the |
| 644 | // classification is erroneous. Since the value is squared, multiply twice. |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 645 | slopSquare *= multiplier * multiplier; |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 648 | if (distance > slopSquare) { |
| 649 | handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY); |
| 650 | mLastFocusX = focusX; |
| 651 | mLastFocusY = focusY; |
| 652 | mAlwaysInTapRegion = false; |
| 653 | mHandler.removeMessages(TAP); |
| 654 | mHandler.removeMessages(SHOW_PRESS); |
| 655 | mHandler.removeMessages(LONG_PRESS); |
| 656 | } |
| 657 | int doubleTapSlopSquare = isGeneratedGesture ? 0 : mDoubleTapTouchSlopSquare; |
| 658 | if (distance > doubleTapSlopSquare) { |
| 659 | mAlwaysInBiggerTapRegion = false; |
| 660 | } |
| 661 | } else if ((Math.abs(scrollX) >= 1) || (Math.abs(scrollY) >= 1)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY); |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 663 | mLastFocusX = focusX; |
| 664 | mLastFocusY = focusY; |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 665 | } |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 666 | final boolean deepPress = |
| 667 | motionClassification == MotionEvent.CLASSIFICATION_DEEP_PRESS; |
| 668 | if (deepPress && hasPendingLongPress) { |
| 669 | mHandler.removeMessages(LONG_PRESS); |
| 670 | mHandler.sendEmptyMessage(LONG_PRESS); |
| 671 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | case MotionEvent.ACTION_UP: |
| 675 | mStillDown = false; |
| 676 | MotionEvent currentUpEvent = MotionEvent.obtain(ev); |
| 677 | if (mIsDoubleTapping) { |
| 678 | // Finally, give the up event of the double-tap |
| 679 | handled |= mDoubleTapListener.onDoubleTapEvent(ev); |
| 680 | } else if (mInLongPress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | mHandler.removeMessages(TAP); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 682 | mInLongPress = false; |
| 683 | } else if (mAlwaysInTapRegion && !mIgnoreNextUpEvent) { |
| 684 | handled = mListener.onSingleTapUp(ev); |
| 685 | if (mDeferConfirmSingleTap && mDoubleTapListener != null) { |
| 686 | mDoubleTapListener.onSingleTapConfirmed(ev); |
| 687 | } |
| 688 | } else if (!mIgnoreNextUpEvent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 690 | // A fling must travel the minimum tap distance |
| 691 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 692 | final int pointerId = ev.getPointerId(0); |
| 693 | velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); |
| 694 | final float velocityY = velocityTracker.getYVelocity(pointerId); |
| 695 | final float velocityX = velocityTracker.getXVelocity(pointerId); |
| 696 | |
| 697 | if ((Math.abs(velocityY) > mMinimumFlingVelocity) |
| 698 | || (Math.abs(velocityX) > mMinimumFlingVelocity)) { |
| 699 | handled = mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY); |
| 700 | } |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 701 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 702 | if (mPreviousUpEvent != null) { |
| 703 | mPreviousUpEvent.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 705 | // Hold the event we obtained above - listeners may have changed the original. |
| 706 | mPreviousUpEvent = currentUpEvent; |
| 707 | if (mVelocityTracker != null) { |
| 708 | // This may have been cleared when we called out to the |
| 709 | // application above. |
| 710 | mVelocityTracker.recycle(); |
| 711 | mVelocityTracker = null; |
| 712 | } |
| 713 | mIsDoubleTapping = false; |
| 714 | mDeferConfirmSingleTap = false; |
| 715 | mIgnoreNextUpEvent = false; |
| 716 | mHandler.removeMessages(SHOW_PRESS); |
| 717 | mHandler.removeMessages(LONG_PRESS); |
| 718 | break; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 719 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 720 | case MotionEvent.ACTION_CANCEL: |
| 721 | cancel(); |
| 722 | break; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 726 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | } |
| 728 | return handled; |
| 729 | } |
| 730 | |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 731 | /** |
| 732 | * Analyzes the given generic motion event and if applicable triggers the |
| 733 | * appropriate callbacks on the {@link OnGestureListener} supplied. |
| 734 | * |
| 735 | * @param ev The current motion event. |
| 736 | * @return true if the {@link OnGestureListener} consumed the event, |
| 737 | * else false. |
| 738 | */ |
| 739 | public boolean onGenericMotionEvent(MotionEvent ev) { |
| 740 | if (mInputEventConsistencyVerifier != null) { |
| 741 | mInputEventConsistencyVerifier.onGenericMotionEvent(ev, 0); |
| 742 | } |
| 743 | |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 744 | final int actionButton = ev.getActionButton(); |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 745 | switch (ev.getActionMasked()) { |
| 746 | case MotionEvent.ACTION_BUTTON_PRESS: |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 747 | if (mContextClickListener != null && !mInContextClick && !mInLongPress |
| 748 | && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY |
| 749 | || actionButton == MotionEvent.BUTTON_SECONDARY)) { |
| 750 | if (mContextClickListener.onContextClick(ev)) { |
| 751 | mInContextClick = true; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 752 | mHandler.removeMessages(LONG_PRESS); |
| 753 | mHandler.removeMessages(TAP); |
| 754 | return true; |
| 755 | } |
| 756 | } |
| 757 | break; |
| 758 | |
| 759 | case MotionEvent.ACTION_BUTTON_RELEASE: |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 760 | if (mInContextClick && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY |
| 761 | || actionButton == MotionEvent.BUTTON_SECONDARY)) { |
| 762 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 763 | mIgnoreNextUpEvent = true; |
| 764 | } |
| 765 | break; |
| 766 | } |
| 767 | return false; |
| 768 | } |
| 769 | |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 770 | private void cancel() { |
| 771 | mHandler.removeMessages(SHOW_PRESS); |
| 772 | mHandler.removeMessages(LONG_PRESS); |
| 773 | mHandler.removeMessages(TAP); |
| 774 | mVelocityTracker.recycle(); |
| 775 | mVelocityTracker = null; |
| 776 | mIsDoubleTapping = false; |
| 777 | mStillDown = false; |
Adam Powell | 17921ee | 2011-06-09 11:39:21 -0700 | [diff] [blame] | 778 | mAlwaysInTapRegion = false; |
| 779 | mAlwaysInBiggerTapRegion = false; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 780 | mDeferConfirmSingleTap = false; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 781 | mInLongPress = false; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 782 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 783 | mIgnoreNextUpEvent = false; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 786 | private void cancelTaps() { |
| 787 | mHandler.removeMessages(SHOW_PRESS); |
| 788 | mHandler.removeMessages(LONG_PRESS); |
| 789 | mHandler.removeMessages(TAP); |
| 790 | mIsDoubleTapping = false; |
| 791 | mAlwaysInTapRegion = false; |
| 792 | mAlwaysInBiggerTapRegion = false; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 793 | mDeferConfirmSingleTap = false; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 794 | mInLongPress = false; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 795 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 796 | mIgnoreNextUpEvent = false; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 797 | } |
| 798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp, |
| 800 | MotionEvent secondDown) { |
| 801 | if (!mAlwaysInBiggerTapRegion) { |
| 802 | return false; |
| 803 | } |
| 804 | |
Adam Powell | af1785f | 2013-09-05 13:44:45 -0700 | [diff] [blame] | 805 | final long deltaTime = secondDown.getEventTime() - firstUp.getEventTime(); |
| 806 | if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | |
| 810 | int deltaX = (int) firstDown.getX() - (int) secondDown.getX(); |
| 811 | int deltaY = (int) firstDown.getY() - (int) secondDown.getY(); |
Dennis Kempin | ac1b31d | 2016-11-02 17:02:25 -0700 | [diff] [blame] | 812 | final boolean isGeneratedGesture = |
| 813 | (firstDown.getFlags() & MotionEvent.FLAG_IS_GENERATED_GESTURE) != 0; |
| 814 | int slopSquare = isGeneratedGesture ? 0 : mDoubleTapSlopSquare; |
| 815 | return (deltaX * deltaX + deltaY * deltaY < slopSquare); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | private void dispatchLongPress() { |
| 819 | mHandler.removeMessages(TAP); |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 820 | mDeferConfirmSingleTap = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | mInLongPress = true; |
| 822 | mListener.onLongPress(mCurrentDownEvent); |
| 823 | } |
| 824 | } |