RecentsAnimation.java revision 1e6d4a9e001f910de9396f4056eb74fdf9b8257c
1e2d721781fc024cbd9a14929741e5b476242291fWinson Chung/*
2e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * Copyright (C) 2018 The Android Open Source Project
3e2d721781fc024cbd9a14929741e5b476242291fWinson Chung *
4e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * Licensed under the Apache License, Version 2.0 (the "License");
5e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * you may not use this file except in compliance with the License.
6e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * You may obtain a copy of the License at
7e2d721781fc024cbd9a14929741e5b476242291fWinson Chung *
8e2d721781fc024cbd9a14929741e5b476242291fWinson Chung *      http://www.apache.org/licenses/LICENSE-2.0
9e2d721781fc024cbd9a14929741e5b476242291fWinson Chung *
10e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * Unless required by applicable law or agreed to in writing, software
11e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * distributed under the License is distributed on an "AS IS" BASIS,
12e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * See the License for the specific language governing permissions and
14e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * limitations under the License.
15e2d721781fc024cbd9a14929741e5b476242291fWinson Chung */
16e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
17e2d721781fc024cbd9a14929741e5b476242291fWinson Chungpackage com.android.server.am;
18e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
19e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.app.ActivityManager.StackId.INVALID_STACK_ID;
20e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
21e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
22e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION;
23e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.view.WindowManager.TRANSIT_NONE;
24e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
25e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
26e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.app.ActivityOptions;
27e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.content.ComponentName;
28e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.content.Intent;
29e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.os.Handler;
30e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.view.IRecentsAnimationRunner;
31e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport com.android.server.wm.RecentsAnimationController.RecentsAnimationCallbacks;
32e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport com.android.server.wm.WindowManagerService;
33e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
34e2d721781fc024cbd9a14929741e5b476242291fWinson Chung/**
35e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * Manages the recents animation, including the reordering of the stacks for the transition and
36e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * cleanup. See {@link com.android.server.wm.RecentsAnimationController}.
37e2d721781fc024cbd9a14929741e5b476242291fWinson Chung */
38e2d721781fc024cbd9a14929741e5b476242291fWinson Chungclass RecentsAnimation implements RecentsAnimationCallbacks {
39e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private static final String TAG = RecentsAnimation.class.getSimpleName();
40e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
41e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private static final int RECENTS_ANIMATION_TIMEOUT = 10 * 1000;
42e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
43e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityManagerService mService;
44e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityStackSupervisor mStackSupervisor;
45e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityStartController mActivityStartController;
46e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final WindowManagerService mWindowManager;
47e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final UserController mUserController;
48e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final Handler mHandler;
49e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
50e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final Runnable mCancelAnimationRunnable;
51e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
52e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    // The stack to restore the home stack behind when the animation is finished
53e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private ActivityStack mRestoreHomeBehindStack;
54e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
55e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    RecentsAnimation(ActivityManagerService am, ActivityStackSupervisor stackSupervisor,
56e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            ActivityStartController activityStartController, WindowManagerService wm,
57e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            UserController userController) {
58e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mService = am;
59e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mStackSupervisor = stackSupervisor;
60e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mActivityStartController = activityStartController;
61e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mHandler = new Handler(mStackSupervisor.mLooper);
62e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mWindowManager = wm;
63e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mUserController = userController;
64e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mCancelAnimationRunnable = () -> {
65e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            // The caller has not finished the animation in a predefined amount of time, so
66e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            // force-cancel the animation
67e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            mWindowManager.cancelRecentsAnimation();
68e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        };
69e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
70e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
71e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    void startRecentsActivity(Intent intent, IRecentsAnimationRunner recentsAnimationRunner,
72e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            ComponentName recentsComponent, int recentsUid) {
731e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        mWindowManager.deferSurfaceLayout();
741e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        try {
751e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Cancel the previous recents animation if necessary
761e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.cancelRecentsAnimation();
77e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
781e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            final boolean hasExistingHomeActivity = mStackSupervisor.getHomeActivity() != null;
791e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            if (!hasExistingHomeActivity) {
801e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                // No home activity
811e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                final ActivityOptions opts = ActivityOptions.makeBasic();
821e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                opts.setLaunchActivityType(ACTIVITY_TYPE_HOME);
831e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                opts.setAvoidMoveToFront();
841e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION);
851e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
861e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mActivityStartController
871e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .obtainStarter(intent, "startRecentsActivity_noHomeActivity")
881e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingUid(recentsUid)
891e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingPackage(recentsComponent.getPackageName())
901e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setActivityOptions(SafeActivityOptions.fromBundle(opts.toBundle()))
911e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setMayWait(mUserController.getCurrentUserId())
921e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .execute();
931e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
94e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
951e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                // TODO: Maybe wait for app to draw in this particular case?
961e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            }
97e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
981e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            final ActivityRecord homeActivity = mStackSupervisor.getHomeActivity();
991e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            final ActivityDisplay display = homeActivity.getDisplay();
100e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1011e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Save the initial position of the home activity stack to be restored to after the
1021e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // animation completes
1031e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mRestoreHomeBehindStack = hasExistingHomeActivity
1041e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    ? display.getStackAboveHome()
1051e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    : null;
106e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1071e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Move the home activity into place for the animation
1081e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            display.moveHomeStackBehindBottomMostVisibleStack();
109e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1101e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Mark the home activity as launch-behind to bump its visibility for the
1111e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // duration of the gesture that is driven by the recents component
1121e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            homeActivity.mLaunchTaskBehind = true;
113e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1141e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Fetch all the surface controls and pass them to the client to get the animation
1151e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // started
1161e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.initializeRecentsAnimation(recentsAnimationRunner, this,
1171e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    display.mDisplayId);
118e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1191e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // If we updated the launch-behind state, update the visibility of the activities after
1201e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // we fetch the visible tasks to be controlled by the animation
1211e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
1221e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1231e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Post a timeout for the animation
1241e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mHandler.postDelayed(mCancelAnimationRunnable, RECENTS_ANIMATION_TIMEOUT);
1251e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        } finally {
1261e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.continueSurfaceLayout();
1271e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        }
128e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
129e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
130e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    @Override
131e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    public void onAnimationFinished(boolean moveHomeToTop) {
132e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mHandler.removeCallbacks(mCancelAnimationRunnable);
133e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        synchronized (mService) {
134e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            if (mWindowManager.getRecentsAnimationController() == null) return;
135e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
136e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            mWindowManager.inSurfaceTransaction(() -> {
1371e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.deferSurfaceLayout();
1381e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                try {
1391e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.cleanupRecentsAnimation();
1401e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1411e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // Move the home stack to the front
1421e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    final ActivityRecord homeActivity = mStackSupervisor.getHomeActivity();
1431e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    if (homeActivity == null) {
1441e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        return;
1451e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
1461e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1471e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // Restore the launched-behind state
1481e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    homeActivity.mLaunchTaskBehind = false;
1491e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1501e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    if (moveHomeToTop) {
1511e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        // Bring the home stack to the front
1521e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        final ActivityStack homeStack = homeActivity.getStack();
1531e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        homeStack.mNoAnimActivities.add(homeActivity);
1541e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        homeStack.moveToFront("RecentsAnimation.onAnimationFinished()");
1551e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    } else {
1561e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        // Restore the home stack to its previous position
1571e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        final ActivityDisplay display = homeActivity.getDisplay();
1581e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        display.moveHomeStackBehindStack(mRestoreHomeBehindStack);
1591e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
1601e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1611e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
1621e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, false);
1631e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
1641e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1651e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // No reason to wait for the pausing activity in this case, as the hiding of
1661e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // surfaces needs to be done immediately.
1671e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.executeAppTransition();
1681e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                } finally {
1691e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.continueSurfaceLayout();
170e2d721781fc024cbd9a14929741e5b476242291fWinson Chung                }
171e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            });
172e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        }
173e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
174e2d721781fc024cbd9a14929741e5b476242291fWinson Chung}
175