Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/back/TouchTracker.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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.wm.shell.back;
18
19 import android.os.SystemProperties;
20 import android.view.RemoteAnimationTarget;
21 import android.window.BackEvent;
22
23 /**
24 * Helper class to record the touch location for gesture and generate back events.
25 */
26 class TouchTracker {
27 private static final String PREDICTIVE_BACK_PROGRESS_THRESHOLD_PROP =
28 "persist.wm.debug.predictive_back_progress_threshold";
29 private static final int PROGRESS_THRESHOLD = SystemProperties
30 .getInt(PREDICTIVE_BACK_PROGRESS_THRESHOLD_PROP, -1);
31 private float mProgressThreshold;
32 /**
33 * Location of the latest touch event
34 */
35 private float mLatestTouchX;
36 private float mLatestTouchY;
37 private boolean mTriggerBack;
38
39 /**
40 * Location of the initial touch event of the back gesture.
41 */
42 private float mInitTouchX;
43 private float mInitTouchY;
44 private float mStartThresholdX;
45 private int mSwipeEdge;
46 private boolean mCancelled;
47
48 void update(float touchX, float touchY) {
49 /**
50 * If back was previously cancelled but the user has started swiping in the forward
51 * direction again, restart back.
52 */
53 if (mCancelled && ((touchX > mLatestTouchX && mSwipeEdge == BackEvent.EDGE_LEFT)
54 || touchX < mLatestTouchX && mSwipeEdge == BackEvent.EDGE_RIGHT)) {
55 mCancelled = false;
56 mStartThresholdX = touchX;
57 }
58 mLatestTouchX = touchX;
59 mLatestTouchY = touchY;
60 }
61
62 void setTriggerBack(boolean triggerBack) {
63 if (mTriggerBack != triggerBack && !triggerBack) {
64 mCancelled = true;
65 }
66 mTriggerBack = triggerBack;
67 }
68
69 void setGestureStartLocation(float touchX, float touchY, int swipeEdge) {
70 mInitTouchX = touchX;
71 mInitTouchY = touchY;
72 mSwipeEdge = swipeEdge;
73 mStartThresholdX = mInitTouchX;
74 }
75
76 void reset() {
77 mInitTouchX = 0;
78 mInitTouchY = 0;
79 mStartThresholdX = 0;
80 mCancelled = false;
81 mTriggerBack = false;
82 mSwipeEdge = BackEvent.EDGE_LEFT;
83 }
84
85 BackEvent createStartEvent(RemoteAnimationTarget target) {
86 return new BackEvent(mInitTouchX, mInitTouchY, 0, mSwipeEdge, target);
87 }
88
89 BackEvent createProgressEvent() {
90 float progressThreshold = PROGRESS_THRESHOLD >= 0
91 ? PROGRESS_THRESHOLD : mProgressThreshold;
92 progressThreshold = progressThreshold == 0 ? 1 : progressThreshold;
93 float progress = 0;
94 // Progress is always 0 when back is cancelled and not restarted.
95 if (!mCancelled) {
96 // If back is committed, progress is the distance between the last and first touch
97 // point, divided by the max drag distance. Otherwise, it's the distance between
98 // the last touch point and the starting threshold, divided by max drag distance.
99 // The starting threshold is initially the first touch location, and updated to
100 // the location everytime back is restarted after being cancelled.
101 float startX = mTriggerBack ? mInitTouchX : mStartThresholdX;
102 float deltaX = Math.max(
103 mSwipeEdge == BackEvent.EDGE_LEFT
104 ? mLatestTouchX - startX
105 : startX - mLatestTouchX,
106 0);
107 progress = Math.min(Math.max(deltaX / progressThreshold, 0), 1);
108 }
109 return createProgressEvent(progress);
110 }
111
112 BackEvent createProgressEvent(float progress) {
113 return new BackEvent(mLatestTouchX, mLatestTouchY, progress, mSwipeEdge, null);
114 }
115
116 public void setProgressThreshold(float progressThreshold) {
117 mProgressThreshold = progressThreshold;
118 }
119 }