blob: 9a5ec2ab17dc04e52e5c4bebd3e5a8e02b7fa9a0 [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;
Benjamin Franza83859f2017-07-03 16:34:14 +010045import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
47import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070048import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070049import static android.view.Display.TYPE_VIRTUAL;
Louis Chang7d0037c2018-08-13 12:42:06 +080050import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Louis Changceeb5062018-09-17 18:13:52 +080051
Wale Ogunwale59507092018-10-29 09:00:30 -070052import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
53import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
Wale Ogunwale59507092018-10-29 09:00:30 -070054import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING;
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;
Wale Ogunwale59507092018-10-29 09:00:30 -070078import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
79import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
80import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
81import static com.android.server.wm.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
82import static com.android.server.wm.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
83import static com.android.server.wm.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Louis Changceeb5062018-09-17 18:13:52 +080084
Svetoslav7008b512015-06-24 18:47:07 -070085import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070086import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070087import android.app.ActivityManager;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070088import android.app.ActivityManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -070090import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -070091import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070093import android.app.WaitResult;
Andrii Kulian88e05cb2017-12-05 17:21:10 -080094import android.app.servertransaction.ActivityLifecycleItem;
95import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -070096import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -080097import android.app.servertransaction.PauseActivityItem;
98import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700100import android.content.Intent;
101import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700103import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.content.pm.PackageManager;
105import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100106import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800108import android.graphics.Rect;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100110import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700111import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700112import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700114import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700116import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700117import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700118import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700120import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700121import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100122import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700123import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700124import android.provider.MediaStore;
Svetoslav7008b512015-06-24 18:47:07 -0700125import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700126import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700127import android.util.EventLog;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700128import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700129import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800131import android.util.SparseIntArray;
Chong Zhangb15758a2015-11-17 12:12:03 -0800132
Andreas Gampea36dc622018-02-05 17:19:22 -0800133import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700134import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800135import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700136import com.android.internal.os.TransferPipe;
Louis Chang7d0037c2018-08-13 12:42:06 +0800137import com.android.internal.os.logging.MetricsLoggerWrapper;
Svetoslav7008b512015-06-24 18:47:07 -0700138import com.android.internal.util.ArrayUtils;
Louis Chang7d0037c2018-08-13 12:42:06 +0800139import com.android.internal.util.function.pooled.PooledLambda;
Wale Ogunwale59507092018-10-29 09:00:30 -0700140import com.android.server.am.ActivityManagerService;
Wale Ogunwale59507092018-10-29 09:00:30 -0700141import com.android.server.am.EventLogTags;
142import com.android.server.am.UserState;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700143
Craig Mautner8d341ef2013-03-26 09:03:27 -0700144import java.io.FileDescriptor;
145import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700146import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700147import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700148import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700149
Wale Ogunwaled32da472018-11-16 07:19:28 -0800150// TODO: This class has become a dumping ground. Let's
151// - Move things relating to the hierarchy to RootWindowContainer
152// - Move things relating to activity life cycles to maybe a new class called ActivityLifeCycler
153// - Move interface things to ActivityTaskManagerService.
154// - All other little things to other files.
155public class ActivityStackSupervisor implements RecentTasks.Callbacks {
Wale Ogunwale98875612018-10-12 07:53:02 -0700156 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_ATM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700157 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700158 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
160 private static final String TAG_STACK = TAG + POSTFIX_STACK;
161 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800162 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800163
Craig Mautnerf3333272013-04-22 10:55:53 -0700164 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700165 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700166
Craig Mautner0eea92c2013-05-16 13:35:39 -0700167 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700168 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700169
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700171 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000173 /** How long we wait until giving up on the activity telling us it released the top state. */
174 static final int TOP_RESUMED_STATE_LOSS_TIMEOUT = 500;
175
Craig Mautner05d29032013-05-03 13:40:13 -0700176 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
177 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
178 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700179 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700180 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700181 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Riddle Hsu7b766fd2019-01-28 21:14:59 +0800182 static final int RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800183 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
184 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Louis Changdcdde952018-12-04 15:38:44 +0800185 static final int REPORT_HOME_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 16;
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000186 static final int TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 17;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800187
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700188 // Used to indicate that windows of activities should be preserved during the resize.
189 static final boolean PRESERVE_WINDOWS = true;
190
Wale Ogunwale040b4702015-08-06 18:10:50 -0700191 // Used to indicate if an object (e.g. task) should be moved/created
192 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700193 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700194
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700195 // Don't execute any calls to resume.
196 static final boolean DEFER_RESUME = true;
197
Winson Chung010927a2016-12-15 16:12:35 -0800198 // Used to indicate that a task is removed it should also be removed from recents.
199 static final boolean REMOVE_FROM_RECENTS = true;
200
Winson Chung6954fc92017-03-24 16:22:12 -0700201 // Used to indicate that pausing an activity should occur immediately without waiting for
202 // the activity callback indicating that it has completed pausing
203 static final boolean PAUSE_IMMEDIATELY = true;
204
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200205 /** True if the docked stack is currently being resized. */
206 private boolean mDockedStackResizing;
207
208 /**
209 * True if there are pending docked bounds that need to be applied after
210 * {@link #mDockedStackResizing} is reset to false.
211 */
212 private boolean mHasPendingDockedBounds;
213 private Rect mPendingDockedBounds;
214 private Rect mPendingTempDockedTaskBounds;
215 private Rect mPendingTempDockedTaskInsetBounds;
216 private Rect mPendingTempOtherTaskBounds;
217 private Rect mPendingTempOtherTaskInsetBounds;
218
Svetoslav7008b512015-06-24 18:47:07 -0700219 // Activity actions an app cannot start if it uses a permission which is not granted.
220 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
221 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700222
Svetoslav7008b512015-06-24 18:47:07 -0700223 static {
224 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
225 Manifest.permission.CAMERA);
226 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
227 Manifest.permission.CAMERA);
228 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
229 Manifest.permission.CALL_PHONE);
230 }
231
Svet Ganov99b60432015-06-27 13:15:22 -0700232 /** Action restriction: launching the activity is not restricted. */
233 private static final int ACTIVITY_RESTRICTION_NONE = 0;
234 /** Action restriction: launching the activity is restricted by a permission. */
235 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
236 /** Action restriction: launching the activity is restricted by an app op. */
237 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700238
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700239 // For debugging to make sure the caller when acquiring/releasing our
240 // wake lock is the system process.
241 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800242 /** The number of distinct task ids that can be assigned to the tasks of a single user */
243 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700244
Wale Ogunwaleb73f3962018-11-20 07:58:22 -0800245 final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800246 RootActivityContainer mRootActivityContainer;
Craig Mautner27084302013-03-25 08:05:25 -0700247
Winson Chung61c9e5a2017-10-11 10:39:32 -0700248 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700249 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800250
Winson Chung61c9e5a2017-10-11 10:39:32 -0700251 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Wale Ogunwaled32da472018-11-16 07:19:28 -0800252 RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700253
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700254 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000255 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700256
257 /** Short cut */
258 WindowManagerService mWindowManager;
259
Garfield Tan891146c2018-10-09 12:14:00 -0700260 /** Common synchronization logic used to save things to disks. */
261 PersisterQueue mPersisterQueue;
262 LaunchParamsPersister mLaunchParamsPersister;
Bryce Leeec55eb02017-12-05 20:51:27 -0800263 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700264
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800265 /**
266 * Maps the task identifier that activities are currently being started in to the userId of the
267 * task. Each time a new task is created, the entry for the userId of the task is incremented
268 */
269 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700270
Bryce Lee4a194382017-04-04 14:32:48 -0700271 /** List of processes waiting to find out when a specific activity becomes visible. */
272 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700273
274 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700275 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700276
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277 /** List of activities that are ready to be stopped, but waiting for the next activity to
278 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800279 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700280
Craig Mautnerf3333272013-04-22 10:55:53 -0700281 /** List of activities that are ready to be finished, but waiting for the previous activity to
282 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700284
Craig Mautner0eea92c2013-05-16 13:35:39 -0700285 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800286 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700287
Wale Ogunwale22e25262016-02-01 10:32:02 -0800288 /** List of activities whose multi-window mode changed that we need to report to the
289 * application */
290 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
291
292 /** List of activities whose picture-in-picture mode changed that we need to report to the
293 * application */
294 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
295
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100296 /**
297 * Animations that for the current transition have requested not to
298 * be considered for the transition animation.
299 */
300 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
301
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000302 /**
303 * Cached value of the topmost resumed activity in the system. Updated when new activity is
304 * resumed.
305 */
306 private ActivityRecord mTopResumedActivity;
307
308 /**
309 * Flag indicating whether we're currently waiting for the previous top activity to handle the
310 * loss of the state and report back before making new activity top resumed.
311 */
312 private boolean mTopResumedActivityWaitingForPrev;
313
Winson Chung5af42fc2017-03-24 17:11:33 -0700314 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
315 * the application */
316 Rect mPipModeChangedTargetStackBounds;
317
Craig Mautnerf3333272013-04-22 10:55:53 -0700318 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700319 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700320
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
322 * is being brought in front of us. */
323 boolean mUserLeaving = false;
324
Craig Mautner0eea92c2013-05-16 13:35:39 -0700325 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700326 * We don't want to allow the device to go to sleep while in the process
327 * of launching an activity. This is primarily to allow alarm intent
328 * receivers to launch an activity and get that to run before the device
329 * goes back to sleep.
330 */
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800331 PowerManager.WakeLock mLaunchingActivityWakeLock;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700332
333 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700334 * Set when the system is going to sleep, until we have
335 * successfully paused the current activity and released our wake lock.
336 * At that point the system is allowed to actually sleep.
337 */
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800338 PowerManager.WakeLock mGoingToSleepWakeLock;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339
Andrii Kulian1e32e022016-09-16 15:29:34 -0700340 /**
341 * Temporary rect used during docked stack resize calculation so we don't need to create a new
342 * object each time.
343 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700344 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700345 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700346
Bryce Lee2a3cc462017-10-27 10:57:35 -0700347 private ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800348
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700349 /** Check if placing task or activity on specified display is allowed. */
Riddle Hsu16567132018-08-16 21:37:47 +0800350 boolean canPlaceEntityOnDisplay(int displayId, int callingPid, int callingUid,
351 ActivityInfo activityInfo) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700352 if (displayId == DEFAULT_DISPLAY) {
353 // No restrictions for the default display.
354 return true;
355 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700356 if (!mService.mSupportsMultiDisplay) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700357 // Can't launch on secondary displays if feature is not supported.
358 return false;
359 }
Andrii Kulian02689a72017-07-06 14:28:59 -0700360 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
361 // Can't place activities to a display that has restricted launch rules.
362 // In this case the request should be made by explicitly adding target display id and
363 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
364 return false;
365 }
366 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700367 }
368
369 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100370 * Used to keep track whether app visibilities got changed since the last pause. Useful to
371 * determine whether to invoke the task stack change listener after pausing.
372 */
373 boolean mAppVisibilitiesChangedSinceLastPause;
374
375 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100376 * Set of tasks that are in resizing mode during an app transition to fill the "void".
377 */
378 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
379
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700380
381 /**
382 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
383 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
384 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
385 * like the docked stack going empty.
386 */
387 private boolean mAllowDockedStackResize = true;
388
Bryce Lee2a3cc462017-10-27 10:57:35 -0700389 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200390
chaviw59b98852017-06-13 12:05:44 -0700391 private PowerManager mPowerManager;
392 private int mDeferResumeCount;
393
Bryce Lee2a3cc462017-10-27 10:57:35 -0700394 private boolean mInitialized;
395
Tony Mak853304c2016-04-18 15:17:41 +0100396 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700397 * Description of a request to start a new activity, which has been held
398 * due to app switches being disabled.
399 */
400 static class PendingActivityLaunch {
401 final ActivityRecord r;
402 final ActivityRecord sourceRecord;
403 final int startFlags;
404 final ActivityStack stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700405 final WindowProcessController callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700406
407 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700408 int _startFlags, ActivityStack _stack, WindowProcessController app) {
Craig Mautneree36c772014-07-16 14:56:05 -0700409 r = _r;
410 sourceRecord = _sourceRecord;
411 startFlags = _startFlags;
412 stack = _stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700413 callerApp = app;
Robert Carr13997f52015-10-23 13:13:39 -0700414 }
415
416 void sendErrorResult(String message) {
417 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700418 if (callerApp.hasThread()) {
419 callerApp.getThread().scheduleCrash(message);
Robert Carr13997f52015-10-23 13:13:39 -0700420 }
421 } catch (RemoteException e) {
422 Slog.e(TAG, "Exception scheduling crash of failed "
423 + "activity launcher sourceRecord=" + sourceRecord, e);
424 }
Craig Mautneree36c772014-07-16 14:56:05 -0700425 }
426 }
427
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700428 public ActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700429 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000430 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700431 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700432 }
433
434 public void initialize() {
435 if (mInitialized) {
436 return;
437 }
438
439 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700440 mRunningTasks = createRunningTasks();
Igor Murashkinc0b47e42018-11-07 15:54:18 -0800441
442 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext,
443 mHandler.getLooper());
Wale Ogunwalef6733932018-06-27 05:14:34 -0700444 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700445
Garfield Tan891146c2018-10-09 12:14:00 -0700446 mPersisterQueue = new PersisterQueue();
447 mLaunchParamsPersister = new LaunchParamsPersister(mPersisterQueue, this);
448 mLaunchParamsController = new LaunchParamsController(mService, mLaunchParamsPersister);
Bryce Leeec55eb02017-12-05 20:51:27 -0800449 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700450 }
451
Garfield Tan891146c2018-10-09 12:14:00 -0700452 void onSystemReady() {
Garfield Tan891146c2018-10-09 12:14:00 -0700453 mLaunchParamsPersister.onSystemReady();
454 }
Bryce Lee2a3cc462017-10-27 10:57:35 -0700455
Garfield Tan0d407f42019-03-07 11:47:01 -0800456 void onUserUnlocked(int userId) {
457 // Only start persisting when the first user is unlocked. The method call is
458 // idempotent so there is no side effect to call it again when the second user is
459 // unlocked.
460 mPersisterQueue.startPersisting();
461 mLaunchParamsPersister.onUnlockUser(userId);
462 }
463
Bryce Lee2a3cc462017-10-27 10:57:35 -0700464 public ActivityMetricsLogger getActivityMetricsLogger() {
465 return mActivityMetricsLogger;
466 }
467
468 public KeyguardController getKeyguardController() {
469 return mKeyguardController;
470 }
471
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800472 void setRecentTasks(RecentTasks recentTasks) {
473 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700474 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800475 }
476
Winson Chung3f0e59a2017-10-25 10:19:05 -0700477 @VisibleForTesting
478 RunningTasks createRunningTasks() {
479 return new RunningTasks();
480 }
481
Jeff Brown2c43c332014-06-12 22:38:59 -0700482 /**
483 * At the time when the constructor runs, the power manager has not yet been
484 * initialized. So we initialize our wakelocks afterwards.
485 */
486 void initPowerManagement() {
Michael Wrighte3001042019-02-05 00:13:14 +0000487 mPowerManager = mService.mContext.getSystemService(PowerManager.class);
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800488 mGoingToSleepWakeLock = mPowerManager
chaviw59b98852017-06-13 12:05:44 -0700489 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800490 mLaunchingActivityWakeLock = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
491 mLaunchingActivityWakeLock.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700492 }
493
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700494 void setWindowManager(WindowManagerService wm) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700495 mWindowManager = wm;
496 getKeyguardController().setWindowManager(wm);
Craig Mautner20e72272013-04-01 13:45:53 -0700497 }
498
Matthew Ng330757d2017-02-28 14:19:17 -0800499 void moveRecentsStackToFront(String reason) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800500 final ActivityStack recentsStack = mRootActivityContainer.getDefaultDisplay().getStack(
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700501 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800502 if (recentsStack != null) {
503 recentsStack.moveToFront(reason);
504 }
505 }
506
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800507 void setNextTaskIdForUserLocked(int taskId, int userId) {
508 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
509 if (taskId > currentTaskId) {
510 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700511 }
512 }
513
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700514 static int nextTaskIdForUser(int taskId, int userId) {
515 int nextTaskId = taskId + 1;
516 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
517 // Wrap around as there will be smaller task ids that are available now.
518 nextTaskId -= MAX_TASK_IDS_PER_USER;
519 }
520 return nextTaskId;
521 }
522
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800523 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800524 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
525 // for a userId u, a taskId can only be in the range
526 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
527 // 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 -0700528 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700529 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwaled32da472018-11-16 07:19:28 -0800530 || mRootActivityContainer.anyTaskForId(
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700531 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700532 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800533 if (candidateTaskId == currentTaskId) {
534 // Something wrong!
535 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
536 throw new IllegalStateException("Cannot get an available task id."
537 + " Reached limit of " + MAX_TASK_IDS_PER_USER
538 + " running tasks per user.");
539 }
540 }
541 mCurTaskIdForUser.put(userId, candidateTaskId);
542 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700543 }
544
Vishnu Nair132ee832018-09-28 15:00:05 -0700545 void waitActivityVisible(ComponentName name, WaitResult result, long startTimeMs) {
546 final WaitInfo waitInfo = new WaitInfo(name, result, startTimeMs);
Bryce Lee4a194382017-04-04 14:32:48 -0700547 mWaitingForActivityVisible.add(waitInfo);
548 }
549
550 void cleanupActivity(ActivityRecord r) {
551 // Make sure this record is no longer in the pending finishes list.
552 // This could happen, for example, if we are trimming activities
553 // down to the max limit while they are still waiting to finish.
554 mFinishingActivities.remove(r);
Bryce Lee4a194382017-04-04 14:32:48 -0700555
556 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800557 if (mWaitingForActivityVisible.get(i).matches(r.mActivityComponent)) {
Bryce Lee4a194382017-04-04 14:32:48 -0700558 mWaitingForActivityVisible.remove(i);
559 }
560 }
561 }
562
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700563 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700564 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700565 }
566
567 void sendWaitingVisibleReportLocked(ActivityRecord r) {
568 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -0700569 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
570 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800571 if (w.matches(r.mActivityComponent)) {
Bryce Lee4a194382017-04-04 14:32:48 -0700572 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700573 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -0700574 result.timeout = false;
575 result.who = w.getComponent();
Vishnu Nair132ee832018-09-28 15:00:05 -0700576 result.totalTime = SystemClock.uptimeMillis() - w.getStartTime();
Bryce Lee4a194382017-04-04 14:32:48 -0700577 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700578 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700579 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700580 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700581 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700582 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700583 }
584
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800585 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
586 if (mWaitingActivityLaunched.isEmpty()) {
587 return;
588 }
589
590 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
591 return;
592 }
593
Chong Zhang5022da32016-06-21 16:31:37 -0700594 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800595
Chong Zhang5022da32016-06-21 16:31:37 -0700596 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
597 WaitResult w = mWaitingActivityLaunched.remove(i);
598 if (w.who == null) {
599 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800600 w.result = result;
601
602 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
603 // will be no followup launch signals. Assign the result and launched component.
604 if (result == START_DELIVERED_TO_TOP) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800605 w.who = r.mActivityComponent;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800606 }
Chong Zhang5022da32016-06-21 16:31:37 -0700607 }
608 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800609
Chong Zhang5022da32016-06-21 16:31:37 -0700610 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700611 mService.mGlobalLock.notifyAll();
Chong Zhang5022da32016-06-21 16:31:37 -0700612 }
613 }
614
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800615 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, long totalTime,
616 @WaitResult.LaunchState int launchState) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700617 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700618 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700619 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700620 if (w.who == null) {
621 changed = true;
622 w.timeout = timeout;
623 if (r != null) {
624 w.who = new ComponentName(r.info.packageName, r.info.name);
625 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700626 w.totalTime = totalTime;
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800627 w.launchState = launchState;
Chong Zhang5022da32016-06-21 16:31:37 -0700628 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700629 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700630 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700631 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700632 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700633 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700634 }
635
Todd Kennedy7440f172015-12-09 14:31:22 -0800636 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
637 ProfilerInfo profilerInfo) {
638 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700639 if (aInfo != null) {
640 // Store the found target back into the intent, because now that
641 // we have it we never want to do this again. For example, if the
642 // user navigates back to this point in the history, we should
643 // always restart the exact same activity.
644 intent.setComponent(new ComponentName(
645 aInfo.applicationInfo.packageName, aInfo.name));
646
647 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700648 if (!aInfo.processName.equals("system")) {
Wale Ogunwale7056a062018-10-18 15:02:50 -0700649 if ((startFlags & (START_FLAG_DEBUG | START_FLAG_NATIVE_DEBUGGING
650 | START_FLAG_TRACK_ALLOCATION)) != 0 || profilerInfo != null) {
Vishnu Nair006ee3f2018-11-09 08:36:01 -0800651
652 // Mimic an AMS synchronous call by passing a message to AMS and wait for AMS
653 // to notify us that the task has completed.
654 // TODO(b/80414790) look into further untangling for the situation where the
655 // caller is on the same thread as the handler we are posting to.
656 synchronized (mService.mGlobalLock) {
657 // Post message to AMS.
658 final Message msg = PooledLambda.obtainMessage(
659 ActivityManagerInternal::setDebugFlagsForStartingActivity,
660 mService.mAmInternal, aInfo, startFlags, profilerInfo,
661 mService.mGlobalLock);
662 mService.mH.sendMessage(msg);
663 try {
664 mService.mGlobalLock.wait();
665 } catch (InterruptedException ignore) {
666
667 }
668 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700669 }
670 }
Todd Kennedyb3b431302017-03-20 16:05:48 -0700671 final String intentLaunchToken = intent.getLaunchToken();
672 if (aInfo.launchToken == null && intentLaunchToken != null) {
673 aInfo.launchToken = intentLaunchToken;
674 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700675 }
676 return aInfo;
677 }
678
Patrick Baumann78380272018-04-04 10:41:01 -0700679 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
680 int filterCallingUid) {
Wale Ogunwale1f573382018-12-03 06:42:54 -0800681 try {
682 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
683 int modifiedFlags = flags
684 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
685 if (intent.isWebIntent()
686 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
687 modifiedFlags |= PackageManager.MATCH_INSTANT;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100688 }
Wale Ogunwale1f573382018-12-03 06:42:54 -0800689
690 // In order to allow cross-profile lookup, we clear the calling identity here.
691 // Note the binder identity won't affect the result, but filterCallingUid will.
692
693 // Cross-user/profile call check are done at the entry points
694 // (e.g. AMS.startActivityAsUser).
695 final long token = Binder.clearCallingIdentity();
696 try {
697 return mService.getPackageManagerInternalLocked().resolveIntent(
698 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
699 } finally {
700 Binder.restoreCallingIdentity(token);
701 }
702 } finally {
703 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Todd Kennedy7440f172015-12-09 14:31:22 -0800704 }
Todd Kennedy7440f172015-12-09 14:31:22 -0800705 }
706
707 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -0700708 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
709 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -0800710 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
711 }
712
Wale Ogunwaled32da472018-11-16 07:19:28 -0800713 boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800714 boolean andResume, boolean checkConfig) throws RemoteException {
715
Wale Ogunwaled32da472018-11-16 07:19:28 -0800716 if (!mRootActivityContainer.allPausedActivitiesComplete()) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800717 // While there are activities pausing we skipping starting any new activities until
718 // pauses are complete. NOTE: that we also do this for activities that are starting in
719 // the paused state because they will first be resumed then paused on the client side.
720 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
721 "realStartActivityLocked: Skipping start of r=" + r
722 + " some activities pausing...");
723 return false;
724 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700725
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800726 final TaskRecord task = r.getTaskRecord();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700727 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -0700728
729 beginDeferResume();
730
Craig Mautner2420ead2013-04-01 17:13:20 -0700731 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700732 r.startFreezingScreenLocked(proc, 0);
chaviw59b98852017-06-13 12:05:44 -0700733
734 // schedule launch ticks to collect information about slow apps.
735 r.startLaunchTickingLocked();
736
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700737 r.setProcess(proc);
chaviw59b98852017-06-13 12:05:44 -0700738
Louis Chang77ce34d2019-01-03 15:45:12 +0800739 // Ensure activity is allowed to be resumed after process has set.
740 if (andResume && !r.canResumeByCompat()) {
741 andResume = false;
742 }
743
Bryce Lee459c0622018-03-19 11:04:01 -0700744 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +0200745 r.notifyUnknownVisibilityLaunched();
746 }
747
chaviw59b98852017-06-13 12:05:44 -0700748 // Have the window manager re-evaluate the orientation of the screen based on the new
749 // activity order. Note that as a result of this, it can call back into the activity
750 // manager with a new orientation. We don't care about that, because the activity is
751 // not currently running so we are just restarting it anyway.
752 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -0700753 // Deferring resume here because we're going to launch new activity shortly.
754 // We don't want to perform a redundant launch of the same record while ensuring
755 // configurations and trying to resume top activity of focused stack.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800756 mRootActivityContainer.ensureVisibilityAndConfig(r, r.getDisplayId(),
Andrii Kulianf4479ee2018-05-23 17:52:48 -0700757 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -0700758 }
chaviw59b98852017-06-13 12:05:44 -0700759
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800760 if (r.getActivityStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
chaviw59b98852017-06-13 12:05:44 -0700761 true /* isTop */)) {
762 // We only set the visibility to true if the activity is allowed to be visible
763 // based on
764 // keyguard state. This avoids setting this into motion in window manager that is
765 // later cancelled due to later calls to ensure visible activities that set
766 // visibility back to false.
767 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700768 }
chaviw59b98852017-06-13 12:05:44 -0700769
chaviw59b98852017-06-13 12:05:44 -0700770 final int applicationInfoUid =
771 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800772 if ((r.mUserId != proc.mUserId) || (r.appInfo.uid != applicationInfoUid)) {
chaviw59b98852017-06-13 12:05:44 -0700773 Slog.wtf(TAG,
774 "User ID for activity changing for " + r
775 + " appInfo.uid=" + r.appInfo.uid
776 + " info.ai.uid=" + applicationInfoUid
Wale Ogunwale9c103022018-10-18 07:44:54 -0700777 + " old=" + r.app + " new=" + proc);
chaviw59b98852017-06-13 12:05:44 -0700778 }
779
Wale Ogunwale9c103022018-10-18 07:44:54 -0700780 proc.clearWaitingToKill();
chaviw59b98852017-06-13 12:05:44 -0700781 r.launchCount++;
782 r.lastLaunchTime = SystemClock.uptimeMillis();
783
784 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
785
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700786 proc.addActivityIfNeeded(r);
787 proc.updateProcessInfo(false, true, true, true);
chaviw59b98852017-06-13 12:05:44 -0700788
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700789 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +0100790 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
791 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
792 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -0700793 && lockTaskController.getLockTaskModeState()
794 == LOCK_TASK_MODE_LOCKED)) {
795 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -0700796 }
797
798 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700799 if (!proc.hasThread()) {
chaviw59b98852017-06-13 12:05:44 -0700800 throw new RemoteException();
801 }
802 List<ResultInfo> results = null;
803 List<ReferrerIntent> newIntents = null;
804 if (andResume) {
805 // We don't need to deliver new intents and/or set results if activity is going
806 // to pause immediately after launch.
807 results = r.results;
808 newIntents = r.newIntents;
809 }
810 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
811 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
812 + " newIntents=" + newIntents + " andResume=" + andResume);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800813 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.mUserId,
chaviw59b98852017-06-13 12:05:44 -0700814 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700815 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -0700816 // Home process is the root process of the task.
Louis Changdcdde952018-12-04 15:38:44 +0800817 updateHomeProcess(task.mActivities.get(0).app);
chaviw59b98852017-06-13 12:05:44 -0700818 }
Wale Ogunwale214f3482018-10-04 11:00:47 -0700819 mService.getPackageManagerInternalLocked().notifyPackageUse(
820 r.intent.getComponent().getPackageName(), NOTIFY_PACKAGE_USE_ACTIVITY);
chaviw59b98852017-06-13 12:05:44 -0700821 r.sleeping = false;
822 r.forceNewConfig = false;
Wale Ogunwale008163e2018-07-23 23:11:08 -0700823 mService.getAppWarningsLocked().onStartActivity(r);
Wale Ogunwale53783742018-09-16 10:21:51 -0700824 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Wale Ogunwale9c103022018-10-18 07:44:54 -0700825 ProfilerInfo profilerInfo = proc.onStartActivity(mService.mTopProcessState);
chaviw59b98852017-06-13 12:05:44 -0700826
chaviw59b98852017-06-13 12:05:44 -0700827 // Because we could be starting an Activity in the system process this may not go
828 // across a Binder interface which would create a new Configuration. Consequently
829 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -0800830
chaviw59b98852017-06-13 12:05:44 -0700831 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
Wale Ogunwale9c103022018-10-18 07:44:54 -0700832 proc.getConfiguration(), r.getMergedOverrideConfiguration());
chaviw59b98852017-06-13 12:05:44 -0700833 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -0800834
chaviw59b98852017-06-13 12:05:44 -0700835 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800836
837
838 // Create activity launch transaction.
Wale Ogunwale9c103022018-10-18 07:44:54 -0700839 final ClientTransaction clientTransaction = ClientTransaction.obtain(
840 proc.getThread(), r.appToken);
lumark588a3e82018-07-20 18:53:54 +0800841
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800842 final DisplayContent dc = r.getDisplay().mDisplayContent;
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800843 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -0700844 System.identityHashCode(r), r.info,
845 // TODO: Have this take the merged configuration instead of separate global
846 // and override configs.
847 mergedConfiguration.getGlobalConfiguration(),
848 mergedConfiguration.getOverrideConfiguration(), r.compat,
Wale Ogunwale9c103022018-10-18 07:44:54 -0700849 r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700850 r.icicle, r.persistentState, results, newIntents,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800851 dc.isNextTransitionForward(), profilerInfo));
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800852
853 // Set desired final state.
854 final ActivityLifecycleItem lifecycleItem;
855 if (andResume) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800856 lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800857 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -0700858 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800859 }
860 clientTransaction.setLifecycleStateRequest(lifecycleItem);
861
862 // Schedule transaction.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700863 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Andrii Kulian86e70fc2019-02-12 11:04:10 +0000864 updateTopResumedActivityIfNeeded();
Craig Mautner2420ead2013-04-01 17:13:20 -0700865
Wale Ogunwale9c103022018-10-18 07:44:54 -0700866 if ((proc.mInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
Wale Ogunwale214f3482018-10-04 11:00:47 -0700867 && mService.mHasHeavyWeightFeature) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700868 // This may be a heavy-weight process! Note that the package manager will ensure
869 // that only activity can run in the main process of the .apk, which is the only
870 // thing that will be considered heavy-weight.
Wale Ogunwale9c103022018-10-18 07:44:54 -0700871 if (proc.mName.equals(proc.mInfo.packageName)) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700872 if (mService.mHeavyWeightProcess != null
873 && mService.mHeavyWeightProcess != proc) {
874 Slog.w(TAG, "Starting new heavy weight process " + proc
chaviw59b98852017-06-13 12:05:44 -0700875 + " when already running "
Wale Ogunwale53783742018-09-16 10:21:51 -0700876 + mService.mHeavyWeightProcess);
chaviw59b98852017-06-13 12:05:44 -0700877 }
Wale Ogunwale53783742018-09-16 10:21:51 -0700878 mService.setHeavyWeightProcess(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700879 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700880 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700881
chaviw59b98852017-06-13 12:05:44 -0700882 } catch (RemoteException e) {
883 if (r.launchFailed) {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700884 // This is the second time we failed -- finish activity and give up.
chaviw59b98852017-06-13 12:05:44 -0700885 Slog.e(TAG, "Second failure launching "
Wale Ogunwale9c103022018-10-18 07:44:54 -0700886 + r.intent.getComponent().flattenToShortString() + ", giving up", e);
887 proc.appDied();
chaviw59b98852017-06-13 12:05:44 -0700888 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
889 "2nd-crash", false);
890 return false;
891 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700892
chaviw59b98852017-06-13 12:05:44 -0700893 // This is the first time we failed -- restart process and
894 // retry.
895 r.launchFailed = true;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700896 proc.removeActivity(r);
chaviw59b98852017-06-13 12:05:44 -0700897 throw e;
898 }
899 } finally {
900 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -0700901 }
902
903 r.launchFailed = false;
904 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -0700905 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -0700906 }
907
Andrii Kulianb372da62018-01-18 10:46:24 -0800908 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
909 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -0700910 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -0700911 // As part of the process of launching, ActivityThread also performs
912 // a resume.
913 stack.minimalResumeActivityLocked(r);
914 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800915 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +0000916 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -0800917 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700918 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -0800919 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -0800920 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -0700921 }
922
923 // Launch the new version setup screen if needed. We do this -after-
924 // launching the initial activity (that is, home), so that it can have
925 // a chance to initialize itself while in the background, making the
926 // switch back to it faster and look better.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800927 if (mRootActivityContainer.isTopDisplayFocusedStack(stack)) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700928 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -0700929 }
930
Dianne Hackborn465fa392014-09-14 14:21:18 -0700931 // Update any services we are bound to that might care about whether
932 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -0700933 if (r.app != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700934 r.app.updateServiceConnectionActivities();
Wale Ogunwaled6ac7622016-05-26 09:02:25 -0700935 }
Dianne Hackborn465fa392014-09-14 14:21:18 -0700936
Craig Mautner2420ead2013-04-01 17:13:20 -0700937 return true;
938 }
939
Louis Changdcdde952018-12-04 15:38:44 +0800940 void updateHomeProcess(WindowProcessController app) {
941 if (app != null && mService.mHomeProcess != app) {
942 if (!mHandler.hasMessages(REPORT_HOME_CHANGED_MSG)) {
943 mHandler.sendEmptyMessage(REPORT_HOME_CHANGED_MSG);
944 }
945 mService.mHomeProcess = app;
946 }
947 }
948
Sudheer Shankafab200f2017-05-17 20:41:53 -0700949 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
950 int extrasSize = 0;
951 if (intent != null) {
952 final Bundle extras = intent.getExtras();
953 if (extras != null) {
954 extrasSize = extras.getSize();
955 }
956 }
957 int icicleSize = (icicle == null ? 0 : icicle.getSize());
958 if (extrasSize + icicleSize > 200000) {
959 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
960 + ", icicle size: " + icicleSize);
961 }
962 }
963
Wale Ogunwale9c103022018-10-18 07:44:54 -0700964 void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700965 // Is this activity's application already running?
Wale Ogunwale9c103022018-10-18 07:44:54 -0700966 final WindowProcessController wpc =
967 mService.getProcessController(r.processName, r.info.applicationInfo.uid);
Craig Mautnere79d42682013-04-01 19:01:53 -0700968
Chilun917173f2019-01-04 16:47:42 +0800969 boolean knownToBeDead = false;
Wale Ogunwale9c103022018-10-18 07:44:54 -0700970 if (wpc != null && wpc.hasThread()) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700971 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700972 if ((r.info.flags & ActivityInfo.FLAG_MULTIPROCESS) == 0
Dianne Hackborn237cefb2013-10-22 18:45:27 -0700973 || !"android".equals(r.info.packageName)) {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700974 // Don't add this if it is a platform component that is marked to run in
975 // multiple processes, because this is actually part of the framework so doesn't
976 // make sense to track as a separate apk in the process.
977 wpc.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode);
Dianne Hackborn237cefb2013-10-22 18:45:27 -0700978 }
Wale Ogunwale9c103022018-10-18 07:44:54 -0700979 realStartActivityLocked(r, wpc, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -0700980 return;
981 } catch (RemoteException e) {
982 Slog.w(TAG, "Exception when starting activity "
983 + r.intent.getComponent().flattenToShortString(), e);
984 }
985
986 // If a dead object exception was thrown -- fall through to
987 // restart the application.
Chilun917173f2019-01-04 16:47:42 +0800988 knownToBeDead = true;
Craig Mautnere79d42682013-04-01 19:01:53 -0700989 }
990
Issei Suzuki5205af72018-11-09 11:51:59 +0100991 // Suppress transition until the new activity becomes ready, otherwise the keyguard can
992 // appear for a short amount of time before the new process with the new activity had the
993 // ability to set its showWhenLocked flags.
994 if (getKeyguardController().isKeyguardLocked()) {
995 r.notifyUnknownVisibilityLaunched();
996 }
997
Ioannis Ilkos3501ca42019-02-06 11:43:48 +0000998 try {
999 if (Trace.isTagEnabled(TRACE_TAG_ACTIVITY_MANAGER)) {
1000 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "dispatchingStartProcess:"
1001 + r.processName);
1002 }
1003 // Post message to start process to avoid possible deadlock of calling into AMS with the
1004 // ATMS lock held.
1005 final Message msg = PooledLambda.obtainMessage(
1006 ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,
1007 r.info.applicationInfo, knownToBeDead, "activity", r.intent.getComponent());
1008 mService.mH.sendMessage(msg);
1009 } finally {
1010 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1011 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001012 }
1013
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001014 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo, String resultWho,
1015 int requestCode, int callingPid, int callingUid, String callingPackage,
1016 boolean ignoreTargetSecurity, boolean launchingInTask,
1017 WindowProcessController callerApp, ActivityRecord resultRecord,
1018 ActivityStack resultStack) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001019 final boolean isCallerRecents = mService.getRecentTasks() != null
1020 && mService.getRecentTasks().isCallerRecents(callingUid);
Wale Ogunwalef6733932018-06-27 05:14:34 -07001021 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001022 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001023 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1024 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1025 // caller is the recents component and we are specifically starting an activity in an
1026 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001027 return true;
1028 }
1029 final int componentRestriction = getComponentRestrictionForCallingPackage(
1030 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1031 final int actionRestriction = getActionRestrictionForCallingPackage(
1032 intent.getAction(), callingPackage, callingPid, callingUid);
1033 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1034 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1035 if (resultRecord != null) {
1036 resultStack.sendActivityResultLocked(-1,
1037 resultRecord, resultWho, requestCode,
1038 Activity.RESULT_CANCELED, null);
1039 }
1040 final String msg;
1041 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1042 msg = "Permission Denial: starting " + intent.toString()
1043 + " from " + callerApp + " (pid=" + callingPid
1044 + ", uid=" + callingUid + ")" + " with revoked permission "
1045 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1046 } else if (!aInfo.exported) {
1047 msg = "Permission Denial: starting " + intent.toString()
1048 + " from " + callerApp + " (pid=" + callingPid
1049 + ", uid=" + callingUid + ")"
1050 + " not exported from uid " + aInfo.applicationInfo.uid;
1051 } else {
1052 msg = "Permission Denial: starting " + intent.toString()
1053 + " from " + callerApp + " (pid=" + callingPid
1054 + ", uid=" + callingUid + ")"
1055 + " requires " + aInfo.permission;
1056 }
1057 Slog.w(TAG, msg);
1058 throw new SecurityException(msg);
1059 }
1060
1061 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1062 final String message = "Appop Denial: starting " + intent.toString()
1063 + " from " + callerApp + " (pid=" + callingPid
1064 + ", uid=" + callingUid + ")"
1065 + " requires " + AppOpsManager.permissionToOp(
1066 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1067 Slog.w(TAG, message);
1068 return false;
1069 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1070 final String message = "Appop Denial: starting " + intent.toString()
1071 + " from " + callerApp + " (pid=" + callingPid
1072 + ", uid=" + callingUid + ")"
1073 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1074 Slog.w(TAG, message);
1075 return false;
1076 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001077
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001078 return true;
1079 }
1080
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001081 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001082 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1083 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001084 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1085 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1086
Andrii Kulian02689a72017-07-06 14:28:59 -07001087 if (callingPid == -1 && callingUid == -1) {
1088 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1089 return true;
1090 }
1091
Wale Ogunwaled32da472018-11-16 07:19:28 -08001092 final ActivityDisplay activityDisplay =
1093 mRootActivityContainer.getActivityDisplayOrCreate(launchDisplayId);
Louis Chang7d0037c2018-08-13 12:42:06 +08001094 if (activityDisplay == null || activityDisplay.isRemoved()) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001095 Slog.w(TAG, "Launch on display check: display not found");
1096 return false;
1097 }
1098
Andrii Kulian02689a72017-07-06 14:28:59 -07001099 // Check if the caller has enough privileges to embed activities and launch to private
1100 // displays.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001101 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001102 callingUid);
1103 if (startAnyPerm == PERMISSION_GRANTED) {
1104 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1105 + " allow launch any on display");
1106 return true;
1107 }
1108
Andrii Kulian8f070292017-09-12 22:56:49 -07001109 // Check if caller is already present on display
1110 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1111
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001112 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1113 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1114 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001115 // Limit launching on virtual displays, because their contents can be read from Surface
1116 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001117 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1118 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1119 + " disallow launch on virtual display for not-embedded activity.");
1120 return false;
1121 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001122 // Check if the caller is allowed to embed activities from other apps.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001123 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001124 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001125 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1126 + " disallow activity embedding without permission.");
1127 return false;
1128 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001129 }
1130
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001131 if (!activityDisplay.isPrivate()) {
1132 // Anyone can launch on a public display.
1133 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1134 + " allow launch on public display");
1135 return true;
1136 }
1137
1138 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001139 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001140 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1141 + " allow launch for owner of the display");
1142 return true;
1143 }
1144
Andrii Kulian8f070292017-09-12 22:56:49 -07001145 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001146 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1147 + " allow launch for caller present on the display");
1148 return true;
1149 }
1150
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001151 Slog.w(TAG, "Launch on display check: denied");
1152 return false;
1153 }
1154
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001155 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001156 final long identity = Binder.clearCallingIdentity();
1157 try {
1158 return UserManager.get(mService.mContext).getUserInfo(userId);
1159 } finally {
1160 Binder.restoreCallingIdentity(identity);
1161 }
1162 }
1163
Svet Ganov99b60432015-06-27 13:15:22 -07001164 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001165 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Wale Ogunwale214f3482018-10-04 11:00:47 -07001166 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001167 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001168 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001169 return ACTIVITY_RESTRICTION_PERMISSION;
1170 }
1171
Christopher Tateff7add02015-08-17 10:23:22 -07001172 if (activityInfo.permission == null) {
1173 return ACTIVITY_RESTRICTION_NONE;
1174 }
1175
Svet Ganov99b60432015-06-27 13:15:22 -07001176 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1177 if (opCode == AppOpsManager.OP_NONE) {
1178 return ACTIVITY_RESTRICTION_NONE;
1179 }
1180
Wale Ogunwalef6733932018-06-27 05:14:34 -07001181 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage)
1182 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001183 if (!ignoreTargetSecurity) {
1184 return ACTIVITY_RESTRICTION_APPOP;
1185 }
Svet Ganov99b60432015-06-27 13:15:22 -07001186 }
1187
1188 return ACTIVITY_RESTRICTION_NONE;
1189 }
1190
Svetoslav7008b512015-06-24 18:47:07 -07001191 private int getActionRestrictionForCallingPackage(String action,
1192 String callingPackage, int callingPid, int callingUid) {
1193 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001194 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001195 }
1196
1197 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1198 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001199 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001200 }
1201
1202 final PackageInfo packageInfo;
1203 try {
1204 packageInfo = mService.mContext.getPackageManager()
1205 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1206 } catch (PackageManager.NameNotFoundException e) {
1207 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001208 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001209 }
1210
1211 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001212 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001213 }
1214
Wale Ogunwalef6733932018-06-27 05:14:34 -07001215 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001216 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001217 }
1218
1219 final int opCode = AppOpsManager.permissionToOpCode(permission);
1220 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001221 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001222 }
1223
Wale Ogunwalef6733932018-06-27 05:14:34 -07001224 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage)
1225 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001226 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001227 }
1228
Svet Ganov99b60432015-06-27 13:15:22 -07001229 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001230 }
1231
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001232 void setLaunchSource(int uid) {
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001233 mLaunchingActivityWakeLock.setWorkSource(new WorkSource(uid));
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001234 }
1235
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001236 void acquireLaunchWakelock() {
1237 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1238 throw new IllegalStateException("Calling must be system uid");
1239 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001240 mLaunchingActivityWakeLock.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001241 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1242 // To be safe, don't allow the wake lock to be held for too long.
1243 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1244 }
1245 }
1246
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001247 /**
Louis Chang89f43fc2018-10-05 10:59:14 +08001248 * Called when all resumed tasks/stacks are idle.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001249 * @return the state of mService.mAm.mBooting before this was called.
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001250 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001251 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001252 private boolean checkFinishBootingLocked() {
Wale Ogunwale53783742018-09-16 10:21:51 -07001253 final boolean booting = mService.isBooting();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001254 boolean enableScreen = false;
Wale Ogunwale53783742018-09-16 10:21:51 -07001255 mService.setBooting(false);
1256 if (!mService.isBooted()) {
1257 mService.setBooted(true);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001258 enableScreen = true;
1259 }
1260 if (booting || enableScreen) {
Wale Ogunwale53783742018-09-16 10:21:51 -07001261 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001262 }
1263 return booting;
1264 }
1265
Craig Mautnerf3333272013-04-22 10:55:53 -07001266 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001267 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001268 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001269 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001270 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001271
Craig Mautnerf3333272013-04-22 10:55:53 -07001272 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001273 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001274 int NS = 0;
1275 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001276 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001277 boolean activityRemoved = false;
1278
Wale Ogunwale7d701172015-03-11 15:36:30 -07001279 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001280 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001281 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1282 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001283 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1284 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001285 if (fromTimeout) {
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -08001286 reportActivityLaunchedLocked(fromTimeout, r, INVALID_DELAY,
1287 -1 /* launchState */);
Craig Mautnerf3333272013-04-22 10:55:53 -07001288 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001289
1290 // This is a hack to semi-deal with a race condition
1291 // in the client where it can be constructed with a
1292 // newer configuration from when we asked it to launch.
1293 // We'll update with whatever configuration it now says
1294 // it used to launch.
1295 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001296 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001297 }
1298
1299 // We are now idle. If someone is waiting for a thumbnail from
1300 // us, we can now deliver.
1301 r.idle = true;
1302
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001303 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Louis Chang89f43fc2018-10-05 10:59:14 +08001304
Louis Chang1eff2482018-11-01 15:46:31 +08001305 // Check if able to finish booting when device is booting and all resumed activities
1306 // are idle.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001307 if ((mService.isBooting() && mRootActivityContainer.allResumedActivitiesIdle())
1308 || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001309 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001310 }
Garfield Tan2746ab52018-07-25 12:33:01 -07001311
1312 // When activity is idle, we consider the relaunch must be successful, so let's clear
1313 // the flag.
1314 r.mRelaunchReason = RELAUNCH_REASON_NONE;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001315 }
1316
Wale Ogunwaled32da472018-11-16 07:19:28 -08001317 if (mRootActivityContainer.allResumedActivitiesIdle()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001318 if (r != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001319 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001320 }
1321
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001322 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001323 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1324 if (VALIDATE_WAKE_LOCK_CALLER &&
1325 Binder.getCallingUid() != Process.myUid()) {
1326 throw new IllegalStateException("Calling must be system uid");
1327 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001328 mLaunchingActivityWakeLock.release();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001329 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001330 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001331 }
1332
1333 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001334 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1335 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001336 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001337 if ((NF = mFinishingActivities.size()) > 0) {
1338 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001339 mFinishingActivities.clear();
1340 }
1341
Craig Mautnerf3333272013-04-22 10:55:53 -07001342 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001343 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001344 mStartingUsers.clear();
1345 }
1346
Craig Mautnerf3333272013-04-22 10:55:53 -07001347 // Stop any activities that are scheduled to do so but have been
1348 // waiting for the next one to start.
1349 for (int i = 0; i < NS; i++) {
1350 r = stops.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001351 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001352 if (stack != null) {
1353 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08001354 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
1355 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07001356 } else {
1357 stack.stopActivityLocked(r);
1358 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001359 }
1360 }
1361
1362 // Finish any activities that are scheduled to do so but have been
1363 // waiting for the next one to start.
1364 for (int i = 0; i < NF; i++) {
1365 r = finishes.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001366 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001367 if (stack != null) {
1368 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1369 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001370 }
1371
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001372 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001373 // Complete user switch
1374 if (startingUsers != null) {
1375 for (int i = 0; i < startingUsers.size(); i++) {
Wale Ogunwale86b74462018-07-02 08:42:43 -07001376 mService.mAmInternal.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001377 }
1378 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001379 }
1380
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001381 mService.mH.post(() -> mService.mAmInternal.trimApplications());
Craig Mautnerf3333272013-04-22 10:55:53 -07001382 //dump();
1383 //mWindowManager.dump();
1384
Craig Mautnerf3333272013-04-22 10:55:53 -07001385 if (activityRemoved) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001386 mRootActivityContainer.resumeFocusedStacksTopActivities();
Craig Mautnerf3333272013-04-22 10:55:53 -07001387 }
1388
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001389 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001390 }
1391
Wale Ogunwaled32da472018-11-16 07:19:28 -08001392 /** This doesn't just find a task, it also moves the task to front. */
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001393 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
1394 boolean forceNonResizeable) {
Louis Changbd48dca2018-08-29 17:44:34 +08001395 ActivityStack currentStack = task.getStack();
Wale Ogunwale66e16852017-10-19 13:35:52 -07001396 if (currentStack == null) {
1397 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
1398 + task + " to front. Stack is null");
1399 return;
1400 }
1401
Craig Mautneraea74a52014-03-08 14:23:10 -08001402 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1403 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001404 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07001405
Louis Changc8b64112018-10-11 09:22:14 +08001406 reason = reason + " findTaskToMoveToFront";
1407 boolean reparented = false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001408 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08001409 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001410 task.updateOverrideConfiguration(bounds);
1411
Wale Ogunwaled32da472018-11-16 07:19:28 -08001412 ActivityStack stack =
1413 mRootActivityContainer.getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001414
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001415 if (stack != currentStack) {
Louis Changc8b64112018-10-11 09:22:14 +08001416 moveHomeStackToFrontIfNeeded(flags, stack.getDisplay(), reason);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001417 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Louis Changc8b64112018-10-11 09:22:14 +08001418 reason);
Louis Changbd48dca2018-08-29 17:44:34 +08001419 currentStack = stack;
Louis Changc8b64112018-10-11 09:22:14 +08001420 reparented = true;
1421 // task.reparent() should already placed the task on top,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001422 // still need moveTaskToFrontLocked() below for any transition settings.
1423 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001424 if (stack.resizeStackWithLaunchBounds()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001425 mRootActivityContainer.resizeStack(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001426 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
1427 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001428 } else {
1429 // WM resizeTask must be done after the task is moved to the correct stack,
1430 // because Task's setBounds() also updates dim layer's bounds, but that has
1431 // dependency on the stack.
1432 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001433 }
1434 }
1435
Louis Changc8b64112018-10-11 09:22:14 +08001436 if (!reparented) {
1437 moveHomeStackToFrontIfNeeded(flags, currentStack.getDisplay(), reason);
1438 }
1439
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001440 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07001441 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001442 r == null ? null : r.appTimeTracker, reason);
1443
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001444 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001445 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001446
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001447 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001448 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001449 }
1450
Louis Changc8b64112018-10-11 09:22:14 +08001451 private void moveHomeStackToFrontIfNeeded(int flags, ActivityDisplay display, String reason) {
1452 final ActivityStack focusedStack = display.getFocusedStack();
1453
1454 if ((display.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
1455 && (flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0)
1456 || (focusedStack != null && focusedStack.isActivityTypeRecents())) {
1457 // We move home stack to front when we are on a fullscreen display and caller has
1458 // requested the home activity to move with it. Or the previous stack is recents.
1459 display.moveHomeStackToFront(reason);
1460 }
1461 }
1462
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001463 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001464 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001465 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001466 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001467 return false;
1468 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001469 return (mService.mSupportsPictureInPicture
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001470 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001471 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001472 }
1473
Bryce Leeec55eb02017-12-05 20:51:27 -08001474 LaunchParamsController getLaunchParamsController() {
1475 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07001476 }
1477
Wale Ogunwaled32da472018-11-16 07:19:28 -08001478 private void deferUpdateRecentsHomeStackBounds() {
1479 mRootActivityContainer.deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
1480 mRootActivityContainer.deferUpdateBounds(ACTIVITY_TYPE_HOME);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001481 }
1482
Wale Ogunwaled32da472018-11-16 07:19:28 -08001483 private void continueUpdateRecentsHomeStackBounds() {
1484 mRootActivityContainer.continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
1485 mRootActivityContainer.continueUpdateBounds(ACTIVITY_TYPE_HOME);
Jorim Jaggi192086e2016-03-11 17:17:03 +01001486 }
1487
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001488 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08001489 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001490 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1491 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled32da472018-11-16 07:19:28 -08001492 final TaskRecord task =
1493 mRootActivityContainer.anyTaskForId(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001494 if (task != null) {
1495 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001496 }
1497 }
1498 mResizingTasksDuringAnimation.clear();
1499 }
1500
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001501 /**
1502 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001503 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001504 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001505 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001506 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07001507
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001508 mWindowManager.deferSurfaceLayout();
1509 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001510 final int windowingMode = fromStack.getWindowingMode();
1511 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
Wale Ogunwaled32da472018-11-16 07:19:28 -08001512 final ActivityDisplay toDisplay =
1513 mRootActivityContainer.getActivityDisplay(toDisplayId);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001514
1515 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001516 // Tell the display we are exiting split-screen mode.
1517 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001518 // We are moving all tasks from the docked stack to the fullscreen stack,
1519 // which is dismissing the docked stack, so resize all other stacks to
1520 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001521 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
1522 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07001523 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
1524 continue;
1525 }
Evan Rosky10475742018-09-05 19:02:48 -07001526 otherStack.setWindowingMode(WINDOWING_MODE_UNDEFINED);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001527 }
1528
1529 // Also disable docked stack resizing since we have manually adjusted the
1530 // size of other stacks above and we don't want to trigger a docked stack
1531 // resize when we remove task from it below and it is detached from the
1532 // display because it no longer contains any tasks.
1533 mAllowDockedStackResize = false;
1534 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001535
Winson Chung5af42fc2017-03-24 17:11:33 -07001536 // If we are moving from the pinned stack, then the animation takes care of updating
1537 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001538 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001539 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001540
Wale Ogunwale388945c2017-10-04 12:13:46 -07001541 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001542 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07001543 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001544 for (int i = 0; i < size; ++i) {
1545 final TaskRecord task = tasks.get(i);
1546 final ActivityStack toStack = toDisplay.getOrCreateStack(
1547 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07001548
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001549 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07001550 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07001551 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001552 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07001553 isTopTask /* animate */, DEFER_RESUME,
1554 schedulePictureInPictureModeChange,
1555 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08001556 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
1557 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001558 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07001559 // Position the tasks in the fullscreen stack in order at the bottom of the
1560 // stack. Also defer resume until all the tasks have been moved to the
1561 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001562 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07001563 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
1564 schedulePictureInPictureModeChange,
1565 "moveTasksToFullscreenStack - NOT_onTop");
1566 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001567 }
1568 }
Winson Chung74666102017-02-22 17:49:24 -08001569
Wale Ogunwaled32da472018-11-16 07:19:28 -08001570 mRootActivityContainer.ensureActivitiesVisible(null, 0, PRESERVE_WINDOWS);
1571 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001572 } finally {
1573 mAllowDockedStackResize = true;
1574 mWindowManager.continueSurfaceLayout();
1575 }
1576 }
1577
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001578 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001579 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
1580 }
1581
1582 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
1583 mWindowManager.inSurfaceTransaction(() ->
1584 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07001585 }
1586
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001587 void setSplitScreenResizing(boolean resizing) {
1588 if (resizing == mDockedStackResizing) {
1589 return;
1590 }
1591
1592 mDockedStackResizing = resizing;
1593 mWindowManager.setDockedStackResizing(resizing);
1594
1595 if (!resizing && mHasPendingDockedBounds) {
1596 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
1597 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
1598 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
1599
1600 mHasPendingDockedBounds = false;
1601 mPendingDockedBounds = null;
1602 mPendingTempDockedTaskBounds = null;
1603 mPendingTempDockedTaskInsetBounds = null;
1604 mPendingTempOtherTaskBounds = null;
1605 mPendingTempOtherTaskInsetBounds = null;
1606 }
1607 }
1608
Jorim Jaggidc249c42015-12-15 14:57:31 -08001609 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001610 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
1611 boolean preserveWindows) {
1612 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
1613 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
1614 false /* deferResume */);
1615 }
1616
Wale Ogunwaled32da472018-11-16 07:19:28 -08001617 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001618 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
1619 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001620
1621 if (!mAllowDockedStackResize) {
1622 // Docked stack resize currently disabled.
1623 return;
1624 }
1625
Wale Ogunwaled32da472018-11-16 07:19:28 -08001626 final ActivityStack stack =
1627 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001628 if (stack == null) {
1629 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1630 return;
1631 }
1632
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001633 if (mDockedStackResizing) {
1634 mHasPendingDockedBounds = true;
1635 mPendingDockedBounds = copyOrNull(dockedBounds);
1636 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
1637 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
1638 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
1639 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
1640 }
1641
Jorim Jaggidc249c42015-12-15 14:57:31 -08001642 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1643 mWindowManager.deferSurfaceLayout();
1644 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001645 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
1646 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001647 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001648 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001649
Andrii Kulian69fb5e42016-04-05 16:47:29 -07001650 // TODO: Checking for isAttached might not be needed as if the user passes in null
1651 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08001652 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
1653 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07001654 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08001655 // In this case we make all other static stacks fullscreen and move all
1656 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001657 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001658
1659 // stack shouldn't contain anymore activities, so nothing to resume.
1660 r = null;
1661 } else {
1662 // Docked stacks occupy a dedicated region on screen so the size of all other
1663 // static stacks need to be adjusted so they don't overlap with the docked stack.
1664 // We get the bounds to use from window manager which has been adjusted for any
1665 // screen controls and is also the same for all stacks.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001666 final ActivityDisplay display = mRootActivityContainer.getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08001667 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001668 for (int i = display.getChildCount() - 1; i >= 0; --i) {
1669 final ActivityStack current = display.getChildAt(i);
Evan Roskyc5abbd82018-10-05 16:02:19 -07001670 if (!current.inSplitScreenSecondaryWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07001671 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001672 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08001673 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07001674 continue;
1675 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001676 if (mDockedStackResizing && !current.isTopActivityVisible()) {
1677 // Non-visible stacks get resized once we're done with the resize
1678 // interaction.
1679 continue;
1680 }
Evan Roskyc5abbd82018-10-05 16:02:19 -07001681 current.getStackDockedModeBounds(dockedBounds,
Wale Ogunwale926aade2017-08-29 11:24:37 -07001682 tempOtherTaskBounds /* currentTempTaskBounds */,
1683 tempRect /* outStackBounds */,
Evan Roskyc5abbd82018-10-05 16:02:19 -07001684 otherTaskRect /* outTempTaskBounds */);
Wale Ogunwale926aade2017-08-29 11:24:37 -07001685
Wale Ogunwaled32da472018-11-16 07:19:28 -08001686 mRootActivityContainer.resizeStack(current,
1687 !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07001688 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
1689 tempOtherTaskInsetBounds, preserveWindows,
1690 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001691 }
1692 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001693 if (!deferResume) {
1694 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
1695 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08001696 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001697 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001698 mWindowManager.continueSurfaceLayout();
1699 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1700 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08001701 }
1702
Robert Carr0d00c2e2016-02-29 17:45:02 -08001703 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Yunfan Chen279f5582018-12-12 15:24:50 -08001704 // TODO(multi-display): The display containing the stack should be passed in.
1705 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001706 mRootActivityContainer.getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08001707 if (stack == null) {
1708 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
1709 return;
1710 }
Winson Chung19953ca2017-04-11 11:19:23 -07001711
1712 // It is possible for the bounds animation from the WM to call this but be delayed by
1713 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
1714 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
1715 // for the AMS lock to be freed. So check and make sure these bounds are still good.
Yunfan Chen279f5582018-12-12 15:24:50 -08001716 final TaskStack stackController = stack.getTaskStack();
Winson Chung8bca9e42017-04-16 15:59:43 -07001717 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07001718 return;
1719 }
1720
Robert Carr0d00c2e2016-02-29 17:45:02 -08001721 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
1722 mWindowManager.deferSurfaceLayout();
1723 try {
1724 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08001725 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02001726 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
1727 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08001728 insetBounds = tempRect;
1729 insetBounds.top = 0;
1730 insetBounds.left = 0;
1731 insetBounds.right = tempPinnedTaskBounds.width();
1732 insetBounds.bottom = tempPinnedTaskBounds.height();
1733 }
Winson Chung15036ca2018-05-31 15:51:47 -07001734 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
1735 // We have finished the animation into PiP, and are resizing the tasks to match the
1736 // stack bounds, while layouts are deferred, update any task state as a part of
1737 // transitioning it from fullscreen into a floating state.
1738 stack.onPipAnimationEndResize();
1739 }
Robert Carr7e4c90e2017-02-15 19:52:38 -08001740 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07001741 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08001742 } finally {
1743 mWindowManager.continueSurfaceLayout();
1744 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1745 }
1746 }
1747
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001748 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08001749 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001750 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07001751 /**
1752 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
1753 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
1754 * that the client receives onStop() before it is reparented. We do this by detaching
1755 * the stack from the display so that it will be considered invisible when
1756 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
1757 * invisible as well and added to the stopping list. After which we process the
1758 * stopping list by handling the idle.
1759 */
Yunfan Chen279f5582018-12-12 15:24:50 -08001760 stack.mForceHidden = true;
1761 stack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
1762 stack.mForceHidden = false;
Winson Chung47900652017-04-06 18:44:25 -07001763 activityIdleInternalLocked(null, false /* fromTimeout */,
1764 true /* processPausingActivites */, null /* configuration */);
1765
1766 // Move all the tasks to the bottom of the fullscreen stack
Yunfan Chen279f5582018-12-12 15:24:50 -08001767 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08001768 } else {
1769 for (int i = tasks.size() - 1; i >= 0; i--) {
1770 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07001771 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08001772 }
1773 }
1774 }
1775
1776 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001777 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07001778 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
1779 * instead moved back onto the fullscreen stack.
1780 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001781 void removeStack(ActivityStack stack) {
1782 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001783 }
1784
Wale Ogunwale68278562017-09-23 17:13:55 -07001785 /**
Winson Chung6954fc92017-03-24 16:22:12 -07001786 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
1787 */
Winson Chung0ec2a352017-10-26 11:38:30 -07001788 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
1789 String reason) {
1790 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
1791 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07001792 }
1793
1794 /**
Winson Chung010927a2016-12-15 16:12:35 -08001795 * Removes the task with the specified task id.
1796 *
1797 * @param taskId Identifier of the task to be removed.
1798 * @param killProcess Kill any process associated with the task if possible.
1799 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07001800 * @param pauseImmediately Pauses all task activities immediately without waiting for the
1801 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08001802 * @return Returns true if the given task was found and removed.
1803 */
Winson Chung6954fc92017-03-24 16:22:12 -07001804 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07001805 boolean pauseImmediately, String reason) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001806 final TaskRecord tr =
1807 mRootActivityContainer.anyTaskForId(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08001808 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07001809 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08001810 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001811 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08001812 if (tr.isPersistable) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001813 mService.notifyTaskPersisterLocked(null, true);
Winson Chung010927a2016-12-15 16:12:35 -08001814 }
1815 return true;
1816 }
1817 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
1818 return false;
1819 }
1820
1821 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
1822 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07001823 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08001824 }
1825 ComponentName component = tr.getBaseIntent().getComponent();
1826 if (component == null) {
1827 Slog.w(TAG, "No component for base intent of task: " + tr);
1828 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,
1833 mService.mAmInternal, tr.userId, component, new Intent(tr.getBaseIntent()));
1834 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);
1850 if (proc.mUserId != tr.userId) {
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
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001863 if (!proc.shouldKillProcessForRemovedTask(tr)) {
1864 // 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 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07001895 boolean restoreRecentTaskLocked(TaskRecord 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();
1899 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001900 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001901 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001902 // Nothing else to do since it is already restored in the right stack.
1903 return true;
1904 }
1905 // Remove current stack association, so we can re-associate the task with the
1906 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001907 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001908 }
1909
Wale Ogunwaleab5de372017-10-18 06:46:31 -07001910 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001911 // TODO: move call for creation here and other place into Stack.addTask()
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001912 task.createTask(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001913 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
1914 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08001915 final ArrayList<ActivityRecord> activities = task.mActivities;
1916 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001917 activities.get(activityNdx).createAppWindowToken();
Wale Ogunwale7de05352014-12-12 15:21:33 -08001918 }
1919 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07001920 }
1921
Winson Chung1dbc8112017-09-28 18:05:31 -07001922 @Override
1923 public void onRecentTaskAdded(TaskRecord task) {
1924 task.touchActiveTime();
1925 }
1926
1927 @Override
Winson Chunge6439102018-07-30 15:48:01 -07001928 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed, boolean killProcess) {
Winson Chung0ec2a352017-10-26 11:38:30 -07001929 if (wasTrimmed) {
1930 // Task was trimmed from the recent tasks list -- remove the active task record as well
1931 // since the user won't really be able to go back to it
Winson Chunge6439102018-07-30 15:48:01 -07001932 removeTaskByIdLocked(task.taskId, killProcess, false /* removeFromRecents */,
1933 !PAUSE_IMMEDIATELY, "recent-task-trimmed");
Winson Chung0ec2a352017-10-26 11:38:30 -07001934 }
Winson Chung1dbc8112017-09-28 18:05:31 -07001935 task.removedFromRecents();
1936 }
1937
Wale Ogunwale040b4702015-08-06 18:10:50 -07001938 /**
Winson Chung74666102017-02-22 17:49:24 -08001939 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
1940 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07001941 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001942 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001943 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001944 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001945 final int stackId = stack.mStackId;
1946 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07001947
Winson Chung74666102017-02-22 17:49:24 -08001948 // Check that we aren't reparenting to the same stack that the task is already in
1949 if (prevStack != null && prevStack.mStackId == stackId) {
1950 Slog.w(TAG, "Can not reparent to same stack, task=" + task
1951 + " already in stackId=" + stackId);
1952 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08001953 }
1954
Winson Chung74666102017-02-22 17:49:24 -08001955 // Ensure that we aren't trying to move into a multi-window stack without multi-window
1956 // support
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001957 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08001958 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001959 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08001960 }
1961
Andrii Kulianeafd9db2017-04-05 22:01:35 -07001962 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
1963 // multi-display.
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001964 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07001965 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
1966 + " reparent task=" + task + " to stackId=" + stackId);
1967 }
1968
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001969 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001970 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001971 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08001972 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
1973 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08001974 }
1975
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001976 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
1977 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001978 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001979 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
1980 + " Moving to a fullscreen stack instead.");
1981 if (prevStack != null) {
1982 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001983 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001984 stack = stack.getDisplay().createStack(
1985 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001986 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001987 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001988 }
1989
1990 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001991 scheduleSleepTimeout();
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001992 if (!mGoingToSleepWakeLock.isHeld()) {
1993 mGoingToSleepWakeLock.acquire();
1994 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001995 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1996 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07001997 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001998 mLaunchingActivityWakeLock.release();
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001999 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002000 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002001 }
David Stevens9440dc82017-03-16 19:00:20 -07002002
Wale Ogunwaled32da472018-11-16 07:19:28 -08002003 mRootActivityContainer.applySleepTokens(false /* applyToStacks */);
David Stevens9440dc82017-03-16 19:00:20 -07002004
2005 checkReadyForSleepLocked(true /* allowDelay */);
2006 }
2007
Craig Mautner8d341ef2013-03-26 09:03:27 -07002008 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002009 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002010
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002011 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002012 final long endTime = System.currentTimeMillis() + timeout;
2013 while (true) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002014 if (!mRootActivityContainer.putStacksToSleep(
2015 true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002016 long timeRemaining = endTime - System.currentTimeMillis();
2017 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002018 try {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002019 mService.mGlobalLock.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002020 } catch (InterruptedException e) {
2021 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002022 } else {
2023 Slog.w(TAG, "Activity manager shutdown timed out");
2024 timedout = true;
2025 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002026 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002027 } else {
2028 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002029 }
2030 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002031
2032 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07002033 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002034
Craig Mautner8d341ef2013-03-26 09:03:27 -07002035 return timedout;
2036 }
2037
2038 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002039 removeSleepTimeouts();
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002040 if (mGoingToSleepWakeLock.isHeld()) {
2041 mGoingToSleepWakeLock.release();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002042 }
David Stevens9440dc82017-03-16 19:00:20 -07002043 }
2044
Craig Mautner0eea92c2013-05-16 13:35:39 -07002045 void activitySleptLocked(ActivityRecord r) {
2046 mGoingToSleepActivities.remove(r);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002047 final ActivityStack s = r.getActivityStack();
David Stevens9440dc82017-03-16 19:00:20 -07002048 if (s != null) {
2049 s.checkReadyForSleep();
2050 } else {
2051 checkReadyForSleepLocked(true);
2052 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002053 }
2054
David Stevens9440dc82017-03-16 19:00:20 -07002055 void checkReadyForSleepLocked(boolean allowDelay) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002056 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002057 // Do not care.
2058 return;
2059 }
2060
Wale Ogunwaled32da472018-11-16 07:19:28 -08002061 if (!mRootActivityContainer.putStacksToSleep(
2062 allowDelay, false /* shuttingDown */)) {
David Stevens18abd0e2017-08-17 14:55:47 -07002063 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002064 }
2065
Wei Wang65c7a152016-06-02 18:51:22 -07002066 // Send launch end powerhint before going sleep
Wale Ogunwaled32da472018-11-16 07:19:28 -08002067 mRootActivityContainer.sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07002068
Craig Mautner0eea92c2013-05-16 13:35:39 -07002069 removeSleepTimeouts();
2070
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002071 if (mGoingToSleepWakeLock.isHeld()) {
2072 mGoingToSleepWakeLock.release();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002073 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07002074 if (mService.mShuttingDown) {
2075 mService.mGlobalLock.notifyAll();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002076 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002077 }
2078
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002079 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07002080 // A resumed activity cannot be stopping. remove from list
2081 mStoppingActivities.remove(r);
2082
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002083 final ActivityStack stack = r.getActivityStack();
Riddle Hsubbb63c22018-10-03 12:28:29 +08002084 if (stack.getDisplay().allResumedActivitiesComplete()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002085 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
lumark588a3e82018-07-20 18:53:54 +08002086 // Make sure activity & window visibility should be identical
2087 // for all displays in this stage.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002088 mRootActivityContainer.executeAppTransitionForAllDisplay();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002089 return true;
2090 }
2091 return false;
2092 }
2093
Craig Mautnerbb742462014-07-07 15:28:55 -07002094 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002095 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002096 final TaskRecord task = r.getTaskRecord();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002097 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002098
2099 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07002100 mRecentTasks.add(task);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002101 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002102 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07002103
2104 // When launching tasks behind, update the last active time of the top task after the new
2105 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002106 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07002107 if (top != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002108 top.getTaskRecord().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07002109 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002110 }
2111
2112 void scheduleLaunchTaskBehindComplete(IBinder token) {
2113 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2114 }
2115
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002116 /** Checks whether the userid is a profile of the current user. */
2117 boolean isCurrentProfileLocked(int userId) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002118 if (userId == mRootActivityContainer.mCurrentUser) return true;
Wale Ogunwale86b74462018-07-02 08:42:43 -07002119 return mService.mAmInternal.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002120 }
2121
Bryce Leeb7c9b802017-05-02 14:20:24 -07002122 /**
2123 * Returns whether a stopping activity is present that should be stopped after visible, rather
2124 * than idle.
2125 * @return {@code true} if such activity is present. {@code false} otherwise.
2126 */
2127 boolean isStoppingNoHistoryActivity() {
2128 // Activities that are marked as nohistory should be stopped immediately after the resumed
2129 // activity has become visible.
2130 for (ActivityRecord record : mStoppingActivities) {
2131 if (record.isNoHistory()) {
2132 return true;
2133 }
2134 }
2135
2136 return false;
2137 }
2138
Winson Chung4dabf232017-01-25 13:25:22 -08002139 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
2140 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002141 ArrayList<ActivityRecord> stops = null;
2142
Wale Ogunwaled32da472018-11-16 07:19:28 -08002143 final boolean nowVisible = mRootActivityContainer.allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002144 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2145 ActivityRecord s = mStoppingActivities.get(activityNdx);
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002146
2147 final boolean animating = s.mAppWindowToken.isSelfAnimating();
2148
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002149 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002150 + " animating=" + animating + " finishing=" + s.finishing);
2151 if (nowVisible && s.finishing) {
2152
2153 // If this activity is finishing, it is sitting on top of
2154 // everyone else but we now know it is no longer needed...
2155 // so get rid of it. Otherwise, we need to go through the
2156 // normal flow and hide it once we determine that it is
2157 // hidden by the activities in front of it.
2158 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
2159 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002160 }
David Stevens9440dc82017-03-16 19:00:20 -07002161 if (remove) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002162 final ActivityStack stack = s.getActivityStack();
David Stevens9440dc82017-03-16 19:00:20 -07002163 final boolean shouldSleepOrShutDown = stack != null
2164 ? stack.shouldSleepOrShutDownActivities()
Wale Ogunwalef6733932018-06-27 05:14:34 -07002165 : mService.isSleepingOrShuttingDownLocked();
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002166 if (!animating || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08002167 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07002168 // Defer processing pausing activities in this iteration and reschedule
2169 // a delayed idle to reprocess it again
2170 removeTimeoutsForActivityLocked(idleActivity);
2171 scheduleIdleTimeoutLocked(idleActivity);
2172 continue;
2173 }
Winson Chung4dabf232017-01-25 13:25:22 -08002174
David Stevens9440dc82017-03-16 19:00:20 -07002175 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
2176 if (stops == null) {
2177 stops = new ArrayList<>();
2178 }
2179 stops.add(s);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02002180
David Stevens9440dc82017-03-16 19:00:20 -07002181 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002182 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002183 }
2184 }
2185
2186 return stops;
2187 }
2188
Craig Mautner27084302013-03-25 08:05:25 -07002189 public void dump(PrintWriter pw, String prefix) {
Riddle Hsu75016992018-09-20 20:37:14 +08002190 pw.println();
2191 pw.println("ActivityStackSupervisor state:");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002192 mRootActivityContainer.dump(pw, prefix);
Riddle Hsu75016992018-09-20 20:37:14 +08002193 pw.print(prefix);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002194 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Wale Ogunwaled32da472018-11-16 07:19:28 -08002195 pw.println(prefix + "mUserStackInFront=" + mRootActivityContainer.mUserStackInFront);
Bryce Lee4a194382017-04-04 14:32:48 -07002196 if (!mWaitingForActivityVisible.isEmpty()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002197 pw.println(prefix + "mWaitingForActivityVisible=");
Bryce Lee4a194382017-04-04 14:32:48 -07002198 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002199 pw.print(prefix + prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
Bryce Lee4a194382017-04-04 14:32:48 -07002200 }
2201 }
Winson Chungc1674272018-02-21 10:15:17 -08002202 pw.print(prefix); pw.print("isHomeRecentsComponent=");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002203 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mRootActivityContainer.mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07002204
Bryce Lee459c0622018-03-19 11:04:01 -07002205 getKeyguardController().dump(pw, prefix);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002206 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07002207 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002208
Dianne Hackborn390517b2013-05-30 15:03:32 -07002209 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2210 boolean needSep, String prefix) {
2211 if (activity != null) {
2212 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2213 if (needSep) {
2214 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002215 }
2216 pw.print(prefix);
2217 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002218 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002219 }
2220 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002221 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002222 }
2223
Dianne Hackborn390517b2013-05-30 15:03:32 -07002224 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002225 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07002226 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002227 String innerPrefix = null;
2228 String[] args = null;
2229 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002230 for (int i=list.size()-1; i>=0; i--) {
2231 final ActivityRecord r = list.get(i);
2232 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2233 continue;
2234 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002235 if (innerPrefix == null) {
2236 innerPrefix = prefix + " ";
2237 args = new String[0];
2238 }
2239 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002240 final boolean full = !brief && (complete || !r.isInHistory());
2241 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002242 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002243 needNL = false;
2244 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07002245 if (header != null) {
2246 pw.println(header);
2247 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002248 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002249 if (lastTask != r.getTaskRecord()) {
2250 lastTask = r.getTaskRecord();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002251 pw.print(prefix);
2252 pw.print(full ? "* " : " ");
2253 pw.println(lastTask);
2254 if (full) {
2255 lastTask.dump(pw, prefix + " ");
2256 } else if (complete) {
2257 // Complete + brief == give a summary. Isn't that obvious?!?
2258 if (lastTask.intent != null) {
2259 pw.print(prefix); pw.print(" ");
2260 pw.println(lastTask.intent.toInsecureStringWithClip());
2261 }
2262 }
2263 }
2264 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2265 pw.print(" #"); pw.print(i); pw.print(": ");
2266 pw.println(r);
2267 if (full) {
2268 r.dump(pw, innerPrefix);
2269 } else if (complete) {
2270 // Complete + brief == give a summary. Isn't that obvious?!?
2271 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2272 if (r.app != null) {
2273 pw.print(innerPrefix); pw.println(r.app);
2274 }
2275 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002276 if (client && r.attachedToProcess()) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002277 // flush anything that is already in the PrintWriter since the thread is going
2278 // to write to the file descriptor directly
2279 pw.flush();
2280 try {
2281 TransferPipe tp = new TransferPipe();
2282 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002283 r.app.getThread().dumpActivity(
2284 tp.getWriteFd(), r.appToken, innerPrefix, args);
2285 // Short timeout, since blocking here can deadlock with the application.
Craig Mautner8d341ef2013-03-26 09:03:27 -07002286 tp.go(fd, 2000);
2287 } finally {
2288 tp.kill();
2289 }
2290 } catch (IOException e) {
2291 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2292 } catch (RemoteException e) {
2293 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2294 }
2295 needNL = true;
2296 }
2297 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002298 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002299 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002300
Craig Mautnerf3333272013-04-22 10:55:53 -07002301 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002302 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2303 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002304 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2305 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002306 }
2307
2308 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002309 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002310 }
2311
Andrii Kulian86e70fc2019-02-12 11:04:10 +00002312 /**
2313 * Updates the record of top resumed activity when it changes and handles reporting of the
2314 * state changes to previous and new top activities. It will immediately dispatch top resumed
2315 * state loss message to previous top activity (if haven't done it already). After the previous
2316 * activity releases the top state and reports back, message about acquiring top state will be
2317 * sent to the new top resumed activity.
2318 */
2319 void updateTopResumedActivityIfNeeded() {
2320 final ActivityRecord prevTopActivity = mTopResumedActivity;
2321 final ActivityStack topStack = mRootActivityContainer.getTopDisplayFocusedStack();
2322 if (topStack == null || topStack.mResumedActivity == prevTopActivity) {
2323 return;
2324 }
2325
2326 // Ask previous activity to release the top state.
2327 final boolean prevActivityReceivedTopState =
2328 prevTopActivity != null && !mTopResumedActivityWaitingForPrev;
2329 // mTopResumedActivityWaitingForPrev == true at this point would mean that an activity
2330 // before the prevTopActivity one hasn't reported back yet. So server never sent the top
2331 // resumed state change message to prevTopActivity.
2332 if (prevActivityReceivedTopState) {
2333 prevTopActivity.scheduleTopResumedActivityChanged(false /* onTop */);
2334 scheduleTopResumedStateLossTimeout(prevTopActivity);
2335 mTopResumedActivityWaitingForPrev = true;
2336 }
2337
2338 // Update the current top activity.
2339 mTopResumedActivity = topStack.mResumedActivity;
2340 scheduleTopResumedActivityStateIfNeeded();
2341 }
2342
2343 /** Schedule top resumed state change if previous top activity already reported back. */
2344 private void scheduleTopResumedActivityStateIfNeeded() {
2345 if (mTopResumedActivity != null && !mTopResumedActivityWaitingForPrev) {
2346 mTopResumedActivity.scheduleTopResumedActivityChanged(true /* onTop */);
2347 }
2348 }
2349
2350 /**
2351 * Limit the time given to the app to report handling of the state loss.
2352 */
2353 private void scheduleTopResumedStateLossTimeout(ActivityRecord r) {
2354 final Message msg = mHandler.obtainMessage(TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG);
2355 msg.obj = r;
2356 r.topResumedStateLossTime = SystemClock.uptimeMillis();
2357 mHandler.sendMessageDelayed(msg, TOP_RESUMED_STATE_LOSS_TIMEOUT);
2358 if (DEBUG_STATES) Slog.v(TAG_STATES, "Waiting for top state to be released by " + r);
2359 }
2360
2361 /**
2362 * Handle a loss of top resumed state by an activity - update internal state and inform next top
2363 * activity if needed.
2364 */
2365 void handleTopResumedStateReleased(boolean timeout) {
2366 if (DEBUG_STATES) {
2367 Slog.v(TAG_STATES, "Top resumed state released "
2368 + (timeout ? " (due to timeout)" : " (transition complete)"));
2369 }
2370 mHandler.removeMessages(TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG);
2371 if (!mTopResumedActivityWaitingForPrev) {
2372 // Top resumed activity state loss already handled.
2373 return;
2374 }
2375 mTopResumedActivityWaitingForPrev = false;
2376 scheduleTopResumedActivityStateIfNeeded();
2377 }
2378
Craig Mautnerf3333272013-04-22 10:55:53 -07002379 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002380 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2381 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002382 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2383 }
2384
Craig Mautner05d29032013-05-03 13:40:13 -07002385 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002386 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2387 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2388 }
Craig Mautner05d29032013-05-03 13:40:13 -07002389 }
2390
Craig Mautner0eea92c2013-05-16 13:35:39 -07002391 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002392 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2393 }
2394
2395 final void scheduleSleepTimeout() {
2396 removeSleepTimeouts();
2397 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2398 }
2399
Riddle Hsu7b766fd2019-01-28 21:14:59 +08002400 void removeRestartTimeouts(ActivityRecord r) {
2401 mHandler.removeMessages(RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG, r);
2402 }
2403
2404 final void scheduleRestartTimeout(ActivityRecord r) {
2405 removeRestartTimeouts(r);
2406 mHandler.sendMessageDelayed(mHandler.obtainMessage(RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG, r),
2407 WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION);
2408 }
2409
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002410 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002411 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002412 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002413 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07002414 }
2415
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002416 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002417 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002418 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002419 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002420 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002421 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002422 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002423 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002424 return;
2425 }
2426
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002427 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002428 if (isSecondaryDisplayPreferred) {
2429 final int actualDisplayId = task.getStack().mDisplayId;
2430 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08002431 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002432 }
Jeff Chang78f54412019-02-14 20:42:27 +08002433
2434 final ActivityDisplay preferredDisplay =
2435 mRootActivityContainer.getActivityDisplay(preferredDisplayId);
2436
2437 final boolean singleTaskInstance = preferredDisplay != null
2438 && preferredDisplay.isSingleTaskInstance();
2439
Mark Renouf2f9ea1e2019-03-05 14:49:06 -05002440 if (preferredDisplayId != actualDisplayId) {
Jeff Chang78f54412019-02-14 20:42:27 +08002441 // Suppress the warning toast if the preferredDisplay was set to singleTask.
2442 // The singleTaskInstance displays will only contain one task and any attempt to
2443 // launch new task will re-route to the default display.
Mark Renouf2f9ea1e2019-03-05 14:49:06 -05002444 if (singleTaskInstance) {
2445 mService.getTaskChangeNotificationController()
2446 .notifyActivityLaunchOnSecondaryDisplayRerouted(task.getTaskInfo(),
2447 preferredDisplayId);
2448 return;
2449 }
Jeff Chang78f54412019-02-14 20:42:27 +08002450
Riddle Hsu16567132018-08-16 21:37:47 +08002451 Slog.w(TAG, "Failed to put " + task + " on display " + preferredDisplayId);
2452 // Display a warning toast that we failed to put a task on a secondary display.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002453 mService.getTaskChangeNotificationController()
Mark Renoufc808f062019-02-07 15:20:37 -05002454 .notifyActivityLaunchOnSecondaryDisplayFailed(task.getTaskInfo(),
2455 preferredDisplayId);
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002456 return;
Riddle Hsu16567132018-08-16 21:37:47 +08002457 } else if (!forceNonResizable && handleForcedResizableTask(task,
2458 FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY)) {
2459 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002460 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002461 }
2462
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002463 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002464 // Display a warning toast that we tried to put an app that doesn't support split-screen
2465 // in split-screen.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002466 mService.getTaskChangeNotificationController().notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07002467
Andrii Kulianc27916642016-04-12 17:59:27 -07002468 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
2469 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002470
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002471 final ActivityStack dockedStack =
2472 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002473 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002474 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002475 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002476 return;
2477 }
2478
Riddle Hsu16567132018-08-16 21:37:47 +08002479 handleForcedResizableTask(task, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN);
2480 }
2481
2482 /**
2483 * @return {@code true} if the top activity of the task is forced to be resizable and the user
2484 * was notified about activity being forced resized.
2485 */
2486 private boolean handleForcedResizableTask(TaskRecord task, int reason) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002487 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002488 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Riddle Hsu16567132018-08-16 21:37:47 +08002489 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002490 final String packageName = topActivity.appInfo.packageName;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002491 mService.getTaskChangeNotificationController().notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002492 task.taskId, reason, packageName);
Riddle Hsu16567132018-08-16 21:37:47 +08002493 return true;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002494 }
Riddle Hsu16567132018-08-16 21:37:47 +08002495 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -08002496 }
2497
Jorim Jaggife89d122015-12-22 16:28:44 +01002498 void activityRelaunchedLocked(IBinder token) {
2499 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07002500 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2501 if (r != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002502 if (r.getActivityStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07002503 r.setSleeping(true, true);
2504 }
2505 }
Jorim Jaggife89d122015-12-22 16:28:44 +01002506 }
2507
2508 void activityRelaunchingLocked(ActivityRecord r) {
2509 mWindowManager.notifyAppRelaunching(r.appToken);
2510 }
2511
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002512 void logStackState() {
2513 mActivityMetricsLogger.logWindowState();
2514 }
2515
Winson Chung5af42fc2017-03-24 17:11:33 -07002516 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07002517 // If the stack is animating in a way where we will be forcing a multi-mode change at the
2518 // end, then ensure that we defer all in between multi-window mode changes
2519 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
2520 return;
2521 }
2522
Wale Ogunwale22e25262016-02-01 10:32:02 -08002523 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
2524 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002525 if (r.attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002526 mMultiWindowModeChangedActivities.add(r);
2527 }
2528 }
2529
2530 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
2531 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
2532 }
2533 }
2534
Winson Chung5af42fc2017-03-24 17:11:33 -07002535 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002536 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002537 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002538 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002539 return;
2540 }
2541
Evan Roskydfe3da72018-10-26 17:21:06 -07002542 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getRequestedOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07002543 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08002544
Winson Chungab76bbc2017-08-14 13:33:51 -07002545 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
2546 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
2547 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002548 if (r.attachedToProcess()) {
Winson Chungab76bbc2017-08-14 13:33:51 -07002549 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08002550 // If we are scheduling pip change, then remove this activity from multi-window
2551 // change list as the processing of pip change will make sure multi-window changed
2552 // message is processed in the right order relative to pip changed.
2553 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07002554 }
Winson Chungab76bbc2017-08-14 13:33:51 -07002555 }
2556 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07002557
Winson Chungab76bbc2017-08-14 13:33:51 -07002558 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
2559 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
2560 }
2561 }
2562
2563 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
2564 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
2565 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
2566 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002567 if (r.attachedToProcess()) {
Winson Chungab76bbc2017-08-14 13:33:51 -07002568 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07002569 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08002570 }
2571 }
2572
chaviw59b98852017-06-13 12:05:44 -07002573 void wakeUp(String reason) {
Michael Wrighte3001042019-02-05 00:13:14 +00002574 mPowerManager.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_APPLICATION,
2575 "android.server.am:TURN_ON:" + reason);
chaviw59b98852017-06-13 12:05:44 -07002576 }
2577
2578 /**
2579 * Begin deferring resume to avoid duplicate resumes in one pass.
2580 */
Riddle Hsu402b4402018-11-06 17:23:15 +08002581 void beginDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07002582 mDeferResumeCount++;
2583 }
2584
2585 /**
2586 * End deferring resume and determine if resume can be called.
2587 */
Riddle Hsu402b4402018-11-06 17:23:15 +08002588 void endDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07002589 mDeferResumeCount--;
2590 }
2591
Wale Ogunwaled32da472018-11-16 07:19:28 -08002592 /** @return True if resume can be called. */
2593 boolean readyToResume() {
chaviw59b98852017-06-13 12:05:44 -07002594 return mDeferResumeCount == 0;
2595 }
2596
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002597 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002598
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002599 public ActivityStackSupervisorHandler(Looper looper) {
2600 super(looper);
2601 }
2602
Winson Chung4dabf232017-01-25 13:25:22 -08002603 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002604 synchronized (mService.mGlobalLock) {
Winson Chung4dabf232017-01-25 13:25:22 -08002605 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
2606 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002608 }
2609
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002610 @Override
2611 public void handleMessage(Message msg) {
2612 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002613 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002614 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002615 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
2616 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07002617 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002618 }
2619 }
2620 } break;
2621 case REPORT_PIP_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002622 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002623 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
2624 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07002625 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
2626 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08002627 }
2628 }
2629 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07002630 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002631 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2632 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002633 // We don't at this point know if the activity is fullscreen,
2634 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08002635 activityIdleInternal((ActivityRecord) msg.obj,
2636 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07002637 } break;
2638 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002639 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08002640 activityIdleInternal((ActivityRecord) msg.obj,
2641 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07002642 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07002643 case RESUME_TOP_ACTIVITY_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002644 synchronized (mService.mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002645 mRootActivityContainer.resumeFocusedStacksTopActivities();
Craig Mautner05d29032013-05-03 13:40:13 -07002646 }
2647 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002648 case SLEEP_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002649 synchronized (mService.mGlobalLock) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002650 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002651 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07002652 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002653 }
2654 }
2655 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002656 case LAUNCH_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002657 synchronized (mService.mGlobalLock) {
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002658 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002659 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
2660 if (VALIDATE_WAKE_LOCK_CALLER
2661 && Binder.getCallingUid() != Process.myUid()) {
2662 throw new IllegalStateException("Calling must be system uid");
2663 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002664 mLaunchingActivityWakeLock.release();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002665 }
2666 }
2667 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07002668 case LAUNCH_TASK_BEHIND_COMPLETE: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002669 synchronized (mService.mGlobalLock) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002670 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07002671 if (r != null) {
2672 handleLaunchTaskBehindCompleteLocked(r);
2673 }
2674 }
2675 } break;
Riddle Hsu7b766fd2019-01-28 21:14:59 +08002676 case RESTART_ACTIVITY_PROCESS_TIMEOUT_MSG: {
2677 final ActivityRecord r = (ActivityRecord) msg.obj;
2678 String processName = null;
2679 int uid = 0;
2680 synchronized (mService.mGlobalLock) {
2681 if (r.attachedToProcess()
2682 && r.isState(ActivityStack.ActivityState.RESTARTING_PROCESS)) {
2683 processName = r.app.mName;
2684 uid = r.app.mUid;
2685 }
2686 }
2687 if (processName != null) {
2688 mService.mAmInternal.killProcess(processName, uid,
2689 "restartActivityProcessTimeout");
2690 }
2691 } break;
Louis Changdcdde952018-12-04 15:38:44 +08002692 case REPORT_HOME_CHANGED_MSG: {
2693 synchronized (mService.mGlobalLock) {
2694 mHandler.removeMessages(REPORT_HOME_CHANGED_MSG);
Chong Zhangc806d902015-11-30 09:44:27 -08002695
Louis Changdcdde952018-12-04 15:38:44 +08002696 // Start home activities on displays with no activities.
2697 mRootActivityContainer.startHomeOnEmptyDisplays("homeChanged");
2698 }
Andrii Kulian86e70fc2019-02-12 11:04:10 +00002699 } break;
2700 case TOP_RESUMED_STATE_LOSS_TIMEOUT_MSG: {
2701 ActivityRecord r = (ActivityRecord) msg.obj;
2702 Slog.w(TAG, "Activity top resumed state loss timeout for " + r);
2703 synchronized (mService.mGlobalLock) {
2704 if (r.hasProcess()) {
2705 mService.logAppTooSlow(r.app, r.topResumedStateLossTime,
2706 "top state loss for " + r);
2707 }
2708 }
2709 handleTopResumedStateReleased(true /* timeout */);
2710 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002711 }
2712 }
2713 }
Craig Mautnered6649f2013-12-02 14:08:25 -08002714
Jorim Jaggic69bd222016-03-15 14:38:37 +01002715 /**
2716 * Puts a task into resizing mode during the next app transition.
2717 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002718 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01002719 */
Bryce Leed3624e12017-11-30 08:51:45 -08002720 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002721 mResizingTasksDuringAnimation.add(task.taskId);
2722 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01002723 }
2724
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002725 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
2726 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002727 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002728 final String callingPackage;
2729 final Intent intent;
2730 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002731 int activityType = ACTIVITY_TYPE_UNDEFINED;
2732 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002733 final ActivityOptions activityOptions = options != null
2734 ? options.getOptions(this)
2735 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002736 if (activityOptions != null) {
2737 activityType = activityOptions.getLaunchActivityType();
2738 windowingMode = activityOptions.getLaunchWindowingMode();
2739 }
2740 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002741 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002742 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002743 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07002744
Matthew Ng606dd802017-06-05 14:06:32 -07002745 mWindowManager.deferSurfaceLayout();
2746 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002747 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07002748 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07002749 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07002750
Matthew Ng606dd802017-06-05 14:06:32 -07002751 // Defer updating the stack in which recents is until the app transition is done, to
2752 // not run into issues where we still need to draw the task in recents but the
2753 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08002754 deferUpdateRecentsHomeStackBounds();
lumark588a3e82018-07-20 18:53:54 +08002755 // TODO(multi-display): currently recents animation only support default display.
Matthew Ng606dd802017-06-05 14:06:32 -07002756 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002757 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002758
Wale Ogunwaled32da472018-11-16 07:19:28 -08002759 task = mRootActivityContainer.anyTaskForId(taskId,
2760 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07002761 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08002762 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07002763 mWindowManager.executeAppTransition();
2764 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002765 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07002766 }
2767
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08002768 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
2769 // We always want to return to the home activity instead of the recents activity
2770 // from whatever is started from the recents activity, so move the home stack
2771 // forward.
Louis Changbd48dca2018-08-29 17:44:34 +08002772 // TODO (b/115289124): Multi-display supports for recents.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002773 mRootActivityContainer.getDefaultDisplay().moveHomeStackToFront(
2774 "startActivityFromRecents");
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08002775 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002776
Matthew Ng606dd802017-06-05 14:06:32 -07002777 // If the user must confirm credentials (e.g. when first launching a work app and the
2778 // Work Challenge is present) let startActivityInPackage handle the intercepting.
Wale Ogunwale86b74462018-07-02 08:42:43 -07002779 if (!mService.mAmInternal.shouldConfirmCredentials(task.userId)
Matthew Ng606dd802017-06-05 14:06:32 -07002780 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07002781 final ActivityRecord targetActivity = task.getTopActivity();
2782
Wale Ogunwaled32da472018-11-16 07:19:28 -08002783 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(
2784 true /* forceSend */, targetActivity);
Igor Murashkin212d06c2018-10-22 16:34:39 -07002785 mActivityMetricsLogger.notifyActivityLaunching(task.intent);
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002786 try {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002787 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002788 true /* fromRecents */);
lumark5e8aff42019-01-11 22:22:59 +08002789 // Apply options to prevent pendingOptions be taken by client to make sure
2790 // the override pending app transition will be applied immediately.
2791 targetActivity.applyOptionsLocked();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002792 } finally {
2793 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
2794 targetActivity);
2795 }
Matthew Ng606dd802017-06-05 14:06:32 -07002796
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002797 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
Bryce Leed3624e12017-11-30 08:51:45 -08002798 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
2799 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07002800 return ActivityManager.START_TASK_TO_FRONT;
2801 }
Matthew Ng606dd802017-06-05 14:06:32 -07002802 callingPackage = task.mCallingPackage;
2803 intent = task.intent;
2804 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
2805 userId = task.userId;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002806 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002807 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07002808 null, 0, 0, options, userId, task, "startActivityFromRecents",
Michal Karpinskiac116df2018-12-10 17:51:42 +00002809 false /* validateIncomingUser */, null /* originatingPendingIntent */,
2810 false /* allowBackgroundActivityStart */);
Matthew Ng606dd802017-06-05 14:06:32 -07002811 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002812 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
2813 // If we are launching the task in the docked stack, put it into resizing mode so
2814 // the window renders full-screen with the background filling the void. Also only
2815 // call this at the end to make sure that tasks exists on the window manager side.
2816 setResizingDuringAnimation(task);
2817
2818 final ActivityDisplay display = task.getStack().getDisplay();
2819 final ActivityStack topSecondaryStack =
2820 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2821 if (topSecondaryStack.isActivityTypeHome()) {
Louis Changbd48dca2018-08-29 17:44:34 +08002822 // If the home activity is the top split-screen secondary stack, then the
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002823 // primary split-screen stack is in the minimized mode which means it can't
2824 // receive input keys, so we should move the focused app to the home app so that
2825 // window manager can correctly calculate the focus window that can receive
2826 // input keys.
Louis Changbd48dca2018-08-29 17:44:34 +08002827 display.moveHomeStackToFront(
2828 "startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08002829
2830 // Immediately update the minimized docked stack mode, the upcoming animation
2831 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
2832 // will do the animation to the target bounds
2833 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002834 }
2835 }
Matthew Ng606dd802017-06-05 14:06:32 -07002836 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002837 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002838 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07002839
2840 /**
Bryce Lee4a194382017-04-04 14:32:48 -07002841 * Internal container to store a match qualifier alongside a WaitResult.
2842 */
2843 static class WaitInfo {
2844 private final ComponentName mTargetComponent;
2845 private final WaitResult mResult;
Vishnu Nair132ee832018-09-28 15:00:05 -07002846 /** Time stamp when we started to wait for {@link WaitResult}. */
2847 private final long mStartTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07002848
Vishnu Nair132ee832018-09-28 15:00:05 -07002849 WaitInfo(ComponentName targetComponent, WaitResult result, long startTimeMs) {
Bryce Lee4a194382017-04-04 14:32:48 -07002850 this.mTargetComponent = targetComponent;
2851 this.mResult = result;
Vishnu Nair132ee832018-09-28 15:00:05 -07002852 this.mStartTimeMs = startTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07002853 }
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
Vishnu Nair132ee832018-09-28 15:00:05 -07002863 public long getStartTime() {
2864 return mStartTimeMs;
2865 }
2866
Bryce Lee4a194382017-04-04 14:32:48 -07002867 public ComponentName getComponent() {
2868 return mTargetComponent;
2869 }
2870
2871 public void dump(PrintWriter pw, String prefix) {
2872 pw.println(prefix + "WaitInfo:");
2873 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
2874 pw.println(prefix + " mResult=");
2875 mResult.dump(pw, prefix);
2876 }
2877 }
Craig Mautner27084302013-03-25 08:05:25 -07002878}