John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 17 | package com.android.server.policy; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.Looper; |
| 22 | import android.os.SystemClock; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 23 | import android.util.Slog; |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 24 | import android.view.GestureDetector; |
Jun Mukai | 9946529 | 2015-10-01 18:42:33 +0900 | [diff] [blame] | 25 | import android.view.InputDevice; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Craig Mautner | 037aa8d | 2013-06-07 10:35:44 -0700 | [diff] [blame] | 27 | import android.view.WindowManagerPolicy.PointerEventListener; |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 28 | import android.widget.OverScroller; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Listens for system-wide input gestures, firing callbacks when detected. |
| 32 | * @hide |
| 33 | */ |
Craig Mautner | 037aa8d | 2013-06-07 10:35:44 -0700 | [diff] [blame] | 34 | public class SystemGesturesPointerEventListener implements PointerEventListener { |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 35 | private static final String TAG = "SystemGestures"; |
| 36 | private static final boolean DEBUG = false; |
| 37 | private static final long SWIPE_TIMEOUT_MS = 500; |
| 38 | private static final int MAX_TRACKED_POINTERS = 32; // max per input system |
| 39 | private static final int UNTRACKED_POINTER = -1; |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 40 | private static final int MAX_FLING_TIME_MILLIS = 5000; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 41 | |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 42 | private static final int SWIPE_NONE = 0; |
| 43 | private static final int SWIPE_FROM_TOP = 1; |
| 44 | private static final int SWIPE_FROM_BOTTOM = 2; |
| 45 | private static final int SWIPE_FROM_RIGHT = 3; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 46 | private static final int SWIPE_FROM_LEFT = 4; |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 47 | |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 48 | private final Context mContext; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 49 | private final int mSwipeStartThreshold; |
John Spurlock | 9ba21fd | 2013-07-29 08:57:32 -0400 | [diff] [blame] | 50 | private final int mSwipeDistanceThreshold; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 51 | private final Callbacks mCallbacks; |
| 52 | private final int[] mDownPointerId = new int[MAX_TRACKED_POINTERS]; |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 53 | private final float[] mDownX = new float[MAX_TRACKED_POINTERS]; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 54 | private final float[] mDownY = new float[MAX_TRACKED_POINTERS]; |
| 55 | private final long[] mDownTime = new long[MAX_TRACKED_POINTERS]; |
| 56 | |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 57 | private GestureDetector mGestureDetector; |
| 58 | private OverScroller mOverscroller; |
| 59 | |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 60 | int screenHeight; |
| 61 | int screenWidth; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 62 | private int mDownPointers; |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 63 | private boolean mSwipeFireable; |
| 64 | private boolean mDebugFireable; |
Jun Mukai | d2e7e35 | 2015-07-22 17:14:02 -0700 | [diff] [blame] | 65 | private boolean mMouseHoveringAtEdge; |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 66 | private long mLastFlingTime; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 67 | |
Craig Mautner | 037aa8d | 2013-06-07 10:35:44 -0700 | [diff] [blame] | 68 | public SystemGesturesPointerEventListener(Context context, Callbacks callbacks) { |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 69 | mContext = context; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 70 | mCallbacks = checkNull("callbacks", callbacks); |
| 71 | mSwipeStartThreshold = checkNull("context", context).getResources() |
| 72 | .getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height); |
John Spurlock | 9ba21fd | 2013-07-29 08:57:32 -0400 | [diff] [blame] | 73 | mSwipeDistanceThreshold = mSwipeStartThreshold; |
| 74 | if (DEBUG) Slog.d(TAG, "mSwipeStartThreshold=" + mSwipeStartThreshold |
| 75 | + " mSwipeDistanceThreshold=" + mSwipeDistanceThreshold); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private static <T> T checkNull(String name, T arg) { |
| 79 | if (arg == null) { |
| 80 | throw new IllegalArgumentException(name + " must not be null"); |
| 81 | } |
| 82 | return arg; |
| 83 | } |
| 84 | |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 85 | public void systemReady() { |
| 86 | Handler h = new Handler(Looper.myLooper()); |
| 87 | mGestureDetector = new GestureDetector(mContext, new FlingGestureDetector(), h); |
| 88 | mOverscroller = new OverScroller(mContext); |
| 89 | } |
| 90 | |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 91 | @Override |
Craig Mautner | 037aa8d | 2013-06-07 10:35:44 -0700 | [diff] [blame] | 92 | public void onPointerEvent(MotionEvent event) { |
Dennis Kempin | fb5168a | 2016-01-04 17:06:53 -0800 | [diff] [blame] | 93 | if (mGestureDetector != null && event.isTouchEvent()) { |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 94 | mGestureDetector.onTouchEvent(event); |
| 95 | } |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 96 | switch (event.getActionMasked()) { |
| 97 | case MotionEvent.ACTION_DOWN: |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 98 | mSwipeFireable = true; |
| 99 | mDebugFireable = true; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 100 | mDownPointers = 0; |
| 101 | captureDown(event, 0); |
Jun Mukai | d2e7e35 | 2015-07-22 17:14:02 -0700 | [diff] [blame] | 102 | if (mMouseHoveringAtEdge) { |
| 103 | mMouseHoveringAtEdge = false; |
| 104 | mCallbacks.onMouseLeaveFromEdge(); |
| 105 | } |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 106 | mCallbacks.onDown(); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 107 | break; |
| 108 | case MotionEvent.ACTION_POINTER_DOWN: |
| 109 | captureDown(event, event.getActionIndex()); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 110 | if (mDebugFireable) { |
| 111 | mDebugFireable = event.getPointerCount() < 5; |
| 112 | if (!mDebugFireable) { |
| 113 | if (DEBUG) Slog.d(TAG, "Firing debug"); |
| 114 | mCallbacks.onDebug(); |
| 115 | } |
| 116 | } |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 117 | break; |
| 118 | case MotionEvent.ACTION_MOVE: |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 119 | if (mSwipeFireable) { |
| 120 | final int swipe = detectSwipe(event); |
| 121 | mSwipeFireable = swipe == SWIPE_NONE; |
| 122 | if (swipe == SWIPE_FROM_TOP) { |
| 123 | if (DEBUG) Slog.d(TAG, "Firing onSwipeFromTop"); |
| 124 | mCallbacks.onSwipeFromTop(); |
| 125 | } else if (swipe == SWIPE_FROM_BOTTOM) { |
| 126 | if (DEBUG) Slog.d(TAG, "Firing onSwipeFromBottom"); |
| 127 | mCallbacks.onSwipeFromBottom(); |
| 128 | } else if (swipe == SWIPE_FROM_RIGHT) { |
| 129 | if (DEBUG) Slog.d(TAG, "Firing onSwipeFromRight"); |
| 130 | mCallbacks.onSwipeFromRight(); |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 131 | } else if (swipe == SWIPE_FROM_LEFT) { |
| 132 | if (DEBUG) Slog.d(TAG, "Firing onSwipeFromLeft"); |
| 133 | mCallbacks.onSwipeFromLeft(); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 134 | } |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 135 | } |
| 136 | break; |
Jun Mukai | d2e7e35 | 2015-07-22 17:14:02 -0700 | [diff] [blame] | 137 | case MotionEvent.ACTION_HOVER_MOVE: |
Jun Mukai | c5bd9bc | 2015-10-15 02:56:15 -0700 | [diff] [blame] | 138 | if (event.isFromSource(InputDevice.SOURCE_MOUSE)) { |
Jun Mukai | d2e7e35 | 2015-07-22 17:14:02 -0700 | [diff] [blame] | 139 | if (!mMouseHoveringAtEdge && event.getY() == 0) { |
| 140 | mCallbacks.onMouseHoverAtTop(); |
| 141 | mMouseHoveringAtEdge = true; |
| 142 | } else if (!mMouseHoveringAtEdge && event.getY() >= screenHeight - 1) { |
| 143 | mCallbacks.onMouseHoverAtBottom(); |
| 144 | mMouseHoveringAtEdge = true; |
| 145 | } else if (mMouseHoveringAtEdge |
| 146 | && (event.getY() > 0 && event.getY() < screenHeight - 1)) { |
| 147 | mCallbacks.onMouseLeaveFromEdge(); |
| 148 | mMouseHoveringAtEdge = false; |
| 149 | } |
| 150 | } |
| 151 | break; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 152 | case MotionEvent.ACTION_UP: |
| 153 | case MotionEvent.ACTION_CANCEL: |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 154 | mSwipeFireable = false; |
| 155 | mDebugFireable = false; |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 156 | mCallbacks.onUpOrCancel(); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | if (DEBUG) Slog.d(TAG, "Ignoring " + event); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private void captureDown(MotionEvent event, int pointerIndex) { |
| 164 | final int pointerId = event.getPointerId(pointerIndex); |
| 165 | final int i = findIndex(pointerId); |
| 166 | if (DEBUG) Slog.d(TAG, "pointer " + pointerId + |
| 167 | " down pointerIndex=" + pointerIndex + " trackingIndex=" + i); |
| 168 | if (i != UNTRACKED_POINTER) { |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 169 | mDownX[i] = event.getX(pointerIndex); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 170 | mDownY[i] = event.getY(pointerIndex); |
| 171 | mDownTime[i] = event.getEventTime(); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 172 | if (DEBUG) Slog.d(TAG, "pointer " + pointerId + |
| 173 | " down x=" + mDownX[i] + " y=" + mDownY[i]); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | private int findIndex(int pointerId) { |
| 178 | for (int i = 0; i < mDownPointers; i++) { |
| 179 | if (mDownPointerId[i] == pointerId) { |
| 180 | return i; |
| 181 | } |
| 182 | } |
| 183 | if (mDownPointers == MAX_TRACKED_POINTERS || pointerId == MotionEvent.INVALID_POINTER_ID) { |
| 184 | return UNTRACKED_POINTER; |
| 185 | } |
| 186 | mDownPointerId[mDownPointers++] = pointerId; |
| 187 | return mDownPointers - 1; |
| 188 | } |
| 189 | |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 190 | private int detectSwipe(MotionEvent move) { |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 191 | final int historySize = move.getHistorySize(); |
| 192 | final int pointerCount = move.getPointerCount(); |
| 193 | for (int p = 0; p < pointerCount; p++) { |
| 194 | final int pointerId = move.getPointerId(p); |
| 195 | final int i = findIndex(pointerId); |
| 196 | if (i != UNTRACKED_POINTER) { |
| 197 | for (int h = 0; h < historySize; h++) { |
| 198 | final long time = move.getHistoricalEventTime(h); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 199 | final float x = move.getHistoricalX(p, h); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 200 | final float y = move.getHistoricalY(p, h); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 201 | final int swipe = detectSwipe(i, time, x, y); |
| 202 | if (swipe != SWIPE_NONE) { |
| 203 | return swipe; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 206 | final int swipe = detectSwipe(i, move.getEventTime(), move.getX(p), move.getY(p)); |
| 207 | if (swipe != SWIPE_NONE) { |
| 208 | return swipe; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 212 | return SWIPE_NONE; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 213 | } |
| 214 | |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 215 | private int detectSwipe(int i, long time, float x, float y) { |
| 216 | final float fromX = mDownX[i]; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 217 | final float fromY = mDownY[i]; |
| 218 | final long elapsed = time - mDownTime[i]; |
| 219 | if (DEBUG) Slog.d(TAG, "pointer " + mDownPointerId[i] |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 220 | + " moved (" + fromX + "->" + x + "," + fromY + "->" + y + ") in " + elapsed); |
| 221 | if (fromY <= mSwipeStartThreshold |
John Spurlock | 9ba21fd | 2013-07-29 08:57:32 -0400 | [diff] [blame] | 222 | && y > fromY + mSwipeDistanceThreshold |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 223 | && elapsed < SWIPE_TIMEOUT_MS) { |
| 224 | return SWIPE_FROM_TOP; |
| 225 | } |
| 226 | if (fromY >= screenHeight - mSwipeStartThreshold |
John Spurlock | 9ba21fd | 2013-07-29 08:57:32 -0400 | [diff] [blame] | 227 | && y < fromY - mSwipeDistanceThreshold |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 228 | && elapsed < SWIPE_TIMEOUT_MS) { |
| 229 | return SWIPE_FROM_BOTTOM; |
| 230 | } |
| 231 | if (fromX >= screenWidth - mSwipeStartThreshold |
John Spurlock | 9ba21fd | 2013-07-29 08:57:32 -0400 | [diff] [blame] | 232 | && x < fromX - mSwipeDistanceThreshold |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 233 | && elapsed < SWIPE_TIMEOUT_MS) { |
| 234 | return SWIPE_FROM_RIGHT; |
| 235 | } |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 236 | if (fromX <= mSwipeStartThreshold |
| 237 | && x > fromX + mSwipeDistanceThreshold |
| 238 | && elapsed < SWIPE_TIMEOUT_MS) { |
| 239 | return SWIPE_FROM_LEFT; |
| 240 | } |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 241 | return SWIPE_NONE; |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 244 | private final class FlingGestureDetector extends GestureDetector.SimpleOnGestureListener { |
| 245 | @Override |
| 246 | public boolean onSingleTapUp(MotionEvent e) { |
| 247 | if (!mOverscroller.isFinished()) { |
| 248 | mOverscroller.forceFinished(true); |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | @Override |
| 253 | public boolean onFling(MotionEvent down, MotionEvent up, |
| 254 | float velocityX, float velocityY) { |
| 255 | mOverscroller.computeScrollOffset(); |
| 256 | long now = SystemClock.uptimeMillis(); |
| 257 | |
| 258 | if (mLastFlingTime != 0 && now > mLastFlingTime + MAX_FLING_TIME_MILLIS) { |
| 259 | mOverscroller.forceFinished(true); |
| 260 | } |
| 261 | mOverscroller.fling(0, 0, (int)velocityX, (int)velocityY, |
| 262 | Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE); |
| 263 | int duration = mOverscroller.getDuration(); |
| 264 | if (duration > MAX_FLING_TIME_MILLIS) { |
| 265 | duration = MAX_FLING_TIME_MILLIS; |
| 266 | } |
| 267 | mLastFlingTime = now; |
| 268 | mCallbacks.onFling(duration); |
| 269 | return true; |
| 270 | } |
| 271 | } |
| 272 | |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 273 | interface Callbacks { |
| 274 | void onSwipeFromTop(); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 275 | void onSwipeFromBottom(); |
| 276 | void onSwipeFromRight(); |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 277 | void onSwipeFromLeft(); |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 278 | void onFling(int durationMs); |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 279 | void onDown(); |
| 280 | void onUpOrCancel(); |
Jun Mukai | d2e7e35 | 2015-07-22 17:14:02 -0700 | [diff] [blame] | 281 | void onMouseHoverAtTop(); |
| 282 | void onMouseHoverAtBottom(); |
| 283 | void onMouseLeaveFromEdge(); |
John Spurlock | ad3e6cb | 2013-04-30 08:47:43 -0400 | [diff] [blame] | 284 | void onDebug(); |
John Spurlock | 57306e6 | 2013-04-22 09:48:49 -0400 | [diff] [blame] | 285 | } |
| 286 | } |