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