blob: b0492be5f5422cef2be51b5ab6412cfdf6741a74 [file] [log] [blame]
Winson Chunge2d72172018-01-25 17:46:20 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Winson Chunge2d72172018-01-25 17:46:20 +000018
Jorim Jaggi54cff642018-03-15 15:51:32 +010019import static android.app.ActivityManager.START_TASK_TO_FRONT;
Winson Chunge2d72172018-01-25 17:46:20 +000020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Winson Chung3e2980e2018-03-29 17:28:57 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
22import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Winson Chunge2d72172018-01-25 17:46:20 +000023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION;
Riddle Hsu2ca561b2019-10-08 21:58:58 +080025import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Winson Chunge2d72172018-01-25 17:46:20 +000026import static android.view.WindowManager.TRANSIT_NONE;
Winson Chungfbbb1582018-11-13 16:09:01 -080027
Wale Ogunwale59507092018-10-29 09:00:30 -070028import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
Tracy Zhou9c675d42019-04-08 00:32:40 -070029import static com.android.server.wm.BoundsAnimationController.BOUNDS;
30import static com.android.server.wm.BoundsAnimationController.FADE_IN;
Adrian Roosb125e0b2019-10-02 14:55:14 +020031import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_RECENTS_ANIMATIONS;
Winson Chung3e2980e2018-03-29 17:28:57 -070032import static com.android.server.wm.RecentsAnimationController.REORDER_KEEP_IN_PLACE;
33import static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_TO_ORIGINAL_POSITION;
34import static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_TO_TOP;
Winson Chunge2d72172018-01-25 17:46:20 +000035
Jorim Jaggi589c5ba2019-07-30 16:50:13 +020036import android.annotation.Nullable;
Winson Chunge2d72172018-01-25 17:46:20 +000037import android.app.ActivityOptions;
38import android.content.ComponentName;
39import android.content.Intent;
Winson Chungddf62972018-02-12 11:10:04 -080040import android.os.RemoteException;
Winson Chung584d6522018-02-07 23:57:38 +000041import android.os.Trace;
Winson Chungddf62972018-02-12 11:10:04 -080042import android.util.Slog;
Winson Chunge2d72172018-01-25 17:46:20 +000043import android.view.IRecentsAnimationRunner;
lumark588a3e82018-07-20 18:53:54 +080044
Wale Ogunwalea38654f2019-11-17 20:37:15 -080045import com.android.internal.util.function.pooled.PooledLambda;
46import com.android.internal.util.function.pooled.PooledPredicate;
Adrian Roosb125e0b2019-10-02 14:55:14 +020047import com.android.server.protolog.common.ProtoLog;
Riddle Hsufd66d4d2019-11-14 10:35:55 +080048import com.android.server.wm.ActivityMetricsLogger.LaunchingState;
Winson Chunge2d72172018-01-25 17:46:20 +000049import com.android.server.wm.RecentsAnimationController.RecentsAnimationCallbacks;
Winson Chunge2d72172018-01-25 17:46:20 +000050
51/**
52 * Manages the recents animation, including the reordering of the stacks for the transition and
53 * cleanup. See {@link com.android.server.wm.RecentsAnimationController}.
54 */
Winson Chung0f7ec962018-05-03 18:03:15 -070055class RecentsAnimation implements RecentsAnimationCallbacks,
Louis Chang677921f2019-12-06 16:44:24 +080056 DisplayContent.OnStackOrderChangedListener {
Winson Chunge2d72172018-01-25 17:46:20 +000057 private static final String TAG = RecentsAnimation.class.getSimpleName();
58
Wale Ogunwalea6191b42018-05-09 07:41:32 -070059 private final ActivityTaskManagerService mService;
Winson Chunge2d72172018-01-25 17:46:20 +000060 private final ActivityStackSupervisor mStackSupervisor;
61 private final ActivityStartController mActivityStartController;
62 private final WindowManagerService mWindowManager;
Louis Chang677921f2019-12-06 16:44:24 +080063 private final DisplayContent mDefaultDisplay;
Riddle Hsu609a8e22019-06-27 16:46:29 -060064 private final Intent mTargetIntent;
65 private final ComponentName mRecentsComponent;
Philip P. Moltmann9c5226f2020-01-10 08:53:43 -080066 private final @Nullable String mRecentsFeatureId;
Riddle Hsu609a8e22019-06-27 16:46:29 -060067 private final int mRecentsUid;
Jorim Jaggi589c5ba2019-07-30 16:50:13 +020068 private final @Nullable WindowProcessController mCaller;
Riddle Hsu609a8e22019-06-27 16:46:29 -060069 private final int mUserId;
70 private final int mTargetActivityType;
Winson Chunge2d72172018-01-25 17:46:20 +000071
Riddle Hsu43233b72019-04-24 23:55:11 +080072 /**
73 * The activity which has been launched behind. We need to remember the activity because the
74 * target stack may have other activities, then we are able to restore the launch-behind state
75 * for the exact activity.
76 */
77 private ActivityRecord mLaunchedTargetActivity;
Winson Chung3e2980e2018-03-29 17:28:57 -070078
79 // The stack to restore the target stack behind when the animation is finished
80 private ActivityStack mRestoreTargetBehindStack;
Winson Chunge2d72172018-01-25 17:46:20 +000081
Wale Ogunwalea6191b42018-05-09 07:41:32 -070082 RecentsAnimation(ActivityTaskManagerService atm, ActivityStackSupervisor stackSupervisor,
Winson Chunge2d72172018-01-25 17:46:20 +000083 ActivityStartController activityStartController, WindowManagerService wm,
Philip P. Moltmann9c5226f2020-01-10 08:53:43 -080084 Intent targetIntent, ComponentName recentsComponent, @Nullable String recentsFeatureId,
85 int recentsUid, @Nullable WindowProcessController caller) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -070086 mService = atm;
Winson Chunge2d72172018-01-25 17:46:20 +000087 mStackSupervisor = stackSupervisor;
Louis Chang149d5c82019-12-30 09:47:39 +080088 mDefaultDisplay = mService.mRootWindowContainer.getDefaultDisplay();
Winson Chunge2d72172018-01-25 17:46:20 +000089 mActivityStartController = activityStartController;
Winson Chunge2d72172018-01-25 17:46:20 +000090 mWindowManager = wm;
Riddle Hsu609a8e22019-06-27 16:46:29 -060091 mTargetIntent = targetIntent;
92 mRecentsComponent = recentsComponent;
Philip P. Moltmann9c5226f2020-01-10 08:53:43 -080093 mRecentsFeatureId = recentsFeatureId;
Riddle Hsu609a8e22019-06-27 16:46:29 -060094 mRecentsUid = recentsUid;
Jorim Jaggi589c5ba2019-07-30 16:50:13 +020095 mCaller = caller;
Riddle Hsu609a8e22019-06-27 16:46:29 -060096 mUserId = atm.getCurrentUserId();
97 mTargetActivityType = targetIntent.getComponent() != null
98 && recentsComponent.equals(targetIntent.getComponent())
99 ? ACTIVITY_TYPE_RECENTS
100 : ACTIVITY_TYPE_HOME;
Winson Chunge2d72172018-01-25 17:46:20 +0000101 }
102
Riddle Hsu609a8e22019-06-27 16:46:29 -0600103 /**
104 * Starts the recents activity in background without animation if the record doesn't exist or
105 * the client isn't launched. If the recents activity is already alive, ensure its configuration
106 * is updated to the current one.
107 */
108 void preloadRecentsActivity() {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200109 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "Preload recents with %s",
110 mTargetIntent);
Riddle Hsu609a8e22019-06-27 16:46:29 -0600111 ActivityStack targetStack = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
112 mTargetActivityType);
113 ActivityRecord targetActivity = getTargetActivity(targetStack);
114 if (targetActivity != null) {
Issei Suzuki1669ea42019-11-06 14:20:59 +0100115 if (targetActivity.mVisibleRequested || targetActivity.isTopRunningActivity()) {
Riddle Hsu609a8e22019-06-27 16:46:29 -0600116 // The activity is ready.
117 return;
118 }
119 if (targetActivity.attachedToProcess()) {
120 // The activity may be relaunched if it cannot handle the current configuration
121 // changes. The activity will be paused state if it is relaunched, otherwise it
122 // keeps the original stopped state.
123 targetActivity.ensureActivityConfiguration(0 /* globalChanges */,
124 false /* preserveWindow */, true /* ignoreVisibility */);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200125 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "Updated config=%s",
126 targetActivity.getConfiguration());
Riddle Hsu609a8e22019-06-27 16:46:29 -0600127 }
128 } else {
129 // Create the activity record. Because the activity is invisible, this doesn't really
130 // start the client.
131 startRecentsActivityInBackground("preloadRecents");
132 targetStack = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED, mTargetActivityType);
133 targetActivity = getTargetActivity(targetStack);
134 if (targetActivity == null) {
135 Slog.w(TAG, "Cannot start " + mTargetIntent);
136 return;
137 }
138 }
139
140 if (!targetActivity.attachedToProcess()) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200141 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "Real start recents");
Wale Ogunwale076c3b12019-11-20 12:17:22 -0800142 mStackSupervisor.startSpecificActivity(targetActivity, false /* andResume */,
Riddle Hsu609a8e22019-06-27 16:46:29 -0600143 false /* checkConfig */);
144 // Make sure the activity won't be involved in transition.
Wale Ogunwaleda8b8272018-11-29 19:37:37 -0800145 if (targetActivity.getDisplayContent() != null) {
146 targetActivity.getDisplayContent().mUnknownAppVisibilityController
147 .appRemovedOrHidden(targetActivity);
Riddle Hsu609a8e22019-06-27 16:46:29 -0600148 }
149 }
150
151 // Invisible activity should be stopped. If the recents activity is alive and its doesn't
152 // need to relaunch by current configuration, then it may be already in stopped state.
153 if (!targetActivity.isState(ActivityStack.ActivityState.STOPPING,
154 ActivityStack.ActivityState.STOPPED)) {
155 // Add to stopping instead of stop immediately. So the client has the chance to perform
156 // traversal in non-stopped state (ViewRootImpl.mStopped) that would initialize more
157 // things (e.g. the measure can be done earlier). The actual stop will be performed when
158 // it reports idle.
Andrii Kulian79d67982019-08-19 11:56:16 -0700159 targetActivity.addToStopping(true /* scheduleIdle */, true /* idleDelayed */,
160 "preloadRecents");
Riddle Hsu609a8e22019-06-27 16:46:29 -0600161 }
162 }
163
164 void startRecentsActivity(IRecentsAnimationRunner recentsAnimationRunner) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200165 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "startRecentsActivity(): intent=%s", mTargetIntent);
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800166 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "RecentsAnimation#startRecentsActivity");
Winson Chungddf62972018-02-12 11:10:04 -0800167
lumark588a3e82018-07-20 18:53:54 +0800168 // TODO(multi-display) currently only support recents animation in default display.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800169 final DisplayContent dc =
Louis Chang149d5c82019-12-30 09:47:39 +0800170 mService.mRootWindowContainer.getDefaultDisplay().mDisplayContent;
Winson Chungddf62972018-02-12 11:10:04 -0800171 if (!mWindowManager.canStartRecentsAnimation()) {
172 notifyAnimationCancelBeforeStart(recentsAnimationRunner);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200173 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS,
174 "Can't start recents animation, nextAppTransition=%s",
175 dc.mAppTransition.getAppTransition());
Winson Chungddf62972018-02-12 11:10:04 -0800176 return;
177 }
178
Winson Chung3e2980e2018-03-29 17:28:57 -0700179 // If the activity is associated with the recents stack, then try and get that first
Winson Chung93442032018-12-04 13:24:29 -0800180 ActivityStack targetStack = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
Winson Chung3e2980e2018-03-29 17:28:57 -0700181 mTargetActivityType);
Riddle Hsu609a8e22019-06-27 16:46:29 -0600182 ActivityRecord targetActivity = getTargetActivity(targetStack);
Winson Chung3e2980e2018-03-29 17:28:57 -0700183 final boolean hasExistingActivity = targetActivity != null;
184 if (hasExistingActivity) {
Louis Chang677921f2019-12-06 16:44:24 +0800185 final DisplayContent display = targetActivity.getDisplay();
Winson Chung3e2980e2018-03-29 17:28:57 -0700186 mRestoreTargetBehindStack = display.getStackAbove(targetStack);
187 if (mRestoreTargetBehindStack == null) {
Winson Chungddf62972018-02-12 11:10:04 -0800188 notifyAnimationCancelBeforeStart(recentsAnimationRunner);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200189 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS,
190 "No stack above target stack=%s", targetStack);
Winson Chungddf62972018-02-12 11:10:04 -0800191 return;
192 }
193 }
194
Winson Chung3e2980e2018-03-29 17:28:57 -0700195 // Send launch hint if we are actually launching the target. If it's already visible
196 // (shouldn't happen in general) we don't need to send it.
Issei Suzuki1669ea42019-11-06 14:20:59 +0100197 if (targetActivity == null || !targetActivity.mVisibleRequested) {
Louis Chang149d5c82019-12-30 09:47:39 +0800198 mService.mRootWindowContainer.sendPowerHintForLaunchStartIfNeeded(
Wale Ogunwaled32da472018-11-16 07:19:28 -0800199 true /* forceSend */, targetActivity);
Jorim Jaggiac960522018-03-22 23:20:36 +0100200 }
201
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800202 final LaunchingState launchingState =
203 mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunching(mTargetIntent);
Jorim Jaggi54cff642018-03-15 15:51:32 +0100204
Jorim Jaggi589c5ba2019-07-30 16:50:13 +0200205 if (mCaller != null) {
206 mCaller.setRunningRecentsAnimation(true);
207 }
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100208
Riddle Hsua0022cd2019-09-09 21:12:41 +0800209 mService.deferWindowLayout();
Winson Chung1e6d4a92018-01-26 10:04:20 -0800210 try {
Winson Chung3e2980e2018-03-29 17:28:57 -0700211 if (hasExistingActivity) {
212 // Move the recents activity into place for the animation if it is not top most
Winson Chung0f7ec962018-05-03 18:03:15 -0700213 mDefaultDisplay.moveStackBehindBottomMostVisibleStack(targetStack);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200214 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "Moved stack=%s behind stack=%s",
215 targetStack, mDefaultDisplay.getStackAbove(targetStack));
Winson Chung00a09692018-04-19 16:14:09 -0700216
217 // If there are multiple tasks in the target stack (ie. the home stack, with 3p
218 // and default launchers coexisting), then move the task to the top as a part of
219 // moving the stack to the front
Louis Changcdec0802019-11-11 11:45:07 +0800220 final Task task = targetActivity.getTask();
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900221 if (targetStack.getTopMostTask() != task) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700222 targetStack.positionChildAtTop(task);
Winson Chung00a09692018-04-19 16:14:09 -0700223 }
Winson Chungddf62972018-02-12 11:10:04 -0800224 } else {
Winson Chung93442032018-12-04 13:24:29 -0800225 // No recents activity, create the new recents activity bottom most
Riddle Hsu609a8e22019-06-27 16:46:29 -0600226 startRecentsActivityInBackground("startRecentsActivity_noTargetActivity");
Winson Chung93442032018-12-04 13:24:29 -0800227
228 // Move the recents activity into place for the animation
Winson Chung2bc60c02019-05-16 17:36:05 -0700229 targetStack = mDefaultDisplay.getStack(WINDOWING_MODE_UNDEFINED,
230 mTargetActivityType);
Riddle Hsu609a8e22019-06-27 16:46:29 -0600231 targetActivity = getTargetActivity(targetStack);
Winson Chung93442032018-12-04 13:24:29 -0800232 mDefaultDisplay.moveStackBehindBottomMostVisibleStack(targetStack);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200233 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "Moved stack=%s behind stack=%s",
234 targetStack, mDefaultDisplay.getStackAbove(targetStack));
Winson Chung93442032018-12-04 13:24:29 -0800235
Winson Chung1e6d4a92018-01-26 10:04:20 -0800236 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Winson Chungba40d3a2018-05-16 09:40:16 -0700237 mWindowManager.executeAppTransition();
Winson Chunge2d72172018-01-25 17:46:20 +0000238
Winson Chung1e6d4a92018-01-26 10:04:20 -0800239 // TODO: Maybe wait for app to draw in this particular case?
Winson Chungc6c3f852018-04-09 15:41:03 -0700240
Adrian Roosb125e0b2019-10-02 14:55:14 +0200241 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "Started intent=%s", mTargetIntent);
Winson Chung1e6d4a92018-01-26 10:04:20 -0800242 }
Winson Chunge2d72172018-01-25 17:46:20 +0000243
Winson Chung3e2980e2018-03-29 17:28:57 -0700244 // Mark the target activity as launch-behind to bump its visibility for the
Winson Chung1e6d4a92018-01-26 10:04:20 -0800245 // duration of the gesture that is driven by the recents component
Winson Chung3e2980e2018-03-29 17:28:57 -0700246 targetActivity.mLaunchTaskBehind = true;
Riddle Hsu43233b72019-04-24 23:55:11 +0800247 mLaunchedTargetActivity = targetActivity;
Winson Chung1e6d4a92018-01-26 10:04:20 -0800248
249 // Fetch all the surface controls and pass them to the client to get the animation
Winson Chung65c5f992018-04-20 14:58:57 -0700250 // started. Cancel any existing recents animation running synchronously (do not hold the
251 // WM lock)
Wale Ogunwalea441b922019-06-27 19:21:44 -0700252 mWindowManager.cancelRecentsAnimation(REORDER_MOVE_TO_ORIGINAL_POSITION,
Winson Chungc6c3f852018-04-09 15:41:03 -0700253 "startRecentsActivity");
Winson Chung3e2980e2018-03-29 17:28:57 -0700254 mWindowManager.initializeRecentsAnimation(mTargetActivityType, recentsAnimationRunner,
Louis Chang677921f2019-12-06 16:44:24 +0800255 this, mDefaultDisplay.getDisplayId(),
wilsonshih417b70c12019-10-16 16:12:02 +0800256 mStackSupervisor.mRecentTasks.getRecentTaskIds(), targetActivity);
Winson Chung1e6d4a92018-01-26 10:04:20 -0800257
258 // If we updated the launch-behind state, update the visibility of the activities after
259 // we fetch the visible tasks to be controlled by the animation
Louis Chang149d5c82019-12-30 09:47:39 +0800260 mService.mRootWindowContainer.ensureActivitiesVisible(null, 0, PRESERVE_WINDOWS);
Jorim Jaggi54cff642018-03-15 15:51:32 +0100261
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800262 mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunched(launchingState,
263 START_TASK_TO_FRONT, targetActivity);
Winson Chung0f7ec962018-05-03 18:03:15 -0700264
265 // Register for stack order changes
266 mDefaultDisplay.registerStackOrderChangedListener(this);
Winson Chungc6c3f852018-04-09 15:41:03 -0700267 } catch (Exception e) {
268 Slog.e(TAG, "Failed to start recents activity", e);
269 throw e;
Winson Chung1e6d4a92018-01-26 10:04:20 -0800270 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800271 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800272 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Winson Chunge2d72172018-01-25 17:46:20 +0000273 }
Winson Chunge2d72172018-01-25 17:46:20 +0000274 }
275
Tracy Zhou9c675d42019-04-08 00:32:40 -0700276 private void finishAnimation(@RecentsAnimationController.ReorderMode int reorderMode,
277 boolean sendUserLeaveHint) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700278 synchronized (mService.mGlobalLock) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200279 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS,
280 "onAnimationFinished(): controller=%s reorderMode=%d",
281 mWindowManager.getRecentsAnimationController(), reorderMode);
Winson Chungf9930162018-02-12 14:51:46 -0800282
Winson Chung0f7ec962018-05-03 18:03:15 -0700283 // Unregister for stack order changes
284 mDefaultDisplay.unregisterStackOrderChangedListener(this);
285
Riddle Hsu0e591722019-05-03 22:22:36 +0800286 final RecentsAnimationController controller =
287 mWindowManager.getRecentsAnimationController();
288 if (controller == null) return;
Winson Chunge2d72172018-01-25 17:46:20 +0000289
Winson Chung3e2980e2018-03-29 17:28:57 -0700290 // Just to be sure end the launch hint in case the target activity was never launched.
291 // However, if we're keeping the activity and making it visible, we can leave it on.
292 if (reorderMode != REORDER_KEEP_IN_PLACE) {
Louis Chang149d5c82019-12-30 09:47:39 +0800293 mService.mRootWindowContainer.sendPowerHintForLaunchEndIfNeeded();
Jorim Jaggiac960522018-03-22 23:20:36 +0100294 }
295
Winson Chung751579d2019-05-28 14:09:09 -0700296 // Once the target is shown, prevent spurious background app switches
297 if (reorderMode == REORDER_MOVE_TO_TOP) {
298 mService.stopAppSwitches();
299 }
300
Jorim Jaggi589c5ba2019-07-30 16:50:13 +0200301 if (mCaller != null) {
302 mCaller.setRunningRecentsAnimation(false);
303 }
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100304
Winson Chunge2d72172018-01-25 17:46:20 +0000305 mWindowManager.inSurfaceTransaction(() -> {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800306 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
Winson Chung584d6522018-02-07 23:57:38 +0000307 "RecentsAnimation#onAnimationFinished_inSurfaceTransaction");
Riddle Hsua0022cd2019-09-09 21:12:41 +0800308 mService.deferWindowLayout();
Winson Chung1e6d4a92018-01-26 10:04:20 -0800309 try {
Winson Chung6a38fca2018-03-28 17:57:09 -0700310 mWindowManager.cleanupRecentsAnimation(reorderMode);
Winson Chunge2d72172018-01-25 17:46:20 +0000311
Winson Chung3e2980e2018-03-29 17:28:57 -0700312 final ActivityStack targetStack = mDefaultDisplay.getStack(
313 WINDOWING_MODE_UNDEFINED, mTargetActivityType);
Riddle Hsu43233b72019-04-24 23:55:11 +0800314 // Prefer to use the original target activity instead of top activity because
315 // we may have moved another task to top (starting 3p launcher).
Winson Chung82389a92018-05-03 10:43:51 -0700316 final ActivityRecord targetActivity = targetStack != null
Riddle Hsu43233b72019-04-24 23:55:11 +0800317 ? targetStack.isInStackLocked(mLaunchedTargetActivity)
Winson Chung82389a92018-05-03 10:43:51 -0700318 : null;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200319 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS,
320 "onAnimationFinished(): targetStack=%s targetActivity=%s "
321 + "mRestoreTargetBehindStack=%s",
322 targetStack, targetActivity, mRestoreTargetBehindStack);
Winson Chung3e2980e2018-03-29 17:28:57 -0700323 if (targetActivity == null) {
Winson Chung1e6d4a92018-01-26 10:04:20 -0800324 return;
325 }
326
327 // Restore the launched-behind state
Winson Chung3e2980e2018-03-29 17:28:57 -0700328 targetActivity.mLaunchTaskBehind = false;
Winson Chung1e6d4a92018-01-26 10:04:20 -0800329
Winson Chung3e2980e2018-03-29 17:28:57 -0700330 if (reorderMode == REORDER_MOVE_TO_TOP) {
331 // Bring the target stack to the front
332 mStackSupervisor.mNoAnimActivities.add(targetActivity);
Tracy Zhou9c675d42019-04-08 00:32:40 -0700333
334 if (sendUserLeaveHint) {
335 // Setting this allows the previous app to PiP.
336 mStackSupervisor.mUserLeaving = true;
Louis Changcdec0802019-11-11 11:45:07 +0800337 targetStack.moveTaskToFrontLocked(targetActivity.getTask(),
Tracy Zhou9c675d42019-04-08 00:32:40 -0700338 true /* noAnimation */, null /* activityOptions */,
339 targetActivity.appTimeTracker,
340 "RecentsAnimation.onAnimationFinished()");
341 } else {
342 targetStack.moveToFront("RecentsAnimation.onAnimationFinished()");
343 }
344
Adrian Roosb125e0b2019-10-02 14:55:14 +0200345 if (WM_DEBUG_RECENTS_ANIMATIONS.isLogToAny()) {
Winson Chungc6c3f852018-04-09 15:41:03 -0700346 final ActivityStack topStack = getTopNonAlwaysOnTopStack();
347 if (topStack != targetStack) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200348 ProtoLog.w(WM_DEBUG_RECENTS_ANIMATIONS,
349 "Expected target stack=%s"
350 + " to be top most but found stack=%s",
351 targetStack, topStack);
Winson Chungc6c3f852018-04-09 15:41:03 -0700352 }
353 }
Winson Chung3e2980e2018-03-29 17:28:57 -0700354 } else if (reorderMode == REORDER_MOVE_TO_ORIGINAL_POSITION){
355 // Restore the target stack to its previous position
Louis Chang677921f2019-12-06 16:44:24 +0800356 final DisplayContent display = targetActivity.getDisplay();
Winson Chung3e2980e2018-03-29 17:28:57 -0700357 display.moveStackBehindStack(targetStack, mRestoreTargetBehindStack);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200358 if (WM_DEBUG_RECENTS_ANIMATIONS.isLogToAny()) {
Winson Chungc6c3f852018-04-09 15:41:03 -0700359 final ActivityStack aboveTargetStack =
360 mDefaultDisplay.getStackAbove(targetStack);
361 if (mRestoreTargetBehindStack != null
362 && aboveTargetStack != mRestoreTargetBehindStack) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200363 ProtoLog.w(WM_DEBUG_RECENTS_ANIMATIONS,
364 "Expected target stack=%s to restored behind stack=%s but"
365 + " it is behind stack=%s",
366 targetStack, mRestoreTargetBehindStack, aboveTargetStack);
Winson Chungc6c3f852018-04-09 15:41:03 -0700367 }
368 }
Winson Chung6a38fca2018-03-28 17:57:09 -0700369 } else {
Riddle Hsu0e591722019-05-03 22:22:36 +0800370 // If there is no recents screenshot animation, we can update the visibility
371 // of target stack immediately because it is visually invisible and the
372 // launch-behind state is restored. That also prevents the next transition
373 // type being disturbed if the visibility is updated after setting the next
374 // transition (the target activity will be one of closing apps).
Winson Chung7a545ae2019-07-16 14:52:13 -0700375 if (!controller.shouldDeferCancelWithScreenshot()
Riddle Hsu0e591722019-05-03 22:22:36 +0800376 && !targetStack.isFocusedStackOnDisplay()) {
Wale Ogunwale076c3b12019-11-20 12:17:22 -0800377 targetStack.ensureActivitiesVisible(null /* starting */,
Riddle Hsu0e591722019-05-03 22:22:36 +0800378 0 /* starting */, false /* preserveWindows */);
379 }
Winson Chung3e2980e2018-03-29 17:28:57 -0700380 // Keep target stack in place, nothing changes, so ignore the transition
381 // logic below
Winson Chung6a38fca2018-03-28 17:57:09 -0700382 return;
Winson Chung1e6d4a92018-01-26 10:04:20 -0800383 }
384
385 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Louis Chang149d5c82019-12-30 09:47:39 +0800386 mService.mRootWindowContainer.ensureActivitiesVisible(null, 0, false);
387 mService.mRootWindowContainer.resumeFocusedStacksTopActivities();
Winson Chung1e6d4a92018-01-26 10:04:20 -0800388
389 // No reason to wait for the pausing activity in this case, as the hiding of
390 // surfaces needs to be done immediately.
391 mWindowManager.executeAppTransition();
Winson Chungf557c3b2018-03-16 10:55:20 -0700392
Evan Rosky0037e5f2019-11-05 10:26:24 -0800393 if (targetStack.getTile() != null) {
394 // Client state may have changed during the recents animation, so force
395 // send task info so the client can synchronize its state.
396 mService.mTaskOrganizerController.dispatchTaskInfoChanged(
397 targetStack.mTile, true /* force */);
398 }
Winson Chungc6c3f852018-04-09 15:41:03 -0700399 } catch (Exception e) {
400 Slog.e(TAG, "Failed to clean up recents activity", e);
401 throw e;
Winson Chung1e6d4a92018-01-26 10:04:20 -0800402 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800403 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800404 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Winson Chunge2d72172018-01-25 17:46:20 +0000405 }
Winson Chunge2d72172018-01-25 17:46:20 +0000406 });
407 }
408 }
Winson Chungddf62972018-02-12 11:10:04 -0800409
Winson Chung65c5f992018-04-20 14:58:57 -0700410 @Override
411 public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode,
Wale Ogunwalea441b922019-06-27 19:21:44 -0700412 boolean sendUserLeaveHint) {
413 finishAnimation(reorderMode, sendUserLeaveHint);
Winson Chung65c5f992018-04-20 14:58:57 -0700414 }
415
Winson Chung0f7ec962018-05-03 18:03:15 -0700416 @Override
Winson Chung65d66d32018-12-13 17:48:39 -0800417 public void onStackOrderChanged(ActivityStack stack) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200418 ProtoLog.d(WM_DEBUG_RECENTS_ANIMATIONS, "onStackOrderChanged(): stack=%s", stack);
Winson Chung65d66d32018-12-13 17:48:39 -0800419 if (mDefaultDisplay.getIndexOf(stack) == -1 || !stack.shouldBeVisible(null)) {
420 // The stack is not visible, so ignore this change
421 return;
422 }
lumark54284462019-03-05 20:44:27 +0800423 final RecentsAnimationController controller =
424 mWindowManager.getRecentsAnimationController();
lumarkf6f34942019-04-29 16:56:50 +0800425 if (controller == null) {
426 return;
427 }
428
Tracy Zhou9c675d42019-04-08 00:32:40 -0700429 final DisplayContent dc =
Louis Chang149d5c82019-12-30 09:47:39 +0800430 mService.mRootWindowContainer.getDefaultDisplay().mDisplayContent;
Tracy Zhou9c675d42019-04-08 00:32:40 -0700431 dc.mBoundsAnimationController.setAnimationType(
Winson Chung7a545ae2019-07-16 14:52:13 -0700432 controller.shouldDeferCancelUntilNextTransition() ? FADE_IN : BOUNDS);
Winson Chung65d66d32018-12-13 17:48:39 -0800433
Winson Chung7a545ae2019-07-16 14:52:13 -0700434 // We defer canceling the recents animation until the next app transition in the following
435 // cases:
436 // 1) The next launching task is not being animated by the recents animation
lumark54284462019-03-05 20:44:27 +0800437 // 2) The next task is home activity. (i.e. pressing home key to back home in recents).
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900438 if ((!controller.isAnimatingTask(stack.getTopMostTask())
Wale Ogunwale21e06482019-11-18 05:14:15 -0800439 || controller.isTargetApp(stack.getTopNonFinishingActivity()))
Winson Chung7a545ae2019-07-16 14:52:13 -0700440 && controller.shouldDeferCancelUntilNextTransition()) {
441 // Always prepare an app transition since we rely on the transition callbacks to cleanup
442 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
443 controller.setCancelOnNextTransitionStart();
lumark54284462019-03-05 20:44:27 +0800444 } else {
445 // Just cancel directly to unleash from launcher when the next launching task is the
446 // current top task.
Wale Ogunwalea441b922019-06-27 19:21:44 -0700447 mWindowManager.cancelRecentsAnimation(REORDER_KEEP_IN_PLACE, "stackOrderChanged");
lumark54284462019-03-05 20:44:27 +0800448 }
Winson Chung0f7ec962018-05-03 18:03:15 -0700449 }
450
Riddle Hsu609a8e22019-06-27 16:46:29 -0600451 private void startRecentsActivityInBackground(String reason) {
452 final ActivityOptions options = ActivityOptions.makeBasic();
453 options.setLaunchActivityType(mTargetActivityType);
454 options.setAvoidMoveToFront();
455 mTargetIntent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION);
456
457 mActivityStartController
458 .obtainStarter(mTargetIntent, reason)
459 .setCallingUid(mRecentsUid)
460 .setCallingPackage(mRecentsComponent.getPackageName())
Philip P. Moltmann9c5226f2020-01-10 08:53:43 -0800461 .setCallingFeatureId(mRecentsFeatureId)
Riddle Hsu609a8e22019-06-27 16:46:29 -0600462 .setActivityOptions(new SafeActivityOptions(options))
Louis Chang54fbb052019-10-16 17:10:17 +0800463 .setUserId(mUserId)
Riddle Hsu609a8e22019-06-27 16:46:29 -0600464 .execute();
465 }
466
Winson Chungddf62972018-02-12 11:10:04 -0800467 /**
468 * Called only when the animation should be canceled prior to starting.
469 */
Jorim Jaggi589c5ba2019-07-30 16:50:13 +0200470 static void notifyAnimationCancelBeforeStart(IRecentsAnimationRunner recentsAnimationRunner) {
Winson Chungddf62972018-02-12 11:10:04 -0800471 try {
Tracy Zhou8089ffa2019-07-30 17:30:43 -0700472 recentsAnimationRunner.onAnimationCanceled(null /* taskSnapshot */);
Winson Chungddf62972018-02-12 11:10:04 -0800473 } catch (RemoteException e) {
474 Slog.e(TAG, "Failed to cancel recents animation before start", e);
475 }
476 }
Winson Chungc6c3f852018-04-09 15:41:03 -0700477
478 /**
479 * @return The top stack that is not always-on-top.
480 */
481 private ActivityStack getTopNonAlwaysOnTopStack() {
Louis Chang2453d062019-11-19 22:30:48 +0800482 for (int i = mDefaultDisplay.getStackCount() - 1; i >= 0; i--) {
483 final ActivityStack s = mDefaultDisplay.getStackAt(i);
Winson Chungc6c3f852018-04-09 15:41:03 -0700484 if (s.getWindowConfiguration().isAlwaysOnTop()) {
485 continue;
486 }
487 return s;
488 }
489 return null;
490 }
Winson Chung00a09692018-04-19 16:14:09 -0700491
492 /**
493 * @return the top activity in the {@param targetStack} matching the {@param component}, or just
494 * the top activity of the top task if no task matches the component.
495 */
Riddle Hsu609a8e22019-06-27 16:46:29 -0600496 private ActivityRecord getTargetActivity(ActivityStack targetStack) {
Winson Chung00a09692018-04-19 16:14:09 -0700497 if (targetStack == null) {
498 return null;
499 }
500
Wale Ogunwalea38654f2019-11-17 20:37:15 -0800501 final PooledPredicate p = PooledLambda.obtainPredicate(RecentsAnimation::matchesTarget,
502 this, PooledLambda.__(Task.class));
503 final Task task = targetStack.getTask(p);
504 p.recycle();
505 return task != null ? task.getTopNonFinishingActivity() : null;
506 }
507
508 private boolean matchesTarget(Task task) {
509 return task.mUserId == mUserId
510 && task.getBaseIntent().getComponent().equals(mTargetIntent.getComponent());
Winson Chung00a09692018-04-19 16:14:09 -0700511 }
Winson Chunge2d72172018-01-25 17:46:20 +0000512}