Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Jorim Jaggi | 54cff64 | 2018-03-15 15:51:32 +0100 | [diff] [blame] | 19 | import static android.app.ActivityManager.START_TASK_TO_FRONT; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 20 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 21 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
| 22 | import static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION; |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 23 | import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 24 | import static android.view.WindowManager.TRANSIT_NONE; |
| 25 | import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS; |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 26 | import static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_HOME_TO_ORIGINAL_POSITION; |
| 27 | import static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_HOME_TO_TOP; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 28 | |
| 29 | import android.app.ActivityOptions; |
| 30 | import android.content.ComponentName; |
| 31 | import android.content.Intent; |
| 32 | import android.os.Handler; |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 34 | import android.os.Trace; |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 35 | import android.util.Slog; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 36 | import android.view.IRecentsAnimationRunner; |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 37 | import com.android.server.wm.RecentsAnimationController; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 38 | import com.android.server.wm.RecentsAnimationController.RecentsAnimationCallbacks; |
| 39 | import com.android.server.wm.WindowManagerService; |
| 40 | |
| 41 | /** |
| 42 | * Manages the recents animation, including the reordering of the stacks for the transition and |
| 43 | * cleanup. See {@link com.android.server.wm.RecentsAnimationController}. |
| 44 | */ |
| 45 | class RecentsAnimation implements RecentsAnimationCallbacks { |
| 46 | private static final String TAG = RecentsAnimation.class.getSimpleName(); |
| 47 | |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 48 | private final ActivityManagerService mService; |
| 49 | private final ActivityStackSupervisor mStackSupervisor; |
| 50 | private final ActivityStartController mActivityStartController; |
| 51 | private final WindowManagerService mWindowManager; |
| 52 | private final UserController mUserController; |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 53 | private final int mCallingPid; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 54 | |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 55 | // The stack to restore the home stack behind when the animation is finished |
| 56 | private ActivityStack mRestoreHomeBehindStack; |
| 57 | |
| 58 | RecentsAnimation(ActivityManagerService am, ActivityStackSupervisor stackSupervisor, |
| 59 | ActivityStartController activityStartController, WindowManagerService wm, |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 60 | UserController userController, int callingPid) { |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 61 | mService = am; |
| 62 | mStackSupervisor = stackSupervisor; |
| 63 | mActivityStartController = activityStartController; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 64 | mWindowManager = wm; |
| 65 | mUserController = userController; |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 66 | mCallingPid = callingPid; |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void startRecentsActivity(Intent intent, IRecentsAnimationRunner recentsAnimationRunner, |
| 70 | ComponentName recentsComponent, int recentsUid) { |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 71 | Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "RecentsAnimation#startRecentsActivity"); |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 72 | |
| 73 | if (!mWindowManager.canStartRecentsAnimation()) { |
| 74 | notifyAnimationCancelBeforeStart(recentsAnimationRunner); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // If the existing home activity is already on top, then cancel |
| 79 | ActivityRecord homeActivity = mStackSupervisor.getHomeActivity(); |
| 80 | final boolean hasExistingHomeActivity = homeActivity != null; |
| 81 | if (hasExistingHomeActivity) { |
| 82 | final ActivityDisplay display = homeActivity.getDisplay(); |
| 83 | mRestoreHomeBehindStack = display.getStackAboveHome(); |
| 84 | if (mRestoreHomeBehindStack == null) { |
| 85 | notifyAnimationCancelBeforeStart(recentsAnimationRunner); |
| 86 | return; |
| 87 | } |
| 88 | } |
| 89 | |
Jorim Jaggi | 54cff64 | 2018-03-15 15:51:32 +0100 | [diff] [blame] | 90 | mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunching(); |
| 91 | |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 92 | mService.setRunningRemoteAnimation(mCallingPid, true); |
| 93 | |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 94 | mWindowManager.deferSurfaceLayout(); |
| 95 | try { |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 96 | final ActivityDisplay display; |
| 97 | if (hasExistingHomeActivity) { |
| 98 | // Move the home activity into place for the animation if it is not already top most |
| 99 | display = homeActivity.getDisplay(); |
| 100 | display.moveHomeStackBehindBottomMostVisibleStack(); |
| 101 | } else { |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 102 | // No home activity |
| 103 | final ActivityOptions opts = ActivityOptions.makeBasic(); |
| 104 | opts.setLaunchActivityType(ACTIVITY_TYPE_HOME); |
| 105 | opts.setAvoidMoveToFront(); |
| 106 | intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NO_ANIMATION); |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 107 | |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 108 | mActivityStartController |
| 109 | .obtainStarter(intent, "startRecentsActivity_noHomeActivity") |
| 110 | .setCallingUid(recentsUid) |
| 111 | .setCallingPackage(recentsComponent.getPackageName()) |
| 112 | .setActivityOptions(SafeActivityOptions.fromBundle(opts.toBundle())) |
| 113 | .setMayWait(mUserController.getCurrentUserId()) |
| 114 | .execute(); |
| 115 | mWindowManager.prepareAppTransition(TRANSIT_NONE, false); |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 116 | |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 117 | homeActivity = mStackSupervisor.getHomeActivity(); |
| 118 | display = homeActivity.getDisplay(); |
| 119 | |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 120 | // TODO: Maybe wait for app to draw in this particular case? |
| 121 | } |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 122 | |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 123 | // Mark the home activity as launch-behind to bump its visibility for the |
| 124 | // duration of the gesture that is driven by the recents component |
| 125 | homeActivity.mLaunchTaskBehind = true; |
| 126 | |
| 127 | // Fetch all the surface controls and pass them to the client to get the animation |
| 128 | // started |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 129 | mWindowManager.cancelRecentsAnimation(REORDER_MOVE_HOME_TO_ORIGINAL_POSITION); |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 130 | mWindowManager.initializeRecentsAnimation(recentsAnimationRunner, this, |
Vadim Tryshev | 593e956 | 2018-03-08 17:15:45 -0800 | [diff] [blame] | 131 | display.mDisplayId, mStackSupervisor.mRecentTasks.getRecentTaskIds()); |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 132 | |
| 133 | // If we updated the launch-behind state, update the visibility of the activities after |
| 134 | // we fetch the visible tasks to be controlled by the animation |
| 135 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS); |
Jorim Jaggi | 54cff64 | 2018-03-15 15:51:32 +0100 | [diff] [blame] | 136 | |
| 137 | mStackSupervisor.getActivityMetricsLogger().notifyActivityLaunched(START_TASK_TO_FRONT, |
| 138 | homeActivity); |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 139 | } finally { |
| 140 | mWindowManager.continueSurfaceLayout(); |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 141 | Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 142 | } |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 146 | public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode) { |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 147 | synchronized (mService) { |
| 148 | if (mWindowManager.getRecentsAnimationController() == null) return; |
| 149 | |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 150 | mService.setRunningRemoteAnimation(mCallingPid, false); |
| 151 | |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 152 | mWindowManager.inSurfaceTransaction(() -> { |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 153 | Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, |
| 154 | "RecentsAnimation#onAnimationFinished_inSurfaceTransaction"); |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 155 | mWindowManager.deferSurfaceLayout(); |
| 156 | try { |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 157 | mWindowManager.cleanupRecentsAnimation(reorderMode); |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 158 | |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 159 | final ActivityRecord homeActivity = mStackSupervisor.getHomeActivity(); |
| 160 | if (homeActivity == null) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // Restore the launched-behind state |
| 165 | homeActivity.mLaunchTaskBehind = false; |
| 166 | |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 167 | if (reorderMode == REORDER_MOVE_HOME_TO_TOP) { |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 168 | // Bring the home stack to the front |
| 169 | final ActivityStack homeStack = homeActivity.getStack(); |
Jorim Jaggi | fa9ed96 | 2018-01-25 00:16:49 +0100 | [diff] [blame] | 170 | mStackSupervisor.mNoAnimActivities.add(homeActivity); |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 171 | homeStack.moveToFront("RecentsAnimation.onAnimationFinished()"); |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 172 | } else if (reorderMode == REORDER_MOVE_HOME_TO_ORIGINAL_POSITION){ |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 173 | // Restore the home stack to its previous position |
| 174 | final ActivityDisplay display = homeActivity.getDisplay(); |
| 175 | display.moveHomeStackBehindStack(mRestoreHomeBehindStack); |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame^] | 176 | } else { |
| 177 | // Keep home stack in place, nothing changes, so ignore the transition logic |
| 178 | // below |
| 179 | return; |
Winson Chung | 1e6d4a9 | 2018-01-26 10:04:20 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | mWindowManager.prepareAppTransition(TRANSIT_NONE, false); |
| 183 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, false); |
| 184 | mStackSupervisor.resumeFocusedStackTopActivityLocked(); |
| 185 | |
| 186 | // No reason to wait for the pausing activity in this case, as the hiding of |
| 187 | // surfaces needs to be done immediately. |
| 188 | mWindowManager.executeAppTransition(); |
| 189 | } finally { |
| 190 | mWindowManager.continueSurfaceLayout(); |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 191 | Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 192 | } |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 193 | }); |
| 194 | } |
| 195 | } |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Called only when the animation should be canceled prior to starting. |
| 199 | */ |
| 200 | private void notifyAnimationCancelBeforeStart(IRecentsAnimationRunner recentsAnimationRunner) { |
| 201 | try { |
| 202 | recentsAnimationRunner.onAnimationCanceled(); |
| 203 | } catch (RemoteException e) { |
| 204 | Slog.e(TAG, "Failed to cancel recents animation before start", e); |
| 205 | } |
| 206 | } |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 207 | } |