blob: 8c5fd8cc89a16fd1335d38d26f4b3a070a341f95 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 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;
Craig Mautner27084302013-03-25 08:05:25 -070018
Andrii Kulian3c9ad072017-08-01 11:45:22 -070019import static android.Manifest.permission.ACTIVITY_EMBEDDING;
Andrii Kulian3a95edc2017-06-28 16:21:07 -070020import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Jorim Jaggife762342016-10-13 14:33:27 +020021import static android.Manifest.permission.START_ANY_ACTIVITY;
Charles Heff9b4dff2017-09-22 10:18:37 +010022import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
Bryce Lee5f0e28f2018-01-30 16:00:03 -080023import static android.app.ActivityManager.START_DELIVERED_TO_TOP;
Wale Ogunwale7056a062018-10-18 15:02:50 -070024import static android.app.ActivityManager.START_FLAG_DEBUG;
25import static android.app.ActivityManager.START_FLAG_NATIVE_DEBUGGING;
26import static android.app.ActivityManager.START_FLAG_TRACK_ALLOCATION;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.app.ActivityManager.START_TASK_TO_FRONT;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070028import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
29import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Vishnu Nair132ee832018-09-28 15:00:05 -070030import static android.app.WaitResult.INVALID_DELAY;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070032import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070034import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
37import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070039import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale214f3482018-10-04 11:00:47 -070040import static android.content.pm.PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070041import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020042import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Adrian Roosa6d6aab2018-04-19 18:58:22 +020043import static android.graphics.Rect.copyOrNull;
chaviw59b98852017-06-13 12:05:44 -070044import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Andrii Kulian79d67982019-08-19 11:56:16 -070045import static android.os.Process.INVALID_UID;
Benjamin Franza83859f2017-07-03 16:34:14 +010046import static android.os.Process.SYSTEM_UID;
Riddle Hsu2ca561b2019-10-08 21:58:58 +080047import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Jorim Jaggife762342016-10-13 14:33:27 +020048import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070049import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070050import static android.view.Display.TYPE_VIRTUAL;
Louis Chang7d0037c2018-08-13 12:42:06 +080051import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Louis Changceeb5062018-09-17 18:13:52 +080052
Wale Ogunwale59507092018-10-29 09:00:30 -070053import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
54import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
Wale Ogunwale59507092018-10-29 09:00:30 -070055import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ALL;
56import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_IDLE;
57import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_PAUSE;
58import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS;
Wale Ogunwale59507092018-10-29 09:00:30 -070059import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STACK;
60import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STATES;
61import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
62import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
63import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_IDLE;
64import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_PAUSE;
65import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RECENTS;
Wale Ogunwale59507092018-10-29 09:00:30 -070066import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STACK;
Wale Ogunwale59507092018-10-29 09:00:30 -070067import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
70import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
71import static com.android.server.wm.ActivityTaskManagerService.ANIMATE;
72import static com.android.server.wm.ActivityTaskManagerService.H.FIRST_SUPERVISOR_STACK_MSG;
73import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE;
Wale Ogunwaled32da472018-11-16 07:19:28 -080074import static com.android.server.wm.RootActivityContainer.MATCH_TASK_IN_STACKS_ONLY;
75import static com.android.server.wm.RootActivityContainer.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS;
76import static com.android.server.wm.RootActivityContainer.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE;
77import static com.android.server.wm.RootActivityContainer.TAG_STATES;
Louis Changcdec0802019-11-11 11:45:07 +080078import static com.android.server.wm.Task.LOCK_TASK_AUTH_LAUNCHABLE;
79import static com.android.server.wm.Task.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
80import static com.android.server.wm.Task.LOCK_TASK_AUTH_WHITELISTED;
81import static com.android.server.wm.Task.REPARENT_KEEP_STACK_AT_FRONT;
82import static com.android.server.wm.Task.REPARENT_LEAVE_STACK_IN_PLACE;
83import static com.android.server.wm.Task.REPARENT_MOVE_STACK_TO_FRONT;
lumark9bca6b42019-10-17 18:35:22 +080084import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION;
Wale Ogunwale2322bed2019-10-10 17:24:19 +020085import static com.android.server.wm.WindowContainer.POSITION_TOP;
Louis Changceeb5062018-09-17 18:13:52 +080086
Svetoslav7008b512015-06-24 18:47:07 -070087import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070088import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.app.ActivityManager;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070090import android.app.ActivityManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070091import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -070092import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -070093import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070094import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070095import android.app.WaitResult;
Andrii Kulian88e05cb2017-12-05 17:21:10 -080096import android.app.servertransaction.ActivityLifecycleItem;
97import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -070098import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -080099import android.app.servertransaction.PauseActivityItem;
100import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700101import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700102import android.content.Intent;
103import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700105import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import android.content.pm.PackageManager;
107import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100108import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800110import android.graphics.Rect;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100112import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700113import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700114import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700118import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700119import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700120import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700122import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700123import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100124import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700125import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700126import android.provider.MediaStore;
Svetoslav7008b512015-06-24 18:47:07 -0700127import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700128import android.util.ArraySet;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700129import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700130import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800131import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800132import android.util.SparseIntArray;
Chong Zhangb15758a2015-11-17 12:12:03 -0800133
Louis Chang6a9be162019-07-15 10:41:32 +0800134import com.android.internal.R;
Andreas Gampea36dc622018-02-05 17:19:22 -0800135import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700136import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800137import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700138import com.android.internal.os.TransferPipe;
Louis Chang7d0037c2018-08-13 12:42:06 +0800139import com.android.internal.os.logging.MetricsLoggerWrapper;
Svetoslav7008b512015-06-24 18:47:07 -0700140import com.android.internal.util.ArrayUtils;
Wale Ogunwalea38654f2019-11-17 20:37:15 -0800141import com.android.internal.util.function.pooled.PooledConsumer;
Louis Chang7d0037c2018-08-13 12:42:06 +0800142import com.android.internal.util.function.pooled.PooledLambda;
Wale Ogunwale59507092018-10-29 09:00:30 -0700143import com.android.server.am.ActivityManagerService;
Wale Ogunwale59507092018-10-29 09:00:30 -0700144import com.android.server.am.UserState;
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800145import com.android.server.wm.ActivityMetricsLogger.LaunchingState;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146
Craig Mautner8d341ef2013-03-26 09:03:27 -0700147import java.io.FileDescriptor;
148import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700149import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700150import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700151import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700152
Wale Ogunwaled32da472018-11-16 07:19:28 -0800153// TODO: This class has become a dumping ground. Let's
154// - Move things relating to the hierarchy to RootWindowContainer
155// - Move things relating to activity life cycles to maybe a new class called ActivityLifeCycler
156// - Move interface things to ActivityTaskManagerService.
157// - All other little things to other files.
158public class ActivityStackSupervisor implements RecentTasks.Callbacks {
Wale Ogunwale98875612018-10-12 07:53:02 -0700159 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_ATM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700160 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700161 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700162 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
163 private static final String TAG_STACK = TAG + POSTFIX_STACK;
164 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800165 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800166
Craig Mautnerf3333272013-04-22 10:55:53 -0700167 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900168 private static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700169
Craig Mautner0eea92c2013-05-16 13:35:39 -0700170 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900171 private static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700172
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700173 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900174 private static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700175
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000176 /** How long we wait until giving up on the activity telling us it released the top state. */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900177 private static final int TOP_RESUMED_STATE_LOSS_TIMEOUT = 500;
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000178
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900179 private static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
180 private static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
181 private static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
182 private static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
183 private static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
184 private static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
185 private static final int RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
186 private static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
187 private static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
188 private static final int REPORT_HOME_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 16;
189 private static final int TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 17;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800190
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700191 // Used to indicate that windows of activities should be preserved during the resize.
192 static final boolean PRESERVE_WINDOWS = true;
193
Wale Ogunwale040b4702015-08-06 18:10:50 -0700194 // Used to indicate if an object (e.g. task) should be moved/created
195 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700196 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700197
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700198 // Don't execute any calls to resume.
199 static final boolean DEFER_RESUME = true;
200
Winson Chung010927a2016-12-15 16:12:35 -0800201 // Used to indicate that a task is removed it should also be removed from recents.
202 static final boolean REMOVE_FROM_RECENTS = true;
203
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200204 /** True if the docked stack is currently being resized. */
205 private boolean mDockedStackResizing;
206
207 /**
208 * True if there are pending docked bounds that need to be applied after
209 * {@link #mDockedStackResizing} is reset to false.
210 */
211 private boolean mHasPendingDockedBounds;
212 private Rect mPendingDockedBounds;
213 private Rect mPendingTempDockedTaskBounds;
214 private Rect mPendingTempDockedTaskInsetBounds;
215 private Rect mPendingTempOtherTaskBounds;
216 private Rect mPendingTempOtherTaskInsetBounds;
217
Svetoslav7008b512015-06-24 18:47:07 -0700218 // Activity actions an app cannot start if it uses a permission which is not granted.
219 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
220 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700221
Svetoslav7008b512015-06-24 18:47:07 -0700222 static {
223 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
224 Manifest.permission.CAMERA);
225 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
226 Manifest.permission.CAMERA);
227 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
228 Manifest.permission.CALL_PHONE);
229 }
230
Svet Ganov99b60432015-06-27 13:15:22 -0700231 /** Action restriction: launching the activity is not restricted. */
232 private static final int ACTIVITY_RESTRICTION_NONE = 0;
233 /** Action restriction: launching the activity is restricted by a permission. */
234 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
235 /** Action restriction: launching the activity is restricted by an app op. */
236 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700237
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700238 // For debugging to make sure the caller when acquiring/releasing our
239 // wake lock is the system process.
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900240 private static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800241 /** The number of distinct task ids that can be assigned to the tasks of a single user */
242 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700243
Wale Ogunwaleb73f3962018-11-20 07:58:22 -0800244 final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800245 RootActivityContainer mRootActivityContainer;
Craig Mautner27084302013-03-25 08:05:25 -0700246
Winson Chung61c9e5a2017-10-11 10:39:32 -0700247 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700248 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800249
Winson Chung61c9e5a2017-10-11 10:39:32 -0700250 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700251 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700252
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900253 private final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000254 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700255
256 /** Short cut */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900257 private WindowManagerService mWindowManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700258
Garfield Tan891146c2018-10-09 12:14:00 -0700259 /** Common synchronization logic used to save things to disks. */
260 PersisterQueue mPersisterQueue;
261 LaunchParamsPersister mLaunchParamsPersister;
Bryce Leeec55eb02017-12-05 20:51:27 -0800262 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700263
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800264 /**
265 * Maps the task identifier that activities are currently being started in to the userId of the
266 * task. Each time a new task is created, the entry for the userId of the task is incremented
267 */
268 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700269
Bryce Lee4a194382017-04-04 14:32:48 -0700270 /** List of processes waiting to find out when a specific activity becomes visible. */
271 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700272
273 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700274 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700275
Craig Mautnerde4ef022013-04-07 19:01:33 -0700276 /** List of activities that are ready to be stopped, but waiting for the next activity to
277 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800278 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700279
Craig Mautnerf3333272013-04-22 10:55:53 -0700280 /** List of activities that are ready to be finished, but waiting for the previous activity to
281 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800282 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700283
Craig Mautner0eea92c2013-05-16 13:35:39 -0700284 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800285 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700286
Wale Ogunwale22e25262016-02-01 10:32:02 -0800287 /** List of activities whose multi-window mode changed that we need to report to the
288 * application */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900289 private final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
Wale Ogunwale22e25262016-02-01 10:32:02 -0800290
291 /** List of activities whose picture-in-picture mode changed that we need to report to the
292 * application */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900293 private final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
Wale Ogunwale22e25262016-02-01 10:32:02 -0800294
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100295 /**
296 * Animations that for the current transition have requested not to
297 * be considered for the transition animation.
298 */
299 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
300
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000301 /**
302 * Cached value of the topmost resumed activity in the system. Updated when new activity is
303 * resumed.
304 */
305 private ActivityRecord mTopResumedActivity;
306
307 /**
308 * Flag indicating whether we're currently waiting for the previous top activity to handle the
309 * loss of the state and report back before making new activity top resumed.
310 */
311 private boolean mTopResumedActivityWaitingForPrev;
312
Winson Chung5af42fc2017-03-24 17:11:33 -0700313 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
314 * the application */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900315 private Rect mPipModeChangedTargetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -0700316
Craig Mautnerf3333272013-04-22 10:55:53 -0700317 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700318 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700319
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
321 * is being brought in front of us. */
322 boolean mUserLeaving = false;
323
Craig Mautner0eea92c2013-05-16 13:35:39 -0700324 /**
Louis Chang6a9be162019-07-15 10:41:32 +0800325 * The system chooser activity which worked as a delegate of
326 * {@link com.android.internal.app.ResolverActivity}.
327 */
328 private ComponentName mSystemChooserActivity;
329
330 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700331 * We don't want to allow the device to go to sleep while in the process
332 * of launching an activity. This is primarily to allow alarm intent
333 * receivers to launch an activity and get that to run before the device
334 * goes back to sleep.
335 */
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800336 PowerManager.WakeLock mLaunchingActivityWakeLock;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700337
338 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339 * Set when the system is going to sleep, until we have
340 * successfully paused the current activity and released our wake lock.
341 * At that point the system is allowed to actually sleep.
342 */
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800343 PowerManager.WakeLock mGoingToSleepWakeLock;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344
Andrii Kulian1e32e022016-09-16 15:29:34 -0700345 /**
346 * Temporary rect used during docked stack resize calculation so we don't need to create a new
347 * object each time.
348 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700349 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700350 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700351
Bryce Lee2a3cc462017-10-27 10:57:35 -0700352 private ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800353
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700354 /** Check if placing task or activity on specified display is allowed. */
Riddle Hsu16567132018-08-16 21:37:47 +0800355 boolean canPlaceEntityOnDisplay(int displayId, int callingPid, int callingUid,
356 ActivityInfo activityInfo) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700357 if (displayId == DEFAULT_DISPLAY) {
358 // No restrictions for the default display.
359 return true;
360 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700361 if (!mService.mSupportsMultiDisplay) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700362 // Can't launch on secondary displays if feature is not supported.
363 return false;
364 }
Andrii Kulian02689a72017-07-06 14:28:59 -0700365 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
366 // Can't place activities to a display that has restricted launch rules.
367 // In this case the request should be made by explicitly adding target display id and
368 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
369 return false;
370 }
371 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700372 }
373
374 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100375 * Used to keep track whether app visibilities got changed since the last pause. Useful to
376 * determine whether to invoke the task stack change listener after pausing.
377 */
378 boolean mAppVisibilitiesChangedSinceLastPause;
379
380 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100381 * Set of tasks that are in resizing mode during an app transition to fill the "void".
382 */
383 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
384
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700385
386 /**
387 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
388 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
389 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
390 * like the docked stack going empty.
391 */
392 private boolean mAllowDockedStackResize = true;
393
Bryce Lee2a3cc462017-10-27 10:57:35 -0700394 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200395
chaviw59b98852017-06-13 12:05:44 -0700396 private PowerManager mPowerManager;
397 private int mDeferResumeCount;
398
Bryce Lee2a3cc462017-10-27 10:57:35 -0700399 private boolean mInitialized;
400
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900401 private final MoveTaskToFullscreenHelper mMoveTaskToFullscreenHelper =
402 new MoveTaskToFullscreenHelper();
403 private class MoveTaskToFullscreenHelper {
404 private ActivityDisplay mToDisplay;
405 private boolean mOnTop;
406 private Task mTopTask;
407 private boolean mSchedulePictureInPictureModeChange;
408
409 void process(ActivityStack fromStack, ActivityDisplay toDisplay, boolean onTop,
410 boolean schedulePictureInPictureModeChange) {
411 mSchedulePictureInPictureModeChange = schedulePictureInPictureModeChange;
412 mToDisplay = toDisplay;
413 mOnTop = onTop;
414 mTopTask = fromStack.getTopMostTask();
415
416 final PooledConsumer c = PooledLambda.obtainConsumer(
417 MoveTaskToFullscreenHelper::processTask, this, PooledLambda.__(Task.class));
418 fromStack.forAllTasks(c, false);
419 c.recycle();
420 mToDisplay = null;
421 mTopTask = null;
422 }
423
424 private void processTask(Task task) {
425 final ActivityStack toStack = mToDisplay.getOrCreateStack(
426 null, mTmpOptions, task, task.getActivityType(), mOnTop);
427
428 if (mOnTop) {
429 final boolean isTopTask = task == mTopTask;
430 // Defer resume until all the tasks have been moved to the fullscreen stack
431 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, isTopTask /*animate*/,
432 DEFER_RESUME, mSchedulePictureInPictureModeChange,
433 "moveTasksToFullscreenStack - onTop");
434 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
435 task.effectiveUid, task.realActivity.flattenToString());
436 } else {
437 // Position the tasks in the fullscreen stack in order at the bottom of the
438 // stack. Also defer resume until all the tasks have been moved to the
439 // fullscreen stack.
440 task.reparent(toStack, ON_TOP, REPARENT_LEAVE_STACK_IN_PLACE,
441 !ANIMATE, DEFER_RESUME, mSchedulePictureInPictureModeChange,
442 "moveTasksToFullscreenStack - NOT_onTop");
443 }
444 }
445 }
446
Tony Mak853304c2016-04-18 15:17:41 +0100447 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700448 * Description of a request to start a new activity, which has been held
449 * due to app switches being disabled.
450 */
451 static class PendingActivityLaunch {
452 final ActivityRecord r;
453 final ActivityRecord sourceRecord;
454 final int startFlags;
455 final ActivityStack stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700456 final WindowProcessController callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700457
Wale Ogunwale586a8ee2019-06-04 13:44:14 +0000458 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
459 int _startFlags, ActivityStack _stack, WindowProcessController app) {
460 r = _r;
461 sourceRecord = _sourceRecord;
462 startFlags = _startFlags;
463 stack = _stack;
464 callerApp = app;
Robert Carr13997f52015-10-23 13:13:39 -0700465 }
466
467 void sendErrorResult(String message) {
468 try {
wilsonshih0c0fb222019-06-21 16:15:08 +0800469 if (callerApp != null && callerApp.hasThread()) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700470 callerApp.getThread().scheduleCrash(message);
Robert Carr13997f52015-10-23 13:13:39 -0700471 }
472 } catch (RemoteException e) {
473 Slog.e(TAG, "Exception scheduling crash of failed "
474 + "activity launcher sourceRecord=" + sourceRecord, e);
475 }
Craig Mautneree36c772014-07-16 14:56:05 -0700476 }
477 }
478
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700479 public ActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700480 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000481 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700482 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700483 }
484
485 public void initialize() {
486 if (mInitialized) {
487 return;
488 }
489
490 mInitialized = true;
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700491 setRunningTasks(new RunningTasks());
Igor Murashkinc0b47e42018-11-07 15:54:18 -0800492
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800493 mActivityMetricsLogger = new ActivityMetricsLogger(this, mHandler.getLooper());
Wale Ogunwalef6733932018-06-27 05:14:34 -0700494 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700495
Garfield Tan891146c2018-10-09 12:14:00 -0700496 mPersisterQueue = new PersisterQueue();
497 mLaunchParamsPersister = new LaunchParamsPersister(mPersisterQueue, this);
498 mLaunchParamsController = new LaunchParamsController(mService, mLaunchParamsPersister);
Bryce Leeec55eb02017-12-05 20:51:27 -0800499 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700500 }
501
Garfield Tan891146c2018-10-09 12:14:00 -0700502 void onSystemReady() {
Garfield Tan891146c2018-10-09 12:14:00 -0700503 mLaunchParamsPersister.onSystemReady();
504 }
Bryce Lee2a3cc462017-10-27 10:57:35 -0700505
Garfield Tan0d407f42019-03-07 11:47:01 -0800506 void onUserUnlocked(int userId) {
507 // Only start persisting when the first user is unlocked. The method call is
508 // idempotent so there is no side effect to call it again when the second user is
509 // unlocked.
510 mPersisterQueue.startPersisting();
511 mLaunchParamsPersister.onUnlockUser(userId);
512 }
513
Bryce Lee2a3cc462017-10-27 10:57:35 -0700514 public ActivityMetricsLogger getActivityMetricsLogger() {
515 return mActivityMetricsLogger;
516 }
517
518 public KeyguardController getKeyguardController() {
519 return mKeyguardController;
520 }
521
Louis Chang6a9be162019-07-15 10:41:32 +0800522 ComponentName getSystemChooserActivity() {
523 if (mSystemChooserActivity == null) {
524 mSystemChooserActivity = ComponentName.unflattenFromString(
525 mService.mContext.getResources().getString(R.string.config_chooserActivity));
526 }
527 return mSystemChooserActivity;
528 }
529
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800530 void setRecentTasks(RecentTasks recentTasks) {
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700531 if (mRecentTasks != null) {
532 mRecentTasks.unregisterCallback(this);
533 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800534 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700535 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800536 }
537
Winson Chung3f0e59a2017-10-25 10:19:05 -0700538 @VisibleForTesting
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700539 void setRunningTasks(RunningTasks runningTasks) {
540 mRunningTasks = runningTasks;
541 }
542
543 RunningTasks getRunningTasks() {
544 return mRunningTasks;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700545 }
546
Jeff Brown2c43c332014-06-12 22:38:59 -0700547 /**
548 * At the time when the constructor runs, the power manager has not yet been
549 * initialized. So we initialize our wakelocks afterwards.
550 */
551 void initPowerManagement() {
Michael Wrighte3001042019-02-05 00:13:14 +0000552 mPowerManager = mService.mContext.getSystemService(PowerManager.class);
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800553 mGoingToSleepWakeLock = mPowerManager
chaviw59b98852017-06-13 12:05:44 -0700554 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800555 mLaunchingActivityWakeLock = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
556 mLaunchingActivityWakeLock.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700557 }
558
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700559 void setWindowManager(WindowManagerService wm) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700560 mWindowManager = wm;
561 getKeyguardController().setWindowManager(wm);
Craig Mautner20e72272013-04-01 13:45:53 -0700562 }
563
Matthew Ng330757d2017-02-28 14:19:17 -0800564 void moveRecentsStackToFront(String reason) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800565 final ActivityStack recentsStack = mRootActivityContainer.getDefaultDisplay().getStack(
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700566 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800567 if (recentsStack != null) {
568 recentsStack.moveToFront(reason);
569 }
570 }
571
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800572 void setNextTaskIdForUserLocked(int taskId, int userId) {
573 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
574 if (taskId > currentTaskId) {
575 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700576 }
577 }
578
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700579 static int nextTaskIdForUser(int taskId, int userId) {
580 int nextTaskId = taskId + 1;
581 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
582 // Wrap around as there will be smaller task ids that are available now.
583 nextTaskId -= MAX_TASK_IDS_PER_USER;
584 }
585 return nextTaskId;
586 }
587
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800588 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800589 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
590 // for a userId u, a taskId can only be in the range
591 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
592 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700593 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700594 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwaled32da472018-11-16 07:19:28 -0800595 || mRootActivityContainer.anyTaskForId(
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700596 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700597 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800598 if (candidateTaskId == currentTaskId) {
599 // Something wrong!
600 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
601 throw new IllegalStateException("Cannot get an available task id."
602 + " Reached limit of " + MAX_TASK_IDS_PER_USER
603 + " running tasks per user.");
604 }
605 }
606 mCurTaskIdForUser.put(userId, candidateTaskId);
607 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700608 }
609
Riddle Hsuc48c8912019-10-31 13:34:27 +0800610 void waitActivityVisible(ComponentName name, WaitResult result) {
611 final WaitInfo waitInfo = new WaitInfo(name, result);
Bryce Lee4a194382017-04-04 14:32:48 -0700612 mWaitingForActivityVisible.add(waitInfo);
613 }
614
615 void cleanupActivity(ActivityRecord r) {
616 // Make sure this record is no longer in the pending finishes list.
617 // This could happen, for example, if we are trimming activities
618 // down to the max limit while they are still waiting to finish.
619 mFinishingActivities.remove(r);
Bryce Lee4a194382017-04-04 14:32:48 -0700620
Riddle Hsuc48c8912019-10-31 13:34:27 +0800621 stopWaitingForActivityVisible(r, WaitResult.INVALID_DELAY);
Bryce Lee4a194382017-04-04 14:32:48 -0700622 }
623
Louis Changa59937a2019-03-20 17:17:22 +0800624 void stopWaitingForActivityVisible(ActivityRecord r) {
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800625 stopWaitingForActivityVisible(r, getActivityMetricsLogger().getLastDrawnDelayMs(r));
Riddle Hsuc48c8912019-10-31 13:34:27 +0800626 }
627
628 void stopWaitingForActivityVisible(ActivityRecord r, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700629 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -0700630 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
631 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800632 if (w.matches(r.mActivityComponent)) {
Bryce Lee4a194382017-04-04 14:32:48 -0700633 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700634 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -0700635 result.timeout = false;
636 result.who = w.getComponent();
Riddle Hsuc48c8912019-10-31 13:34:27 +0800637 result.totalTime = totalTime;
Bryce Lee4a194382017-04-04 14:32:48 -0700638 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700639 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700640 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700641 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700642 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700643 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700644 }
645
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800646 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
647 if (mWaitingActivityLaunched.isEmpty()) {
648 return;
649 }
650
651 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
652 return;
653 }
654
Chong Zhang5022da32016-06-21 16:31:37 -0700655 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800656
Chong Zhang5022da32016-06-21 16:31:37 -0700657 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
658 WaitResult w = mWaitingActivityLaunched.remove(i);
659 if (w.who == null) {
660 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800661 w.result = result;
662
663 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
664 // will be no followup launch signals. Assign the result and launched component.
665 if (result == START_DELIVERED_TO_TOP) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800666 w.who = r.mActivityComponent;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800667 }
Chong Zhang5022da32016-06-21 16:31:37 -0700668 }
669 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800670
Chong Zhang5022da32016-06-21 16:31:37 -0700671 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700672 mService.mGlobalLock.notifyAll();
Chong Zhang5022da32016-06-21 16:31:37 -0700673 }
674 }
675
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800676 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, long totalTime,
677 @WaitResult.LaunchState int launchState) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700678 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700679 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700680 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700681 if (w.who == null) {
682 changed = true;
683 w.timeout = timeout;
684 if (r != null) {
685 w.who = new ComponentName(r.info.packageName, r.info.name);
686 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700687 w.totalTime = totalTime;
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800688 w.launchState = launchState;
Chong Zhang5022da32016-06-21 16:31:37 -0700689 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700690 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700691 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700692 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700693 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700694 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700695 }
696
Todd Kennedy7440f172015-12-09 14:31:22 -0800697 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
698 ProfilerInfo profilerInfo) {
699 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700700 if (aInfo != null) {
701 // Store the found target back into the intent, because now that
702 // we have it we never want to do this again. For example, if the
703 // user navigates back to this point in the history, we should
704 // always restart the exact same activity.
705 intent.setComponent(new ComponentName(
706 aInfo.applicationInfo.packageName, aInfo.name));
707
708 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700709 if (!aInfo.processName.equals("system")) {
Wale Ogunwale7056a062018-10-18 15:02:50 -0700710 if ((startFlags & (START_FLAG_DEBUG | START_FLAG_NATIVE_DEBUGGING
711 | START_FLAG_TRACK_ALLOCATION)) != 0 || profilerInfo != null) {
Vishnu Nair006ee3f2018-11-09 08:36:01 -0800712
713 // Mimic an AMS synchronous call by passing a message to AMS and wait for AMS
714 // to notify us that the task has completed.
715 // TODO(b/80414790) look into further untangling for the situation where the
716 // caller is on the same thread as the handler we are posting to.
717 synchronized (mService.mGlobalLock) {
718 // Post message to AMS.
719 final Message msg = PooledLambda.obtainMessage(
720 ActivityManagerInternal::setDebugFlagsForStartingActivity,
721 mService.mAmInternal, aInfo, startFlags, profilerInfo,
722 mService.mGlobalLock);
723 mService.mH.sendMessage(msg);
724 try {
725 mService.mGlobalLock.wait();
726 } catch (InterruptedException ignore) {
727
728 }
729 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700730 }
731 }
Todd Kennedyb3b431302017-03-20 16:05:48 -0700732 final String intentLaunchToken = intent.getLaunchToken();
733 if (aInfo.launchToken == null && intentLaunchToken != null) {
734 aInfo.launchToken = intentLaunchToken;
735 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700736 }
737 return aInfo;
738 }
739
Patrick Baumann78380272018-04-04 10:41:01 -0700740 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
741 int filterCallingUid) {
Wale Ogunwale1f573382018-12-03 06:42:54 -0800742 try {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800743 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "resolveIntent");
Wale Ogunwale1f573382018-12-03 06:42:54 -0800744 int modifiedFlags = flags
745 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
746 if (intent.isWebIntent()
747 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
748 modifiedFlags |= PackageManager.MATCH_INSTANT;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100749 }
Wale Ogunwale1f573382018-12-03 06:42:54 -0800750
751 // In order to allow cross-profile lookup, we clear the calling identity here.
752 // Note the binder identity won't affect the result, but filterCallingUid will.
753
754 // Cross-user/profile call check are done at the entry points
755 // (e.g. AMS.startActivityAsUser).
756 final long token = Binder.clearCallingIdentity();
757 try {
758 return mService.getPackageManagerInternalLocked().resolveIntent(
759 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
760 } finally {
761 Binder.restoreCallingIdentity(token);
762 }
763 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800764 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Todd Kennedy7440f172015-12-09 14:31:22 -0800765 }
Todd Kennedy7440f172015-12-09 14:31:22 -0800766 }
767
768 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -0700769 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
770 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -0800771 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
772 }
773
Wale Ogunwaled32da472018-11-16 07:19:28 -0800774 boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800775 boolean andResume, boolean checkConfig) throws RemoteException {
776
Wale Ogunwaled32da472018-11-16 07:19:28 -0800777 if (!mRootActivityContainer.allPausedActivitiesComplete()) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800778 // While there are activities pausing we skipping starting any new activities until
779 // pauses are complete. NOTE: that we also do this for activities that are starting in
780 // the paused state because they will first be resumed then paused on the client side.
781 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
782 "realStartActivityLocked: Skipping start of r=" + r
783 + " some activities pausing...");
784 return false;
785 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700786
Louis Changcdec0802019-11-11 11:45:07 +0800787 final Task task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700788 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -0700789
790 beginDeferResume();
791
Craig Mautner2420ead2013-04-01 17:13:20 -0700792 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700793 r.startFreezingScreenLocked(proc, 0);
chaviw59b98852017-06-13 12:05:44 -0700794
795 // schedule launch ticks to collect information about slow apps.
796 r.startLaunchTickingLocked();
797
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700798 r.setProcess(proc);
chaviw59b98852017-06-13 12:05:44 -0700799
Louis Chang77ce34d2019-01-03 15:45:12 +0800800 // Ensure activity is allowed to be resumed after process has set.
801 if (andResume && !r.canResumeByCompat()) {
802 andResume = false;
803 }
804
Riddle Hsu13c27ae2019-10-05 23:10:15 +0800805 r.notifyUnknownVisibilityLaunchedForKeyguardTransition();
Jorim Jaggi838c2452017-08-28 15:44:43 +0200806
chaviw59b98852017-06-13 12:05:44 -0700807 // Have the window manager re-evaluate the orientation of the screen based on the new
808 // activity order. Note that as a result of this, it can call back into the activity
809 // manager with a new orientation. We don't care about that, because the activity is
810 // not currently running so we are just restarting it anyway.
811 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -0700812 // Deferring resume here because we're going to launch new activity shortly.
813 // We don't want to perform a redundant launch of the same record while ensuring
814 // configurations and trying to resume top activity of focused stack.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800815 mRootActivityContainer.ensureVisibilityAndConfig(r, r.getDisplayId(),
Andrii Kulianf4479ee2018-05-23 17:52:48 -0700816 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -0700817 }
chaviw59b98852017-06-13 12:05:44 -0700818
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800819 if (r.getActivityStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
Issei Suzuki1669ea42019-11-06 14:20:59 +0100820 true /* isTop */) && r.allowMoveToFront()) {
821 // We only set the visibility to true if the activity is not being launched in
822 // background, and is allowed to be visible based on keyguard state. This avoids
823 // setting this into motion in window manager that is later cancelled due to later
824 // calls to ensure visible activities that set visibility back to false.
chaviw59b98852017-06-13 12:05:44 -0700825 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700826 }
chaviw59b98852017-06-13 12:05:44 -0700827
chaviw59b98852017-06-13 12:05:44 -0700828 final int applicationInfoUid =
829 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700830 if ((r.mUserId != proc.mUserId) || (r.info.applicationInfo.uid != applicationInfoUid)) {
chaviw59b98852017-06-13 12:05:44 -0700831 Slog.wtf(TAG,
832 "User ID for activity changing for " + r
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700833 + " appInfo.uid=" + r.info.applicationInfo.uid
chaviw59b98852017-06-13 12:05:44 -0700834 + " info.ai.uid=" + applicationInfoUid
Wale Ogunwale9c103022018-10-18 07:44:54 -0700835 + " old=" + r.app + " new=" + proc);
chaviw59b98852017-06-13 12:05:44 -0700836 }
837
chaviw59b98852017-06-13 12:05:44 -0700838 r.launchCount++;
839 r.lastLaunchTime = SystemClock.uptimeMillis();
840
841 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
842
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700843 proc.addActivityIfNeeded(r);
chaviw59b98852017-06-13 12:05:44 -0700844
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700845 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +0100846 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
847 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
848 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -0700849 && lockTaskController.getLockTaskModeState()
850 == LOCK_TASK_MODE_LOCKED)) {
851 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -0700852 }
853
854 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700855 if (!proc.hasThread()) {
chaviw59b98852017-06-13 12:05:44 -0700856 throw new RemoteException();
857 }
858 List<ResultInfo> results = null;
859 List<ReferrerIntent> newIntents = null;
860 if (andResume) {
861 // We don't need to deliver new intents and/or set results if activity is going
862 // to pause immediately after launch.
863 results = r.results;
864 newIntents = r.newIntents;
865 }
866 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Andrii Kulian06b8dcb2019-07-10 21:09:38 -0700867 "Launching: " + r + " savedState=" + r.getSavedState()
868 + " with results=" + results + " newIntents=" + newIntents
869 + " andResume=" + andResume);
Jeff Changd136e772019-11-05 20:33:52 +0800870 EventLogTags.writeWmRestartActivity(r.mUserId, System.identityHashCode(r),
871 task.mTaskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700872 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -0700873 // Home process is the root process of the task.
Wale Ogunwalea38654f2019-11-17 20:37:15 -0800874 updateHomeProcess(task.getBottomMostActivity().app);
chaviw59b98852017-06-13 12:05:44 -0700875 }
Wale Ogunwale214f3482018-10-04 11:00:47 -0700876 mService.getPackageManagerInternalLocked().notifyPackageUse(
877 r.intent.getComponent().getPackageName(), NOTIFY_PACKAGE_USE_ACTIVITY);
chaviw59b98852017-06-13 12:05:44 -0700878 r.sleeping = false;
879 r.forceNewConfig = false;
Wale Ogunwale008163e2018-07-23 23:11:08 -0700880 mService.getAppWarningsLocked().onStartActivity(r);
Wale Ogunwale53783742018-09-16 10:21:51 -0700881 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
chaviw59b98852017-06-13 12:05:44 -0700882
chaviw59b98852017-06-13 12:05:44 -0700883 // Because we could be starting an Activity in the system process this may not go
884 // across a Binder interface which would create a new Configuration. Consequently
885 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -0800886
chaviw59b98852017-06-13 12:05:44 -0700887 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
Wale Ogunwale9c103022018-10-18 07:44:54 -0700888 proc.getConfiguration(), r.getMergedOverrideConfiguration());
chaviw59b98852017-06-13 12:05:44 -0700889 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -0800890
Andrii Kulian06b8dcb2019-07-10 21:09:38 -0700891 logIfTransactionTooLarge(r.intent, r.getSavedState());
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800892
893
894 // Create activity launch transaction.
Wale Ogunwale9c103022018-10-18 07:44:54 -0700895 final ClientTransaction clientTransaction = ClientTransaction.obtain(
896 proc.getThread(), r.appToken);
lumark588a3e82018-07-20 18:53:54 +0800897
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800898 final DisplayContent dc = r.getDisplay().mDisplayContent;
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800899 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -0700900 System.identityHashCode(r), r.info,
901 // TODO: Have this take the merged configuration instead of separate global
902 // and override configs.
903 mergedConfiguration.getGlobalConfiguration(),
904 mergedConfiguration.getOverrideConfiguration(), r.compat,
Wale Ogunwale9c103022018-10-18 07:44:54 -0700905 r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),
Andrii Kulian06b8dcb2019-07-10 21:09:38 -0700906 r.getSavedState(), r.getPersistentSavedState(), results, newIntents,
Sunny Goyald40c3452019-03-20 12:46:55 -0700907 dc.isNextTransitionForward(), proc.createProfilerInfoIfNeeded(),
908 r.assistToken));
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800909
910 // Set desired final state.
911 final ActivityLifecycleItem lifecycleItem;
912 if (andResume) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800913 lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800914 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -0700915 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800916 }
917 clientTransaction.setLifecycleStateRequest(lifecycleItem);
918
919 // Schedule transaction.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700920 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Craig Mautner2420ead2013-04-01 17:13:20 -0700921
Wale Ogunwale9c103022018-10-18 07:44:54 -0700922 if ((proc.mInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
Wale Ogunwale214f3482018-10-04 11:00:47 -0700923 && mService.mHasHeavyWeightFeature) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700924 // This may be a heavy-weight process! Note that the package manager will ensure
925 // that only activity can run in the main process of the .apk, which is the only
926 // thing that will be considered heavy-weight.
Wale Ogunwale9c103022018-10-18 07:44:54 -0700927 if (proc.mName.equals(proc.mInfo.packageName)) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700928 if (mService.mHeavyWeightProcess != null
929 && mService.mHeavyWeightProcess != proc) {
930 Slog.w(TAG, "Starting new heavy weight process " + proc
chaviw59b98852017-06-13 12:05:44 -0700931 + " when already running "
Wale Ogunwale53783742018-09-16 10:21:51 -0700932 + mService.mHeavyWeightProcess);
chaviw59b98852017-06-13 12:05:44 -0700933 }
Wale Ogunwale53783742018-09-16 10:21:51 -0700934 mService.setHeavyWeightProcess(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700935 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700936 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700937
chaviw59b98852017-06-13 12:05:44 -0700938 } catch (RemoteException e) {
939 if (r.launchFailed) {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700940 // This is the second time we failed -- finish activity and give up.
chaviw59b98852017-06-13 12:05:44 -0700941 Slog.e(TAG, "Second failure launching "
Wale Ogunwale9c103022018-10-18 07:44:54 -0700942 + r.intent.getComponent().flattenToShortString() + ", giving up", e);
943 proc.appDied();
Andrii Kulian7dd39bb2019-07-22 13:11:10 -0700944 r.finishIfPossible("2nd-crash", false /* oomAdj */);
chaviw59b98852017-06-13 12:05:44 -0700945 return false;
946 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700947
chaviw59b98852017-06-13 12:05:44 -0700948 // This is the first time we failed -- restart process and
949 // retry.
950 r.launchFailed = true;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700951 proc.removeActivity(r);
chaviw59b98852017-06-13 12:05:44 -0700952 throw e;
953 }
954 } finally {
955 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -0700956 }
957
958 r.launchFailed = false;
Riddle Hsu5477b572019-11-12 16:07:29 +0800959 if (stack.updateLruList(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -0700960 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -0700961 }
962
Andrii Kulianb372da62018-01-18 10:46:24 -0800963 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
964 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -0700965 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -0700966 // As part of the process of launching, ActivityThread also performs
967 // a resume.
968 stack.minimalResumeActivityLocked(r);
969 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800970 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +0000971 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -0800972 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700973 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -0800974 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -0800975 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -0700976 }
Riddle Hsu17e38422019-04-12 16:55:11 +0800977 // Perform OOM scoring after the activity state is set, so the process can be updated with
978 // the latest state.
979 proc.onStartActivity(mService.mTopProcessState, r.info);
Craig Mautner2420ead2013-04-01 17:13:20 -0700980
981 // Launch the new version setup screen if needed. We do this -after-
982 // launching the initial activity (that is, home), so that it can have
983 // a chance to initialize itself while in the background, making the
984 // switch back to it faster and look better.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800985 if (mRootActivityContainer.isTopDisplayFocusedStack(stack)) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700986 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -0700987 }
988
Dianne Hackborn465fa392014-09-14 14:21:18 -0700989 // Update any services we are bound to that might care about whether
990 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -0700991 if (r.app != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700992 r.app.updateServiceConnectionActivities();
Wale Ogunwaled6ac7622016-05-26 09:02:25 -0700993 }
Dianne Hackborn465fa392014-09-14 14:21:18 -0700994
Craig Mautner2420ead2013-04-01 17:13:20 -0700995 return true;
996 }
997
Louis Changdcdde952018-12-04 15:38:44 +0800998 void updateHomeProcess(WindowProcessController app) {
999 if (app != null && mService.mHomeProcess != app) {
1000 if (!mHandler.hasMessages(REPORT_HOME_CHANGED_MSG)) {
1001 mHandler.sendEmptyMessage(REPORT_HOME_CHANGED_MSG);
1002 }
1003 mService.mHomeProcess = app;
1004 }
1005 }
1006
Sudheer Shankafab200f2017-05-17 20:41:53 -07001007 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1008 int extrasSize = 0;
1009 if (intent != null) {
1010 final Bundle extras = intent.getExtras();
1011 if (extras != null) {
1012 extrasSize = extras.getSize();
1013 }
1014 }
1015 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1016 if (extrasSize + icicleSize > 200000) {
1017 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1018 + ", icicle size: " + icicleSize);
1019 }
1020 }
1021
Wale Ogunwale076c3b12019-11-20 12:17:22 -08001022 void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001023 // Is this activity's application already running?
Wale Ogunwale9c103022018-10-18 07:44:54 -07001024 final WindowProcessController wpc =
1025 mService.getProcessController(r.processName, r.info.applicationInfo.uid);
Craig Mautnere79d42682013-04-01 19:01:53 -07001026
Chilun917173f2019-01-04 16:47:42 +08001027 boolean knownToBeDead = false;
Wale Ogunwale9c103022018-10-18 07:44:54 -07001028 if (wpc != null && wpc.hasThread()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001029 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001030 realStartActivityLocked(r, wpc, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001031 return;
1032 } catch (RemoteException e) {
1033 Slog.w(TAG, "Exception when starting activity "
1034 + r.intent.getComponent().flattenToShortString(), e);
1035 }
1036
1037 // If a dead object exception was thrown -- fall through to
1038 // restart the application.
Chilun917173f2019-01-04 16:47:42 +08001039 knownToBeDead = true;
Craig Mautnere79d42682013-04-01 19:01:53 -07001040 }
1041
Riddle Hsu13c27ae2019-10-05 23:10:15 +08001042 r.notifyUnknownVisibilityLaunchedForKeyguardTransition();
Issei Suzuki5205af72018-11-09 11:51:59 +01001043
Riddle Hsu32dbdca2019-05-17 23:10:16 -06001044 final boolean isTop = andResume && r.isTopRunningActivity();
1045 mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
Craig Mautnere79d42682013-04-01 19:01:53 -07001046 }
1047
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001048 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo, String resultWho,
1049 int requestCode, int callingPid, int callingUid, String callingPackage,
1050 boolean ignoreTargetSecurity, boolean launchingInTask,
1051 WindowProcessController callerApp, ActivityRecord resultRecord,
1052 ActivityStack resultStack) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001053 final boolean isCallerRecents = mService.getRecentTasks() != null
1054 && mService.getRecentTasks().isCallerRecents(callingUid);
Wale Ogunwalef6733932018-06-27 05:14:34 -07001055 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001056 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001057 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1058 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1059 // caller is the recents component and we are specifically starting an activity in an
1060 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001061 return true;
1062 }
1063 final int componentRestriction = getComponentRestrictionForCallingPackage(
1064 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1065 final int actionRestriction = getActionRestrictionForCallingPackage(
1066 intent.getAction(), callingPackage, callingPid, callingUid);
1067 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1068 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1069 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -07001070 resultRecord.sendResult(INVALID_UID, resultWho, requestCode,
1071 Activity.RESULT_CANCELED, null /* data */);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001072 }
1073 final String msg;
1074 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1075 msg = "Permission Denial: starting " + intent.toString()
1076 + " from " + callerApp + " (pid=" + callingPid
1077 + ", uid=" + callingUid + ")" + " with revoked permission "
1078 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1079 } else if (!aInfo.exported) {
1080 msg = "Permission Denial: starting " + intent.toString()
1081 + " from " + callerApp + " (pid=" + callingPid
1082 + ", uid=" + callingUid + ")"
1083 + " not exported from uid " + aInfo.applicationInfo.uid;
1084 } else {
1085 msg = "Permission Denial: starting " + intent.toString()
1086 + " from " + callerApp + " (pid=" + callingPid
1087 + ", uid=" + callingUid + ")"
1088 + " requires " + aInfo.permission;
1089 }
1090 Slog.w(TAG, msg);
1091 throw new SecurityException(msg);
1092 }
1093
1094 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1095 final String message = "Appop Denial: starting " + intent.toString()
1096 + " from " + callerApp + " (pid=" + callingPid
1097 + ", uid=" + callingUid + ")"
1098 + " requires " + AppOpsManager.permissionToOp(
1099 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1100 Slog.w(TAG, message);
1101 return false;
1102 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1103 final String message = "Appop Denial: starting " + intent.toString()
1104 + " from " + callerApp + " (pid=" + callingPid
1105 + ", uid=" + callingUid + ")"
1106 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1107 Slog.w(TAG, message);
1108 return false;
1109 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001110
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001111 return true;
1112 }
1113
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001114 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001115 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1116 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001117 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1118 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1119
Andrii Kulian02689a72017-07-06 14:28:59 -07001120 if (callingPid == -1 && callingUid == -1) {
1121 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1122 return true;
1123 }
1124
Wale Ogunwaled32da472018-11-16 07:19:28 -08001125 final ActivityDisplay activityDisplay =
1126 mRootActivityContainer.getActivityDisplayOrCreate(launchDisplayId);
Louis Chang7d0037c2018-08-13 12:42:06 +08001127 if (activityDisplay == null || activityDisplay.isRemoved()) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001128 Slog.w(TAG, "Launch on display check: display not found");
1129 return false;
1130 }
1131
Andrii Kulian02689a72017-07-06 14:28:59 -07001132 // Check if the caller has enough privileges to embed activities and launch to private
1133 // displays.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001134 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001135 callingUid);
1136 if (startAnyPerm == PERMISSION_GRANTED) {
1137 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1138 + " allow launch any on display");
1139 return true;
1140 }
1141
Andrii Kulian8f070292017-09-12 22:56:49 -07001142 // Check if caller is already present on display
1143 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1144
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001145 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1146 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1147 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001148 // Limit launching on virtual displays, because their contents can be read from Surface
1149 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001150 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1151 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1152 + " disallow launch on virtual display for not-embedded activity.");
1153 return false;
1154 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001155 // Check if the caller is allowed to embed activities from other apps.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001156 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001157 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001158 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1159 + " disallow activity embedding without permission.");
1160 return false;
1161 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001162 }
1163
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001164 if (!activityDisplay.isPrivate()) {
1165 // Anyone can launch on a public display.
1166 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1167 + " allow launch on public display");
1168 return true;
1169 }
1170
1171 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001172 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001173 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1174 + " allow launch for owner of the display");
1175 return true;
1176 }
1177
Andrii Kulian8f070292017-09-12 22:56:49 -07001178 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001179 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1180 + " allow launch for caller present on the display");
1181 return true;
1182 }
1183
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001184 Slog.w(TAG, "Launch on display check: denied");
1185 return false;
1186 }
1187
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001188 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001189 final long identity = Binder.clearCallingIdentity();
1190 try {
1191 return UserManager.get(mService.mContext).getUserInfo(userId);
1192 } finally {
1193 Binder.restoreCallingIdentity(identity);
1194 }
1195 }
1196
Svet Ganov99b60432015-06-27 13:15:22 -07001197 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001198 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Wale Ogunwale214f3482018-10-04 11:00:47 -07001199 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001200 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001201 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001202 return ACTIVITY_RESTRICTION_PERMISSION;
1203 }
1204
Christopher Tateff7add02015-08-17 10:23:22 -07001205 if (activityInfo.permission == null) {
1206 return ACTIVITY_RESTRICTION_NONE;
1207 }
1208
Svet Ganov99b60432015-06-27 13:15:22 -07001209 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1210 if (opCode == AppOpsManager.OP_NONE) {
1211 return ACTIVITY_RESTRICTION_NONE;
1212 }
1213
Philip P. Moltmann59076d82019-08-19 15:00:40 -07001214 // TODO moltmann b/136595429: Set featureId from caller
1215 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage, /* featureId */ null)
Wale Ogunwalef6733932018-06-27 05:14:34 -07001216 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001217 if (!ignoreTargetSecurity) {
1218 return ACTIVITY_RESTRICTION_APPOP;
1219 }
Svet Ganov99b60432015-06-27 13:15:22 -07001220 }
1221
1222 return ACTIVITY_RESTRICTION_NONE;
1223 }
1224
Svetoslav7008b512015-06-24 18:47:07 -07001225 private int getActionRestrictionForCallingPackage(String action,
1226 String callingPackage, int callingPid, int callingUid) {
1227 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001228 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001229 }
1230
1231 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1232 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001233 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001234 }
1235
1236 final PackageInfo packageInfo;
1237 try {
1238 packageInfo = mService.mContext.getPackageManager()
Andrii Kuliana8ccae42019-07-24 18:35:22 +00001239 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
Svetoslav7008b512015-06-24 18:47:07 -07001240 } catch (PackageManager.NameNotFoundException e) {
1241 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001242 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001243 }
1244
1245 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001246 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001247 }
1248
Wale Ogunwalef6733932018-06-27 05:14:34 -07001249 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001250 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001251 }
1252
1253 final int opCode = AppOpsManager.permissionToOpCode(permission);
1254 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001255 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001256 }
1257
Philip P. Moltmann59076d82019-08-19 15:00:40 -07001258 // TODO moltmann b/136595429: Set componentId from caller
1259 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage, /* featureId */ null)
Wale Ogunwalef6733932018-06-27 05:14:34 -07001260 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001261 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001262 }
1263
Svet Ganov99b60432015-06-27 13:15:22 -07001264 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001265 }
1266
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001267 void setLaunchSource(int uid) {
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001268 mLaunchingActivityWakeLock.setWorkSource(new WorkSource(uid));
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001269 }
1270
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001271 void acquireLaunchWakelock() {
1272 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1273 throw new IllegalStateException("Calling must be system uid");
1274 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001275 mLaunchingActivityWakeLock.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001276 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1277 // To be safe, don't allow the wake lock to be held for too long.
1278 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1279 }
1280 }
1281
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001282 /**
Louis Chang89f43fc2018-10-05 10:59:14 +08001283 * Called when all resumed tasks/stacks are idle.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001284 * @return the state of mService.mAm.mBooting before this was called.
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001285 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001286 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001287 private boolean checkFinishBootingLocked() {
Wale Ogunwale53783742018-09-16 10:21:51 -07001288 final boolean booting = mService.isBooting();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001289 boolean enableScreen = false;
Wale Ogunwale53783742018-09-16 10:21:51 -07001290 mService.setBooting(false);
1291 if (!mService.isBooted()) {
1292 mService.setBooted(true);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001293 enableScreen = true;
1294 }
1295 if (booting || enableScreen) {
Wale Ogunwale53783742018-09-16 10:21:51 -07001296 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001297 }
1298 return booting;
1299 }
1300
Craig Mautnerf3333272013-04-22 10:55:53 -07001301 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001302 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001303 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001304 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001305 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001306
Craig Mautnerf3333272013-04-22 10:55:53 -07001307 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001308 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001309 int NS = 0;
1310 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001311 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001312 boolean activityRemoved = false;
1313
Wale Ogunwale7d701172015-03-11 15:36:30 -07001314 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001315 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001316 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1317 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001318 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1319 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001320 if (fromTimeout) {
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -08001321 reportActivityLaunchedLocked(fromTimeout, r, INVALID_DELAY,
1322 -1 /* launchState */);
Craig Mautnerf3333272013-04-22 10:55:53 -07001323 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001324
1325 // This is a hack to semi-deal with a race condition
1326 // in the client where it can be constructed with a
1327 // newer configuration from when we asked it to launch.
1328 // We'll update with whatever configuration it now says
1329 // it used to launch.
1330 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001331 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001332 }
1333
1334 // We are now idle. If someone is waiting for a thumbnail from
1335 // us, we can now deliver.
1336 r.idle = true;
1337
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001338 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Louis Chang89f43fc2018-10-05 10:59:14 +08001339
Louis Chang1eff2482018-11-01 15:46:31 +08001340 // Check if able to finish booting when device is booting and all resumed activities
1341 // are idle.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001342 if ((mService.isBooting() && mRootActivityContainer.allResumedActivitiesIdle())
1343 || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001344 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001345 }
Garfield Tan2746ab52018-07-25 12:33:01 -07001346
1347 // When activity is idle, we consider the relaunch must be successful, so let's clear
1348 // the flag.
1349 r.mRelaunchReason = RELAUNCH_REASON_NONE;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001350 }
1351
Wale Ogunwaled32da472018-11-16 07:19:28 -08001352 if (mRootActivityContainer.allResumedActivitiesIdle()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001353 if (r != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001354 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001355 }
1356
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001357 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001358 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1359 if (VALIDATE_WAKE_LOCK_CALLER &&
1360 Binder.getCallingUid() != Process.myUid()) {
1361 throw new IllegalStateException("Calling must be system uid");
1362 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001363 mLaunchingActivityWakeLock.release();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001364 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001365 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001366 }
1367
1368 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001369 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1370 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001371 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001372 if ((NF = mFinishingActivities.size()) > 0) {
1373 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001374 mFinishingActivities.clear();
1375 }
1376
Craig Mautnerf3333272013-04-22 10:55:53 -07001377 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001378 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001379 mStartingUsers.clear();
1380 }
1381
Craig Mautnerf3333272013-04-22 10:55:53 -07001382 // Stop any activities that are scheduled to do so but have been
1383 // waiting for the next one to start.
1384 for (int i = 0; i < NS; i++) {
1385 r = stops.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001386 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001387 if (stack != null) {
1388 if (r.finishing) {
Andrii Kulian7dd39bb2019-07-22 13:11:10 -07001389 // TODO(b/137329632): Wait for idle of the right activity, not just any.
1390 r.destroyIfPossible("activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07001391 } else {
Andrii Kulian79d67982019-08-19 11:56:16 -07001392 r.stopIfPossible();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001393 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001394 }
1395 }
1396
1397 // Finish any activities that are scheduled to do so but have been
1398 // waiting for the next one to start.
1399 for (int i = 0; i < NF; i++) {
1400 r = finishes.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001401 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001402 if (stack != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -07001403 activityRemoved |= r.destroyImmediately(true /* removeFromApp */, "finish-idle");
Wale Ogunwale7d701172015-03-11 15:36:30 -07001404 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001405 }
1406
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001407 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001408 // Complete user switch
1409 if (startingUsers != null) {
1410 for (int i = 0; i < startingUsers.size(); i++) {
Wale Ogunwale86b74462018-07-02 08:42:43 -07001411 mService.mAmInternal.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001412 }
1413 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001414 }
1415
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001416 mService.mH.post(() -> mService.mAmInternal.trimApplications());
Craig Mautnerf3333272013-04-22 10:55:53 -07001417 //dump();
1418 //mWindowManager.dump();
1419
Craig Mautnerf3333272013-04-22 10:55:53 -07001420 if (activityRemoved) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001421 mRootActivityContainer.resumeFocusedStacksTopActivities();
Craig Mautnerf3333272013-04-22 10:55:53 -07001422 }
1423
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001424 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001425 }
1426
Wale Ogunwaled32da472018-11-16 07:19:28 -08001427 /** This doesn't just find a task, it also moves the task to front. */
Louis Changcdec0802019-11-11 11:45:07 +08001428 void findTaskToMoveToFront(Task task, int flags, ActivityOptions options, String reason,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001429 boolean forceNonResizeable) {
Louis Changbd48dca2018-08-29 17:44:34 +08001430 ActivityStack currentStack = task.getStack();
Wale Ogunwale66e16852017-10-19 13:35:52 -07001431 if (currentStack == null) {
1432 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
1433 + task + " to front. Stack is null");
1434 return;
1435 }
1436
Craig Mautneraea74a52014-03-08 14:23:10 -08001437 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1438 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001439 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07001440
Louis Changc8b64112018-10-11 09:22:14 +08001441 reason = reason + " findTaskToMoveToFront";
1442 boolean reparented = false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001443 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08001444 final Rect bounds = options.getLaunchBounds();
Evan Roskya4cc3a92019-06-28 13:25:01 -07001445 task.setBounds(bounds);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001446
Wale Ogunwaled32da472018-11-16 07:19:28 -08001447 ActivityStack stack =
1448 mRootActivityContainer.getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001449
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001450 if (stack != currentStack) {
Louis Changc8b64112018-10-11 09:22:14 +08001451 moveHomeStackToFrontIfNeeded(flags, stack.getDisplay(), reason);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001452 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Louis Changc8b64112018-10-11 09:22:14 +08001453 reason);
Louis Changbd48dca2018-08-29 17:44:34 +08001454 currentStack = stack;
Louis Changc8b64112018-10-11 09:22:14 +08001455 reparented = true;
1456 // task.reparent() should already placed the task on top,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001457 // still need moveTaskToFrontLocked() below for any transition settings.
1458 }
Evan Roskydbe2ce52019-07-18 11:13:17 -07001459 if (stack.shouldResizeStackWithLaunchBounds()) {
1460 stack.resize(bounds, null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1461 !PRESERVE_WINDOWS, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001462 } else {
1463 // WM resizeTask must be done after the task is moved to the correct stack,
1464 // because Task's setBounds() also updates dim layer's bounds, but that has
1465 // dependency on the stack.
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001466 task.resize(false /* relayout */, false /* forced */);
Chong Zhang0fa656b2015-08-31 15:17:21 -07001467 }
1468 }
1469
Louis Changc8b64112018-10-11 09:22:14 +08001470 if (!reparented) {
1471 moveHomeStackToFrontIfNeeded(flags, currentStack.getDisplay(), reason);
1472 }
1473
Wale Ogunwale21e06482019-11-18 05:14:15 -08001474 final ActivityRecord r = task.getTopNonFinishingActivity();
chaviw0d562bf2018-03-15 14:24:14 -07001475 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001476 r == null ? null : r.appTimeTracker, reason);
1477
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001478 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001479 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001480
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001481 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001482 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001483 }
1484
Louis Changc8b64112018-10-11 09:22:14 +08001485 private void moveHomeStackToFrontIfNeeded(int flags, ActivityDisplay display, String reason) {
1486 final ActivityStack focusedStack = display.getFocusedStack();
1487
1488 if ((display.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
1489 && (flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0)
1490 || (focusedStack != null && focusedStack.isActivityTypeRecents())) {
1491 // We move home stack to front when we are on a fullscreen display and caller has
1492 // requested the home activity to move with it. Or the previous stack is recents.
1493 display.moveHomeStackToFront(reason);
1494 }
1495 }
1496
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001497 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001498 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001499 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001500 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001501 return false;
1502 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001503 return (mService.mSupportsPictureInPicture
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001504 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001505 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001506 }
1507
Bryce Leeec55eb02017-12-05 20:51:27 -08001508 LaunchParamsController getLaunchParamsController() {
1509 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07001510 }
1511
Wale Ogunwaled32da472018-11-16 07:19:28 -08001512 private void deferUpdateRecentsHomeStackBounds() {
1513 mRootActivityContainer.deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
1514 mRootActivityContainer.deferUpdateBounds(ACTIVITY_TYPE_HOME);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001515 }
1516
Wale Ogunwaled32da472018-11-16 07:19:28 -08001517 private void continueUpdateRecentsHomeStackBounds() {
1518 mRootActivityContainer.continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
1519 mRootActivityContainer.continueUpdateBounds(ACTIVITY_TYPE_HOME);
Jorim Jaggi192086e2016-03-11 17:17:03 +01001520 }
1521
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001522 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08001523 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001524 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1525 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Louis Changcdec0802019-11-11 11:45:07 +08001526 final Task task =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001527 mRootActivityContainer.anyTaskForId(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001528 if (task != null) {
1529 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001530 }
1531 }
1532 mResizingTasksDuringAnimation.clear();
1533 }
1534
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001535 /**
1536 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001537 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001538 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001539 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001540 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07001541
Riddle Hsua0022cd2019-09-09 21:12:41 +08001542 mService.deferWindowLayout();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001543 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001544 final int windowingMode = fromStack.getWindowingMode();
Wale Ogunwaled32da472018-11-16 07:19:28 -08001545 final ActivityDisplay toDisplay =
1546 mRootActivityContainer.getActivityDisplay(toDisplayId);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001547
1548 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001549 // We are moving all tasks from the docked stack to the fullscreen stack,
1550 // which is dismissing the docked stack, so resize all other stacks to
1551 // fullscreen here already so we don't end up with resize trashing.
Louis Chang2453d062019-11-19 22:30:48 +08001552 for (int i = toDisplay.getStackCount() - 1; i >= 0; --i) {
1553 final ActivityStack otherStack = toDisplay.getStackAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07001554 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
1555 continue;
1556 }
Evan Rosky10475742018-09-05 19:02:48 -07001557 otherStack.setWindowingMode(WINDOWING_MODE_UNDEFINED);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001558 }
1559
1560 // Also disable docked stack resizing since we have manually adjusted the
1561 // size of other stacks above and we don't want to trigger a docked stack
1562 // resize when we remove task from it below and it is detached from the
1563 // display because it no longer contains any tasks.
1564 mAllowDockedStackResize = false;
1565 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001566
Winson Chung5af42fc2017-03-24 17:11:33 -07001567 // If we are moving from the pinned stack, then the animation takes care of updating
1568 // the picture-in-picture mode.
Ben Lin6db8fb22019-10-18 16:03:44 -07001569 final boolean schedulePictureInPictureModeChange =
1570 windowingMode == WINDOWING_MODE_PINNED;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001571
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001572 if (fromStack.hasChild()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001573 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001574 mMoveTaskToFullscreenHelper.process(
1575 fromStack, toDisplay, onTop, schedulePictureInPictureModeChange);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001576 }
Winson Chung74666102017-02-22 17:49:24 -08001577
Wale Ogunwaled32da472018-11-16 07:19:28 -08001578 mRootActivityContainer.ensureActivitiesVisible(null, 0, PRESERVE_WINDOWS);
1579 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001580 } finally {
1581 mAllowDockedStackResize = true;
Riddle Hsua0022cd2019-09-09 21:12:41 +08001582 mService.continueWindowLayout();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001583 }
1584 }
1585
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001586 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001587 mWindowManager.inSurfaceTransaction(() ->
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001588 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, DEFAULT_DISPLAY, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07001589 }
1590
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001591 void setSplitScreenResizing(boolean resizing) {
1592 if (resizing == mDockedStackResizing) {
1593 return;
1594 }
1595
1596 mDockedStackResizing = resizing;
1597 mWindowManager.setDockedStackResizing(resizing);
1598
1599 if (!resizing && mHasPendingDockedBounds) {
1600 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
1601 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
1602 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
1603
1604 mHasPendingDockedBounds = false;
1605 mPendingDockedBounds = null;
1606 mPendingTempDockedTaskBounds = null;
1607 mPendingTempDockedTaskInsetBounds = null;
1608 mPendingTempOtherTaskBounds = null;
1609 mPendingTempOtherTaskInsetBounds = null;
1610 }
1611 }
1612
Jorim Jaggidc249c42015-12-15 14:57:31 -08001613 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001614 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
1615 boolean preserveWindows) {
1616 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
1617 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
1618 false /* deferResume */);
1619 }
1620
Wale Ogunwaled32da472018-11-16 07:19:28 -08001621 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001622 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
1623 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001624
1625 if (!mAllowDockedStackResize) {
1626 // Docked stack resize currently disabled.
1627 return;
1628 }
1629
Wale Ogunwaled32da472018-11-16 07:19:28 -08001630 final ActivityStack stack =
1631 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001632 if (stack == null) {
1633 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1634 return;
1635 }
1636
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001637 if (mDockedStackResizing) {
1638 mHasPendingDockedBounds = true;
1639 mPendingDockedBounds = copyOrNull(dockedBounds);
1640 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
1641 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
1642 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
1643 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
1644 }
1645
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001646 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "resizeDockedStack");
Riddle Hsua0022cd2019-09-09 21:12:41 +08001647 mService.deferWindowLayout();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001648 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001649 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
1650 mAllowDockedStackResize = false;
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001651 ActivityRecord r = stack.topRunningActivity();
Evan Roskydbe2ce52019-07-18 11:13:17 -07001652 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
1653 !PRESERVE_WINDOWS, DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001654
Andrii Kulian69fb5e42016-04-05 16:47:29 -07001655 // TODO: Checking for isAttached might not be needed as if the user passes in null
1656 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08001657 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
1658 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07001659 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08001660 // In this case we make all other static stacks fullscreen and move all
1661 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001662 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001663
1664 // stack shouldn't contain anymore activities, so nothing to resume.
1665 r = null;
1666 } else {
1667 // Docked stacks occupy a dedicated region on screen so the size of all other
1668 // static stacks need to be adjusted so they don't overlap with the docked stack.
1669 // We get the bounds to use from window manager which has been adjusted for any
1670 // screen controls and is also the same for all stacks.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001671 final ActivityDisplay display = mRootActivityContainer.getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08001672 final Rect otherTaskRect = new Rect();
Louis Chang2453d062019-11-19 22:30:48 +08001673 for (int i = display.getStackCount() - 1; i >= 0; --i) {
1674 final ActivityStack current = display.getStackAt(i);
Evan Roskyc5abbd82018-10-05 16:02:19 -07001675 if (!current.inSplitScreenSecondaryWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07001676 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001677 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08001678 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07001679 continue;
1680 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001681 if (mDockedStackResizing && !current.isTopActivityVisible()) {
1682 // Non-visible stacks get resized once we're done with the resize
1683 // interaction.
1684 continue;
1685 }
Evan Roskyc5abbd82018-10-05 16:02:19 -07001686 current.getStackDockedModeBounds(dockedBounds,
Wale Ogunwale926aade2017-08-29 11:24:37 -07001687 tempOtherTaskBounds /* currentTempTaskBounds */,
1688 tempRect /* outStackBounds */,
Evan Roskyc5abbd82018-10-05 16:02:19 -07001689 otherTaskRect /* outTempTaskBounds */);
Wale Ogunwale926aade2017-08-29 11:24:37 -07001690
Evan Roskydbe2ce52019-07-18 11:13:17 -07001691 if (tempRect.isEmpty()) {
1692 // If this scenario is hit, it means something is not working right.
1693 // Empty/null bounds implies fullscreen. In the event that this stack
1694 // *should* be fullscreen, its mode should be set explicitly in a form
1695 // of setWindowingMode so that other parts of the system are updated
1696 // properly.
1697 throw new IllegalArgumentException("Trying to set null bounds on a"
1698 + " non-fullscreen stack");
1699 }
1700
1701 current.resize(tempRect,
Wale Ogunwale926aade2017-08-29 11:24:37 -07001702 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
Evan Roskydbe2ce52019-07-18 11:13:17 -07001703 tempOtherTaskInsetBounds, preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001704 }
1705 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001706 if (!deferResume) {
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001707 stack.ensureVisibleActivitiesConfiguration(r, preserveWindows);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001708 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08001709 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001710 mAllowDockedStackResize = true;
Riddle Hsua0022cd2019-09-09 21:12:41 +08001711 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001712 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001713 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08001714 }
1715
Robert Carr0d00c2e2016-02-29 17:45:02 -08001716 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Yunfan Chen279f5582018-12-12 15:24:50 -08001717 // TODO(multi-display): The display containing the stack should be passed in.
1718 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001719 mRootActivityContainer.getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08001720 if (stack == null) {
1721 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
1722 return;
1723 }
Winson Chung19953ca2017-04-11 11:19:23 -07001724
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001725 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "resizePinnedStack");
Riddle Hsua0022cd2019-09-09 21:12:41 +08001726 mService.deferWindowLayout();
Robert Carr0d00c2e2016-02-29 17:45:02 -08001727 try {
Robert Carr7e4c90e2017-02-15 19:52:38 -08001728 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02001729 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
1730 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08001731 insetBounds = tempRect;
1732 insetBounds.top = 0;
1733 insetBounds.left = 0;
1734 insetBounds.right = tempPinnedTaskBounds.width();
1735 insetBounds.bottom = tempPinnedTaskBounds.height();
1736 }
Winson Chung15036ca2018-05-31 15:51:47 -07001737 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
1738 // We have finished the animation into PiP, and are resizing the tasks to match the
1739 // stack bounds, while layouts are deferred, update any task state as a part of
1740 // transitioning it from fullscreen into a floating state.
1741 stack.onPipAnimationEndResize();
1742 }
Evan Roskydbe2ce52019-07-18 11:13:17 -07001743 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds, !PRESERVE_WINDOWS,
1744 !DEFER_RESUME);
Robert Carr0d00c2e2016-02-29 17:45:02 -08001745 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +08001746 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001747 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Robert Carr0d00c2e2016-02-29 17:45:02 -08001748 }
1749 }
1750
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001751 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001752 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07001753 /**
1754 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
1755 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
1756 * that the client receives onStop() before it is reparented. We do this by detaching
1757 * the stack from the display so that it will be considered invisible when
Louis Chang2453d062019-11-19 22:30:48 +08001758 * ensureActivitiesVisible() is called, and all of its activities will be marked
Winson Chung47900652017-04-06 18:44:25 -07001759 * invisible as well and added to the stopping list. After which we process the
1760 * stopping list by handling the idle.
1761 */
Yunfan Chen279f5582018-12-12 15:24:50 -08001762 stack.mForceHidden = true;
Wale Ogunwale076c3b12019-11-20 12:17:22 -08001763 stack.ensureActivitiesVisible(null, 0, PRESERVE_WINDOWS);
Yunfan Chen279f5582018-12-12 15:24:50 -08001764 stack.mForceHidden = false;
Winson Chung47900652017-04-06 18:44:25 -07001765 activityIdleInternalLocked(null, false /* fromTimeout */,
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001766 true /* processPausingActivities */, null /* configuration */);
Winson Chung47900652017-04-06 18:44:25 -07001767
1768 // Move all the tasks to the bottom of the fullscreen stack
Yunfan Chen279f5582018-12-12 15:24:50 -08001769 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08001770 } else {
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001771 final PooledConsumer c = PooledLambda.obtainConsumer(
1772 ActivityStackSupervisor::processRemoveTask, this, PooledLambda.__(Task.class));
1773 stack.forAllTasks(c);
1774 c.recycle();
Winson Chung010927a2016-12-15 16:12:35 -08001775 }
1776 }
1777
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001778 private void processRemoveTask(Task task) {
1779 removeTask(task, true /* killProcess */, REMOVE_FROM_RECENTS, "remove-stack");
1780 }
1781
Winson Chung010927a2016-12-15 16:12:35 -08001782 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001783 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07001784 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
1785 * instead moved back onto the fullscreen stack.
1786 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001787 void removeStack(ActivityStack stack) {
1788 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001789 }
1790
Wale Ogunwale68278562017-09-23 17:13:55 -07001791 /**
Winson Chung010927a2016-12-15 16:12:35 -08001792 * Removes the task with the specified task id.
1793 *
1794 * @param taskId Identifier of the task to be removed.
1795 * @param killProcess Kill any process associated with the task if possible.
1796 * @param removeFromRecents Whether to also remove the task from recents.
1797 * @return Returns true if the given task was found and removed.
1798 */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001799 boolean removeTaskById(int taskId, boolean killProcess, boolean removeFromRecents,
Louis Chang7b03ad92019-08-21 12:32:33 +08001800 String reason) {
Louis Changcdec0802019-11-11 11:45:07 +08001801 final Task task =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001802 mRootActivityContainer.anyTaskForId(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +08001803 if (task != null) {
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001804 removeTask(task, killProcess, removeFromRecents, reason);
Winson Chung010927a2016-12-15 16:12:35 -08001805 return true;
1806 }
1807 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
1808 return false;
1809 }
1810
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001811 void removeTask(Task task, boolean killProcess, boolean removeFromRecents, String reason) {
1812 task.removeTaskActivitiesLocked(reason);
1813 cleanUpRemovedTaskLocked(task, killProcess, removeFromRecents);
1814 mService.getLockTaskController().clearLockedTask(task);
1815 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
1816 if (task.isPersistable) {
1817 mService.notifyTaskPersisterLocked(null, true);
1818 }
1819 }
1820
Louis Changcdec0802019-11-11 11:45:07 +08001821 void cleanUpRemovedTaskLocked(Task task, boolean killProcess, boolean removeFromRecents) {
Winson Chung010927a2016-12-15 16:12:35 -08001822 if (removeFromRecents) {
Louis Changcdec0802019-11-11 11:45:07 +08001823 mRecentTasks.remove(task);
Winson Chung010927a2016-12-15 16:12:35 -08001824 }
Louis Changcdec0802019-11-11 11:45:07 +08001825 ComponentName component = task.getBaseIntent().getComponent();
Winson Chung010927a2016-12-15 16:12:35 -08001826 if (component == null) {
Louis Changcdec0802019-11-11 11:45:07 +08001827 Slog.w(TAG, "No component for base intent of task: " + task);
Winson Chung010927a2016-12-15 16:12:35 -08001828 return;
1829 }
1830
1831 // Find any running services associated with this app and stop if needed.
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001832 final Message msg = PooledLambda.obtainMessage(ActivityManagerInternal::cleanUpServices,
Louis Changcdec0802019-11-11 11:45:07 +08001833 mService.mAmInternal, task.mUserId, component, new Intent(task.getBaseIntent()));
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001834 mService.mH.sendMessage(msg);
Winson Chung010927a2016-12-15 16:12:35 -08001835
1836 if (!killProcess) {
1837 return;
1838 }
1839
1840 // Determine if the process(es) for this task should be killed.
1841 final String pkg = component.getPackageName();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001842 ArrayList<Object> procsToKill = new ArrayList<>();
1843 ArrayMap<String, SparseArray<WindowProcessController>> pmap =
1844 mService.mProcessNames.getMap();
Winson Chung010927a2016-12-15 16:12:35 -08001845 for (int i = 0; i < pmap.size(); i++) {
1846
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001847 SparseArray<WindowProcessController> uids = pmap.valueAt(i);
Winson Chung010927a2016-12-15 16:12:35 -08001848 for (int j = 0; j < uids.size(); j++) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001849 WindowProcessController proc = uids.valueAt(j);
Louis Changcdec0802019-11-11 11:45:07 +08001850 if (proc.mUserId != task.mUserId) {
Winson Chung010927a2016-12-15 16:12:35 -08001851 // Don't kill process for a different user.
1852 continue;
1853 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001854 if (proc == mService.mHomeProcess) {
Winson Chung010927a2016-12-15 16:12:35 -08001855 // Don't kill the home process along with tasks from the same package.
1856 continue;
1857 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001858 if (!proc.mPkgList.contains(pkg)) {
Winson Chung010927a2016-12-15 16:12:35 -08001859 // Don't kill process that is not associated with this task.
1860 continue;
1861 }
1862
Louis Changcdec0802019-11-11 11:45:07 +08001863 if (!proc.shouldKillProcessForRemovedTask(task)) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001864 // Don't kill process(es) that has an activity in a different task that is also
Yunfan Chenafc15832018-07-26 16:34:28 +09001865 // in recents, or has an activity not stopped.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001866 return;
Winson Chung010927a2016-12-15 16:12:35 -08001867 }
1868
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001869 if (proc.hasForegroundServices()) {
Winson Chung010927a2016-12-15 16:12:35 -08001870 // Don't kill process(es) with foreground service.
1871 return;
1872 }
1873
1874 // Add process to kill list.
1875 procsToKill.add(proc);
1876 }
1877 }
1878
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001879 // Kill the running processes. Post on handle since we don't want to hold the service lock
1880 // while calling into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -07001881 final Message m = PooledLambda.obtainMessage(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001882 ActivityManagerInternal::killProcessesForRemovedTask, mService.mAmInternal,
1883 procsToKill);
Yohei Yukawae2fa39e2018-09-22 13:13:10 -07001884 mService.mH.sendMessage(m);
Winson Chung010927a2016-12-15 16:12:35 -08001885 }
1886
Chong Zhang5dcb2752015-08-18 13:50:26 -07001887 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07001888 * Called to restore the state of the task into the stack that it's supposed to go into.
1889 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07001890 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001891 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07001892 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001893 * @return true if the task has been restored successfully.
1894 */
Louis Changcdec0802019-11-11 11:45:07 +08001895 boolean restoreRecentTaskLocked(Task task, ActivityOptions aOptions, boolean onTop) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001896 final ActivityStack stack =
1897 mRootActivityContainer.getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001898 final ActivityStack currentStack = task.getStack();
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001899
1900 if (currentStack == stack) {
1901 // Nothing else to do since it is already restored in the right stack.
1902 return true;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001903 }
1904
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001905 if (currentStack != null) {
1906 // Task has already been restored once. Just re-parent it to the new stack.
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07001907 task.reparent(stack, POSITION_TOP, true /*moveParents*/, "restoreRecentTaskLocked");
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001908 return true;
1909 }
1910
1911 stack.addChild(task, onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001912 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
1913 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08001914 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07001915 }
1916
Winson Chung1dbc8112017-09-28 18:05:31 -07001917 @Override
Louis Changcdec0802019-11-11 11:45:07 +08001918 public void onRecentTaskAdded(Task task) {
Winson Chung1dbc8112017-09-28 18:05:31 -07001919 task.touchActiveTime();
1920 }
1921
1922 @Override
Louis Changcdec0802019-11-11 11:45:07 +08001923 public void onRecentTaskRemoved(Task task, boolean wasTrimmed, boolean killProcess) {
Winson Chung0ec2a352017-10-26 11:38:30 -07001924 if (wasTrimmed) {
1925 // Task was trimmed from the recent tasks list -- remove the active task record as well
1926 // since the user won't really be able to go back to it
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001927 removeTaskById(task.mTaskId, killProcess, false /* removeFromRecents */,
Louis Chang7b03ad92019-08-21 12:32:33 +08001928 "recent-task-trimmed");
Winson Chung0ec2a352017-10-26 11:38:30 -07001929 }
Winson Chung1dbc8112017-09-28 18:05:31 -07001930 task.removedFromRecents();
1931 }
1932
Wale Ogunwale040b4702015-08-06 18:10:50 -07001933 /**
Winson Chung74666102017-02-22 17:49:24 -08001934 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
1935 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07001936 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001937 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Louis Changcdec0802019-11-11 11:45:07 +08001938 ActivityStack getReparentTargetStack(Task task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001939 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001940 final int stackId = stack.mStackId;
1941 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07001942
Winson Chung74666102017-02-22 17:49:24 -08001943 // Check that we aren't reparenting to the same stack that the task is already in
1944 if (prevStack != null && prevStack.mStackId == stackId) {
1945 Slog.w(TAG, "Can not reparent to same stack, task=" + task
1946 + " already in stackId=" + stackId);
1947 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08001948 }
1949
Winson Chung74666102017-02-22 17:49:24 -08001950 // Ensure that we aren't trying to move into a multi-window stack without multi-window
1951 // support
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001952 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08001953 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001954 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08001955 }
1956
Andrii Kulianeafd9db2017-04-05 22:01:35 -07001957 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
1958 // multi-display.
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001959 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07001960 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
1961 + " reparent task=" + task + " to stackId=" + stackId);
1962 }
1963
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001964 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001965 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001966 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08001967 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
1968 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08001969 }
1970
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001971 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
1972 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001973 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001974 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
1975 + " Moving to a fullscreen stack instead.");
1976 if (prevStack != null) {
1977 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001978 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001979 stack = stack.getDisplay().createStack(
1980 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001981 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001982 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001983 }
1984
1985 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001986 scheduleSleepTimeout();
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001987 if (!mGoingToSleepWakeLock.isHeld()) {
1988 mGoingToSleepWakeLock.acquire();
1989 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001990 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1991 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07001992 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001993 mLaunchingActivityWakeLock.release();
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001994 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001995 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001996 }
David Stevens9440dc82017-03-16 19:00:20 -07001997
Wale Ogunwaled32da472018-11-16 07:19:28 -08001998 mRootActivityContainer.applySleepTokens(false /* applyToStacks */);
David Stevens9440dc82017-03-16 19:00:20 -07001999
2000 checkReadyForSleepLocked(true /* allowDelay */);
2001 }
2002
Craig Mautner8d341ef2013-03-26 09:03:27 -07002003 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002004 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002005
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002006 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002007 final long endTime = System.currentTimeMillis() + timeout;
2008 while (true) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002009 if (!mRootActivityContainer.putStacksToSleep(
2010 true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002011 long timeRemaining = endTime - System.currentTimeMillis();
2012 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002013 try {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002014 mService.mGlobalLock.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002015 } catch (InterruptedException e) {
2016 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002017 } else {
2018 Slog.w(TAG, "Activity manager shutdown timed out");
2019 timedout = true;
2020 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002021 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002022 } else {
2023 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002024 }
2025 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002026
2027 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07002028 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002029
Craig Mautner8d341ef2013-03-26 09:03:27 -07002030 return timedout;
2031 }
2032
2033 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002034 removeSleepTimeouts();
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002035 if (mGoingToSleepWakeLock.isHeld()) {
2036 mGoingToSleepWakeLock.release();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002037 }
David Stevens9440dc82017-03-16 19:00:20 -07002038 }
2039
Craig Mautner0eea92c2013-05-16 13:35:39 -07002040 void activitySleptLocked(ActivityRecord r) {
2041 mGoingToSleepActivities.remove(r);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002042 final ActivityStack s = r.getActivityStack();
David Stevens9440dc82017-03-16 19:00:20 -07002043 if (s != null) {
2044 s.checkReadyForSleep();
2045 } else {
2046 checkReadyForSleepLocked(true);
2047 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002048 }
2049
David Stevens9440dc82017-03-16 19:00:20 -07002050 void checkReadyForSleepLocked(boolean allowDelay) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002051 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002052 // Do not care.
2053 return;
2054 }
2055
Wale Ogunwaled32da472018-11-16 07:19:28 -08002056 if (!mRootActivityContainer.putStacksToSleep(
2057 allowDelay, false /* shuttingDown */)) {
David Stevens18abd0e2017-08-17 14:55:47 -07002058 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002059 }
2060
Wei Wang65c7a152016-06-02 18:51:22 -07002061 // Send launch end powerhint before going sleep
Wale Ogunwaled32da472018-11-16 07:19:28 -08002062 mRootActivityContainer.sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07002063
Craig Mautner0eea92c2013-05-16 13:35:39 -07002064 removeSleepTimeouts();
2065
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002066 if (mGoingToSleepWakeLock.isHeld()) {
2067 mGoingToSleepWakeLock.release();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002068 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07002069 if (mService.mShuttingDown) {
2070 mService.mGlobalLock.notifyAll();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002071 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002072 }
2073
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002074 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07002075 // A resumed activity cannot be stopping. remove from list
2076 mStoppingActivities.remove(r);
2077
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002078 final ActivityStack stack = r.getActivityStack();
Riddle Hsubbb63c22018-10-03 12:28:29 +08002079 if (stack.getDisplay().allResumedActivitiesComplete()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002080 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
lumark588a3e82018-07-20 18:53:54 +08002081 // Make sure activity & window visibility should be identical
2082 // for all displays in this stage.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002083 mRootActivityContainer.executeAppTransitionForAllDisplay();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002084 return true;
2085 }
2086 return false;
2087 }
2088
Craig Mautnerbb742462014-07-07 15:28:55 -07002089 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002090 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Louis Changcdec0802019-11-11 11:45:07 +08002091 final Task task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002092 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002093
Winson Chung1dbc8112017-09-28 18:05:31 -07002094 mRecentTasks.add(task);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002095 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
Wale Ogunwale076c3b12019-11-20 12:17:22 -08002096 stack.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Winson730bf062016-03-31 18:04:56 -07002097
2098 // When launching tasks behind, update the last active time of the top task after the new
2099 // task has been shown briefly
Wale Ogunwale21e06482019-11-18 05:14:15 -08002100 final ActivityRecord top = stack.getTopNonFinishingActivity();
Winson730bf062016-03-31 18:04:56 -07002101 if (top != null) {
Louis Changcdec0802019-11-11 11:45:07 +08002102 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07002103 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002104 }
2105
2106 void scheduleLaunchTaskBehindComplete(IBinder token) {
2107 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2108 }
2109
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002110 /** Checks whether the userid is a profile of the current user. */
2111 boolean isCurrentProfileLocked(int userId) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002112 if (userId == mRootActivityContainer.mCurrentUser) return true;
Wale Ogunwale86b74462018-07-02 08:42:43 -07002113 return mService.mAmInternal.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002114 }
2115
Bryce Leeb7c9b802017-05-02 14:20:24 -07002116 /**
2117 * Returns whether a stopping activity is present that should be stopped after visible, rather
2118 * than idle.
2119 * @return {@code true} if such activity is present. {@code false} otherwise.
2120 */
2121 boolean isStoppingNoHistoryActivity() {
2122 // Activities that are marked as nohistory should be stopped immediately after the resumed
2123 // activity has become visible.
2124 for (ActivityRecord record : mStoppingActivities) {
2125 if (record.isNoHistory()) {
2126 return true;
2127 }
2128 }
2129
2130 return false;
2131 }
2132
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002133 // TODO: Change method name to reflect what it actually does.
Winson Chung4dabf232017-01-25 13:25:22 -08002134 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
2135 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002136 ArrayList<ActivityRecord> stops = null;
2137
Wale Ogunwaled32da472018-11-16 07:19:28 -08002138 final boolean nowVisible = mRootActivityContainer.allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002139 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2140 ActivityRecord s = mStoppingActivities.get(activityNdx);
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002141
lumark9bca6b42019-10-17 18:35:22 +08002142 final boolean animating = s.isAnimating(TRANSITION);
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002143
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002144 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002145 + " animating=" + animating + " finishing=" + s.finishing);
2146 if (nowVisible && s.finishing) {
2147
2148 // If this activity is finishing, it is sitting on top of
2149 // everyone else but we now know it is no longer needed...
2150 // so get rid of it. Otherwise, we need to go through the
2151 // normal flow and hide it once we determine that it is
2152 // hidden by the activities in front of it.
2153 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
2154 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002155 }
David Stevens9440dc82017-03-16 19:00:20 -07002156 if (remove) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002157 final ActivityStack stack = s.getActivityStack();
David Stevens9440dc82017-03-16 19:00:20 -07002158 final boolean shouldSleepOrShutDown = stack != null
2159 ? stack.shouldSleepOrShutDownActivities()
Wale Ogunwalef6733932018-06-27 05:14:34 -07002160 : mService.isSleepingOrShuttingDownLocked();
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002161 if (!animating || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08002162 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07002163 // Defer processing pausing activities in this iteration and reschedule
2164 // a delayed idle to reprocess it again
2165 removeTimeoutsForActivityLocked(idleActivity);
2166 scheduleIdleTimeoutLocked(idleActivity);
2167 continue;
2168 }
Winson Chung4dabf232017-01-25 13:25:22 -08002169
David Stevens9440dc82017-03-16 19:00:20 -07002170 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
2171 if (stops == null) {
2172 stops = new ArrayList<>();
2173 }
2174 stops.add(s);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02002175
David Stevens9440dc82017-03-16 19:00:20 -07002176 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002177 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002178 }
2179 }
2180
2181 return stops;
2182 }
2183
Craig Mautner27084302013-03-25 08:05:25 -07002184 public void dump(PrintWriter pw, String prefix) {
Riddle Hsu75016992018-09-20 20:37:14 +08002185 pw.println();
2186 pw.println("ActivityStackSupervisor state:");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002187 mRootActivityContainer.dump(pw, prefix);
Riddle Hsu75016992018-09-20 20:37:14 +08002188 pw.print(prefix);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002189 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Wale Ogunwaled32da472018-11-16 07:19:28 -08002190 pw.println(prefix + "mUserStackInFront=" + mRootActivityContainer.mUserStackInFront);
Bryce Lee4a194382017-04-04 14:32:48 -07002191 if (!mWaitingForActivityVisible.isEmpty()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002192 pw.println(prefix + "mWaitingForActivityVisible=");
Bryce Lee4a194382017-04-04 14:32:48 -07002193 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002194 pw.print(prefix + prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
Bryce Lee4a194382017-04-04 14:32:48 -07002195 }
2196 }
Winson Chungc1674272018-02-21 10:15:17 -08002197 pw.print(prefix); pw.print("isHomeRecentsComponent=");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002198 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mRootActivityContainer.mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07002199
Bryce Lee459c0622018-03-19 11:04:01 -07002200 getKeyguardController().dump(pw, prefix);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002201 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07002202 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002203
Dianne Hackborn390517b2013-05-30 15:03:32 -07002204 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2205 boolean needSep, String prefix) {
2206 if (activity != null) {
2207 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2208 if (needSep) {
2209 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002210 }
2211 pw.print(prefix);
2212 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002213 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002214 }
2215 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002216 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002217 }
2218
Dianne Hackborn390517b2013-05-30 15:03:32 -07002219 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002220 String prefix, String label, boolean complete, boolean brief, boolean client,
Louis Changcdec0802019-11-11 11:45:07 +08002221 String dumpPackage, boolean needNL, String header, Task lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002222 String innerPrefix = null;
2223 String[] args = null;
2224 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002225 for (int i=list.size()-1; i>=0; i--) {
2226 final ActivityRecord r = list.get(i);
2227 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2228 continue;
2229 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002230 if (innerPrefix == null) {
2231 innerPrefix = prefix + " ";
2232 args = new String[0];
2233 }
2234 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002235 final boolean full = !brief && (complete || !r.isInHistory());
2236 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002237 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002238 needNL = false;
2239 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07002240 if (header != null) {
2241 pw.println(header);
2242 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002243 }
Louis Changcdec0802019-11-11 11:45:07 +08002244 if (lastTask != r.getTask()) {
2245 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002246 pw.print(prefix);
2247 pw.print(full ? "* " : " ");
2248 pw.println(lastTask);
2249 if (full) {
2250 lastTask.dump(pw, prefix + " ");
2251 } else if (complete) {
2252 // Complete + brief == give a summary. Isn't that obvious?!?
2253 if (lastTask.intent != null) {
2254 pw.print(prefix); pw.print(" ");
Hui Yu6d5c3b92019-10-22 15:35:53 -07002255 pw.println(lastTask.intent.toInsecureString());
Craig Mautner8d341ef2013-03-26 09:03:27 -07002256 }
2257 }
2258 }
2259 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2260 pw.print(" #"); pw.print(i); pw.print(": ");
2261 pw.println(r);
2262 if (full) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07002263 r.dump(pw, innerPrefix, true /* dumpAll */);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002264 } else if (complete) {
2265 // Complete + brief == give a summary. Isn't that obvious?!?
2266 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2267 if (r.app != null) {
2268 pw.print(innerPrefix); pw.println(r.app);
2269 }
2270 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002271 if (client && r.attachedToProcess()) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002272 // flush anything that is already in the PrintWriter since the thread is going
2273 // to write to the file descriptor directly
2274 pw.flush();
2275 try {
2276 TransferPipe tp = new TransferPipe();
2277 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002278 r.app.getThread().dumpActivity(
2279 tp.getWriteFd(), r.appToken, innerPrefix, args);
2280 // Short timeout, since blocking here can deadlock with the application.
Craig Mautner8d341ef2013-03-26 09:03:27 -07002281 tp.go(fd, 2000);
2282 } finally {
2283 tp.kill();
2284 }
2285 } catch (IOException e) {
2286 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2287 } catch (RemoteException e) {
2288 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2289 }
2290 needNL = true;
2291 }
2292 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002293 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002294 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002295
Craig Mautnerf3333272013-04-22 10:55:53 -07002296 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002297 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2298 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002299 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2300 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002301 }
2302
2303 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002304 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002305 }
2306
Andrii Kulian86e70fc2019-02-12 11:04:10 +00002307 /**
2308 * Updates the record of top resumed activity when it changes and handles reporting of the
2309 * state changes to previous and new top activities. It will immediately dispatch top resumed
2310 * state loss message to previous top activity (if haven't done it already). After the previous
2311 * activity releases the top state and reports back, message about acquiring top state will be
2312 * sent to the new top resumed activity.
2313 */
2314 void updateTopResumedActivityIfNeeded() {
2315 final ActivityRecord prevTopActivity = mTopResumedActivity;
2316 final ActivityStack topStack = mRootActivityContainer.getTopDisplayFocusedStack();
2317 if (topStack == null || topStack.mResumedActivity == prevTopActivity) {
2318 return;
2319 }
2320
2321 // Ask previous activity to release the top state.
2322 final boolean prevActivityReceivedTopState =
2323 prevTopActivity != null && !mTopResumedActivityWaitingForPrev;
2324 // mTopResumedActivityWaitingForPrev == true at this point would mean that an activity
2325 // before the prevTopActivity one hasn't reported back yet. So server never sent the top
2326 // resumed state change message to prevTopActivity.
Louis Chang3b21bdc2019-03-25 15:49:14 +08002327 if (prevActivityReceivedTopState
2328 && prevTopActivity.scheduleTopResumedActivityChanged(false /* onTop */)) {
Andrii Kulian86e70fc2019-02-12 11:04:10 +00002329 scheduleTopResumedStateLossTimeout(prevTopActivity);
2330 mTopResumedActivityWaitingForPrev = true;
2331 }
2332
2333 // Update the current top activity.
2334 mTopResumedActivity = topStack.mResumedActivity;
2335 scheduleTopResumedActivityStateIfNeeded();
2336 }
2337
2338 /** Schedule top resumed state change if previous top activity already reported back. */
2339 private void scheduleTopResumedActivityStateIfNeeded() {
2340 if (mTopResumedActivity != null && !mTopResumedActivityWaitingForPrev) {
2341 mTopResumedActivity.scheduleTopResumedActivityChanged(true /* onTop */);
2342 }
2343 }
2344
2345 /**
2346 * Limit the time given to the app to report handling of the state loss.
2347 */
2348 private void scheduleTopResumedStateLossTimeout(ActivityRecord r) {
2349 final Message msg = mHandler.obtainMessage(TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG);
2350 msg.obj = r;
2351 r.topResumedStateLossTime = SystemClock.uptimeMillis();
2352 mHandler.sendMessageDelayed(msg, TOP_RESUMED_STATE_LOSS_TIMEOUT);
2353 if (DEBUG_STATES) Slog.v(TAG_STATES, "Waiting for top state to be released by " + r);
2354 }
2355
2356 /**
2357 * Handle a loss of top resumed state by an activity - update internal state and inform next top
2358 * activity if needed.
2359 */
2360 void handleTopResumedStateReleased(boolean timeout) {
2361 if (DEBUG_STATES) {
2362 Slog.v(TAG_STATES, "Top resumed state released "
2363 + (timeout ? " (due to timeout)" : " (transition complete)"));
2364 }
2365 mHandler.removeMessages(TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG);
2366 if (!mTopResumedActivityWaitingForPrev) {
2367 // Top resumed activity state loss already handled.
2368 return;
2369 }
2370 mTopResumedActivityWaitingForPrev = false;
2371 scheduleTopResumedActivityStateIfNeeded();
2372 }
2373
Craig Mautnerf3333272013-04-22 10:55:53 -07002374 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002375 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2376 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2378 }
2379
Craig Mautner05d29032013-05-03 13:40:13 -07002380 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002381 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2382 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2383 }
Craig Mautner05d29032013-05-03 13:40:13 -07002384 }
2385
Craig Mautner0eea92c2013-05-16 13:35:39 -07002386 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002387 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2388 }
2389
2390 final void scheduleSleepTimeout() {
2391 removeSleepTimeouts();
2392 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2393 }
2394
Riddle Hsu7b766fd2019-01-28 21:14:59 +08002395 void removeRestartTimeouts(ActivityRecord r) {
2396 mHandler.removeMessages(RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG, r);
2397 }
2398
2399 final void scheduleRestartTimeout(ActivityRecord r) {
2400 removeRestartTimeouts(r);
2401 mHandler.sendMessageDelayed(mHandler.obtainMessage(RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG, r),
2402 WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION);
2403 }
2404
Louis Changcdec0802019-11-11 11:45:07 +08002405 void handleNonResizableTaskIfNeeded(Task task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002406 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002407 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002408 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07002409 }
2410
Louis Changcdec0802019-11-11 11:45:07 +08002411 void handleNonResizableTaskIfNeeded(Task task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002412 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002413 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002414 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002415 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002416 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002417 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002418 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002419 return;
2420 }
2421
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002422 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002423 if (isSecondaryDisplayPreferred) {
2424 final int actualDisplayId = task.getStack().mDisplayId;
2425 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08002426 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002427 }
Jeff Chang78f54412019-02-14 20:42:27 +08002428
2429 final ActivityDisplay preferredDisplay =
2430 mRootActivityContainer.getActivityDisplay(preferredDisplayId);
2431
2432 final boolean singleTaskInstance = preferredDisplay != null
2433 && preferredDisplay.isSingleTaskInstance();
2434
Mark Renouf2f9ea1e2019-03-05 14:49:06 -05002435 if (preferredDisplayId != actualDisplayId) {
Jeff Chang78f54412019-02-14 20:42:27 +08002436 // Suppress the warning toast if the preferredDisplay was set to singleTask.
2437 // The singleTaskInstance displays will only contain one task and any attempt to
2438 // launch new task will re-route to the default display.
Mark Renouf2f9ea1e2019-03-05 14:49:06 -05002439 if (singleTaskInstance) {
2440 mService.getTaskChangeNotificationController()
2441 .notifyActivityLaunchOnSecondaryDisplayRerouted(task.getTaskInfo(),
2442 preferredDisplayId);
2443 return;
2444 }
Jeff Chang78f54412019-02-14 20:42:27 +08002445
Riddle Hsu16567132018-08-16 21:37:47 +08002446 Slog.w(TAG, "Failed to put " + task + " on display " + preferredDisplayId);
2447 // Display a warning toast that we failed to put a task on a secondary display.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002448 mService.getTaskChangeNotificationController()
Mark Renoufc808f062019-02-07 15:20:37 -05002449 .notifyActivityLaunchOnSecondaryDisplayFailed(task.getTaskInfo(),
2450 preferredDisplayId);
Riddle Hsubed3db42019-03-15 20:26:27 +08002451 } else if (!forceNonResizable) {
2452 handleForcedResizableTaskIfNeeded(task, FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002453 }
Riddle Hsubed3db42019-03-15 20:26:27 +08002454 // The information about not support secondary display should already be notified, we
2455 // don't want to show another message on default display about split-screen. And it may
2456 // be the case that a resizable activity is launched on a non-resizable task.
2457 return;
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002458 }
2459
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002460 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulianc27916642016-04-12 17:59:27 -07002461 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
2462 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002463
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002464 final ActivityStack dockedStack =
2465 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002466 if (dockedStack != null) {
Riddle Hsubed3db42019-03-15 20:26:27 +08002467 // Display a warning toast that we tried to put an app that doesn't support
2468 // split-screen in split-screen.
2469 mService.getTaskChangeNotificationController()
2470 .notifyActivityDismissingDockedStack();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002471 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002472 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002473 return;
2474 }
2475
Riddle Hsubed3db42019-03-15 20:26:27 +08002476 handleForcedResizableTaskIfNeeded(task, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN);
Riddle Hsu16567132018-08-16 21:37:47 +08002477 }
2478
Riddle Hsubed3db42019-03-15 20:26:27 +08002479 /** Notifies that the top activity of the task is forced to be resizeable. */
Louis Changcdec0802019-11-11 11:45:07 +08002480 private void handleForcedResizableTaskIfNeeded(Task task, int reason) {
Wale Ogunwale21e06482019-11-18 05:14:15 -08002481 final ActivityRecord topActivity = task.getTopNonFinishingActivity();
Riddle Hsubed3db42019-03-15 20:26:27 +08002482 if (topActivity == null || topActivity.noDisplay
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002483 || !topActivity.isNonResizableOrForcedResizable(task.getWindowingMode())) {
Riddle Hsubed3db42019-03-15 20:26:27 +08002484 return;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002485 }
Riddle Hsubed3db42019-03-15 20:26:27 +08002486 mService.getTaskChangeNotificationController().notifyActivityForcedResizable(
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002487 task.mTaskId, reason, topActivity.info.applicationInfo.packageName);
Chong Zhangb15758a2015-11-17 12:12:03 -08002488 }
2489
Jorim Jaggife89d122015-12-22 16:28:44 +01002490 void activityRelaunchedLocked(IBinder token) {
David Stevens9440dc82017-03-16 19:00:20 -07002491 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2492 if (r != null) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002493 r.finishRelaunching();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002494 if (r.getActivityStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07002495 r.setSleeping(true, true);
2496 }
2497 }
Jorim Jaggife89d122015-12-22 16:28:44 +01002498 }
2499
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002500 void logStackState() {
2501 mActivityMetricsLogger.logWindowState();
2502 }
2503
Louis Changcdec0802019-11-11 11:45:07 +08002504 void scheduleUpdateMultiWindowMode(Task task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07002505 // If the stack is animating in a way where we will be forcing a multi-mode change at the
2506 // end, then ensure that we defer all in between multi-window mode changes
2507 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
2508 return;
2509 }
2510
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002511 final PooledConsumer c = PooledLambda.obtainConsumer(
2512 ActivityStackSupervisor::addToMultiWindowModeChangedList, this,
2513 PooledLambda.__(ActivityRecord.class));
2514 task.forAllActivities(c);
2515 c.recycle();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002516
2517 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
2518 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
2519 }
2520 }
2521
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002522 private void addToMultiWindowModeChangedList(ActivityRecord r) {
2523 if (r.attachedToProcess()) {
2524 mMultiWindowModeChangedActivities.add(r);
2525 }
2526 }
2527
Louis Changcdec0802019-11-11 11:45:07 +08002528 void scheduleUpdatePictureInPictureModeIfNeeded(Task task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002529 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002530 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002531 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002532 return;
2533 }
2534
Evan Roskydfe3da72018-10-26 17:21:06 -07002535 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getRequestedOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07002536 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08002537
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002538 private void scheduleUpdatePictureInPictureModeIfNeeded(Task task, Rect targetStackBounds) {
2539 final PooledConsumer c = PooledLambda.obtainConsumer(
2540 ActivityStackSupervisor::addToPipModeChangedList, this,
2541 PooledLambda.__(ActivityRecord.class));
2542 task.forAllActivities(c);
2543 c.recycle();
2544
Winson Chungab76bbc2017-08-14 13:33:51 -07002545 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07002546
Winson Chungab76bbc2017-08-14 13:33:51 -07002547 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
2548 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
2549 }
2550 }
2551
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002552 private void addToPipModeChangedList(ActivityRecord r) {
2553 if (!r.attachedToProcess()) return;
2554
2555 mPipModeChangedActivities.add(r);
2556 // If we are scheduling pip change, then remove this activity from multi-window
2557 // change list as the processing of pip change will make sure multi-window changed
2558 // message is processed in the right order relative to pip changed.
2559 mMultiWindowModeChangedActivities.remove(r);
2560 }
2561
Louis Changcdec0802019-11-11 11:45:07 +08002562 void updatePictureInPictureMode(Task task, Rect targetStackBounds, boolean forceUpdate) {
Winson Chungab76bbc2017-08-14 13:33:51 -07002563 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002564 final PooledConsumer c = PooledLambda.obtainConsumer(
2565 ActivityRecord::updatePictureInPictureMode,
2566 PooledLambda.__(ActivityRecord.class), targetStackBounds, forceUpdate);
2567 task.forAllActivities(c);
2568 c.recycle();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002569 }
2570
chaviw59b98852017-06-13 12:05:44 -07002571 void wakeUp(String reason) {
Michael Wrighte3001042019-02-05 00:13:14 +00002572 mPowerManager.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_APPLICATION,
2573 "android.server.am:TURN_ON:" + reason);
chaviw59b98852017-06-13 12:05:44 -07002574 }
2575
2576 /**
2577 * Begin deferring resume to avoid duplicate resumes in one pass.
2578 */
Riddle Hsu402b4402018-11-06 17:23:15 +08002579 void beginDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07002580 mDeferResumeCount++;
2581 }
2582
2583 /**
2584 * End deferring resume and determine if resume can be called.
2585 */
Riddle Hsu402b4402018-11-06 17:23:15 +08002586 void endDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07002587 mDeferResumeCount--;
2588 }
2589
Wale Ogunwaled32da472018-11-16 07:19:28 -08002590 /** @return True if resume can be called. */
2591 boolean readyToResume() {
chaviw59b98852017-06-13 12:05:44 -07002592 return mDeferResumeCount == 0;
2593 }
2594
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002595 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002596
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002597 public ActivityStackSupervisorHandler(Looper looper) {
2598 super(looper);
2599 }
2600
Winson Chung4dabf232017-01-25 13:25:22 -08002601 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002602 synchronized (mService.mGlobalLock) {
Winson Chung4dabf232017-01-25 13:25:22 -08002603 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
2604 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002605 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002606 }
2607
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002608 @Override
2609 public void handleMessage(Message msg) {
2610 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002611 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002612 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002613 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
2614 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07002615 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002616 }
2617 }
2618 } break;
2619 case REPORT_PIP_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002620 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002621 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
2622 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07002623 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
2624 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08002625 }
2626 }
2627 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07002628 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002629 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2630 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002631 // We don't at this point know if the activity is fullscreen,
2632 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08002633 activityIdleInternal((ActivityRecord) msg.obj,
2634 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07002635 } break;
2636 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002637 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08002638 activityIdleInternal((ActivityRecord) msg.obj,
2639 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07002640 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07002641 case RESUME_TOP_ACTIVITY_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002642 synchronized (mService.mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002643 mRootActivityContainer.resumeFocusedStacksTopActivities();
Craig Mautner05d29032013-05-03 13:40:13 -07002644 }
2645 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002646 case SLEEP_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002647 synchronized (mService.mGlobalLock) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002648 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002649 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07002650 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002651 }
2652 }
2653 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002654 case LAUNCH_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002655 synchronized (mService.mGlobalLock) {
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002656 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002657 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
2658 if (VALIDATE_WAKE_LOCK_CALLER
2659 && Binder.getCallingUid() != Process.myUid()) {
2660 throw new IllegalStateException("Calling must be system uid");
2661 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002662 mLaunchingActivityWakeLock.release();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002663 }
2664 }
2665 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07002666 case LAUNCH_TASK_BEHIND_COMPLETE: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002667 synchronized (mService.mGlobalLock) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002668 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07002669 if (r != null) {
2670 handleLaunchTaskBehindCompleteLocked(r);
2671 }
2672 }
2673 } break;
Riddle Hsu7b766fd2019-01-28 21:14:59 +08002674 case RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG: {
2675 final ActivityRecord r = (ActivityRecord) msg.obj;
2676 String processName = null;
2677 int uid = 0;
2678 synchronized (mService.mGlobalLock) {
2679 if (r.attachedToProcess()
2680 && r.isState(ActivityStack.ActivityState.RESTARTING_PROCESS)) {
2681 processName = r.app.mName;
2682 uid = r.app.mUid;
2683 }
2684 }
2685 if (processName != null) {
2686 mService.mAmInternal.killProcess(processName, uid,
2687 "restartActivityProcessTimeout");
2688 }
2689 } break;
Louis Changdcdde952018-12-04 15:38:44 +08002690 case REPORT_HOME_CHANGED_MSG: {
2691 synchronized (mService.mGlobalLock) {
2692 mHandler.removeMessages(REPORT_HOME_CHANGED_MSG);
Chong Zhangc806d902015-11-30 09:44:27 -08002693
Louis Changdcdde952018-12-04 15:38:44 +08002694 // Start home activities on displays with no activities.
2695 mRootActivityContainer.startHomeOnEmptyDisplays("homeChanged");
2696 }
Andrii Kulian86e70fc2019-02-12 11:04:10 +00002697 } break;
2698 case TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG: {
2699 ActivityRecord r = (ActivityRecord) msg.obj;
2700 Slog.w(TAG, "Activity top resumed state loss timeout for " + r);
2701 synchronized (mService.mGlobalLock) {
2702 if (r.hasProcess()) {
2703 mService.logAppTooSlow(r.app, r.topResumedStateLossTime,
2704 "top state loss for " + r);
2705 }
2706 }
2707 handleTopResumedStateReleased(true /* timeout */);
2708 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002709 }
2710 }
2711 }
Craig Mautnered6649f2013-12-02 14:08:25 -08002712
Jorim Jaggic69bd222016-03-15 14:38:37 +01002713 /**
2714 * Puts a task into resizing mode during the next app transition.
2715 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002716 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01002717 */
Louis Changcdec0802019-11-11 11:45:07 +08002718 void setResizingDuringAnimation(Task task) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002719 mResizingTasksDuringAnimation.add(task.mTaskId);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002720 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01002721 }
2722
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002723 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
2724 SafeActivityOptions options) {
Louis Changcdec0802019-11-11 11:45:07 +08002725 Task task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002726 final String callingPackage;
2727 final Intent intent;
2728 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002729 int activityType = ACTIVITY_TYPE_UNDEFINED;
2730 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002731 final ActivityOptions activityOptions = options != null
2732 ? options.getOptions(this)
2733 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002734 if (activityOptions != null) {
2735 activityType = activityOptions.getLaunchActivityType();
2736 windowingMode = activityOptions.getLaunchWindowingMode();
Winson Chungcdd4c3f2019-04-17 15:40:11 -07002737 if (activityOptions.freezeRecentTasksReordering()
2738 && mRecentTasks.isCallerRecents(callingUid)) {
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002739 mRecentTasks.setFreezeTaskListReordering();
2740 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002741 }
2742 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002743 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002744 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002745 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07002746
Riddle Hsua0022cd2019-09-09 21:12:41 +08002747 mService.deferWindowLayout();
Matthew Ng606dd802017-06-05 14:06:32 -07002748 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002749 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale83b8a6b2019-06-27 20:15:15 -07002750 mWindowManager.setDockedStackCreateStateLocked(
Matthew Ngbf155872017-10-27 15:24:39 -07002751 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07002752
Matthew Ng606dd802017-06-05 14:06:32 -07002753 // Defer updating the stack in which recents is until the app transition is done, to
2754 // not run into issues where we still need to draw the task in recents but the
2755 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08002756 deferUpdateRecentsHomeStackBounds();
lumark588a3e82018-07-20 18:53:54 +08002757 // TODO(multi-display): currently recents animation only support default display.
Matthew Ng606dd802017-06-05 14:06:32 -07002758 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002759 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002760
Wale Ogunwaled32da472018-11-16 07:19:28 -08002761 task = mRootActivityContainer.anyTaskForId(taskId,
2762 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07002763 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08002764 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07002765 mWindowManager.executeAppTransition();
2766 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002767 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07002768 }
2769
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08002770 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
2771 // We always want to return to the home activity instead of the recents activity
2772 // from whatever is started from the recents activity, so move the home stack
2773 // forward.
Louis Changbd48dca2018-08-29 17:44:34 +08002774 // TODO (b/115289124): Multi-display supports for recents.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002775 mRootActivityContainer.getDefaultDisplay().moveHomeStackToFront(
2776 "startActivityFromRecents");
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08002777 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002778
Matthew Ng606dd802017-06-05 14:06:32 -07002779 // If the user must confirm credentials (e.g. when first launching a work app and the
2780 // Work Challenge is present) let startActivityInPackage handle the intercepting.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002781 if (!mService.mAmInternal.shouldConfirmCredentials(task.mUserId)
Matthew Ng606dd802017-06-05 14:06:32 -07002782 && task.getRootActivity() != null) {
Wale Ogunwale21e06482019-11-18 05:14:15 -08002783 final ActivityRecord targetActivity = task.getTopNonFinishingActivity();
Bryce Lee28d80422017-07-21 13:25:13 -07002784
Wale Ogunwaled32da472018-11-16 07:19:28 -08002785 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(
2786 true /* forceSend */, targetActivity);
Riddle Hsufd66d4d2019-11-14 10:35:55 +08002787 final LaunchingState launchingState =
2788 mActivityMetricsLogger.notifyActivityLaunching(task.intent);
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002789 try {
Ricky Waiaca8a772019-04-04 16:01:06 +01002790 mService.moveTaskToFrontLocked(null /* appThread */, null /* callingPackage */,
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002791 task.mTaskId, 0, options, true /* fromRecents */);
lumark5e8aff42019-01-11 22:22:59 +08002792 // Apply options to prevent pendingOptions be taken by client to make sure
2793 // the override pending app transition will be applied immediately.
2794 targetActivity.applyOptionsLocked();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002795 } finally {
Riddle Hsufd66d4d2019-11-14 10:35:55 +08002796 mActivityMetricsLogger.notifyActivityLaunched(launchingState,
2797 START_TASK_TO_FRONT, targetActivity);
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002798 }
Matthew Ng606dd802017-06-05 14:06:32 -07002799
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002800 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
Wale Ogunwale21e06482019-11-18 05:14:15 -08002801 task.getTopNonFinishingActivity(), ActivityManager.START_TASK_TO_FRONT,
Bryce Leed3624e12017-11-30 08:51:45 -08002802 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07002803 return ActivityManager.START_TASK_TO_FRONT;
2804 }
Matthew Ng606dd802017-06-05 14:06:32 -07002805 callingPackage = task.mCallingPackage;
2806 intent = task.intent;
2807 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002808 userId = task.mUserId;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002809 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002810 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07002811 null, 0, 0, options, userId, task, "startActivityFromRecents",
Michal Karpinskiac116df2018-12-10 17:51:42 +00002812 false /* validateIncomingUser */, null /* originatingPendingIntent */,
2813 false /* allowBackgroundActivityStart */);
Matthew Ng606dd802017-06-05 14:06:32 -07002814 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002815 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
2816 // If we are launching the task in the docked stack, put it into resizing mode so
2817 // the window renders full-screen with the background filling the void. Also only
2818 // call this at the end to make sure that tasks exists on the window manager side.
2819 setResizingDuringAnimation(task);
2820
2821 final ActivityDisplay display = task.getStack().getDisplay();
2822 final ActivityStack topSecondaryStack =
2823 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2824 if (topSecondaryStack.isActivityTypeHome()) {
Louis Changbd48dca2018-08-29 17:44:34 +08002825 // If the home activity is the top split-screen secondary stack, then the
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002826 // primary split-screen stack is in the minimized mode which means it can't
2827 // receive input keys, so we should move the focused app to the home app so that
2828 // window manager can correctly calculate the focus window that can receive
2829 // input keys.
lumark95f20632019-06-19 16:42:35 +08002830 display.moveHomeActivityToTop(
Louis Changbd48dca2018-08-29 17:44:34 +08002831 "startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08002832
2833 // Immediately update the minimized docked stack mode, the upcoming animation
2834 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
2835 // will do the animation to the target bounds
2836 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002837 }
2838 }
Riddle Hsua0022cd2019-09-09 21:12:41 +08002839 mService.continueWindowLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002840 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002841 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07002842
2843 /**
Bryce Lee4a194382017-04-04 14:32:48 -07002844 * Internal container to store a match qualifier alongside a WaitResult.
2845 */
2846 static class WaitInfo {
2847 private final ComponentName mTargetComponent;
2848 private final WaitResult mResult;
2849
Riddle Hsuc48c8912019-10-31 13:34:27 +08002850 WaitInfo(ComponentName targetComponent, WaitResult result) {
Bryce Lee4a194382017-04-04 14:32:48 -07002851 this.mTargetComponent = targetComponent;
2852 this.mResult = result;
2853 }
2854
Wale Ogunwale3270f172017-04-26 07:29:42 -07002855 public boolean matches(ComponentName targetComponent) {
2856 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07002857 }
2858
2859 public WaitResult getResult() {
2860 return mResult;
2861 }
2862
2863 public ComponentName getComponent() {
2864 return mTargetComponent;
2865 }
2866
2867 public void dump(PrintWriter pw, String prefix) {
2868 pw.println(prefix + "WaitInfo:");
2869 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
2870 pw.println(prefix + " mResult=");
2871 mResult.dump(pw, prefix);
2872 }
2873 }
Craig Mautner27084302013-03-25 08:05:25 -07002874}