blob: 686b3bb9d127f42850427dd4831694de36b6bacb [file] [log] [blame]
Winson Chungfa7053782016-11-08 15:45:10 -08001/*
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
17package com.android.systemui.pip.phone;
18
Winson Chungfa7053782016-11-08 15:45:10 -080019import android.graphics.PointF;
Winson Chung040e7222017-03-21 10:47:09 -070020import android.util.Log;
Winson Chungfa7053782016-11-08 15:45:10 -080021import android.view.MotionEvent;
22import android.view.VelocityTracker;
23import android.view.ViewConfiguration;
24
Winson Chung29a78652017-02-09 18:35:26 -080025import java.io.PrintWriter;
26
Winson Chungfa7053782016-11-08 15:45:10 -080027/**
28 * This keeps track of the touch state throughout the current touch gesture.
29 */
30public class PipTouchState {
Winson Chung29a78652017-02-09 18:35:26 -080031 private static final String TAG = "PipTouchHandler";
Winson Chung427cec42017-07-17 11:35:39 -070032 private static final boolean DEBUG = false;
Winson Chungfa7053782016-11-08 15:45:10 -080033
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 Chung85d39982017-02-24 15:21:25 -080042 private boolean mAllowTouches = true;
Winson Chung2a82fe52017-02-02 14:43:34 -080043 private boolean mIsUserInteracting = false;
Winson Chungfa7053782016-11-08 15:45:10 -080044 private boolean mIsDragging = false;
45 private boolean mStartedDragging = false;
Winson Chungd5a01592016-11-11 16:25:04 -080046 private boolean mAllowDraggingOffscreen = false;
Winson Chungfa7053782016-11-08 15:45:10 -080047 private int mActivePointerId;
48
49 public PipTouchState(ViewConfiguration viewConfig) {
50 mViewConfig = viewConfig;
51 }
52
53 /**
Winson Chung85d39982017-02-24 15:21:25 -080054 * Resets this state.
55 */
56 public void reset() {
57 mAllowDraggingOffscreen = false;
58 mIsDragging = false;
59 mStartedDragging = false;
60 mIsUserInteracting = false;
61 }
62
63 /**
Winson Chung8ec0e162017-07-07 14:49:49 -070064 * Processes a given touch event and updates the state.
Winson Chungfa7053782016-11-08 15:45:10 -080065 */
66 public void onTouchEvent(MotionEvent ev) {
67 switch (ev.getAction()) {
68 case MotionEvent.ACTION_DOWN: {
Winson Chung85d39982017-02-24 15:21:25 -080069 if (!mAllowTouches) {
70 return;
71 }
72
Winson Chungfa7053782016-11-08 15:45:10 -080073 // Initialize the velocity tracker
74 initOrResetVelocityTracker();
Winson Chung85d39982017-02-24 15:21:25 -080075
Winson Chungfa7053782016-11-08 15:45:10 -080076 mActivePointerId = ev.getPointerId(0);
Winson Chung040e7222017-03-21 10:47:09 -070077 if (DEBUG) {
78 Log.e(TAG, "Setting active pointer id on DOWN: " + mActivePointerId);
79 }
Winson Chungfa7053782016-11-08 15:45:10 -080080 mLastTouch.set(ev.getX(), ev.getY());
81 mDownTouch.set(mLastTouch);
Winson Chungd5a01592016-11-11 16:25:04 -080082 mAllowDraggingOffscreen = true;
Winson Chung2a82fe52017-02-02 14:43:34 -080083 mIsUserInteracting = true;
Winson Chungfa7053782016-11-08 15:45:10 -080084 break;
85 }
86 case MotionEvent.ACTION_MOVE: {
Winson Chung85d39982017-02-24 15:21:25 -080087 // Skip event if we did not start processing this touch gesture
88 if (!mIsUserInteracting) {
89 break;
90 }
91
Winson Chungfa7053782016-11-08 15:45:10 -080092 // Update the velocity tracker
93 mVelocityTracker.addMovement(ev);
94 int pointerIndex = ev.findPointerIndex(mActivePointerId);
Winson Chung040e7222017-03-21 10:47:09 -070095 if (pointerIndex == -1) {
96 Log.e(TAG, "Invalid active pointer id on MOVE: " + mActivePointerId);
97 break;
98 }
99
Winson Chungfa7053782016-11-08 15:45:10 -0800100 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 Chung85d39982017-02-24 15:21:25 -0800118 // Skip event if we did not start processing this touch gesture
119 if (!mIsUserInteracting) {
120 break;
121 }
122
Winson Chungfa7053782016-11-08 15:45:10 -0800123 // 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 Chung040e7222017-03-21 10:47:09 -0700132 if (DEBUG) {
133 Log.e(TAG, "Relinquish active pointer id on POINTER_UP: " +
134 mActivePointerId);
135 }
Winson Chungfa7053782016-11-08 15:45:10 -0800136 mLastTouch.set(ev.getX(newPointerIndex), ev.getY(newPointerIndex));
137 }
138 break;
139 }
140 case MotionEvent.ACTION_UP: {
Winson Chung85d39982017-02-24 15:21:25 -0800141 // Skip event if we did not start processing this touch gesture
142 if (!mIsUserInteracting) {
143 break;
144 }
145
Winson Chungfa7053782016-11-08 15:45:10 -0800146 // 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 Chung040e7222017-03-21 10:47:09 -0700153 if (pointerIndex == -1) {
154 Log.e(TAG, "Invalid active pointer id on UP: " + mActivePointerId);
155 break;
156 }
157
Winson Chungfa7053782016-11-08 15:45:10 -0800158 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 Mellor57d22552017-03-09 15:37:13 -0800192 * @return the down touch position.
193 */
194 public PointF getDownTouchPosition() {
195 return mDownTouch;
196 }
197
198 /**
Winson Chungfa7053782016-11-08 15:45:10 -0800199 * @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 Chung2a82fe52017-02-02 14:43:34 -0800214 * @return whether the user is currently interacting with the PiP.
215 */
216 public boolean isUserInteracting() {
217 return mIsUserInteracting;
218 }
219
220 /**
Winson Chungfa7053782016-11-08 15:45:10 -0800221 * @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 Chungd5a01592016-11-11 16:25:04 -0800227 /**
Winson Chung85d39982017-02-24 15:21:25 -0800228 * 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 Chungd5a01592016-11-11 16:25:04 -0800241 * 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 Chungfa7053782016-11-08 15:45:10 -0800254 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 Chung29a78652017-02-09 18:35:26 -0800268
269 public void dump(PrintWriter pw, String prefix) {
270 final String innerPrefix = prefix + " ";
271 pw.println(prefix + TAG);
Winson Chung85d39982017-02-24 15:21:25 -0800272 pw.println(innerPrefix + "mAllowTouches=" + mAllowTouches);
Winson Chung040e7222017-03-21 10:47:09 -0700273 pw.println(innerPrefix + "mActivePointerId=" + mActivePointerId);
Winson Chung29a78652017-02-09 18:35:26 -0800274 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 Chungfa7053782016-11-08 15:45:10 -0800284}