summaryrefslogtreecommitdiffstats
path: root/quickstep/recents_ui_overrides/src/com/android/quickstep/inputconsumers/FallbackNoButtonInputConsumer.java
blob: d05ca2a1612b0f9d77cbb6e5e273868303c03bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.quickstep.inputconsumers;

import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_POINTER_DOWN;
import static android.view.MotionEvent.ACTION_POINTER_UP;
import static android.view.MotionEvent.ACTION_UP;
import static android.view.MotionEvent.INVALID_POINTER_ID;

import static com.android.quickstep.WindowTransformSwipeHandler.MAX_SWIPE_DURATION;
import static com.android.quickstep.WindowTransformSwipeHandler.MIN_PROGRESS_FOR_OVERVIEW;
import static com.android.quickstep.WindowTransformSwipeHandler.MIN_SWIPE_DURATION;
import static com.android.quickstep.inputconsumers.OtherActivityInputConsumer.QUICKSTEP_TOUCH_SLOP_RATIO;
import static com.android.systemui.shared.system.ActivityManagerWrapper.CLOSE_SYSTEM_WINDOWS_REASON_RECENTS;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import com.android.launcher3.DeviceProfile;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.R;
import com.android.quickstep.ActivityControlHelper;
import com.android.quickstep.OverviewComponentObserver;
import com.android.quickstep.SwipeSharedState;
import com.android.quickstep.util.ClipAnimationHelper;
import com.android.quickstep.util.ClipAnimationHelper.TransformParams;
import com.android.quickstep.util.NavBarPosition;
import com.android.quickstep.util.RecentsAnimationListenerSet;
import com.android.quickstep.util.SwipeAnimationTargetSet;
import com.android.quickstep.util.SwipeAnimationTargetSet.SwipeAnimationListener;
import com.android.systemui.shared.system.ActivityManagerWrapper;
import com.android.systemui.shared.system.BackgroundExecutor;
import com.android.systemui.shared.system.InputMonitorCompat;
import com.android.systemui.shared.system.RemoteAnimationTargetCompat;

public class FallbackNoButtonInputConsumer implements InputConsumer, SwipeAnimationListener {

    private static final int STATE_NOT_FINISHED = 0;
    private static final int STATE_FINISHED_TO_HOME = 1;
    private static final int STATE_FINISHED_TO_APP = 2;

    private static final float PROGRESS_TO_END_GESTURE = -2;

    private final ActivityControlHelper mActivityControlHelper;
    private final InputMonitorCompat mInputMonitor;
    private final Context mContext;
    private final NavBarPosition mNavBarPosition;
    private final SwipeSharedState mSwipeSharedState;
    private final OverviewComponentObserver mOverviewComponentObserver;
    private final int mRunningTaskId;

    private final ClipAnimationHelper mClipAnimationHelper;
    private final TransformParams mTransformParams = new TransformParams();
    private final float mTransitionDragLength;
    private final DeviceProfile mDP;

    private final RectF mSwipeTouchRegion;
    private final boolean mDisableHorizontalSwipe;

    private final PointF mDownPos = new PointF();
    private final PointF mLastPos = new PointF();

    private int mActivePointerId = -1;
    // Slop used to determine when we say that the gesture has started.
    private boolean mPassedPilferInputSlop;

    private VelocityTracker mVelocityTracker;

    // Distance after which we start dragging the window.
    private final float mTouchSlop;

    // Might be displacement in X or Y, depending on the direction we are swiping from the nav bar.
    private float mStartDisplacement;
    private SwipeAnimationTargetSet mSwipeAnimationTargetSet;
    private float mProgress;

    private int mState = STATE_NOT_FINISHED;

    public FallbackNoButtonInputConsumer(Context context,
            ActivityControlHelper activityControlHelper, InputMonitorCompat inputMonitor,
            SwipeSharedState swipeSharedState, RectF swipeTouchRegion,
            OverviewComponentObserver overviewComponentObserver,
            boolean disableHorizontalSwipe, RunningTaskInfo runningTaskInfo) {
        mContext = context;
        mActivityControlHelper = activityControlHelper;
        mInputMonitor = inputMonitor;
        mOverviewComponentObserver = overviewComponentObserver;
        mRunningTaskId = runningTaskInfo.id;

        mSwipeSharedState = swipeSharedState;
        mSwipeTouchRegion = swipeTouchRegion;
        mDisableHorizontalSwipe = disableHorizontalSwipe;

        mNavBarPosition = new NavBarPosition(context);
        mVelocityTracker = VelocityTracker.obtain();

        mTouchSlop = QUICKSTEP_TOUCH_SLOP_RATIO
                * ViewConfiguration.get(context).getScaledTouchSlop();

        mClipAnimationHelper = new ClipAnimationHelper(context);

        mDP = InvariantDeviceProfile.INSTANCE.get(context).getDeviceProfile(context).copy(context);
        Rect tempRect = new Rect();
        mTransitionDragLength = mActivityControlHelper.getSwipeUpDestinationAndLength(
                mDP, context, tempRect);
        mClipAnimationHelper.updateTargetRect(tempRect);
    }

    @Override
    public int getType() {
        return TYPE_FALLBACK_NO_BUTTON;
    }

    @Override
    public void onMotionEvent(MotionEvent ev) {
        if (mVelocityTracker == null) {
            return;
        }

        mVelocityTracker.addMovement(ev);
        if (ev.getActionMasked() == ACTION_POINTER_UP) {
            mVelocityTracker.clear();
        }

        switch (ev.getActionMasked()) {
            case ACTION_DOWN: {
                mActivePointerId = ev.getPointerId(0);
                mDownPos.set(ev.getX(), ev.getY());
                mLastPos.set(mDownPos);
                break;
            }
            case ACTION_POINTER_DOWN: {
                if (!mPassedPilferInputSlop) {
                    // Cancel interaction in case of multi-touch interaction
                    int ptrIdx = ev.getActionIndex();
                    if (!mSwipeTouchRegion.contains(ev.getX(ptrIdx), ev.getY(ptrIdx))) {
                        forceCancelGesture(ev);
                    }
                }
                break;
            }
            case ACTION_POINTER_UP: {
                int ptrIdx = ev.getActionIndex();
                int ptrId = ev.getPointerId(ptrIdx);
                if (ptrId == mActivePointerId) {
                    final int newPointerIdx = ptrIdx == 0 ? 1 : 0;
                    mDownPos.set(
                            ev.getX(newPointerIdx) - (mLastPos.x - mDownPos.x),
                            ev.getY(newPointerIdx) - (mLastPos.y - mDownPos.y));
                    mLastPos.set(ev.getX(newPointerIdx), ev.getY(newPointerIdx));
                    mActivePointerId = ev.getPointerId(newPointerIdx);
                }
                break;
            }
            case ACTION_MOVE: {
                int pointerIndex = ev.findPointerIndex(mActivePointerId);
                if (pointerIndex == INVALID_POINTER_ID) {
                    break;
                }
                mLastPos.set(ev.getX(pointerIndex), ev.getY(pointerIndex));
                float displacement = getDisplacement(ev);

                if (!mPassedPilferInputSlop) {
                    if (mDisableHorizontalSwipe && Math.abs(mLastPos.x - mDownPos.x)
                            > Math.abs(mLastPos.y - mDownPos.y)) {
                        // Horizontal gesture is not allowed in this region
                        forceCancelGesture(ev);
                        break;
                    }

                    if (Math.abs(displacement) >= mTouchSlop) {
                        mPassedPilferInputSlop = true;

                        // Deferred gesture, start the animation and gesture tracking once
                        // we pass the actual touch slop
                        startTouchTrackingForWindowAnimation(displacement);
                    }
                } else {
                    updateDisplacement(displacement - mStartDisplacement);
                }
                break;
            }
            case ACTION_CANCEL:
            case ACTION_UP: {
                finishTouchTracking(ev);
                break;
            }
        }
    }

    private void startTouchTrackingForWindowAnimation(float displacement) {
        mStartDisplacement = Math.min(displacement, -mTouchSlop);

        RecentsAnimationListenerSet listenerSet =
                mSwipeSharedState.newRecentsAnimationListenerSet();
        listenerSet.addListener(this);
        Intent homeIntent = mOverviewComponentObserver.getHomeIntent();
        BackgroundExecutor.get().submit(
                () -> ActivityManagerWrapper.getInstance().startRecentsActivity(
                        homeIntent, null, listenerSet, null, null));

        ActivityManagerWrapper.getInstance().closeSystemWindows(
                CLOSE_SYSTEM_WINDOWS_REASON_RECENTS);
        mInputMonitor.pilferPointers();
    }

    private void updateDisplacement(float displacement) {
        mProgress = displacement / mTransitionDragLength;
        mTransformParams.setProgress(mProgress);

        if (mSwipeAnimationTargetSet != null) {
            mClipAnimationHelper.applyTransform(mSwipeAnimationTargetSet, mTransformParams);
        }
    }

    private void forceCancelGesture(MotionEvent ev) {
        int action = ev.getAction();
        ev.setAction(ACTION_CANCEL);
        finishTouchTracking(ev);
        ev.setAction(action);
    }

    /**
     * Called when the gesture has ended. Does not correlate to the completion of the interaction as
     * the animation can still be running.
     */
    private void finishTouchTracking(MotionEvent ev) {
        if (ev.getAction() == ACTION_CANCEL) {
            mState = STATE_FINISHED_TO_APP;
        } else {
            mVelocityTracker.computeCurrentVelocity(1000,
                    ViewConfiguration.get(mContext).getScaledMaximumFlingVelocity());
            float velocityX = mVelocityTracker.getXVelocity(mActivePointerId);
            float velocityY = mVelocityTracker.getYVelocity(mActivePointerId);
            float velocity = mNavBarPosition.isRightEdge() ? velocityX
                    : mNavBarPosition.isLeftEdge() ? -velocityX
                            : velocityY;
            float flingThreshold = mContext.getResources()
                    .getDimension(R.dimen.quickstep_fling_threshold_velocity);
            boolean isFling = Math.abs(velocity) > flingThreshold;

            boolean goingHome;
            if (!isFling) {
                goingHome = -mProgress >= MIN_PROGRESS_FOR_OVERVIEW;
            } else {
                goingHome = velocity < 0;
            }

            if (goingHome) {
                mState = STATE_FINISHED_TO_HOME;
            } else {
                mState = STATE_FINISHED_TO_APP;
            }
        }

        if (mSwipeAnimationTargetSet != null) {
            finishAnimationTargetSet();
        }
    }

    private void finishAnimationTargetSet() {
        if (mState == STATE_FINISHED_TO_APP) {
            mSwipeAnimationTargetSet.finishController(false, null, false);
        } else {
            if (mProgress < PROGRESS_TO_END_GESTURE) {
                mSwipeAnimationTargetSet.finishController(true, null, true);
            } else {
                long duration = (long) (Math.min(mProgress - PROGRESS_TO_END_GESTURE, 1)
                        * MAX_SWIPE_DURATION / Math.abs(PROGRESS_TO_END_GESTURE));
                if (duration < 0) {
                    duration = MIN_SWIPE_DURATION;
                }

                ValueAnimator anim = ValueAnimator.ofFloat(mProgress, PROGRESS_TO_END_GESTURE);
                anim.addUpdateListener(a -> {
                    float p = (Float) anim.getAnimatedValue();
                    mTransformParams.setProgress(p);
                    mClipAnimationHelper.applyTransform(mSwipeAnimationTargetSet, mTransformParams);
                });
                anim.setDuration(duration);
                anim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mSwipeAnimationTargetSet.finishController(true, null, true);
                    }
                });
                anim.start();
            }
        }
    }

    @Override
    public void onRecentsAnimationStart(SwipeAnimationTargetSet targetSet) {
        mSwipeAnimationTargetSet = targetSet;
        Rect overviewStackBounds = new Rect(0, 0, mDP.widthPx, mDP.heightPx);
        RemoteAnimationTargetCompat runningTaskTarget = targetSet.findTask(mRunningTaskId);

        mDP.updateIsSeascape(mContext.getSystemService(WindowManager.class));
        if (runningTaskTarget != null) {
            mClipAnimationHelper.updateSource(overviewStackBounds, runningTaskTarget);
        }
        mClipAnimationHelper.prepareAnimation(mDP, false /* isOpening */);

        overviewStackBounds
                .inset(-overviewStackBounds.width() / 5, -overviewStackBounds.height() / 5);
        mClipAnimationHelper.updateTargetRect(overviewStackBounds);
        mClipAnimationHelper.applyTransform(mSwipeAnimationTargetSet, mTransformParams);

        if (mState != STATE_NOT_FINISHED) {
            finishAnimationTargetSet();
        }
    }

    @Override
    public void onRecentsAnimationCanceled() { }

    private float getDisplacement(MotionEvent ev) {
        if (mNavBarPosition.isRightEdge()) {
            return ev.getX() - mDownPos.x;
        } else if (mNavBarPosition.isLeftEdge()) {
            return mDownPos.x - ev.getX();
        } else {
            return ev.getY() - mDownPos.y;
        }
    }

    @Override
    public boolean allowInterceptByParent() {
        return !mPassedPilferInputSlop;
    }
}