RecentsAnimation.java revision 00a0969b50b88000d9cc2ddb7af0582c32b20c32
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
1954cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggiimport static android.app.ActivityManager.START_TASK_TO_FRONT;
20f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chungimport static android.app.AppOpsManager.OP_ASSIST_STRUCTURE;
21f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chungimport static android.app.AppOpsManager.OP_NONE;
22e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
233e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chungimport static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
243e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chungimport static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
25e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION;
27584d652a1dba2b09975a1555c71ed339374faac7Winson Chungimport static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
28e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static android.view.WindowManager.TRANSIT_NONE;
29e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
303e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chungimport static com.android.server.wm.RecentsAnimationController.REORDER_KEEP_IN_PLACE;
313e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chungimport static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_TO_ORIGINAL_POSITION;
323e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chungimport static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_TO_TOP;
33e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
34e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.app.ActivityOptions;
35f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chungimport android.app.AppOpsManager;
36f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chungimport android.app.IAssistDataReceiver;
37e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.content.ComponentName;
38f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chungimport android.content.Context;
39e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.content.Intent;
40ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chungimport android.os.RemoteException;
41584d652a1dba2b09975a1555c71ed339374faac7Winson Chungimport android.os.Trace;
42ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chungimport android.util.Slog;
43e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport android.view.IRecentsAnimationRunner;
446a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chungimport com.android.server.wm.RecentsAnimationController;
45e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport com.android.server.wm.RecentsAnimationController.RecentsAnimationCallbacks;
46e2d721781fc024cbd9a14929741e5b476242291fWinson Chungimport com.android.server.wm.WindowManagerService;
47e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
48e2d721781fc024cbd9a14929741e5b476242291fWinson Chung/**
49e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * Manages the recents animation, including the reordering of the stacks for the transition and
50e2d721781fc024cbd9a14929741e5b476242291fWinson Chung * cleanup. See {@link com.android.server.wm.RecentsAnimationController}.
51e2d721781fc024cbd9a14929741e5b476242291fWinson Chung */
52e2d721781fc024cbd9a14929741e5b476242291fWinson Chungclass RecentsAnimation implements RecentsAnimationCallbacks {
53e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private static final String TAG = RecentsAnimation.class.getSimpleName();
54c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    // TODO (b/73188263): Reset debugging flags
55c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    private static final boolean DEBUG = true;
56e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
57e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityManagerService mService;
58e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityStackSupervisor mStackSupervisor;
59e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityStartController mActivityStartController;
60e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final WindowManagerService mWindowManager;
61e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final UserController mUserController;
623e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    private final ActivityDisplay mDefaultDisplay;
63bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi    private final int mCallingPid;
64e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
653e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    private int mTargetActivityType;
66f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung    private AssistDataRequester mAssistDataRequester;
673e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung
683e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    // The stack to restore the target stack behind when the animation is finished
693e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    private ActivityStack mRestoreTargetBehindStack;
70e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
71e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    RecentsAnimation(ActivityManagerService am, ActivityStackSupervisor stackSupervisor,
72e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            ActivityStartController activityStartController, WindowManagerService wm,
73bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi            UserController userController, int callingPid) {
74e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mService = am;
75e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mStackSupervisor = stackSupervisor;
763e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        mDefaultDisplay = stackSupervisor.getDefaultDisplay();
77e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mActivityStartController = activityStartController;
78e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mWindowManager = wm;
79e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mUserController = userController;
80bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi        mCallingPid = callingPid;
81e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
82e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
83e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    void startRecentsActivity(Intent intent, IRecentsAnimationRunner recentsAnimationRunner,
84f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            ComponentName recentsComponent, int recentsUid,
85f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            IAssistDataReceiver assistDataReceiver) {
86f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung        if (DEBUG) Slog.d(TAG, "startRecentsActivity(): intent=" + intent
87f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                + " assistDataReceiver=" + assistDataReceiver);
88584d652a1dba2b09975a1555c71ed339374faac7Winson Chung        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "RecentsAnimation#startRecentsActivity");
89ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
90ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        if (!mWindowManager.canStartRecentsAnimation()) {
91ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            notifyAnimationCancelBeforeStart(recentsAnimationRunner);
92c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (DEBUG) Slog.d(TAG, "Can't start recents animation, nextAppTransition="
93c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        + mWindowManager.getPendingAppTransition());
94ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            return;
95ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
96ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
973e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // If the activity is associated with the recents stack, then try and get that first
983e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        mTargetActivityType = intent.getComponent() != null
993e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                && recentsComponent.equals(intent.getComponent())
1003e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        ? ACTIVITY_TYPE_RECENTS
1013e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        : ACTIVITY_TYPE_HOME;
1023e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        final ActivityStack targetStack = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
1033e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                mTargetActivityType);
10400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        ActivityRecord targetActivity = getTargetActivity(targetStack, intent.getComponent());
1053e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        final boolean hasExistingActivity = targetActivity != null;
1063e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        if (hasExistingActivity) {
1073e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            final ActivityDisplay display = targetActivity.getDisplay();
1083e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mRestoreTargetBehindStack = display.getStackAbove(targetStack);
1093e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (mRestoreTargetBehindStack == null) {
110ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung                notifyAnimationCancelBeforeStart(recentsAnimationRunner);
111c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "No stack above target stack=" + targetStack);
112ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung                return;
113ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            }
114ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
115ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1163e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // Send launch hint if we are actually launching the target. If it's already visible
1173e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // (shouldn't happen in general) we don't need to send it.
1183e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        if (targetActivity == null || !targetActivity.visible) {
119ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            mStackSupervisor.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
1203e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity);
121ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi        }
122ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
12354cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi        mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunching();
12454cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi
125bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi        mService.setRunningRemoteAnimation(mCallingPid, true);
126bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
1271e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        mWindowManager.deferSurfaceLayout();
1281e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        try {
129f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Kick off the assist data request in the background before showing the target activity
130f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (assistDataReceiver != null) {
131f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                final AppOpsManager appOpsManager = (AppOpsManager)
132f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        mService.mContext.getSystemService(Context.APP_OPS_SERVICE);
133f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                final AssistDataReceiverProxy proxy = new AssistDataReceiverProxy(
134f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        assistDataReceiver, recentsComponent.getPackageName());
135f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = new AssistDataRequester(mService.mContext, mService,
136f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        mWindowManager, appOpsManager, proxy, this, OP_ASSIST_STRUCTURE, OP_NONE);
137f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.requestAssistData(mStackSupervisor.getTopVisibleActivities(),
138f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        true /* fetchData */, false /* fetchScreenshots */,
139f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        true /* allowFetchData */, false /* allowFetchScreenshots */,
140f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        recentsUid, recentsComponent.getPackageName());
141f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
142f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
143ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            final ActivityDisplay display;
1443e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (hasExistingActivity) {
1453e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // Move the recents activity into place for the animation if it is not top most
1463e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                display = targetActivity.getDisplay();
1473e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                display.moveStackBehindBottomMostVisibleStack(targetStack);
148c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Moved stack=" + targetStack + " behind stack="
149c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + display.getStackAbove(targetStack));
15000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
15100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // If there are multiple tasks in the target stack (ie. the home stack, with 3p
15200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // and default launchers coexisting), then move the task to the top as a part of
15300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // moving the stack to the front
15400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                if (targetStack.topTask() != targetActivity.getTask()) {
15500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                    targetStack.addTask(targetActivity.getTask(), true /* toTop */,
15600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                            "startRecentsActivity");
15700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                }
158ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            } else {
1593e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // No recents activity
1603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                ActivityOptions options = ActivityOptions.makeBasic();
1613e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setLaunchActivityType(mTargetActivityType);
1623e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setAvoidMoveToFront();
1631e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION);
1641e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1651e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mActivityStartController
1663e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .obtainStarter(intent, "startRecentsActivity_noTargetActivity")
1671e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingUid(recentsUid)
1681e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingPackage(recentsComponent.getPackageName())
1693e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .setActivityOptions(SafeActivityOptions.fromBundle(options.toBundle()))
1701e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setMayWait(mUserController.getCurrentUserId())
1711e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .execute();
1721e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
173e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1743e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                targetActivity = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
1753e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mTargetActivityType).getTopActivity();
1763e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                display = targetActivity.getDisplay();
177ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1781e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                // TODO: Maybe wait for app to draw in this particular case?
179c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
180c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Started intent=" + intent);
1811e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            }
182e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1833e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Mark the target activity as launch-behind to bump its visibility for the
1841e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // duration of the gesture that is driven by the recents component
1853e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            targetActivity.mLaunchTaskBehind = true;
186e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1871e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Fetch all the surface controls and pass them to the client to get the animation
1881e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // started
189c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            mWindowManager.cancelRecentsAnimation(REORDER_MOVE_TO_ORIGINAL_POSITION,
190c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    "startRecentsActivity");
1913e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mWindowManager.initializeRecentsAnimation(mTargetActivityType, recentsAnimationRunner,
1923e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    this, display.mDisplayId, mStackSupervisor.mRecentTasks.getRecentTaskIds());
193e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1941e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // If we updated the launch-behind state, update the visibility of the activities after
1951e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // we fetch the visible tasks to be controlled by the animation
1961e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
19754cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi
19854cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi            mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunched(START_TASK_TO_FRONT,
1993e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity);
200c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        } catch (Exception e) {
201c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            Slog.e(TAG, "Failed to start recents activity", e);
202c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            throw e;
2031e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        } finally {
2041e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.continueSurfaceLayout();
205584d652a1dba2b09975a1555c71ed339374faac7Winson Chung            Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2061e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        }
207e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
208e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
209e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    @Override
2106a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung    public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode) {
211e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        synchronized (mService) {
212c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (DEBUG) Slog.d(TAG, "onAnimationFinished(): controller="
213c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        + mWindowManager.getRecentsAnimationController()
214c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        + " reorderMode=" + reorderMode);
215f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
216f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Cancel the associated assistant data request
217f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (mAssistDataRequester != null) {
218f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.cancel();
219f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = null;
220f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
221f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
222e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            if (mWindowManager.getRecentsAnimationController() == null) return;
223e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
2243e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Just to be sure end the launch hint in case the target activity was never launched.
2253e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // However, if we're keeping the activity and making it visible, we can leave it on.
2263e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (reorderMode != REORDER_KEEP_IN_PLACE) {
227ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi                mStackSupervisor.sendPowerHintForLaunchEndIfNeeded();
228ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            }
229ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
230bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi            mService.setRunningRemoteAnimation(mCallingPid, false);
231bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
232e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            mWindowManager.inSurfaceTransaction(() -> {
233584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER,
234584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                        "RecentsAnimation#onAnimationFinished_inSurfaceTransaction");
2351e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.deferSurfaceLayout();
2361e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                try {
2376a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    mWindowManager.cleanupRecentsAnimation(reorderMode);
2381e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2393e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    final ActivityStack targetStack = mDefaultDisplay.getStack(
2403e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                            WINDOWING_MODE_UNDEFINED, mTargetActivityType);
2413e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    final ActivityRecord targetActivity = targetStack.getTopActivity();
242c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    if (DEBUG) Slog.d(TAG, "onAnimationFinished(): targetStack=" + targetStack
243c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " targetActivity=" + targetActivity
244c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " mRestoreTargetBehindStack=" + mRestoreTargetBehindStack);
2453e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (targetActivity == null) {
2461e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        return;
2471e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2481e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2491e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // Restore the launched-behind state
2503e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity.mLaunchTaskBehind = false;
2513e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung
2523e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (reorderMode == REORDER_MOVE_TO_TOP) {
2533e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Bring the target stack to the front
2543e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mStackSupervisor.mNoAnimActivities.add(targetActivity);
2553e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        targetStack.moveToFront("RecentsAnimation.onAnimationFinished()");
256c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
257c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack topStack = getTopNonAlwaysOnTopStack();
258c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (topStack != targetStack) {
259c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
260c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to be top most but found stack=" + topStack);
261c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
262c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2633e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    } else if (reorderMode == REORDER_MOVE_TO_ORIGINAL_POSITION){
2643e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Restore the target stack to its previous position
2653e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        final ActivityDisplay display = targetActivity.getDisplay();
2663e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        display.moveStackBehindStack(targetStack, mRestoreTargetBehindStack);
267c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
268c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack aboveTargetStack =
269c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    mDefaultDisplay.getStackAbove(targetStack);
270c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (mRestoreTargetBehindStack != null
271c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    && aboveTargetStack != mRestoreTargetBehindStack) {
272c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
273c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to restored behind stack=" + mRestoreTargetBehindStack
274c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " but it is behind stack=" + aboveTargetStack);
275c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
276c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2776a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    } else {
2783e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Keep target stack in place, nothing changes, so ignore the transition
2793e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // logic below
2806a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                        return;
2811e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2821e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2831e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2841e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, false);
2851e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
2861e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2871e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // No reason to wait for the pausing activity in this case, as the hiding of
2881e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // surfaces needs to be done immediately.
2891e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.executeAppTransition();
290f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung
291f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    // After reordering the stacks, reset the minimized state. At this point, either
2923e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // the target activity is now top-most and we will stay minimized (if in
2933e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // split-screen), or we will have returned to the app, and the minimized state
2943e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // should be reset
295f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    mWindowManager.checkSplitScreenMinimizedChanged(true /* animate */);
296c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                } catch (Exception e) {
297c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    Slog.e(TAG, "Failed to clean up recents activity", e);
298c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    throw e;
2991e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                } finally {
3001e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.continueSurfaceLayout();
301584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                    Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
302e2d721781fc024cbd9a14929741e5b476242291fWinson Chung                }
303e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            });
304e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        }
305e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
306ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
307ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    /**
308ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     * Called only when the animation should be canceled prior to starting.
309ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     */
310ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    private void notifyAnimationCancelBeforeStart(IRecentsAnimationRunner recentsAnimationRunner) {
311ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        try {
312ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            recentsAnimationRunner.onAnimationCanceled();
313ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        } catch (RemoteException e) {
314ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            Slog.e(TAG, "Failed to cancel recents animation before start", e);
315ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
316ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    }
317c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
318c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    /**
319c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     * @return The top stack that is not always-on-top.
320c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     */
321c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    private ActivityStack getTopNonAlwaysOnTopStack() {
322c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        for (int i = mDefaultDisplay.getChildCount() - 1; i >= 0; i--) {
323c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            final ActivityStack s = mDefaultDisplay.getChildAt(i);
324c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (s.getWindowConfiguration().isAlwaysOnTop()) {
325c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                continue;
326c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            }
327c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            return s;
328c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        }
329c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        return null;
330c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    }
33100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
33200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    /**
33300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     * @return the top activity in the {@param targetStack} matching the {@param component}, or just
33400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     * the top activity of the top task if no task matches the component.
33500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     */
33600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    private ActivityRecord getTargetActivity(ActivityStack targetStack, ComponentName component) {
33700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        if (targetStack == null) {
33800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            return null;
33900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        }
34000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
34100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        for (int i = targetStack.getChildCount() - 1; i >= 0; i--) {
34200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            final TaskRecord task = (TaskRecord) targetStack.getChildAt(i);
34300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            if (task.getBaseIntent().getComponent().equals(component)) {
34400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                return task.getTopActivity();
34500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            }
34600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        }
34700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        return targetStack.getTopActivity();
34800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    }
349e2d721781fc024cbd9a14929741e5b476242291fWinson Chung}
350