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