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 */
520f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chungclass RecentsAnimation implements RecentsAnimationCallbacks,
530f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        ActivityDisplay.OnStackOrderChangedListener {
54e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private static final String TAG = RecentsAnimation.class.getSimpleName();
5544a7e2ecad3d40efc809608c5238feb7b538b8c4Winson Chung    private static final boolean DEBUG = false;
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
1433e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (hasExistingActivity) {
1443e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // Move the recents activity into place for the animation if it is not top most
1450f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                mDefaultDisplay.moveStackBehindBottomMostVisibleStack(targetStack);
146c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Moved stack=" + targetStack + " behind stack="
1470f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                            + mDefaultDisplay.getStackAbove(targetStack));
14800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
14900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // If there are multiple tasks in the target stack (ie. the home stack, with 3p
15000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // and default launchers coexisting), then move the task to the top as a part of
15100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // moving the stack to the front
15200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                if (targetStack.topTask() != targetActivity.getTask()) {
15300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                    targetStack.addTask(targetActivity.getTask(), true /* toTop */,
15400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                            "startRecentsActivity");
15500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                }
156ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            } else {
1573e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // No recents activity
1583e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                ActivityOptions options = ActivityOptions.makeBasic();
1593e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setLaunchActivityType(mTargetActivityType);
1603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setAvoidMoveToFront();
1611e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION);
1621e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1631e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mActivityStartController
1643e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .obtainStarter(intent, "startRecentsActivity_noTargetActivity")
1651e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingUid(recentsUid)
1661e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingPackage(recentsComponent.getPackageName())
1673e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .setActivityOptions(SafeActivityOptions.fromBundle(options.toBundle()))
1681e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setMayWait(mUserController.getCurrentUserId())
1691e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .execute();
1701e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
171ba40d3a107d57fb40af413d9118f8261dc362139Winson Chung                mWindowManager.executeAppTransition();
172e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1733e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                targetActivity = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
1743e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mTargetActivityType).getTopActivity();
175ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1761e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                // TODO: Maybe wait for app to draw in this particular case?
177c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
178c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Started intent=" + intent);
1791e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            }
180e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1813e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Mark the target activity as launch-behind to bump its visibility for the
1821e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // duration of the gesture that is driven by the recents component
1833e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            targetActivity.mLaunchTaskBehind = true;
184e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1851e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Fetch all the surface controls and pass them to the client to get the animation
18665c5f99aae6969e6353e3be8991619c076450e43Winson Chung            // started. Cancel any existing recents animation running synchronously (do not hold the
18765c5f99aae6969e6353e3be8991619c076450e43Winson Chung            // WM lock)
18865c5f99aae6969e6353e3be8991619c076450e43Winson Chung            mWindowManager.cancelRecentsAnimationSynchronously(REORDER_MOVE_TO_ORIGINAL_POSITION,
189c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    "startRecentsActivity");
1903e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mWindowManager.initializeRecentsAnimation(mTargetActivityType, recentsAnimationRunner,
1910f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                    this, mDefaultDisplay.mDisplayId,
1920f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                    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);
2000f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung
2010f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            // Register for stack order changes
2020f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            mDefaultDisplay.registerStackOrderChangedListener(this);
203c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        } catch (Exception e) {
204c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            Slog.e(TAG, "Failed to start recents activity", e);
205c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            throw e;
2061e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        } finally {
2071e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.continueSurfaceLayout();
208584d652a1dba2b09975a1555c71ed339374faac7Winson Chung            Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2091e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        }
210e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
211e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
21265c5f99aae6969e6353e3be8991619c076450e43Winson Chung    private void finishAnimation(@RecentsAnimationController.ReorderMode int reorderMode) {
213e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        synchronized (mService) {
214c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (DEBUG) Slog.d(TAG, "onAnimationFinished(): controller="
21565c5f99aae6969e6353e3be8991619c076450e43Winson Chung                    + mWindowManager.getRecentsAnimationController()
21665c5f99aae6969e6353e3be8991619c076450e43Winson Chung                    + " reorderMode=" + reorderMode);
217f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
218f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Cancel the associated assistant data request
219f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (mAssistDataRequester != null) {
220f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.cancel();
221f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = null;
222f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
223f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
2240f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            // Unregister for stack order changes
2250f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            mDefaultDisplay.unregisterStackOrderChangedListener(this);
2260f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung
227e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            if (mWindowManager.getRecentsAnimationController() == null) return;
228e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
2293e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Just to be sure end the launch hint in case the target activity was never launched.
2303e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // However, if we're keeping the activity and making it visible, we can leave it on.
2313e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (reorderMode != REORDER_KEEP_IN_PLACE) {
232ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi                mStackSupervisor.sendPowerHintForLaunchEndIfNeeded();
233ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            }
234ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
235bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi            mService.setRunningRemoteAnimation(mCallingPid, false);
236bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
237e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            mWindowManager.inSurfaceTransaction(() -> {
238584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER,
239584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                        "RecentsAnimation#onAnimationFinished_inSurfaceTransaction");
2401e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.deferSurfaceLayout();
2411e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                try {
2426a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    mWindowManager.cleanupRecentsAnimation(reorderMode);
2431e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2443e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    final ActivityStack targetStack = mDefaultDisplay.getStack(
2453e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                            WINDOWING_MODE_UNDEFINED, mTargetActivityType);
24682389a9333718fd24ab1d7bc046b696074d65956Winson Chung                    final ActivityRecord targetActivity = targetStack != null
24782389a9333718fd24ab1d7bc046b696074d65956Winson Chung                            ? targetStack.getTopActivity()
24882389a9333718fd24ab1d7bc046b696074d65956Winson Chung                            : null;
249c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    if (DEBUG) Slog.d(TAG, "onAnimationFinished(): targetStack=" + targetStack
250c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " targetActivity=" + targetActivity
251c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " mRestoreTargetBehindStack=" + mRestoreTargetBehindStack);
2523e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (targetActivity == null) {
2531e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        return;
2541e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2551e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2561e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // Restore the launched-behind state
2573e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity.mLaunchTaskBehind = false;
2583e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung
2593e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (reorderMode == REORDER_MOVE_TO_TOP) {
2603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Bring the target stack to the front
2613e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mStackSupervisor.mNoAnimActivities.add(targetActivity);
2623e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        targetStack.moveToFront("RecentsAnimation.onAnimationFinished()");
263c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
264c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack topStack = getTopNonAlwaysOnTopStack();
265c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (topStack != targetStack) {
266c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
267c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to be top most but found stack=" + topStack);
268c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
269c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2703e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    } else if (reorderMode == REORDER_MOVE_TO_ORIGINAL_POSITION){
2713e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Restore the target stack to its previous position
2723e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        final ActivityDisplay display = targetActivity.getDisplay();
2733e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        display.moveStackBehindStack(targetStack, mRestoreTargetBehindStack);
274c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
275c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack aboveTargetStack =
276c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    mDefaultDisplay.getStackAbove(targetStack);
277c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (mRestoreTargetBehindStack != null
278c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    && aboveTargetStack != mRestoreTargetBehindStack) {
279c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
280c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to restored behind stack=" + mRestoreTargetBehindStack
281c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " but it is behind stack=" + aboveTargetStack);
282c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
283c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2846a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    } else {
2853e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Keep target stack in place, nothing changes, so ignore the transition
2863e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // logic below
2876a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                        return;
2881e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2891e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2901e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2911e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, false);
2921e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
2931e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2941e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // No reason to wait for the pausing activity in this case, as the hiding of
2951e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // surfaces needs to be done immediately.
2961e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.executeAppTransition();
297f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung
298f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    // After reordering the stacks, reset the minimized state. At this point, either
2993e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // the target activity is now top-most and we will stay minimized (if in
3003e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // split-screen), or we will have returned to the app, and the minimized state
3013e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // should be reset
302f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    mWindowManager.checkSplitScreenMinimizedChanged(true /* animate */);
303c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                } catch (Exception e) {
304c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    Slog.e(TAG, "Failed to clean up recents activity", e);
305c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    throw e;
3061e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                } finally {
3071e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.continueSurfaceLayout();
308584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                    Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
309e2d721781fc024cbd9a14929741e5b476242291fWinson Chung                }
310e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            });
311e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        }
312e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
313ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
31465c5f99aae6969e6353e3be8991619c076450e43Winson Chung    @Override
31565c5f99aae6969e6353e3be8991619c076450e43Winson Chung    public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode,
31665c5f99aae6969e6353e3be8991619c076450e43Winson Chung            boolean runSychronously) {
31765c5f99aae6969e6353e3be8991619c076450e43Winson Chung        if (runSychronously) {
31865c5f99aae6969e6353e3be8991619c076450e43Winson Chung            finishAnimation(reorderMode);
31965c5f99aae6969e6353e3be8991619c076450e43Winson Chung        } else {
32065c5f99aae6969e6353e3be8991619c076450e43Winson Chung            mService.mHandler.post(() -> finishAnimation(reorderMode));
32165c5f99aae6969e6353e3be8991619c076450e43Winson Chung        }
32265c5f99aae6969e6353e3be8991619c076450e43Winson Chung    }
32365c5f99aae6969e6353e3be8991619c076450e43Winson Chung
3240f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung    @Override
3250f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung    public void onStackOrderChanged() {
3260f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        // If the activity display stack order changes, cancel any running recents animation in
3270f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        // place
3280f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        mWindowManager.cancelRecentsAnimationSynchronously(REORDER_KEEP_IN_PLACE,
3290f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                "stackOrderChanged");
3300f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung    }
3310f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung
332ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    /**
333ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     * Called only when the animation should be canceled prior to starting.
334ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     */
335ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    private void notifyAnimationCancelBeforeStart(IRecentsAnimationRunner recentsAnimationRunner) {
336ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        try {
337ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            recentsAnimationRunner.onAnimationCanceled();
338ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        } catch (RemoteException e) {
339ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            Slog.e(TAG, "Failed to cancel recents animation before start", e);
340ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
341ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    }
342c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
343c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    /**
344c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     * @return The top stack that is not always-on-top.
345c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     */
346c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    private ActivityStack getTopNonAlwaysOnTopStack() {
347c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        for (int i = mDefaultDisplay.getChildCount() - 1; i >= 0; i--) {
348c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            final ActivityStack s = mDefaultDisplay.getChildAt(i);
349c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (s.getWindowConfiguration().isAlwaysOnTop()) {
350c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                continue;
351c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            }
352c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            return s;
353c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        }
354c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        return null;
355c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    }
35600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
35700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    /**
35800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     * @return the top activity in the {@param targetStack} matching the {@param component}, or just
35900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     * the top activity of the top task if no task matches the component.
36000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     */
36100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    private ActivityRecord getTargetActivity(ActivityStack targetStack, ComponentName component) {
36200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        if (targetStack == null) {
36300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            return null;
36400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        }
36500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
36600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        for (int i = targetStack.getChildCount() - 1; i >= 0; i--) {
36700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            final TaskRecord task = (TaskRecord) targetStack.getChildAt(i);
36800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            if (task.getBaseIntent().getComponent().equals(component)) {
36900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                return task.getTopActivity();
37000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            }
37100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        }
37200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        return targetStack.getTopActivity();
37300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    }
374e2d721781fc024cbd9a14929741e5b476242291fWinson Chung}
375