summaryrefslogtreecommitdiffstats
path: root/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
blob: 311411f4cf8439768abd73c0d4cf21f0d6e0cdc3 (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
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (C) 2018 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;

import static com.android.launcher3.LauncherState.OVERVIEW;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.ActivityOptions;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.UserHandle;
import android.support.annotation.UiThread;
import android.support.annotation.WorkerThread;
import android.util.SparseArray;
import android.view.ViewConfiguration;

import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.MainThreadExecutor;
import com.android.launcher3.Utilities;
import com.android.launcher3.anim.AnimationSuccessListener;
import com.android.launcher3.anim.Interpolators;
import com.android.launcher3.states.InternalStateHandler;
import com.android.launcher3.util.TraceHelper;
import com.android.quickstep.ActivityControlHelper.FallbackActivityControllerHelper;
import com.android.quickstep.ActivityControlHelper.LauncherActivityControllerHelper;
import com.android.quickstep.views.RecentsView;
import com.android.quickstep.views.TaskView;
import com.android.systemui.shared.recents.view.AppTransitionAnimationSpecCompat;
import com.android.systemui.shared.recents.view.AppTransitionAnimationSpecsFuture;
import com.android.systemui.shared.recents.view.RecentsTransition;
import com.android.systemui.shared.system.ActivityManagerWrapper;
import com.android.systemui.shared.system.AssistDataReceiver;
import com.android.systemui.shared.system.BackgroundExecutor;
import com.android.systemui.shared.system.RecentsAnimationControllerCompat;
import com.android.systemui.shared.system.RecentsAnimationListener;
import com.android.systemui.shared.system.RemoteAnimationTargetCompat;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * Helper class to handle various atomic commands for switching between Overview.
 */
@TargetApi(Build.VERSION_CODES.P)
public class OverviewCommandHelper extends InternalStateHandler {

    private static final int RID_RESET_SWIPE_HANDLER = 0;
    private static final int RID_CANCEL_CONTROLLER = 1;
    private static final int RID_CANCEL_ZOOM_OUT_ANIMATION = 2;

    private static final long RECENTS_LAUNCH_DURATION = 150;

    private static final String TAG = "OverviewCommandHelper";
    private static final boolean DEBUG_START_FALLBACK_ACTIVITY = false;

    private final Context mContext;
    private final ActivityManagerWrapper mAM;
    private final RecentsModel mRecentsModel;
    private final MainThreadExecutor mMainThreadExecutor;

    public final Intent homeIntent;
    public final ComponentName launcher;

    private final SparseArray<Runnable> mCurrentCommandFinishRunnables = new SparseArray<>();
    // Monotonically increasing command ids.
    private int mCurrentCommandId = 0;

    private long mLastToggleTime;
    private WindowTransformSwipeHandler mWindowTransformSwipeHandler;

    private final Point mWindowSize = new Point();
    private final Rect mTaskTargetRect = new Rect();
    private final RectF mTempTaskTargetRect = new RectF();

    public OverviewCommandHelper(Context context) {
        mContext = context;
        mAM = ActivityManagerWrapper.getInstance();
        mMainThreadExecutor = new MainThreadExecutor();
        mRecentsModel = RecentsModel.getInstance(mContext);

        homeIntent = new Intent(Intent.ACTION_MAIN)
                .addCategory(Intent.CATEGORY_HOME)
                .setPackage(context.getPackageName())
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ResolveInfo info = context.getPackageManager().resolveActivity(homeIntent, 0);

        if (DEBUG_START_FALLBACK_ACTIVITY) {
            launcher = new ComponentName(context, RecentsActivity.class);
            homeIntent.addCategory(Intent.CATEGORY_DEFAULT)
                    .removeCategory(Intent.CATEGORY_HOME);
        } else {
            launcher = new ComponentName(context.getPackageName(), info.activityInfo.name);
        }

        // Clear the packageName as system can fail to dedupe it b/64108432
        homeIntent.setComponent(launcher).setPackage(null);
    }

    private void openRecents() {
        Intent intent = addToIntent(new Intent(homeIntent));
        mContext.startActivity(intent);
        initWhenReady();
    }

    @UiThread
    private void addFinishCommand(int requestId, int id, Runnable action) {
        if (requestId < mCurrentCommandId) {
            action.run();
        } else {
            mCurrentCommandFinishRunnables.put(id, action);
        }
    }

    @UiThread
    private void clearFinishCommand(int requestId, int id) {
        if (requestId == mCurrentCommandId) {
            mCurrentCommandFinishRunnables.remove(id);
        }
    }

    @UiThread
    private void initSwipeHandler(ActivityControlHelper helper, long time,
            Consumer<WindowTransformSwipeHandler> onAnimationInitCallback) {
        final int commandId = mCurrentCommandId;
        RunningTaskInfo taskInfo = ActivityManagerWrapper.getInstance().getRunningTask();
        final WindowTransformSwipeHandler handler =
                new WindowTransformSwipeHandler(taskInfo, mContext, time, helper);

        // Preload the plan
        mRecentsModel.loadTasks(taskInfo.id, null);
        mWindowTransformSwipeHandler = handler;

        mTempTaskTargetRect.setEmpty();
        handler.setGestureEndCallback(() -> {
            if (mWindowTransformSwipeHandler == handler) {
                mWindowTransformSwipeHandler = null;
                mTempTaskTargetRect.setEmpty();
            }
            clearFinishCommand(commandId, RID_RESET_SWIPE_HANDLER);
            clearFinishCommand(commandId, RID_CANCEL_CONTROLLER);
        });
        handler.initWhenReady();
        addFinishCommand(commandId, RID_RESET_SWIPE_HANDLER, handler::reset);

        TraceHelper.beginSection(TAG);
        Runnable startActivity = () -> helper.startRecents(mContext, homeIntent,
                new AssistDataReceiver() {
                    @Override
                    public void onHandleAssistData(Bundle bundle) {
                        mRecentsModel.preloadAssistData(taskInfo.id, bundle);
                    }
                },
                new RecentsAnimationListener() {
                    public void onAnimationStart(
                            RecentsAnimationControllerCompat controller,
                            RemoteAnimationTargetCompat[] apps, Rect homeContentInsets,
                            Rect minimizedHomeBounds) {
                        if (mWindowTransformSwipeHandler == handler) {
                            TraceHelper.partitionSection(TAG, "Received");
                            handler.onRecentsAnimationStart(controller, apps, homeContentInsets,
                                    minimizedHomeBounds);
                            mTempTaskTargetRect.set(handler.getTargetRect(mWindowSize));

                            mMainThreadExecutor.execute(() -> {
                                addFinishCommand(commandId,
                                        RID_CANCEL_CONTROLLER, () -> controller.finish(true));
                                if (commandId == mCurrentCommandId) {
                                    onAnimationInitCallback.accept(handler);
                                }
                            });
                        } else {
                            TraceHelper.endSection(TAG, "Finishing no handler");
                            controller.finish(false /* toHome */);
                        }
                    }

                    public void onAnimationCanceled() {
                        TraceHelper.endSection(TAG, "Cancelled: " + handler);
                        if (mWindowTransformSwipeHandler == handler) {
                            handler.onRecentsAnimationCanceled();
                        }
                    }
                });

        // We should almost always get touch-town on background thread. This is an edge case
        // when the background Choreographer has not yet initialized.
        BackgroundExecutor.get().submit(startActivity);
    }

    @UiThread
    private void startZoomOutAnim(final WindowTransformSwipeHandler handler) {
        final int commandId = mCurrentCommandId;
        ValueAnimator anim = ValueAnimator.ofInt(0, -handler.getTransitionLength());
        anim.addUpdateListener((a) -> handler.updateDisplacement((Integer) a.getAnimatedValue()));
        anim.addListener(new AnimationSuccessListener() {
            @Override
            public void onAnimationSuccess(Animator animator) {
                handler.onGestureEnded(0);
                clearFinishCommand(commandId, RID_CANCEL_ZOOM_OUT_ANIMATION);
            }
        });
        handler.onGestureStarted();
        anim.setDuration(RECENTS_LAUNCH_DURATION);
        anim.setInterpolator(Interpolators.AGGRESSIVE_EASE);
        anim.start();
        addFinishCommand(commandId, RID_CANCEL_ZOOM_OUT_ANIMATION, anim::cancel);
    }

    public void onOverviewToggle() {
        long time = SystemClock.elapsedRealtime();
        mMainThreadExecutor.execute(() -> {
            long elapsedTime = time - mLastToggleTime;
            mLastToggleTime = time;

            mCurrentCommandId++;
            mTempTaskTargetRect.round(mTaskTargetRect);
            boolean isQuickTap = elapsedTime < ViewConfiguration.getDoubleTapTimeout();
            int runnableCount = mCurrentCommandFinishRunnables.size();
            if (runnableCount > 0) {
                for (int i = 0; i < runnableCount; i++) {
                    mCurrentCommandFinishRunnables.valueAt(i).run();
                }
                mCurrentCommandFinishRunnables.clear();
                isQuickTap = true;
            }

            ActivityControlHelper helper = getActivityControlHelper();
            RecentsView recents = helper.getVisibleRecentsView();
            if (recents != null) {
                int childCount = recents.getChildCount();
                if (childCount != 0) {
                    ((TaskView) recents.getChildAt(childCount >= 2 ? 1 : 0)).launchTask(true);
                }

                // There are not enough tasks. Skip
                return;
            }

            if (isQuickTap) {
                // Focus last task. Start is on background thread so that all ActivityManager calls
                // are serialized
                BackgroundExecutor.get().submit(this::startLastTask);
                return;
            }
            if (helper.switchToRecentsIfVisible()) {
                return;
            }

            initSwipeHandler(helper, time, this::startZoomOutAnim);
        });
    }

    public void onOverviewShown() {
        getLauncher().runOnUiThread(() -> {
                    if (isOverviewAlmostVisible()) {
                        final RecentsView rv = getLauncher().getOverviewPanel();
                        rv.snapToTaskAfterNext();
                    } else {
                        openRecents();
                    }
                }
        );
    }

    public void onOverviewHidden() {
        getLauncher().runOnUiThread(() -> {
                    if (isOverviewAlmostVisible()) {
                        final RecentsView rv = getLauncher().getOverviewPanel();
                        rv.launchNextTask();
                    }
                }
        );
    }

    @WorkerThread
    private void startLastTask() {
        // TODO: This should go through recents model.
        List<RecentTaskInfo> tasks = mAM.getRecentTasks(2, UserHandle.myUserId());
        if (tasks.size() > 1) {
            RecentTaskInfo rti = tasks.get(1);

            final ActivityOptions options;
            if (!mTaskTargetRect.isEmpty()) {
                final Rect targetRect = new Rect(mTaskTargetRect);
                targetRect.offset(Utilities.isRtl(mContext.getResources())
                        ? - mTaskTargetRect.width() : mTaskTargetRect.width(), 0);
                final AppTransitionAnimationSpecCompat specCompat =
                        new AppTransitionAnimationSpecCompat(rti.id, null, targetRect);
                AppTransitionAnimationSpecsFuture specFuture =
                        new AppTransitionAnimationSpecsFuture(mMainThreadExecutor.getHandler()) {

                    @Override
                    public List<AppTransitionAnimationSpecCompat> composeSpecs() {
                        return Collections.singletonList(specCompat);
                    }
                };
                options = RecentsTransition.createAspectScaleAnimation(mContext,
                        mMainThreadExecutor.getHandler(), true /* scaleUp */,
                        specFuture, () -> {});
            } else {
                options = ActivityOptions.makeBasic();
            }
            mAM.startActivityFromRecents(rti.id, options);
        }
    }

    private boolean isOverviewAlmostVisible() {
        if (clearReference()) {
            return true;
        }
        if (!mAM.getRunningTask().topActivity.equals(launcher)) {
            return false;
        }
        Launcher launcher = getLauncher();
        return launcher != null && launcher.isStarted() && launcher.isInState(OVERVIEW);
    }

    private Launcher getLauncher() {
        return (Launcher) LauncherAppState.getInstance(mContext).getModel().getCallback();
    }

    @Override
    protected boolean init(Launcher launcher, boolean alreadyOnHome) {
        AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome);
        launcher.getStateManager().goToState(OVERVIEW, alreadyOnHome);
        clearReference();
        return false;
    }

    public boolean isUsingFallbackActivity() {
        return DEBUG_START_FALLBACK_ACTIVITY;
    }

    public ActivityControlHelper getActivityControlHelper() {
        if (DEBUG_START_FALLBACK_ACTIVITY) {
            return new FallbackActivityControllerHelper();
        } else {
            return new LauncherActivityControllerHelper();
        }
    }
}