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 | |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 19 | import static android.util.StatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DEEP_PRESS; |
| 20 | import static android.util.StatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DOUBLE_TAP; |
| 21 | import static android.util.StatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__LONG_PRESS; |
| 22 | import static android.util.StatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__SCROLL; |
| 23 | import static android.util.StatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__SINGLE_TAP; |
| 24 | import static android.util.StatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__UNKNOWN_CLASSIFICATION; |
| 25 | |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 26 | import android.compat.annotation.UnsupportedAppUsage; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Jackal Guo | 198a262 | 2018-11-19 11:51:31 +0800 | [diff] [blame] | 28 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.Message; |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
| 32 | import android.util.StatsLog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Detects various gestures and events using the supplied {@link MotionEvent}s. |
| 36 | * The {@link OnGestureListener} callback will notify users when a particular |
| 37 | * motion event has occurred. This class should only be used with {@link MotionEvent}s |
| 38 | * reported via touch (don't use for trackball events). |
| 39 | * |
| 40 | * To use this class: |
| 41 | * <ul> |
| 42 | * <li>Create an instance of the {@code GestureDetector} for your {@link View} |
| 43 | * <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call |
| 44 | * {@link #onTouchEvent(MotionEvent)}. The methods defined in your callback |
| 45 | * will be executed when the events occur. |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 46 | * <li>If listening for {@link OnContextClickListener#onContextClick(MotionEvent)} |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 47 | * you must call {@link #onGenericMotionEvent(MotionEvent)} |
| 48 | * in {@link View#onGenericMotionEvent(MotionEvent)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * </ul> |
| 50 | */ |
| 51 | public class GestureDetector { |
| 52 | /** |
| 53 | * The listener that is used to notify when gestures occur. |
| 54 | * If you want to listen for all the different gestures then implement |
| 55 | * this interface. If you only want to listen for a subset it might |
| 56 | * be easier to extend {@link SimpleOnGestureListener}. |
| 57 | */ |
| 58 | public interface OnGestureListener { |
| 59 | |
| 60 | /** |
| 61 | * Notified when a tap occurs with the down {@link MotionEvent} |
| 62 | * that triggered it. This will be triggered immediately for |
| 63 | * every down event. All other events should be preceded by this. |
| 64 | * |
| 65 | * @param e The down motion event. |
| 66 | */ |
| 67 | boolean onDown(MotionEvent e); |
| 68 | |
| 69 | /** |
| 70 | * The user has performed a down {@link MotionEvent} and not performed |
| 71 | * a move or up yet. This event is commonly used to provide visual |
| 72 | * feedback to the user to let them know that their action has been |
| 73 | * recognized i.e. highlight an element. |
| 74 | * |
| 75 | * @param e The down motion event |
| 76 | */ |
| 77 | void onShowPress(MotionEvent e); |
| 78 | |
| 79 | /** |
| 80 | * Notified when a tap occurs with the up {@link MotionEvent} |
| 81 | * that triggered it. |
| 82 | * |
| 83 | * @param e The up motion event that completed the first tap |
| 84 | * @return true if the event is consumed, else false |
| 85 | */ |
| 86 | boolean onSingleTapUp(MotionEvent e); |
| 87 | |
| 88 | /** |
| 89 | * Notified when a scroll occurs with the initial on down {@link MotionEvent} and the |
| 90 | * current move {@link MotionEvent}. The distance in x and y is also supplied for |
| 91 | * convenience. |
| 92 | * |
| 93 | * @param e1 The first down motion event that started the scrolling. |
| 94 | * @param e2 The move motion event that triggered the current onScroll. |
| 95 | * @param distanceX The distance along the X axis that has been scrolled since the last |
| 96 | * call to onScroll. This is NOT the distance between {@code e1} |
| 97 | * and {@code e2}. |
| 98 | * @param distanceY The distance along the Y axis that has been scrolled since the last |
| 99 | * call to onScroll. This is NOT the distance between {@code e1} |
| 100 | * and {@code e2}. |
| 101 | * @return true if the event is consumed, else false |
| 102 | */ |
| 103 | boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY); |
| 104 | |
| 105 | /** |
| 106 | * Notified when a long press occurs with the initial on down {@link MotionEvent} |
| 107 | * that trigged it. |
| 108 | * |
| 109 | * @param e The initial on down motion event that started the longpress. |
| 110 | */ |
| 111 | void onLongPress(MotionEvent e); |
| 112 | |
| 113 | /** |
| 114 | * Notified of a fling event when it occurs with the initial on down {@link MotionEvent} |
| 115 | * and the matching up {@link MotionEvent}. The calculated velocity is supplied along |
| 116 | * the x and y axis in pixels per second. |
| 117 | * |
| 118 | * @param e1 The first down motion event that started the fling. |
| 119 | * @param e2 The move motion event that triggered the current onFling. |
| 120 | * @param velocityX The velocity of this fling measured in pixels per second |
| 121 | * along the x axis. |
| 122 | * @param velocityY The velocity of this fling measured in pixels per second |
| 123 | * along the y axis. |
| 124 | * @return true if the event is consumed, else false |
| 125 | */ |
| 126 | boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * The listener that is used to notify when a double-tap or a confirmed |
| 131 | * single-tap occur. |
| 132 | */ |
| 133 | public interface OnDoubleTapListener { |
| 134 | /** |
| 135 | * Notified when a single-tap occurs. |
| 136 | * <p> |
| 137 | * Unlike {@link OnGestureListener#onSingleTapUp(MotionEvent)}, this |
| 138 | * will only be called after the detector is confident that the user's |
| 139 | * first tap is not followed by a second tap leading to a double-tap |
| 140 | * gesture. |
| 141 | * |
| 142 | * @param e The down motion event of the single-tap. |
| 143 | * @return true if the event is consumed, else false |
| 144 | */ |
| 145 | boolean onSingleTapConfirmed(MotionEvent e); |
| 146 | |
| 147 | /** |
Paul Sowden | afe846d | 2019-10-31 11:01:56 -0700 | [diff] [blame] | 148 | * Notified when a double-tap occurs. Triggered on the down event of second tap. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | * |
| 150 | * @param e The down motion event of the first tap of the double-tap. |
| 151 | * @return true if the event is consumed, else false |
| 152 | */ |
| 153 | boolean onDoubleTap(MotionEvent e); |
| 154 | |
| 155 | /** |
| 156 | * Notified when an event within a double-tap gesture occurs, including |
| 157 | * the down, move, and up events. |
| 158 | * |
| 159 | * @param e The motion event that occurred during the double-tap gesture. |
| 160 | * @return true if the event is consumed, else false |
| 161 | */ |
| 162 | boolean onDoubleTapEvent(MotionEvent e); |
| 163 | } |
| 164 | |
| 165 | /** |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 166 | * The listener that is used to notify when a context click occurs. When listening for a |
| 167 | * context click ensure that you call {@link #onGenericMotionEvent(MotionEvent)} in |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 168 | * {@link View#onGenericMotionEvent(MotionEvent)}. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 169 | */ |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 170 | public interface OnContextClickListener { |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 171 | /** |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 172 | * Notified when a context click occurs. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 173 | * |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 174 | * @param e The motion event that occurred during the context click. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 175 | * @return true if the event is consumed, else false |
| 176 | */ |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 177 | boolean onContextClick(MotionEvent e); |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | * A convenience class to extend when you only want to listen for a subset |
| 182 | * of all the gestures. This implements all methods in the |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 183 | * {@link OnGestureListener}, {@link OnDoubleTapListener}, and {@link OnContextClickListener} |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 184 | * 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] | 185 | */ |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 186 | public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener, |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 187 | OnContextClickListener { |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | public boolean onSingleTapUp(MotionEvent e) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | public void onLongPress(MotionEvent e) { |
| 194 | } |
| 195 | |
| 196 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
| 197 | float distanceX, float distanceY) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, |
| 202 | float velocityY) { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | public void onShowPress(MotionEvent e) { |
| 207 | } |
| 208 | |
| 209 | public boolean onDown(MotionEvent e) { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | public boolean onDoubleTap(MotionEvent e) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | public boolean onDoubleTapEvent(MotionEvent e) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | public boolean onSingleTapConfirmed(MotionEvent e) { |
| 222 | return false; |
| 223 | } |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 224 | |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 225 | public boolean onContextClick(MotionEvent e) { |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 230 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | private int mTouchSlopSquare; |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 232 | private int mDoubleTapTouchSlopSquare; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | private int mDoubleTapSlopSquare; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 234 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | private int mMinimumFlingVelocity; |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 236 | private int mMaximumFlingVelocity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | |
Jackal Guo | 198a262 | 2018-11-19 11:51:31 +0800 | [diff] [blame] | 238 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout(); |
| 240 | private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout(); |
Adam Powell | af1785f | 2013-09-05 13:44:45 -0700 | [diff] [blame] | 242 | 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] | 243 | |
| 244 | // constants for Message.what used by GestureHandler below |
| 245 | private static final int SHOW_PRESS = 1; |
| 246 | private static final int LONG_PRESS = 2; |
| 247 | private static final int TAP = 3; |
| 248 | |
| 249 | private final Handler mHandler; |
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 final OnGestureListener mListener; |
| 252 | private OnDoubleTapListener mDoubleTapListener; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 253 | private OnContextClickListener mContextClickListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
| 255 | private boolean mStillDown; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 256 | private boolean mDeferConfirmSingleTap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | private boolean mInLongPress; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 258 | private boolean mInContextClick; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 259 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | private boolean mAlwaysInTapRegion; |
| 261 | private boolean mAlwaysInBiggerTapRegion; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 262 | private boolean mIgnoreNextUpEvent; |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 263 | // Whether a classification has been recorded by statsd for the current event stream. Reset on |
| 264 | // ACTION_DOWN. |
| 265 | private boolean mHasRecordedClassification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
| 267 | private MotionEvent mCurrentDownEvent; |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 268 | private MotionEvent mCurrentMotionEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | private MotionEvent mPreviousUpEvent; |
| 270 | |
| 271 | /** |
| 272 | * True when the user is still touching for the second tap (down, move, and |
| 273 | * up events). Can only be true if there is a double tap listener attached. |
| 274 | */ |
| 275 | private boolean mIsDoubleTapping; |
| 276 | |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 277 | private float mLastFocusX; |
| 278 | private float mLastFocusY; |
| 279 | private float mDownFocusX; |
| 280 | private float mDownFocusY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | |
| 282 | private boolean mIsLongpressEnabled; |
| 283 | |
| 284 | /** |
| 285 | * Determines speed during touch scrolling |
| 286 | */ |
| 287 | private VelocityTracker mVelocityTracker; |
| 288 | |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 289 | /** |
| 290 | * Consistency verifier for debugging purposes. |
| 291 | */ |
| 292 | private final InputEventConsistencyVerifier mInputEventConsistencyVerifier = |
| 293 | InputEventConsistencyVerifier.isInstrumentationEnabled() ? |
| 294 | new InputEventConsistencyVerifier(this, 0) : null; |
| 295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | private class GestureHandler extends Handler { |
| 297 | GestureHandler() { |
| 298 | super(); |
| 299 | } |
| 300 | |
| 301 | GestureHandler(Handler handler) { |
| 302 | super(handler.getLooper()); |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void handleMessage(Message msg) { |
| 307 | switch (msg.what) { |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 308 | case SHOW_PRESS: |
| 309 | mListener.onShowPress(mCurrentDownEvent); |
| 310 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 312 | case LONG_PRESS: |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 313 | recordGestureClassification(msg.arg1); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 314 | dispatchLongPress(); |
| 315 | break; |
| 316 | |
| 317 | case TAP: |
| 318 | // If the user's finger is still down, do not count it as a tap |
| 319 | if (mDoubleTapListener != null) { |
| 320 | if (!mStillDown) { |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 321 | recordGestureClassification( |
| 322 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__SINGLE_TAP); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 323 | mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent); |
| 324 | } else { |
| 325 | mDeferConfirmSingleTap = true; |
| 326 | } |
| 327 | } |
| 328 | break; |
| 329 | |
| 330 | default: |
| 331 | throw new RuntimeException("Unknown message " + msg); //never |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Creates a GestureDetector with the supplied listener. |
| 338 | * This variant of the constructor should be used from a non-UI thread |
| 339 | * (as it allows specifying the Handler). |
| 340 | * |
| 341 | * @param listener the listener invoked for all the callbacks, this must |
| 342 | * not be null. |
| 343 | * @param handler the handler to use |
| 344 | * |
| 345 | * @throws NullPointerException if either {@code listener} or |
| 346 | * {@code handler} is null. |
| 347 | * |
| 348 | * @deprecated Use {@link #GestureDetector(android.content.Context, |
| 349 | * android.view.GestureDetector.OnGestureListener, android.os.Handler)} instead. |
| 350 | */ |
| 351 | @Deprecated |
| 352 | public GestureDetector(OnGestureListener listener, Handler handler) { |
| 353 | this(null, listener, handler); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Creates a GestureDetector with the supplied listener. |
| 358 | * You may only use this constructor from a UI thread (this is the usual situation). |
| 359 | * @see android.os.Handler#Handler() |
| 360 | * |
| 361 | * @param listener the listener invoked for all the callbacks, this must |
| 362 | * not be null. |
| 363 | * |
| 364 | * @throws NullPointerException if {@code listener} is null. |
| 365 | * |
| 366 | * @deprecated Use {@link #GestureDetector(android.content.Context, |
| 367 | * android.view.GestureDetector.OnGestureListener)} instead. |
| 368 | */ |
| 369 | @Deprecated |
| 370 | public GestureDetector(OnGestureListener listener) { |
| 371 | this(null, listener, null); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Creates a GestureDetector with the supplied listener. |
Scott Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 376 | * 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] | 377 | * @see android.os.Handler#Handler() |
| 378 | * |
| 379 | * @param context the application's context |
| 380 | * @param listener the listener invoked for all the callbacks, this must |
Paul Sowden | afe846d | 2019-10-31 11:01:56 -0700 | [diff] [blame] | 381 | * not be null. If the listener implements the {@link OnDoubleTapListener} or |
| 382 | * {@link OnContextClickListener} then it will also be set as the listener for |
| 383 | * these callbacks (for example when using the {@link SimpleOnGestureListener}). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | * |
| 385 | * @throws NullPointerException if {@code listener} is null. |
| 386 | */ |
| 387 | public GestureDetector(Context context, OnGestureListener listener) { |
| 388 | this(context, listener, null); |
| 389 | } |
| 390 | |
| 391 | /** |
Scott Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 392 | * Creates a GestureDetector with the supplied listener that runs deferred events on the |
| 393 | * thread associated with the supplied {@link android.os.Handler}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | * @see android.os.Handler#Handler() |
| 395 | * |
| 396 | * @param context the application's context |
| 397 | * @param listener the listener invoked for all the callbacks, this must |
Paul Sowden | afe846d | 2019-10-31 11:01:56 -0700 | [diff] [blame] | 398 | * not be null. If the listener implements the {@link OnDoubleTapListener} or |
| 399 | * {@link OnContextClickListener} then it will also be set as the listener for |
| 400 | * these callbacks (for example when using the {@link SimpleOnGestureListener}). |
Scott Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 401 | * @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] | 402 | * |
| 403 | * @throws NullPointerException if {@code listener} is null. |
| 404 | */ |
| 405 | public GestureDetector(Context context, OnGestureListener listener, Handler handler) { |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 406 | if (handler != null) { |
| 407 | mHandler = new GestureHandler(handler); |
| 408 | } else { |
| 409 | mHandler = new GestureHandler(); |
| 410 | } |
| 411 | mListener = listener; |
| 412 | if (listener instanceof OnDoubleTapListener) { |
| 413 | setOnDoubleTapListener((OnDoubleTapListener) listener); |
| 414 | } |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 415 | if (listener instanceof OnContextClickListener) { |
| 416 | setContextClickListener((OnContextClickListener) listener); |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 417 | } |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 418 | init(context); |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Scott Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 422 | * Creates a GestureDetector with the supplied listener that runs deferred events on the |
| 423 | * thread associated with the supplied {@link android.os.Handler}. |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 424 | * @see android.os.Handler#Handler() |
| 425 | * |
| 426 | * @param context the application's context |
| 427 | * @param listener the listener invoked for all the callbacks, this must |
| 428 | * not be null. |
Scott Main | c27ea25 | 2013-08-30 13:56:05 -0700 | [diff] [blame] | 429 | * @param handler the handler to use for running deferred listener events. |
| 430 | * @param unused currently not used. |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 431 | * |
| 432 | * @throws NullPointerException if {@code listener} is null. |
| 433 | */ |
| 434 | public GestureDetector(Context context, OnGestureListener listener, Handler handler, |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 435 | boolean unused) { |
| 436 | this(context, listener, handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 439 | private void init(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | if (mListener == null) { |
| 441 | throw new NullPointerException("OnGestureListener must not be null"); |
| 442 | } |
| 443 | mIsLongpressEnabled = true; |
| 444 | |
| 445 | // Fallback to support pre-donuts releases |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 446 | int touchSlop, doubleTapSlop, doubleTapTouchSlop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | if (context == null) { |
| 448 | //noinspection deprecation |
| 449 | touchSlop = ViewConfiguration.getTouchSlop(); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 450 | 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] | 451 | doubleTapSlop = ViewConfiguration.getDoubleTapSlop(); |
| 452 | //noinspection deprecation |
| 453 | mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity(); |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 454 | mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | } else { |
| 456 | final ViewConfiguration configuration = ViewConfiguration.get(context); |
| 457 | touchSlop = configuration.getScaledTouchSlop(); |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 458 | doubleTapTouchSlop = configuration.getScaledDoubleTapTouchSlop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | doubleTapSlop = configuration.getScaledDoubleTapSlop(); |
| 460 | mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity(); |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 461 | mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
| 463 | mTouchSlopSquare = touchSlop * touchSlop; |
Gilles Debunne | 006fa48 | 2011-10-27 17:23:36 -0700 | [diff] [blame] | 464 | mDoubleTapTouchSlopSquare = doubleTapTouchSlop * doubleTapTouchSlop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Sets the listener which will be called for double-tap and related |
| 470 | * gestures. |
| 471 | * |
| 472 | * @param onDoubleTapListener the listener invoked for all the callbacks, or |
| 473 | * null to stop listening for double-tap gestures. |
| 474 | */ |
| 475 | public void setOnDoubleTapListener(OnDoubleTapListener onDoubleTapListener) { |
| 476 | mDoubleTapListener = onDoubleTapListener; |
| 477 | } |
| 478 | |
| 479 | /** |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 480 | * Sets the listener which will be called for context clicks. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 481 | * |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 482 | * @param onContextClickListener the listener invoked for all the callbacks, or null to stop |
| 483 | * listening for context clicks. |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 484 | */ |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 485 | public void setContextClickListener(OnContextClickListener onContextClickListener) { |
| 486 | mContextClickListener = onContextClickListener; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | * Set whether longpress is enabled, if this is enabled when a user |
| 491 | * presses and holds down you get a longpress event and nothing further. |
| 492 | * If it's disabled the user can press and hold down and then later |
| 493 | * moved their finger and you will get scroll events. By default |
| 494 | * longpress is enabled. |
| 495 | * |
| 496 | * @param isLongpressEnabled whether longpress should be enabled. |
| 497 | */ |
| 498 | public void setIsLongpressEnabled(boolean isLongpressEnabled) { |
| 499 | mIsLongpressEnabled = isLongpressEnabled; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * @return true if longpress is enabled, else false. |
| 504 | */ |
| 505 | public boolean isLongpressEnabled() { |
| 506 | return mIsLongpressEnabled; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Analyzes the given motion event and if applicable triggers the |
| 511 | * appropriate callbacks on the {@link OnGestureListener} supplied. |
| 512 | * |
| 513 | * @param ev The current motion event. |
| 514 | * @return true if the {@link OnGestureListener} consumed the event, |
| 515 | * else false. |
| 516 | */ |
| 517 | public boolean onTouchEvent(MotionEvent ev) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 518 | if (mInputEventConsistencyVerifier != null) { |
| 519 | mInputEventConsistencyVerifier.onTouchEvent(ev, 0); |
| 520 | } |
| 521 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | final int action = ev.getAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 524 | if (mCurrentMotionEvent != null) { |
| 525 | mCurrentMotionEvent.recycle(); |
| 526 | } |
| 527 | mCurrentMotionEvent = MotionEvent.obtain(ev); |
| 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | if (mVelocityTracker == null) { |
| 530 | mVelocityTracker = VelocityTracker.obtain(); |
| 531 | } |
| 532 | mVelocityTracker.addMovement(ev); |
| 533 | |
Adam Powell | f90165a | 2012-08-31 11:11:39 -0700 | [diff] [blame] | 534 | final boolean pointerUp = |
| 535 | (action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 536 | final int skipIndex = pointerUp ? ev.getActionIndex() : -1; |
Dennis Kempin | ac1b31d | 2016-11-02 17:02:25 -0700 | [diff] [blame] | 537 | final boolean isGeneratedGesture = |
| 538 | (ev.getFlags() & MotionEvent.FLAG_IS_GENERATED_GESTURE) != 0; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 539 | |
| 540 | // Determine focal point |
| 541 | float sumX = 0, sumY = 0; |
| 542 | final int count = ev.getPointerCount(); |
| 543 | for (int i = 0; i < count; i++) { |
| 544 | if (skipIndex == i) continue; |
| 545 | sumX += ev.getX(i); |
| 546 | sumY += ev.getY(i); |
| 547 | } |
| 548 | final int div = pointerUp ? count - 1 : count; |
| 549 | final float focusX = sumX / div; |
| 550 | final float focusY = sumY / div; |
| 551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | boolean handled = false; |
| 553 | |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 554 | switch (action & MotionEvent.ACTION_MASK) { |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 555 | case MotionEvent.ACTION_POINTER_DOWN: |
| 556 | mDownFocusX = mLastFocusX = focusX; |
| 557 | mDownFocusY = mLastFocusY = focusY; |
| 558 | // Cancel long press and taps |
| 559 | cancelTaps(); |
| 560 | break; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 561 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 562 | case MotionEvent.ACTION_POINTER_UP: |
| 563 | mDownFocusX = mLastFocusX = focusX; |
| 564 | mDownFocusY = mLastFocusY = focusY; |
Adam Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 565 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 566 | // Check the dot product of current velocities. |
| 567 | // If the pointer that left was opposing another velocity vector, clear. |
| 568 | mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); |
| 569 | final int upIndex = ev.getActionIndex(); |
| 570 | final int id1 = ev.getPointerId(upIndex); |
| 571 | final float x1 = mVelocityTracker.getXVelocity(id1); |
| 572 | final float y1 = mVelocityTracker.getYVelocity(id1); |
| 573 | for (int i = 0; i < count; i++) { |
| 574 | if (i == upIndex) continue; |
Adam Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 575 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 576 | final int id2 = ev.getPointerId(i); |
| 577 | final float x = x1 * mVelocityTracker.getXVelocity(id2); |
| 578 | final float y = y1 * mVelocityTracker.getYVelocity(id2); |
Adam Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 579 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 580 | final float dot = x + y; |
| 581 | if (dot < 0) { |
| 582 | mVelocityTracker.clear(); |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | break; |
| 587 | |
| 588 | case MotionEvent.ACTION_DOWN: |
| 589 | if (mDoubleTapListener != null) { |
| 590 | boolean hadTapMessage = mHandler.hasMessages(TAP); |
| 591 | if (hadTapMessage) mHandler.removeMessages(TAP); |
| 592 | if ((mCurrentDownEvent != null) && (mPreviousUpEvent != null) |
| 593 | && hadTapMessage |
| 594 | && isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, ev)) { |
| 595 | // This is a second tap |
| 596 | mIsDoubleTapping = true; |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 597 | recordGestureClassification( |
| 598 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DOUBLE_TAP); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 599 | // Give a callback with the first tap of the double-tap |
| 600 | handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent); |
| 601 | // Give a callback with down event of the double-tap |
| 602 | handled |= mDoubleTapListener.onDoubleTapEvent(ev); |
| 603 | } else { |
| 604 | // This is a first tap |
| 605 | mHandler.sendEmptyMessageDelayed(TAP, DOUBLE_TAP_TIMEOUT); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | mDownFocusX = mLastFocusX = focusX; |
| 610 | mDownFocusY = mLastFocusY = focusY; |
| 611 | if (mCurrentDownEvent != null) { |
| 612 | mCurrentDownEvent.recycle(); |
| 613 | } |
| 614 | mCurrentDownEvent = MotionEvent.obtain(ev); |
| 615 | mAlwaysInTapRegion = true; |
| 616 | mAlwaysInBiggerTapRegion = true; |
| 617 | mStillDown = true; |
| 618 | mInLongPress = false; |
| 619 | mDeferConfirmSingleTap = false; |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 620 | mHasRecordedClassification = false; |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 621 | |
| 622 | if (mIsLongpressEnabled) { |
| 623 | mHandler.removeMessages(LONG_PRESS); |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 624 | mHandler.sendMessageAtTime( |
| 625 | mHandler.obtainMessage( |
| 626 | LONG_PRESS, |
| 627 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__LONG_PRESS, |
| 628 | 0 /* arg2 */), |
| 629 | mCurrentDownEvent.getDownTime() |
| 630 | + ViewConfiguration.getLongPressTimeout()); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 631 | } |
| 632 | mHandler.sendEmptyMessageAtTime(SHOW_PRESS, |
| 633 | mCurrentDownEvent.getDownTime() + TAP_TIMEOUT); |
| 634 | handled |= mListener.onDown(ev); |
| 635 | break; |
| 636 | |
| 637 | case MotionEvent.ACTION_MOVE: |
| 638 | if (mInLongPress || mInContextClick) { |
Adam Powell | cd66359 | 2012-09-11 09:52:37 -0700 | [diff] [blame] | 639 | break; |
| 640 | } |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 641 | |
| 642 | final int motionClassification = ev.getClassification(); |
| 643 | final boolean hasPendingLongPress = mHandler.hasMessages(LONG_PRESS); |
| 644 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 645 | final float scrollX = mLastFocusX - focusX; |
| 646 | final float scrollY = mLastFocusY - focusY; |
| 647 | if (mIsDoubleTapping) { |
| 648 | // Give the move events of the double-tap |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 649 | recordGestureClassification( |
| 650 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DOUBLE_TAP); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | handled |= mDoubleTapListener.onDoubleTapEvent(ev); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 652 | } else if (mAlwaysInTapRegion) { |
| 653 | final int deltaX = (int) (focusX - mDownFocusX); |
| 654 | final int deltaY = (int) (focusY - mDownFocusY); |
| 655 | int distance = (deltaX * deltaX) + (deltaY * deltaY); |
| 656 | int slopSquare = isGeneratedGesture ? 0 : mTouchSlopSquare; |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 657 | |
| 658 | final boolean ambiguousGesture = |
| 659 | motionClassification == MotionEvent.CLASSIFICATION_AMBIGUOUS_GESTURE; |
| 660 | final boolean shouldInhibitDefaultAction = |
| 661 | hasPendingLongPress && ambiguousGesture; |
| 662 | if (shouldInhibitDefaultAction) { |
| 663 | // Inhibit default long press |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 664 | final float multiplier = ViewConfiguration.getAmbiguousGestureMultiplier(); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 665 | if (distance > slopSquare) { |
| 666 | // The default action here is to remove long press. But if the touch |
| 667 | // slop below gets increased, and we never exceed the modified touch |
| 668 | // slop while still receiving AMBIGUOUS_GESTURE, we risk that *nothing* |
| 669 | // will happen in response to user input. To prevent this, |
| 670 | // reschedule long press with a modified timeout. |
| 671 | mHandler.removeMessages(LONG_PRESS); |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 672 | final long longPressTimeout = ViewConfiguration.getLongPressTimeout(); |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 673 | mHandler.sendMessageAtTime( |
| 674 | mHandler.obtainMessage( |
| 675 | LONG_PRESS, |
| 676 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__LONG_PRESS, |
| 677 | 0 /* arg2 */), |
| 678 | ev.getDownTime() + (long) (longPressTimeout * multiplier)); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 679 | } |
| 680 | // Inhibit default scroll. If a gesture is ambiguous, we prevent scroll |
| 681 | // until the gesture is resolved. |
| 682 | // However, for safety, simply increase the touch slop in case the |
| 683 | // classification is erroneous. Since the value is squared, multiply twice. |
Siarhei Vishniakou | 8b047dd | 2019-01-24 16:04:13 -0800 | [diff] [blame] | 684 | slopSquare *= multiplier * multiplier; |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 687 | if (distance > slopSquare) { |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 688 | recordGestureClassification( |
| 689 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__SCROLL); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 690 | handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY); |
| 691 | mLastFocusX = focusX; |
| 692 | mLastFocusY = focusY; |
| 693 | mAlwaysInTapRegion = false; |
| 694 | mHandler.removeMessages(TAP); |
| 695 | mHandler.removeMessages(SHOW_PRESS); |
| 696 | mHandler.removeMessages(LONG_PRESS); |
| 697 | } |
| 698 | int doubleTapSlopSquare = isGeneratedGesture ? 0 : mDoubleTapTouchSlopSquare; |
| 699 | if (distance > doubleTapSlopSquare) { |
| 700 | mAlwaysInBiggerTapRegion = false; |
| 701 | } |
| 702 | } else if ((Math.abs(scrollX) >= 1) || (Math.abs(scrollY) >= 1)) { |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 703 | recordGestureClassification(TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__SCROLL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY); |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 705 | mLastFocusX = focusX; |
| 706 | mLastFocusY = focusY; |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 707 | } |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 708 | final boolean deepPress = |
| 709 | motionClassification == MotionEvent.CLASSIFICATION_DEEP_PRESS; |
| 710 | if (deepPress && hasPendingLongPress) { |
| 711 | mHandler.removeMessages(LONG_PRESS); |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 712 | mHandler.sendMessage( |
| 713 | mHandler.obtainMessage( |
| 714 | LONG_PRESS, |
| 715 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DEEP_PRESS, |
| 716 | 0 /* arg2 */)); |
Siarhei Vishniakou | 07f440a | 2017-12-12 16:35:19 -0800 | [diff] [blame] | 717 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 718 | break; |
| 719 | |
| 720 | case MotionEvent.ACTION_UP: |
| 721 | mStillDown = false; |
| 722 | MotionEvent currentUpEvent = MotionEvent.obtain(ev); |
| 723 | if (mIsDoubleTapping) { |
| 724 | // Finally, give the up event of the double-tap |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 725 | recordGestureClassification( |
| 726 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DOUBLE_TAP); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 727 | handled |= mDoubleTapListener.onDoubleTapEvent(ev); |
| 728 | } else if (mInLongPress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | mHandler.removeMessages(TAP); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 730 | mInLongPress = false; |
| 731 | } else if (mAlwaysInTapRegion && !mIgnoreNextUpEvent) { |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 732 | recordGestureClassification( |
| 733 | TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__SINGLE_TAP); |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 734 | handled = mListener.onSingleTapUp(ev); |
| 735 | if (mDeferConfirmSingleTap && mDoubleTapListener != null) { |
| 736 | mDoubleTapListener.onSingleTapConfirmed(ev); |
| 737 | } |
| 738 | } else if (!mIgnoreNextUpEvent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 740 | // A fling must travel the minimum tap distance |
| 741 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 742 | final int pointerId = ev.getPointerId(0); |
| 743 | velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); |
| 744 | final float velocityY = velocityTracker.getYVelocity(pointerId); |
| 745 | final float velocityX = velocityTracker.getXVelocity(pointerId); |
| 746 | |
| 747 | if ((Math.abs(velocityY) > mMinimumFlingVelocity) |
| 748 | || (Math.abs(velocityX) > mMinimumFlingVelocity)) { |
| 749 | handled = mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY); |
| 750 | } |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 751 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 752 | if (mPreviousUpEvent != null) { |
| 753 | mPreviousUpEvent.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | } |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 755 | // Hold the event we obtained above - listeners may have changed the original. |
| 756 | mPreviousUpEvent = currentUpEvent; |
| 757 | if (mVelocityTracker != null) { |
| 758 | // This may have been cleared when we called out to the |
| 759 | // application above. |
| 760 | mVelocityTracker.recycle(); |
| 761 | mVelocityTracker = null; |
| 762 | } |
| 763 | mIsDoubleTapping = false; |
| 764 | mDeferConfirmSingleTap = false; |
| 765 | mIgnoreNextUpEvent = false; |
| 766 | mHandler.removeMessages(SHOW_PRESS); |
| 767 | mHandler.removeMessages(LONG_PRESS); |
| 768 | break; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 769 | |
Siarhei Vishniakou | d60090d | 2017-12-12 16:24:26 -0800 | [diff] [blame] | 770 | case MotionEvent.ACTION_CANCEL: |
| 771 | cancel(); |
| 772 | break; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 776 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | } |
| 778 | return handled; |
| 779 | } |
| 780 | |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 781 | /** |
| 782 | * Analyzes the given generic motion event and if applicable triggers the |
| 783 | * appropriate callbacks on the {@link OnGestureListener} supplied. |
| 784 | * |
| 785 | * @param ev The current motion event. |
| 786 | * @return true if the {@link OnGestureListener} consumed the event, |
| 787 | * else false. |
| 788 | */ |
| 789 | public boolean onGenericMotionEvent(MotionEvent ev) { |
| 790 | if (mInputEventConsistencyVerifier != null) { |
| 791 | mInputEventConsistencyVerifier.onGenericMotionEvent(ev, 0); |
| 792 | } |
| 793 | |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 794 | final int actionButton = ev.getActionButton(); |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 795 | switch (ev.getActionMasked()) { |
| 796 | case MotionEvent.ACTION_BUTTON_PRESS: |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 797 | if (mContextClickListener != null && !mInContextClick && !mInLongPress |
| 798 | && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY |
| 799 | || actionButton == MotionEvent.BUTTON_SECONDARY)) { |
| 800 | if (mContextClickListener.onContextClick(ev)) { |
| 801 | mInContextClick = true; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 802 | mHandler.removeMessages(LONG_PRESS); |
| 803 | mHandler.removeMessages(TAP); |
| 804 | return true; |
| 805 | } |
| 806 | } |
| 807 | break; |
| 808 | |
| 809 | case MotionEvent.ACTION_BUTTON_RELEASE: |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 810 | if (mInContextClick && (actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY |
| 811 | || actionButton == MotionEvent.BUTTON_SECONDARY)) { |
| 812 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 813 | mIgnoreNextUpEvent = true; |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | return false; |
| 818 | } |
| 819 | |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 820 | private void cancel() { |
| 821 | mHandler.removeMessages(SHOW_PRESS); |
| 822 | mHandler.removeMessages(LONG_PRESS); |
| 823 | mHandler.removeMessages(TAP); |
| 824 | mVelocityTracker.recycle(); |
| 825 | mVelocityTracker = null; |
| 826 | mIsDoubleTapping = false; |
| 827 | mStillDown = false; |
Adam Powell | 17921ee | 2011-06-09 11:39:21 -0700 | [diff] [blame] | 828 | mAlwaysInTapRegion = false; |
| 829 | mAlwaysInBiggerTapRegion = false; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 830 | mDeferConfirmSingleTap = false; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 831 | mInLongPress = false; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 832 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 833 | mIgnoreNextUpEvent = false; |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 836 | private void cancelTaps() { |
| 837 | mHandler.removeMessages(SHOW_PRESS); |
| 838 | mHandler.removeMessages(LONG_PRESS); |
| 839 | mHandler.removeMessages(TAP); |
| 840 | mIsDoubleTapping = false; |
| 841 | mAlwaysInTapRegion = false; |
| 842 | mAlwaysInBiggerTapRegion = false; |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 843 | mDeferConfirmSingleTap = false; |
Mady Mellor | a6b1645 | 2015-04-14 18:03:34 -0700 | [diff] [blame] | 844 | mInLongPress = false; |
Mady Mellor | 015020e | 2015-06-05 14:41:22 -0700 | [diff] [blame] | 845 | mInContextClick = false; |
Mady Mellor | b093344 | 2015-05-27 14:15:57 -0700 | [diff] [blame] | 846 | mIgnoreNextUpEvent = false; |
Adam Powell | 05a1e1f | 2012-08-29 13:54:44 -0700 | [diff] [blame] | 847 | } |
| 848 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp, |
| 850 | MotionEvent secondDown) { |
| 851 | if (!mAlwaysInBiggerTapRegion) { |
| 852 | return false; |
| 853 | } |
| 854 | |
Adam Powell | af1785f | 2013-09-05 13:44:45 -0700 | [diff] [blame] | 855 | final long deltaTime = secondDown.getEventTime() - firstUp.getEventTime(); |
| 856 | 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] | 857 | return false; |
| 858 | } |
| 859 | |
| 860 | int deltaX = (int) firstDown.getX() - (int) secondDown.getX(); |
| 861 | int deltaY = (int) firstDown.getY() - (int) secondDown.getY(); |
Dennis Kempin | ac1b31d | 2016-11-02 17:02:25 -0700 | [diff] [blame] | 862 | final boolean isGeneratedGesture = |
| 863 | (firstDown.getFlags() & MotionEvent.FLAG_IS_GENERATED_GESTURE) != 0; |
| 864 | int slopSquare = isGeneratedGesture ? 0 : mDoubleTapSlopSquare; |
| 865 | return (deltaX * deltaX + deltaY * deltaY < slopSquare); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | private void dispatchLongPress() { |
| 869 | mHandler.removeMessages(TAP); |
Adam Powell | eca3e60 | 2013-02-15 11:26:45 -0800 | [diff] [blame] | 870 | mDeferConfirmSingleTap = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | mInLongPress = true; |
| 872 | mListener.onLongPress(mCurrentDownEvent); |
| 873 | } |
Philip Quinn | 5be044f | 2019-01-29 12:47:09 -0800 | [diff] [blame] | 874 | |
| 875 | private void recordGestureClassification(int classification) { |
| 876 | if (mHasRecordedClassification |
| 877 | || classification |
| 878 | == TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__UNKNOWN_CLASSIFICATION) { |
| 879 | // Only record the first classification for an event stream. |
| 880 | return; |
| 881 | } |
| 882 | if (mCurrentDownEvent == null || mCurrentMotionEvent == null) { |
| 883 | // If the complete event stream wasn't seen, don't record anything. |
| 884 | mHasRecordedClassification = true; |
| 885 | return; |
| 886 | } |
| 887 | StatsLog.write( |
| 888 | StatsLog.TOUCH_GESTURE_CLASSIFIED, |
| 889 | getClass().getName(), |
| 890 | classification, |
| 891 | (int) (SystemClock.uptimeMillis() - mCurrentMotionEvent.getDownTime()), |
| 892 | (float) Math.hypot(mCurrentMotionEvent.getRawX() - mCurrentDownEvent.getRawX(), |
| 893 | mCurrentMotionEvent.getRawY() - mCurrentDownEvent.getRawY())); |
| 894 | mHasRecordedClassification = true; |
| 895 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 896 | } |