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) { |
| 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 Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 305 | if (mDoubleTapListener != null) { |
| 306 | if (!mStillDown) { |
| 307 | mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent); |
| 308 | } else { |
| 309 | mDeferConfirmSingleTap = true; |
| 310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 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 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(); |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 430 | doubleTapTouchSlop = touchSlop; // Hack rather than adding a hiden 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 | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 608 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | handled |= mDoubleTapListener.onDoubleTapEvent(ev); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 613 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY); |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 633 | mLastFocusX = focusX; |
| 634 | mLastFocusY = focusY; |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 635 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | mHandler.removeMessages(TAP); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 646 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 654 | // 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 Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 665 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 666 | if (mPreviousUpEvent != null) { |
| 667 | mPreviousUpEvent.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 669 | // 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 Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 683 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 684 | case MotionEvent.ACTION_CANCEL: |
| 685 | cancel(); |
| 686 | break; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 690 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | } |
| 692 | return handled; |
| 693 | } |
| 694 | |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 695 | /** |
| 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 Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 708 | final int actionButton = ev.getActionButton(); |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 709 | switch (ev.getActionMasked()) { |
| 710 | case MotionEvent.ACTION_BUTTON_PRESS: |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 711 | 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 Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 716 | mHandler.removeMessages(LONG_PRESS); |
| 717 | mHandler.removeMessages(TAP); |
| 718 | return true; |
| 719 | } |
| 720 | } |
| 721 | break; |
| 722 | |
| 723 | case MotionEvent.ACTION_BUTTON_RELEASE: |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 724 | if (mInContextClick && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY |
| 725 | || actionButton == MotionEvent.BUTTON_SECONDARY)) { |
| 726 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 727 | mIgnoreNextUpEvent = true; |
| 728 | } |
| 729 | break; |
| 730 | } |
| 731 | return false; |
| 732 | } |
| 733 | |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 734 | 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 Powell | 17921ee | 2011-06-09 11:39:21 -0700 | [diff] [blame] | 742 | mAlwaysInTapRegion = false; |
| 743 | mAlwaysInBiggerTapRegion = false; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 744 | mDeferConfirmSingleTap = false; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 745 | mInLongPress = false; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 746 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 747 | mIgnoreNextUpEvent = false; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 750 | 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 Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 757 | mDeferConfirmSingleTap = false; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 758 | mInLongPress = false; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 759 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 760 | mIgnoreNextUpEvent = false; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 761 | } |
| 762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp, |
| 764 | MotionEvent secondDown) { |
| 765 | if (!mAlwaysInBiggerTapRegion) { |
| 766 | return false; |
| 767 | } |
| 768 | |
Adam Powell | af1785f | 2013-09-05 13:44:45 -0700 | [diff] [blame] | 769 | final long deltaTime = secondDown.getEventTime() - firstUp.getEventTime(); |
| 770 | 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] | 771 | return false; |
| 772 | } |
| 773 | |
| 774 | int deltaX = (int) firstDown.getX() - (int) secondDown.getX(); |
| 775 | int deltaY = (int) firstDown.getY() - (int) secondDown.getY(); |
Dennis Kempin | ac1b31d | 2016-11-02 17:02:25 -0700 | [diff] [blame] | 776 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | private void dispatchLongPress() { |
| 783 | mHandler.removeMessages(TAP); |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 784 | mDeferConfirmSingleTap = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | mInLongPress = true; |
| 786 | mListener.onLongPress(mCurrentDownEvent); |
| 787 | } |
| 788 | } |