Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.systemui.pip.phone; |
| 18 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 19 | import android.graphics.PointF; |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 20 | import android.util.Log; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 21 | import android.view.MotionEvent; |
| 22 | import android.view.VelocityTracker; |
| 23 | import android.view.ViewConfiguration; |
| 24 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 25 | import java.io.PrintWriter; |
| 26 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 27 | /** |
| 28 | * This keeps track of the touch state throughout the current touch gesture. |
| 29 | */ |
| 30 | public class PipTouchState { |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 31 | private static final String TAG = "PipTouchHandler"; |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 32 | private static final boolean DEBUG = true; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 33 | |
| 34 | private ViewConfiguration mViewConfig; |
| 35 | |
| 36 | private VelocityTracker mVelocityTracker; |
| 37 | private final PointF mDownTouch = new PointF(); |
| 38 | private final PointF mDownDelta = new PointF(); |
| 39 | private final PointF mLastTouch = new PointF(); |
| 40 | private final PointF mLastDelta = new PointF(); |
| 41 | private final PointF mVelocity = new PointF(); |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 42 | private boolean mAllowTouches = true; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 43 | private boolean mIsUserInteracting = false; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 44 | private boolean mIsDragging = false; |
| 45 | private boolean mStartedDragging = false; |
Winson Chung | d5a0159 | 2016-11-11 16:25:04 -0800 | [diff] [blame] | 46 | private boolean mAllowDraggingOffscreen = false; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 47 | private int mActivePointerId; |
| 48 | |
| 49 | public PipTouchState(ViewConfiguration viewConfig) { |
| 50 | mViewConfig = viewConfig; |
| 51 | } |
| 52 | |
| 53 | /** |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 54 | * Resets this state. |
| 55 | */ |
| 56 | public void reset() { |
| 57 | mAllowDraggingOffscreen = false; |
| 58 | mIsDragging = false; |
| 59 | mStartedDragging = false; |
| 60 | mIsUserInteracting = false; |
| 61 | } |
| 62 | |
| 63 | /** |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 64 | * Processess a given touch event and updates the state. |
| 65 | */ |
| 66 | public void onTouchEvent(MotionEvent ev) { |
| 67 | switch (ev.getAction()) { |
| 68 | case MotionEvent.ACTION_DOWN: { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 69 | if (!mAllowTouches) { |
| 70 | return; |
| 71 | } |
| 72 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 73 | // Initialize the velocity tracker |
| 74 | initOrResetVelocityTracker(); |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 75 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 76 | mActivePointerId = ev.getPointerId(0); |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 77 | if (DEBUG) { |
| 78 | Log.e(TAG, "Setting active pointer id on DOWN: " + mActivePointerId); |
| 79 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 80 | mLastTouch.set(ev.getX(), ev.getY()); |
| 81 | mDownTouch.set(mLastTouch); |
Winson Chung | d5a0159 | 2016-11-11 16:25:04 -0800 | [diff] [blame] | 82 | mAllowDraggingOffscreen = true; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 83 | mIsUserInteracting = true; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | case MotionEvent.ACTION_MOVE: { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 87 | // Skip event if we did not start processing this touch gesture |
| 88 | if (!mIsUserInteracting) { |
| 89 | break; |
| 90 | } |
| 91 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 92 | // Update the velocity tracker |
| 93 | mVelocityTracker.addMovement(ev); |
| 94 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 95 | if (pointerIndex == -1) { |
| 96 | Log.e(TAG, "Invalid active pointer id on MOVE: " + mActivePointerId); |
| 97 | break; |
| 98 | } |
| 99 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 100 | float x = ev.getX(pointerIndex); |
| 101 | float y = ev.getY(pointerIndex); |
| 102 | mLastDelta.set(x - mLastTouch.x, y - mLastTouch.y); |
| 103 | mDownDelta.set(x - mDownTouch.x, y - mDownTouch.y); |
| 104 | |
| 105 | boolean hasMovedBeyondTap = mDownDelta.length() > mViewConfig.getScaledTouchSlop(); |
| 106 | if (!mIsDragging) { |
| 107 | if (hasMovedBeyondTap) { |
| 108 | mIsDragging = true; |
| 109 | mStartedDragging = true; |
| 110 | } |
| 111 | } else { |
| 112 | mStartedDragging = false; |
| 113 | } |
| 114 | mLastTouch.set(x, y); |
| 115 | break; |
| 116 | } |
| 117 | case MotionEvent.ACTION_POINTER_UP: { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 118 | // Skip event if we did not start processing this touch gesture |
| 119 | if (!mIsUserInteracting) { |
| 120 | break; |
| 121 | } |
| 122 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 123 | // Update the velocity tracker |
| 124 | mVelocityTracker.addMovement(ev); |
| 125 | |
| 126 | int pointerIndex = ev.getActionIndex(); |
| 127 | int pointerId = ev.getPointerId(pointerIndex); |
| 128 | if (pointerId == mActivePointerId) { |
| 129 | // Select a new active pointer id and reset the movement state |
| 130 | final int newPointerIndex = (pointerIndex == 0) ? 1 : 0; |
| 131 | mActivePointerId = ev.getPointerId(newPointerIndex); |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 132 | if (DEBUG) { |
| 133 | Log.e(TAG, "Relinquish active pointer id on POINTER_UP: " + |
| 134 | mActivePointerId); |
| 135 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 136 | mLastTouch.set(ev.getX(newPointerIndex), ev.getY(newPointerIndex)); |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | case MotionEvent.ACTION_UP: { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 141 | // Skip event if we did not start processing this touch gesture |
| 142 | if (!mIsUserInteracting) { |
| 143 | break; |
| 144 | } |
| 145 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 146 | // Update the velocity tracker |
| 147 | mVelocityTracker.addMovement(ev); |
| 148 | mVelocityTracker.computeCurrentVelocity(1000, |
| 149 | mViewConfig.getScaledMaximumFlingVelocity()); |
| 150 | mVelocity.set(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); |
| 151 | |
| 152 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 153 | if (pointerIndex == -1) { |
| 154 | Log.e(TAG, "Invalid active pointer id on UP: " + mActivePointerId); |
| 155 | break; |
| 156 | } |
| 157 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 158 | mLastTouch.set(ev.getX(pointerIndex), ev.getY(pointerIndex)); |
| 159 | |
| 160 | // Fall through to clean up |
| 161 | } |
| 162 | case MotionEvent.ACTION_CANCEL: { |
| 163 | recycleVelocityTracker(); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @return the velocity of the active touch pointer at the point it is lifted off the screen. |
| 171 | */ |
| 172 | public PointF getVelocity() { |
| 173 | return mVelocity; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @return the last touch position of the active pointer. |
| 178 | */ |
| 179 | public PointF getLastTouchPosition() { |
| 180 | return mLastTouch; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return the movement delta between the last handled touch event and the previous touch |
| 185 | * position. |
| 186 | */ |
| 187 | public PointF getLastTouchDelta() { |
| 188 | return mLastDelta; |
| 189 | } |
| 190 | |
| 191 | /** |
Mady Mellor | 57d2255 | 2017-03-09 15:37:13 -0800 | [diff] [blame] | 192 | * @return the down touch position. |
| 193 | */ |
| 194 | public PointF getDownTouchPosition() { |
| 195 | return mDownTouch; |
| 196 | } |
| 197 | |
| 198 | /** |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 199 | * @return the movement delta between the last handled touch event and the down touch |
| 200 | * position. |
| 201 | */ |
| 202 | public PointF getDownTouchDelta() { |
| 203 | return mDownDelta; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @return whether the user has started dragging. |
| 208 | */ |
| 209 | public boolean isDragging() { |
| 210 | return mIsDragging; |
| 211 | } |
| 212 | |
| 213 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 214 | * @return whether the user is currently interacting with the PiP. |
| 215 | */ |
| 216 | public boolean isUserInteracting() { |
| 217 | return mIsUserInteracting; |
| 218 | } |
| 219 | |
| 220 | /** |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 221 | * @return whether the user has started dragging just in the last handled touch event. |
| 222 | */ |
| 223 | public boolean startedDragging() { |
| 224 | return mStartedDragging; |
| 225 | } |
| 226 | |
Winson Chung | d5a0159 | 2016-11-11 16:25:04 -0800 | [diff] [blame] | 227 | /** |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 228 | * Sets whether touching is currently allowed. |
| 229 | */ |
| 230 | public void setAllowTouches(boolean allowTouches) { |
| 231 | mAllowTouches = allowTouches; |
| 232 | |
| 233 | // If the user happens to touch down before this is sent from the system during a transition |
| 234 | // then block any additional handling by resetting the state now |
| 235 | if (mIsUserInteracting) { |
| 236 | reset(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
Winson Chung | d5a0159 | 2016-11-11 16:25:04 -0800 | [diff] [blame] | 241 | * Disallows dragging offscreen for the duration of the current gesture. |
| 242 | */ |
| 243 | public void setDisallowDraggingOffscreen() { |
| 244 | mAllowDraggingOffscreen = false; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @return whether dragging offscreen is allowed during this gesture. |
| 249 | */ |
| 250 | public boolean allowDraggingOffscreen() { |
| 251 | return mAllowDraggingOffscreen; |
| 252 | } |
| 253 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 254 | private void initOrResetVelocityTracker() { |
| 255 | if (mVelocityTracker == null) { |
| 256 | mVelocityTracker = VelocityTracker.obtain(); |
| 257 | } else { |
| 258 | mVelocityTracker.clear(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private void recycleVelocityTracker() { |
| 263 | if (mVelocityTracker != null) { |
| 264 | mVelocityTracker.recycle(); |
| 265 | mVelocityTracker = null; |
| 266 | } |
| 267 | } |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 268 | |
| 269 | public void dump(PrintWriter pw, String prefix) { |
| 270 | final String innerPrefix = prefix + " "; |
| 271 | pw.println(prefix + TAG); |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 272 | pw.println(innerPrefix + "mAllowTouches=" + mAllowTouches); |
Winson Chung | 040e722 | 2017-03-21 10:47:09 -0700 | [diff] [blame^] | 273 | pw.println(innerPrefix + "mActivePointerId=" + mActivePointerId); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 274 | pw.println(innerPrefix + "mDownTouch=" + mDownTouch); |
| 275 | pw.println(innerPrefix + "mDownDelta=" + mDownDelta); |
| 276 | pw.println(innerPrefix + "mLastTouch=" + mLastTouch); |
| 277 | pw.println(innerPrefix + "mLastDelta=" + mLastDelta); |
| 278 | pw.println(innerPrefix + "mVelocity=" + mVelocity); |
| 279 | pw.println(innerPrefix + "mIsUserInteracting=" + mIsUserInteracting); |
| 280 | pw.println(innerPrefix + "mIsDragging=" + mIsDragging); |
| 281 | pw.println(innerPrefix + "mStartedDragging=" + mStartedDragging); |
| 282 | pw.println(innerPrefix + "mAllowDraggingOffscreen=" + mAllowDraggingOffscreen); |
| 283 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 284 | } |