RecentsAnimation.java revision 65c5f99aae6969e6353e3be8991619c076450e43
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);
1043e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        ActivityRecord targetActivity = targetStack != null
1053e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                ? targetStack.getTopActivity()
1063e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                : null;
1073e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        final boolean hasExistingActivity = targetActivity != null;
1083e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        if (hasExistingActivity) {
1093e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            final ActivityDisplay display = targetActivity.getDisplay();
1103e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mRestoreTargetBehindStack = display.getStackAbove(targetStack);
1113e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (mRestoreTargetBehindStack == null) {
112ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung                notifyAnimationCancelBeforeStart(recentsAnimationRunner);
113c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "No stack above target stack=" + targetStack);
114ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung                return;
115ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            }
116ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
117ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1183e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // Send launch hint if we are actually launching the target. If it's already visible
1193e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // (shouldn't happen in general) we don't need to send it.
1203e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        if (targetActivity == null || !targetActivity.visible) {
121ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            mStackSupervisor.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
1223e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity);
123ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi        }
124ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
12554cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi        mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunching();
12654cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi
127bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi        mService.setRunningRemoteAnimation(mCallingPid, true);
128bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
1291e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        mWindowManager.deferSurfaceLayout();
1301e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        try {
131f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Kick off the assist data request in the background before showing the target activity
132f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (assistDataReceiver != null) {
133f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                final AppOpsManager appOpsManager = (AppOpsManager)
134f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        mService.mContext.getSystemService(Context.APP_OPS_SERVICE);
135f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                final AssistDataReceiverProxy proxy = new AssistDataReceiverProxy(
136f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        assistDataReceiver, recentsComponent.getPackageName());
137f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = new AssistDataRequester(mService.mContext, mService,
138f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        mWindowManager, appOpsManager, proxy, this, OP_ASSIST_STRUCTURE, OP_NONE);
139f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.requestAssistData(mStackSupervisor.getTopVisibleActivities(),
140f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        true /* fetchData */, false /* fetchScreenshots */,
141f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        true /* allowFetchData */, false /* allowFetchScreenshots */,
142f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        recentsUid, recentsComponent.getPackageName());
143f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
144f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
145ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            final ActivityDisplay display;
1463e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (hasExistingActivity) {
1473e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // Move the recents activity into place for the animation if it is not top most
1483e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                display = targetActivity.getDisplay();
1493e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                display.moveStackBehindBottomMostVisibleStack(targetStack);
150c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Moved stack=" + targetStack + " behind stack="
151c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + display.getStackAbove(targetStack));
152ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            } else {
1533e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // No recents activity
1543e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                ActivityOptions options = ActivityOptions.makeBasic();
1553e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setLaunchActivityType(mTargetActivityType);
1563e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setAvoidMoveToFront();
1571e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION);
1581e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1591e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mActivityStartController
1603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .obtainStarter(intent, "startRecentsActivity_noTargetActivity")
1611e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingUid(recentsUid)
1621e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingPackage(recentsComponent.getPackageName())
1633e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .setActivityOptions(SafeActivityOptions.fromBundle(options.toBundle()))
1641e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setMayWait(mUserController.getCurrentUserId())
1651e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .execute();
1661e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
167e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1683e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                targetActivity = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
1693e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mTargetActivityType).getTopActivity();
1703e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                display = targetActivity.getDisplay();
171ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1721e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                // TODO: Maybe wait for app to draw in this particular case?
173c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
174c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Started intent=" + intent);
1751e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            }
176e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1773e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Mark the target activity as launch-behind to bump its visibility for the
1781e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // duration of the gesture that is driven by the recents component
1793e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            targetActivity.mLaunchTaskBehind = true;
180e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1811e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Fetch all the surface controls and pass them to the client to get the animation
18265c5f99aae6969e6353e3be8991619c076450e43Winson Chung            // started. Cancel any existing recents animation running synchronously (do not hold the
18365c5f99aae6969e6353e3be8991619c076450e43Winson Chung            // WM lock)
18465c5f99aae6969e6353e3be8991619c076450e43Winson Chung            mWindowManager.cancelRecentsAnimationSynchronously(REORDER_MOVE_TO_ORIGINAL_POSITION,
185c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    "startRecentsActivity");
1863e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mWindowManager.initializeRecentsAnimation(mTargetActivityType, recentsAnimationRunner,
1873e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    this, display.mDisplayId, mStackSupervisor.mRecentTasks.getRecentTaskIds());
188e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1891e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // If we updated the launch-behind state, update the visibility of the activities after
1901e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // we fetch the visible tasks to be controlled by the animation
1911e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
19254cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi
19354cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi            mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunched(START_TASK_TO_FRONT,
1943e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity);
195c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        } catch (Exception e) {
196c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            Slog.e(TAG, "Failed to start recents activity", e);
197c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            throw e;
1981e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        } finally {
1991e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.continueSurfaceLayout();
200584d652a1dba2b09975a1555c71ed339374faac7Winson Chung            Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2011e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        }
202e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
203e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
20465c5f99aae6969e6353e3be8991619c076450e43Winson Chung    private void finishAnimation(@RecentsAnimationController.ReorderMode int reorderMode) {
205e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        synchronized (mService) {
206c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (DEBUG) Slog.d(TAG, "onAnimationFinished(): controller="
20765c5f99aae6969e6353e3be8991619c076450e43Winson Chung                    + mWindowManager.getRecentsAnimationController()
20865c5f99aae6969e6353e3be8991619c076450e43Winson Chung                    + " reorderMode=" + reorderMode);
209f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
210f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Cancel the associated assistant data request
211f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (mAssistDataRequester != null) {
212f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.cancel();
213f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = null;
214f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
215f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
216e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            if (mWindowManager.getRecentsAnimationController() == null) return;
217e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
2183e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Just to be sure end the launch hint in case the target activity was never launched.
2193e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // However, if we're keeping the activity and making it visible, we can leave it on.
2203e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (reorderMode != REORDER_KEEP_IN_PLACE) {
221ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi                mStackSupervisor.sendPowerHintForLaunchEndIfNeeded();
222ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            }
223ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
224bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi            mService.setRunningRemoteAnimation(mCallingPid, false);
225bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
226e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            mWindowManager.inSurfaceTransaction(() -> {
227584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER,
228584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                        "RecentsAnimation#onAnimationFinished_inSurfaceTransaction");
2291e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.deferSurfaceLayout();
2301e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                try {
2316a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    mWindowManager.cleanupRecentsAnimation(reorderMode);
2321e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2333e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    final ActivityStack targetStack = mDefaultDisplay.getStack(
2343e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                            WINDOWING_MODE_UNDEFINED, mTargetActivityType);
2353e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    final ActivityRecord targetActivity = targetStack.getTopActivity();
236c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    if (DEBUG) Slog.d(TAG, "onAnimationFinished(): targetStack=" + targetStack
237c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " targetActivity=" + targetActivity
238c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " mRestoreTargetBehindStack=" + mRestoreTargetBehindStack);
2393e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (targetActivity == null) {
2401e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        return;
2411e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2421e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2431e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // Restore the launched-behind state
2443e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity.mLaunchTaskBehind = false;
2453e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung
2463e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (reorderMode == REORDER_MOVE_TO_TOP) {
2473e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Bring the target stack to the front
2483e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mStackSupervisor.mNoAnimActivities.add(targetActivity);
2493e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        targetStack.moveToFront("RecentsAnimation.onAnimationFinished()");
250c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
251c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack topStack = getTopNonAlwaysOnTopStack();
252c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (topStack != targetStack) {
253c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
254c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to be top most but found stack=" + topStack);
255c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
256c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2573e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    } else if (reorderMode == REORDER_MOVE_TO_ORIGINAL_POSITION){
2583e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Restore the target stack to its previous position
2593e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        final ActivityDisplay display = targetActivity.getDisplay();
2603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        display.moveStackBehindStack(targetStack, mRestoreTargetBehindStack);
261c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
262c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack aboveTargetStack =
263c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    mDefaultDisplay.getStackAbove(targetStack);
264c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (mRestoreTargetBehindStack != null
265c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    && aboveTargetStack != mRestoreTargetBehindStack) {
266c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
267c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to restored behind stack=" + mRestoreTargetBehindStack
268c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " but it is behind stack=" + aboveTargetStack);
269c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
270c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2716a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    } else {
2723e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Keep target stack in place, nothing changes, so ignore the transition
2733e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // logic below
2746a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                        return;
2751e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2761e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2771e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2781e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, false);
2791e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
2801e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2811e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // No reason to wait for the pausing activity in this case, as the hiding of
2821e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // surfaces needs to be done immediately.
2831e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.executeAppTransition();
284f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung
285f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    // After reordering the stacks, reset the minimized state. At this point, either
2863e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // the target activity is now top-most and we will stay minimized (if in
2873e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // split-screen), or we will have returned to the app, and the minimized state
2883e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // should be reset
289f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    mWindowManager.checkSplitScreenMinimizedChanged(true /* animate */);
290c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                } catch (Exception e) {
291c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    Slog.e(TAG, "Failed to clean up recents activity", e);
292c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    throw e;
2931e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                } finally {
2941e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.continueSurfaceLayout();
295584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                    Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
296e2d721781fc024cbd9a14929741e5b476242291fWinson Chung                }
297e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            });
298e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        }
299e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
300ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
30165c5f99aae6969e6353e3be8991619c076450e43Winson Chung    @Override
30265c5f99aae6969e6353e3be8991619c076450e43Winson Chung    public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode,
30365c5f99aae6969e6353e3be8991619c076450e43Winson Chung            boolean runSychronously) {
30465c5f99aae6969e6353e3be8991619c076450e43Winson Chung        if (runSychronously) {
30565c5f99aae6969e6353e3be8991619c076450e43Winson Chung            finishAnimation(reorderMode);
30665c5f99aae6969e6353e3be8991619c076450e43Winson Chung        } else {
30765c5f99aae6969e6353e3be8991619c076450e43Winson Chung            mService.mHandler.post(() -> finishAnimation(reorderMode));
30865c5f99aae6969e6353e3be8991619c076450e43Winson Chung        }
30965c5f99aae6969e6353e3be8991619c076450e43Winson Chung    }
31065c5f99aae6969e6353e3be8991619c076450e43Winson Chung
311ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    /**
312ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     * Called only when the animation should be canceled prior to starting.
313ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     */
314ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    private void notifyAnimationCancelBeforeStart(IRecentsAnimationRunner recentsAnimationRunner) {
315ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        try {
316ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            recentsAnimationRunner.onAnimationCanceled();
317ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        } catch (RemoteException e) {
318ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            Slog.e(TAG, "Failed to cancel recents animation before start", e);
319ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
320ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    }
321c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
322c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    /**
323c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     * @return The top stack that is not always-on-top.
324c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     */
325c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    private ActivityStack getTopNonAlwaysOnTopStack() {
326c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        for (int i = mDefaultDisplay.getChildCount() - 1; i >= 0; i--) {
327c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            final ActivityStack s = mDefaultDisplay.getChildAt(i);
328c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (s.getWindowConfiguration().isAlwaysOnTop()) {
329c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                continue;
330c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            }
331c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            return s;
332c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        }
333c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        return null;
334c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    }
335e2d721781fc024cbd9a14929741e5b476242291fWinson Chung}
336