RecentsAnimation.java revision ba40d3a107d57fb40af413d9118f8261dc362139
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();
55c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    // TODO (b/73188263): Reset debugging flags
56c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    private static final boolean DEBUG = true;
57e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
58e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityManagerService mService;
59e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityStackSupervisor mStackSupervisor;
60e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final ActivityStartController mActivityStartController;
61e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final WindowManagerService mWindowManager;
62e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    private final UserController mUserController;
633e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    private final ActivityDisplay mDefaultDisplay;
64bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi    private final int mCallingPid;
65e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
663e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    private int mTargetActivityType;
67f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung    private AssistDataRequester mAssistDataRequester;
683e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung
693e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    // The stack to restore the target stack behind when the animation is finished
703e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung    private ActivityStack mRestoreTargetBehindStack;
71e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
72e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    RecentsAnimation(ActivityManagerService am, ActivityStackSupervisor stackSupervisor,
73e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            ActivityStartController activityStartController, WindowManagerService wm,
74bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi            UserController userController, int callingPid) {
75e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mService = am;
76e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mStackSupervisor = stackSupervisor;
773e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        mDefaultDisplay = stackSupervisor.getDefaultDisplay();
78e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mActivityStartController = activityStartController;
79e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mWindowManager = wm;
80e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        mUserController = userController;
81bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi        mCallingPid = callingPid;
82e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
83e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
84e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    void startRecentsActivity(Intent intent, IRecentsAnimationRunner recentsAnimationRunner,
85f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            ComponentName recentsComponent, int recentsUid,
86f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            IAssistDataReceiver assistDataReceiver) {
87f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung        if (DEBUG) Slog.d(TAG, "startRecentsActivity(): intent=" + intent
88f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                + " assistDataReceiver=" + assistDataReceiver);
89584d652a1dba2b09975a1555c71ed339374faac7Winson Chung        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "RecentsAnimation#startRecentsActivity");
90ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
91ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        if (!mWindowManager.canStartRecentsAnimation()) {
92ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            notifyAnimationCancelBeforeStart(recentsAnimationRunner);
93c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (DEBUG) Slog.d(TAG, "Can't start recents animation, nextAppTransition="
94c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        + mWindowManager.getPendingAppTransition());
95ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            return;
96ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
97ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
983e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // If the activity is associated with the recents stack, then try and get that first
993e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        mTargetActivityType = intent.getComponent() != null
1003e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                && recentsComponent.equals(intent.getComponent())
1013e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        ? ACTIVITY_TYPE_RECENTS
1023e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        : ACTIVITY_TYPE_HOME;
1033e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        final ActivityStack targetStack = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
1043e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                mTargetActivityType);
10500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        ActivityRecord targetActivity = getTargetActivity(targetStack, intent.getComponent());
1063e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        final boolean hasExistingActivity = targetActivity != null;
1073e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        if (hasExistingActivity) {
1083e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            final ActivityDisplay display = targetActivity.getDisplay();
1093e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mRestoreTargetBehindStack = display.getStackAbove(targetStack);
1103e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (mRestoreTargetBehindStack == null) {
111ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung                notifyAnimationCancelBeforeStart(recentsAnimationRunner);
112c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "No stack above target stack=" + targetStack);
113ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung                return;
114ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            }
115ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
116ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1173e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // Send launch hint if we are actually launching the target. If it's already visible
1183e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        // (shouldn't happen in general) we don't need to send it.
1193e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung        if (targetActivity == null || !targetActivity.visible) {
120ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            mStackSupervisor.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
1213e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity);
122ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi        }
123ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
12454cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi        mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunching();
12554cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi
126bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi        mService.setRunningRemoteAnimation(mCallingPid, true);
127bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
1281e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        mWindowManager.deferSurfaceLayout();
1291e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        try {
130f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Kick off the assist data request in the background before showing the target activity
131f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (assistDataReceiver != null) {
132f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                final AppOpsManager appOpsManager = (AppOpsManager)
133f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        mService.mContext.getSystemService(Context.APP_OPS_SERVICE);
134f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                final AssistDataReceiverProxy proxy = new AssistDataReceiverProxy(
135f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        assistDataReceiver, recentsComponent.getPackageName());
136f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = new AssistDataRequester(mService.mContext, mService,
137f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        mWindowManager, appOpsManager, proxy, this, OP_ASSIST_STRUCTURE, OP_NONE);
138f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.requestAssistData(mStackSupervisor.getTopVisibleActivities(),
139f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        true /* fetchData */, false /* fetchScreenshots */,
140f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        true /* allowFetchData */, false /* allowFetchScreenshots */,
141f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                        recentsUid, recentsComponent.getPackageName());
142f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
143f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
1443e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (hasExistingActivity) {
1453e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // Move the recents activity into place for the animation if it is not top most
1460f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                mDefaultDisplay.moveStackBehindBottomMostVisibleStack(targetStack);
147c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Moved stack=" + targetStack + " behind stack="
1480f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                            + mDefaultDisplay.getStackAbove(targetStack));
14900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
15000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // If there are multiple tasks in the target stack (ie. the home stack, with 3p
15100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // and default launchers coexisting), then move the task to the top as a part of
15200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                // moving the stack to the front
15300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                if (targetStack.topTask() != targetActivity.getTask()) {
15400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                    targetStack.addTask(targetActivity.getTask(), true /* toTop */,
15500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                            "startRecentsActivity");
15600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                }
157ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            } else {
1583e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                // No recents activity
1593e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                ActivityOptions options = ActivityOptions.makeBasic();
1603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setLaunchActivityType(mTargetActivityType);
1613e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                options.setAvoidMoveToFront();
1621e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION);
1631e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
1641e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mActivityStartController
1653e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .obtainStarter(intent, "startRecentsActivity_noTargetActivity")
1661e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingUid(recentsUid)
1671e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setCallingPackage(recentsComponent.getPackageName())
1683e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        .setActivityOptions(SafeActivityOptions.fromBundle(options.toBundle()))
1691e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .setMayWait(mUserController.getCurrentUserId())
1701e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        .execute();
1711e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
172ba40d3a107d57fb40af413d9118f8261dc362139Winson Chung                mWindowManager.executeAppTransition();
173e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1743e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                targetActivity = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
1753e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mTargetActivityType).getTopActivity();
176ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
1771e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                // TODO: Maybe wait for app to draw in this particular case?
178c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
179c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                if (DEBUG) Slog.d(TAG, "Started intent=" + intent);
1801e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            }
181e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1823e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Mark the target activity as launch-behind to bump its visibility for the
1831e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // duration of the gesture that is driven by the recents component
1843e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            targetActivity.mLaunchTaskBehind = true;
185e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1861e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // Fetch all the surface controls and pass them to the client to get the animation
18765c5f99aae6969e6353e3be8991619c076450e43Winson Chung            // started. Cancel any existing recents animation running synchronously (do not hold the
18865c5f99aae6969e6353e3be8991619c076450e43Winson Chung            // WM lock)
18965c5f99aae6969e6353e3be8991619c076450e43Winson Chung            mWindowManager.cancelRecentsAnimationSynchronously(REORDER_MOVE_TO_ORIGINAL_POSITION,
190c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    "startRecentsActivity");
1913e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            mWindowManager.initializeRecentsAnimation(mTargetActivityType, recentsAnimationRunner,
1920f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                    this, mDefaultDisplay.mDisplayId,
1930f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                    mStackSupervisor.mRecentTasks.getRecentTaskIds());
194e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
1951e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // If we updated the launch-behind state, update the visibility of the activities after
1961e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            // we fetch the visible tasks to be controlled by the animation
1971e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
19854cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi
19954cff64ec6ef818e270eb39a74d6a58068553d66Jorim Jaggi            mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunched(START_TASK_TO_FRONT,
2003e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity);
2010f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung
2020f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            // Register for stack order changes
2030f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            mDefaultDisplay.registerStackOrderChangedListener(this);
204c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        } catch (Exception e) {
205c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            Slog.e(TAG, "Failed to start recents activity", e);
206c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            throw e;
2071e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        } finally {
2081e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung            mWindowManager.continueSurfaceLayout();
209584d652a1dba2b09975a1555c71ed339374faac7Winson Chung            Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2101e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung        }
211e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
212e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
21365c5f99aae6969e6353e3be8991619c076450e43Winson Chung    private void finishAnimation(@RecentsAnimationController.ReorderMode int reorderMode) {
214e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        synchronized (mService) {
215c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (DEBUG) Slog.d(TAG, "onAnimationFinished(): controller="
21665c5f99aae6969e6353e3be8991619c076450e43Winson Chung                    + mWindowManager.getRecentsAnimationController()
21765c5f99aae6969e6353e3be8991619c076450e43Winson Chung                    + " reorderMode=" + reorderMode);
218f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
219f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            // Cancel the associated assistant data request
220f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            if (mAssistDataRequester != null) {
221f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester.cancel();
222f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung                mAssistDataRequester = null;
223f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung            }
224f993016ac6375ddb18f61a98b62e8a01aa890a55Winson Chung
2250f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            // Unregister for stack order changes
2260f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung            mDefaultDisplay.unregisterStackOrderChangedListener(this);
2270f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung
228e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            if (mWindowManager.getRecentsAnimationController() == null) return;
229e2d721781fc024cbd9a14929741e5b476242291fWinson Chung
2303e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // Just to be sure end the launch hint in case the target activity was never launched.
2313e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            // However, if we're keeping the activity and making it visible, we can leave it on.
2323e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung            if (reorderMode != REORDER_KEEP_IN_PLACE) {
233ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi                mStackSupervisor.sendPowerHintForLaunchEndIfNeeded();
234ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi            }
235ac96052733c4c07f68f81c203fa05a05940c0f31Jorim Jaggi
236bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi            mService.setRunningRemoteAnimation(mCallingPid, false);
237bc2aabe84568c6b1a54c1b1467a539781488c8caJorim Jaggi
238e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            mWindowManager.inSurfaceTransaction(() -> {
239584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER,
240584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                        "RecentsAnimation#onAnimationFinished_inSurfaceTransaction");
2411e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                mWindowManager.deferSurfaceLayout();
2421e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                try {
2436a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    mWindowManager.cleanupRecentsAnimation(reorderMode);
2441e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2453e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    final ActivityStack targetStack = mDefaultDisplay.getStack(
2463e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                            WINDOWING_MODE_UNDEFINED, mTargetActivityType);
24782389a9333718fd24ab1d7bc046b696074d65956Winson Chung                    final ActivityRecord targetActivity = targetStack != null
24882389a9333718fd24ab1d7bc046b696074d65956Winson Chung                            ? targetStack.getTopActivity()
24982389a9333718fd24ab1d7bc046b696074d65956Winson Chung                            : null;
250c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    if (DEBUG) Slog.d(TAG, "onAnimationFinished(): targetStack=" + targetStack
251c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " targetActivity=" + targetActivity
252c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            + " mRestoreTargetBehindStack=" + mRestoreTargetBehindStack);
2533e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (targetActivity == null) {
2541e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                        return;
2551e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2561e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2571e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // Restore the launched-behind state
2583e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    targetActivity.mLaunchTaskBehind = false;
2593e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung
2603e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    if (reorderMode == REORDER_MOVE_TO_TOP) {
2613e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Bring the target stack to the front
2623e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        mStackSupervisor.mNoAnimActivities.add(targetActivity);
2633e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        targetStack.moveToFront("RecentsAnimation.onAnimationFinished()");
264c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
265c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack topStack = getTopNonAlwaysOnTopStack();
266c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (topStack != targetStack) {
267c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
268c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to be top most but found stack=" + topStack);
269c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
270c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2713e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    } else if (reorderMode == REORDER_MOVE_TO_ORIGINAL_POSITION){
2723e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Restore the target stack to its previous position
2733e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        final ActivityDisplay display = targetActivity.getDisplay();
2743e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        display.moveStackBehindStack(targetStack, mRestoreTargetBehindStack);
275c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        if (DEBUG) {
276c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            final ActivityStack aboveTargetStack =
277c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    mDefaultDisplay.getStackAbove(targetStack);
278c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            if (mRestoreTargetBehindStack != null
279c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                    && aboveTargetStack != mRestoreTargetBehindStack) {
280c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                Slog.w(TAG, "Expected target stack=" + targetStack
281c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " to restored behind stack=" + mRestoreTargetBehindStack
282c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                                        + " but it is behind stack=" + aboveTargetStack);
283c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                            }
284c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                        }
2856a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                    } else {
2863e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // Keep target stack in place, nothing changes, so ignore the transition
2873e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                        // logic below
2886a38fca2d81e5d5bc0343c57e4db3629c3a9a619Winson Chung                        return;
2891e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    }
2901e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2911e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2921e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, false);
2931e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
2941e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung
2951e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // No reason to wait for the pausing activity in this case, as the hiding of
2961e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    // surfaces needs to be done immediately.
2971e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.executeAppTransition();
298f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung
299f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    // After reordering the stacks, reset the minimized state. At this point, either
3003e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // the target activity is now top-most and we will stay minimized (if in
3013e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // split-screen), or we will have returned to the app, and the minimized state
3023e2980ed3251ec5f8ca85d7ce23f44daf717dd82Winson Chung                    // should be reset
303f557c3b56547de332b1f116f316b3a81dabd230dWinson Chung                    mWindowManager.checkSplitScreenMinimizedChanged(true /* animate */);
304c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                } catch (Exception e) {
305c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    Slog.e(TAG, "Failed to clean up recents activity", e);
306c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                    throw e;
3071e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                } finally {
3081e6d4a9e001f910de9396f4056eb74fdf9b8257cWinson Chung                    mWindowManager.continueSurfaceLayout();
309584d652a1dba2b09975a1555c71ed339374faac7Winson Chung                    Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
310e2d721781fc024cbd9a14929741e5b476242291fWinson Chung                }
311e2d721781fc024cbd9a14929741e5b476242291fWinson Chung            });
312e2d721781fc024cbd9a14929741e5b476242291fWinson Chung        }
313e2d721781fc024cbd9a14929741e5b476242291fWinson Chung    }
314ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung
31565c5f99aae6969e6353e3be8991619c076450e43Winson Chung    @Override
31665c5f99aae6969e6353e3be8991619c076450e43Winson Chung    public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode,
31765c5f99aae6969e6353e3be8991619c076450e43Winson Chung            boolean runSychronously) {
31865c5f99aae6969e6353e3be8991619c076450e43Winson Chung        if (runSychronously) {
31965c5f99aae6969e6353e3be8991619c076450e43Winson Chung            finishAnimation(reorderMode);
32065c5f99aae6969e6353e3be8991619c076450e43Winson Chung        } else {
32165c5f99aae6969e6353e3be8991619c076450e43Winson Chung            mService.mHandler.post(() -> finishAnimation(reorderMode));
32265c5f99aae6969e6353e3be8991619c076450e43Winson Chung        }
32365c5f99aae6969e6353e3be8991619c076450e43Winson Chung    }
32465c5f99aae6969e6353e3be8991619c076450e43Winson Chung
3250f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung    @Override
3260f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung    public void onStackOrderChanged() {
3270f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        // If the activity display stack order changes, cancel any running recents animation in
3280f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        // place
3290f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung        mWindowManager.cancelRecentsAnimationSynchronously(REORDER_KEEP_IN_PLACE,
3300f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung                "stackOrderChanged");
3310f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung    }
3320f7ec96b756a90def4c5dd786d18b798d343ad3dWinson Chung
333ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    /**
334ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     * Called only when the animation should be canceled prior to starting.
335ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung     */
336ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    private void notifyAnimationCancelBeforeStart(IRecentsAnimationRunner recentsAnimationRunner) {
337ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        try {
338ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            recentsAnimationRunner.onAnimationCanceled();
339ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        } catch (RemoteException e) {
340ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung            Slog.e(TAG, "Failed to cancel recents animation before start", e);
341ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung        }
342ddf62975798eff2a68422d075441a4bfcf1cd6b4Winson Chung    }
343c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung
344c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    /**
345c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     * @return The top stack that is not always-on-top.
346c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung     */
347c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    private ActivityStack getTopNonAlwaysOnTopStack() {
348c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        for (int i = mDefaultDisplay.getChildCount() - 1; i >= 0; i--) {
349c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            final ActivityStack s = mDefaultDisplay.getChildAt(i);
350c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            if (s.getWindowConfiguration().isAlwaysOnTop()) {
351c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung                continue;
352c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            }
353c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung            return s;
354c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        }
355c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung        return null;
356c6c3f851dc0c2e7f17581b55d08f17b508c791afWinson Chung    }
35700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
35800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    /**
35900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     * @return the top activity in the {@param targetStack} matching the {@param component}, or just
36000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     * the top activity of the top task if no task matches the component.
36100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung     */
36200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    private ActivityRecord getTargetActivity(ActivityStack targetStack, ComponentName component) {
36300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        if (targetStack == null) {
36400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            return null;
36500a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        }
36600a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung
36700a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        for (int i = targetStack.getChildCount() - 1; i >= 0; i--) {
36800a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            final TaskRecord task = (TaskRecord) targetStack.getChildAt(i);
36900a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            if (task.getBaseIntent().getComponent().equals(component)) {
37000a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung                return task.getTopActivity();
37100a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung            }
37200a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        }
37300a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung        return targetStack.getTopActivity();
37400a0969b50b88000d9cc2ddb7af0582c32b20c32Winson Chung    }
375e2d721781fc024cbd9a14929741e5b476242291fWinson Chung}
376