blob: df760306f12ab486a7168fa77b173120ffa9e168 [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
Craig Mautner05d29032013-05-03 13:40:13 -0700173 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
174 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
175 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700176 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700177 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700178 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800179 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
180 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Louis Changdcdde952018-12-04 15:38:44 +0800181 static final int REPORT_HOME_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 16;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800182
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700183 // Used to indicate that windows of activities should be preserved during the resize.
184 static final boolean PRESERVE_WINDOWS = true;
185
Wale Ogunwale040b4702015-08-06 18:10:50 -0700186 // Used to indicate if an object (e.g. task) should be moved/created
187 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700188 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700189
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700190 // Don't execute any calls to resume.
191 static final boolean DEFER_RESUME = true;
192
Winson Chung010927a2016-12-15 16:12:35 -0800193 // Used to indicate that a task is removed it should also be removed from recents.
194 static final boolean REMOVE_FROM_RECENTS = true;
195
Winson Chung6954fc92017-03-24 16:22:12 -0700196 // Used to indicate that pausing an activity should occur immediately without waiting for
197 // the activity callback indicating that it has completed pausing
198 static final boolean PAUSE_IMMEDIATELY = true;
199
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200200 /** True if the docked stack is currently being resized. */
201 private boolean mDockedStackResizing;
202
203 /**
204 * True if there are pending docked bounds that need to be applied after
205 * {@link #mDockedStackResizing} is reset to false.
206 */
207 private boolean mHasPendingDockedBounds;
208 private Rect mPendingDockedBounds;
209 private Rect mPendingTempDockedTaskBounds;
210 private Rect mPendingTempDockedTaskInsetBounds;
211 private Rect mPendingTempOtherTaskBounds;
212 private Rect mPendingTempOtherTaskInsetBounds;
213
Svetoslav7008b512015-06-24 18:47:07 -0700214 // Activity actions an app cannot start if it uses a permission which is not granted.
215 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
216 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700217
Svetoslav7008b512015-06-24 18:47:07 -0700218 static {
219 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
220 Manifest.permission.CAMERA);
221 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
222 Manifest.permission.CAMERA);
223 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
224 Manifest.permission.CALL_PHONE);
225 }
226
Svet Ganov99b60432015-06-27 13:15:22 -0700227 /** Action restriction: launching the activity is not restricted. */
228 private static final int ACTIVITY_RESTRICTION_NONE = 0;
229 /** Action restriction: launching the activity is restricted by a permission. */
230 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
231 /** Action restriction: launching the activity is restricted by an app op. */
232 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700233
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700234 // For debugging to make sure the caller when acquiring/releasing our
235 // wake lock is the system process.
236 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800237 /** The number of distinct task ids that can be assigned to the tasks of a single user */
238 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700239
Wale Ogunwaleb73f3962018-11-20 07:58:22 -0800240 final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800241 RootActivityContainer mRootActivityContainer;
Craig Mautner27084302013-03-25 08:05:25 -0700242
Winson Chung61c9e5a2017-10-11 10:39:32 -0700243 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700244 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800245
Winson Chung61c9e5a2017-10-11 10:39:32 -0700246 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Wale Ogunwaled32da472018-11-16 07:19:28 -0800247 RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700248
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700249 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000250 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700251
252 /** Short cut */
253 WindowManagerService mWindowManager;
254
Garfield Tan891146c2018-10-09 12:14:00 -0700255 /** Common synchronization logic used to save things to disks. */
256 PersisterQueue mPersisterQueue;
257 LaunchParamsPersister mLaunchParamsPersister;
Bryce Leeec55eb02017-12-05 20:51:27 -0800258 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700259
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800260 /**
261 * Maps the task identifier that activities are currently being started in to the userId of the
262 * task. Each time a new task is created, the entry for the userId of the task is incremented
263 */
264 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700265
Craig Mautnerde4ef022013-04-07 19:01:33 -0700266 /** List of activities that are waiting for a new activity to become visible before completing
267 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700268 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
269 // mStoppingActivities when something else comes up.
270 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700271
Bryce Lee4a194382017-04-04 14:32:48 -0700272 /** List of processes waiting to find out when a specific activity becomes visible. */
273 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700274
275 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700276 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700277
Craig Mautnerde4ef022013-04-07 19:01:33 -0700278 /** List of activities that are ready to be stopped, but waiting for the next activity to
279 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800280 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700281
Craig Mautnerf3333272013-04-22 10:55:53 -0700282 /** List of activities that are ready to be finished, but waiting for the previous activity to
283 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800284 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Craig Mautner0eea92c2013-05-16 13:35:39 -0700286 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800287 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700288
Wale Ogunwale22e25262016-02-01 10:32:02 -0800289 /** List of activities whose multi-window mode changed that we need to report to the
290 * application */
291 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
292
293 /** List of activities whose picture-in-picture mode changed that we need to report to the
294 * application */
295 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
296
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100297 /**
298 * Animations that for the current transition have requested not to
299 * be considered for the transition animation.
300 */
301 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
302
Winson Chung5af42fc2017-03-24 17:11:33 -0700303 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
304 * the application */
305 Rect mPipModeChangedTargetStackBounds;
306
Craig Mautnerf3333272013-04-22 10:55:53 -0700307 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700308 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700309
Craig Mautnerde4ef022013-04-07 19:01:33 -0700310 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
311 * is being brought in front of us. */
312 boolean mUserLeaving = false;
313
Craig Mautner0eea92c2013-05-16 13:35:39 -0700314 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700315 * We don't want to allow the device to go to sleep while in the process
316 * of launching an activity. This is primarily to allow alarm intent
317 * receivers to launch an activity and get that to run before the device
318 * goes back to sleep.
319 */
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800320 PowerManager.WakeLock mLaunchingActivityWakeLock;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700321
322 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700323 * Set when the system is going to sleep, until we have
324 * successfully paused the current activity and released our wake lock.
325 * At that point the system is allowed to actually sleep.
326 */
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800327 PowerManager.WakeLock mGoingToSleepWakeLock;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700328
Andrii Kulian1e32e022016-09-16 15:29:34 -0700329 /**
330 * Temporary rect used during docked stack resize calculation so we don't need to create a new
331 * object each time.
332 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700333 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700334 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700335
Bryce Lee2a3cc462017-10-27 10:57:35 -0700336 private ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800337
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700338 /** Check if placing task or activity on specified display is allowed. */
Riddle Hsu16567132018-08-16 21:37:47 +0800339 boolean canPlaceEntityOnDisplay(int displayId, int callingPid, int callingUid,
340 ActivityInfo activityInfo) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700341 if (displayId == DEFAULT_DISPLAY) {
342 // No restrictions for the default display.
343 return true;
344 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700345 if (!mService.mSupportsMultiDisplay) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700346 // Can't launch on secondary displays if feature is not supported.
347 return false;
348 }
Andrii Kulian02689a72017-07-06 14:28:59 -0700349 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
350 // Can't place activities to a display that has restricted launch rules.
351 // In this case the request should be made by explicitly adding target display id and
352 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
353 return false;
354 }
355 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700356 }
357
358 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100359 * Used to keep track whether app visibilities got changed since the last pause. Useful to
360 * determine whether to invoke the task stack change listener after pausing.
361 */
362 boolean mAppVisibilitiesChangedSinceLastPause;
363
364 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100365 * Set of tasks that are in resizing mode during an app transition to fill the "void".
366 */
367 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
368
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700369
370 /**
371 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
372 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
373 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
374 * like the docked stack going empty.
375 */
376 private boolean mAllowDockedStackResize = true;
377
Bryce Lee2a3cc462017-10-27 10:57:35 -0700378 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200379
chaviw59b98852017-06-13 12:05:44 -0700380 private PowerManager mPowerManager;
381 private int mDeferResumeCount;
382
Bryce Lee2a3cc462017-10-27 10:57:35 -0700383 private boolean mInitialized;
384
Tony Mak853304c2016-04-18 15:17:41 +0100385 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700386 * Description of a request to start a new activity, which has been held
387 * due to app switches being disabled.
388 */
389 static class PendingActivityLaunch {
390 final ActivityRecord r;
391 final ActivityRecord sourceRecord;
392 final int startFlags;
393 final ActivityStack stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700394 final WindowProcessController callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700395
396 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700397 int _startFlags, ActivityStack _stack, WindowProcessController app) {
Craig Mautneree36c772014-07-16 14:56:05 -0700398 r = _r;
399 sourceRecord = _sourceRecord;
400 startFlags = _startFlags;
401 stack = _stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700402 callerApp = app;
Robert Carr13997f52015-10-23 13:13:39 -0700403 }
404
405 void sendErrorResult(String message) {
406 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700407 if (callerApp.hasThread()) {
408 callerApp.getThread().scheduleCrash(message);
Robert Carr13997f52015-10-23 13:13:39 -0700409 }
410 } catch (RemoteException e) {
411 Slog.e(TAG, "Exception scheduling crash of failed "
412 + "activity launcher sourceRecord=" + sourceRecord, e);
413 }
Craig Mautneree36c772014-07-16 14:56:05 -0700414 }
415 }
416
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700417 public ActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700418 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000419 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700420 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700421 }
422
423 public void initialize() {
424 if (mInitialized) {
425 return;
426 }
427
428 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700429 mRunningTasks = createRunningTasks();
Igor Murashkinc0b47e42018-11-07 15:54:18 -0800430
431 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext,
432 mHandler.getLooper());
Wale Ogunwalef6733932018-06-27 05:14:34 -0700433 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700434
Garfield Tan891146c2018-10-09 12:14:00 -0700435 mPersisterQueue = new PersisterQueue();
436 mLaunchParamsPersister = new LaunchParamsPersister(mPersisterQueue, this);
437 mLaunchParamsController = new LaunchParamsController(mService, mLaunchParamsPersister);
Bryce Leeec55eb02017-12-05 20:51:27 -0800438 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700439 }
440
Garfield Tan891146c2018-10-09 12:14:00 -0700441 void onSystemReady() {
442 mPersisterQueue.startPersisting();
443 mLaunchParamsPersister.onSystemReady();
444 }
Bryce Lee2a3cc462017-10-27 10:57:35 -0700445
446 public ActivityMetricsLogger getActivityMetricsLogger() {
447 return mActivityMetricsLogger;
448 }
449
450 public KeyguardController getKeyguardController() {
451 return mKeyguardController;
452 }
453
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800454 void setRecentTasks(RecentTasks recentTasks) {
455 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700456 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800457 }
458
Winson Chung3f0e59a2017-10-25 10:19:05 -0700459 @VisibleForTesting
460 RunningTasks createRunningTasks() {
461 return new RunningTasks();
462 }
463
Jeff Brown2c43c332014-06-12 22:38:59 -0700464 /**
465 * At the time when the constructor runs, the power manager has not yet been
466 * initialized. So we initialize our wakelocks afterwards.
467 */
468 void initPowerManagement() {
Michael Wrighte3001042019-02-05 00:13:14 +0000469 mPowerManager = mService.mContext.getSystemService(PowerManager.class);
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800470 mGoingToSleepWakeLock = mPowerManager
chaviw59b98852017-06-13 12:05:44 -0700471 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Andrii Kulianc598b2d2019-02-07 17:16:38 -0800472 mLaunchingActivityWakeLock = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
473 mLaunchingActivityWakeLock.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700474 }
475
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700476 void setWindowManager(WindowManagerService wm) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700477 mWindowManager = wm;
478 getKeyguardController().setWindowManager(wm);
Craig Mautner20e72272013-04-01 13:45:53 -0700479 }
480
Matthew Ng330757d2017-02-28 14:19:17 -0800481 void moveRecentsStackToFront(String reason) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800482 final ActivityStack recentsStack = mRootActivityContainer.getDefaultDisplay().getStack(
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700483 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800484 if (recentsStack != null) {
485 recentsStack.moveToFront(reason);
486 }
487 }
488
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800489 void setNextTaskIdForUserLocked(int taskId, int userId) {
490 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
491 if (taskId > currentTaskId) {
492 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700493 }
494 }
495
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700496 static int nextTaskIdForUser(int taskId, int userId) {
497 int nextTaskId = taskId + 1;
498 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
499 // Wrap around as there will be smaller task ids that are available now.
500 nextTaskId -= MAX_TASK_IDS_PER_USER;
501 }
502 return nextTaskId;
503 }
504
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800505 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800506 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
507 // for a userId u, a taskId can only be in the range
508 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
509 // 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 -0700510 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700511 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwaled32da472018-11-16 07:19:28 -0800512 || mRootActivityContainer.anyTaskForId(
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700513 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700514 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800515 if (candidateTaskId == currentTaskId) {
516 // Something wrong!
517 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
518 throw new IllegalStateException("Cannot get an available task id."
519 + " Reached limit of " + MAX_TASK_IDS_PER_USER
520 + " running tasks per user.");
521 }
522 }
523 mCurTaskIdForUser.put(userId, candidateTaskId);
524 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700525 }
526
Vishnu Nair132ee832018-09-28 15:00:05 -0700527 void waitActivityVisible(ComponentName name, WaitResult result, long startTimeMs) {
528 final WaitInfo waitInfo = new WaitInfo(name, result, startTimeMs);
Bryce Lee4a194382017-04-04 14:32:48 -0700529 mWaitingForActivityVisible.add(waitInfo);
530 }
531
532 void cleanupActivity(ActivityRecord r) {
533 // Make sure this record is no longer in the pending finishes list.
534 // This could happen, for example, if we are trimming activities
535 // down to the max limit while they are still waiting to finish.
536 mFinishingActivities.remove(r);
537 mActivitiesWaitingForVisibleActivity.remove(r);
538
539 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800540 if (mWaitingForActivityVisible.get(i).matches(r.mActivityComponent)) {
Bryce Lee4a194382017-04-04 14:32:48 -0700541 mWaitingForActivityVisible.remove(i);
542 }
543 }
544 }
545
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700546 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700547 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700548 }
549
550 void sendWaitingVisibleReportLocked(ActivityRecord r) {
551 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -0700552 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
553 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800554 if (w.matches(r.mActivityComponent)) {
Bryce Lee4a194382017-04-04 14:32:48 -0700555 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700556 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -0700557 result.timeout = false;
558 result.who = w.getComponent();
Vishnu Nair132ee832018-09-28 15:00:05 -0700559 result.totalTime = SystemClock.uptimeMillis() - w.getStartTime();
Bryce Lee4a194382017-04-04 14:32:48 -0700560 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700561 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700562 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700563 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700564 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700565 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700566 }
567
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800568 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
569 if (mWaitingActivityLaunched.isEmpty()) {
570 return;
571 }
572
573 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
574 return;
575 }
576
Chong Zhang5022da32016-06-21 16:31:37 -0700577 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800578
Chong Zhang5022da32016-06-21 16:31:37 -0700579 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
580 WaitResult w = mWaitingActivityLaunched.remove(i);
581 if (w.who == null) {
582 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800583 w.result = result;
584
585 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
586 // will be no followup launch signals. Assign the result and launched component.
587 if (result == START_DELIVERED_TO_TOP) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800588 w.who = r.mActivityComponent;
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800589 }
Chong Zhang5022da32016-06-21 16:31:37 -0700590 }
591 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -0800592
Chong Zhang5022da32016-06-21 16:31:37 -0700593 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700594 mService.mGlobalLock.notifyAll();
Chong Zhang5022da32016-06-21 16:31:37 -0700595 }
596 }
597
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800598 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, long totalTime,
599 @WaitResult.LaunchState int launchState) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700600 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700601 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700602 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700603 if (w.who == null) {
604 changed = true;
605 w.timeout = timeout;
606 if (r != null) {
607 w.who = new ComponentName(r.info.packageName, r.info.name);
608 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700609 w.totalTime = totalTime;
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800610 w.launchState = launchState;
Chong Zhang5022da32016-06-21 16:31:37 -0700611 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700612 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700613 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700614 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700615 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700616 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700617 }
618
Todd Kennedy7440f172015-12-09 14:31:22 -0800619 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
620 ProfilerInfo profilerInfo) {
621 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700622 if (aInfo != null) {
623 // Store the found target back into the intent, because now that
624 // we have it we never want to do this again. For example, if the
625 // user navigates back to this point in the history, we should
626 // always restart the exact same activity.
627 intent.setComponent(new ComponentName(
628 aInfo.applicationInfo.packageName, aInfo.name));
629
630 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700631 if (!aInfo.processName.equals("system")) {
Wale Ogunwale7056a062018-10-18 15:02:50 -0700632 if ((startFlags & (START_FLAG_DEBUG | START_FLAG_NATIVE_DEBUGGING
633 | START_FLAG_TRACK_ALLOCATION)) != 0 || profilerInfo != null) {
Vishnu Nair006ee3f2018-11-09 08:36:01 -0800634
635 // Mimic an AMS synchronous call by passing a message to AMS and wait for AMS
636 // to notify us that the task has completed.
637 // TODO(b/80414790) look into further untangling for the situation where the
638 // caller is on the same thread as the handler we are posting to.
639 synchronized (mService.mGlobalLock) {
640 // Post message to AMS.
641 final Message msg = PooledLambda.obtainMessage(
642 ActivityManagerInternal::setDebugFlagsForStartingActivity,
643 mService.mAmInternal, aInfo, startFlags, profilerInfo,
644 mService.mGlobalLock);
645 mService.mH.sendMessage(msg);
646 try {
647 mService.mGlobalLock.wait();
648 } catch (InterruptedException ignore) {
649
650 }
651 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700652 }
653 }
Todd Kennedyb3b431302017-03-20 16:05:48 -0700654 final String intentLaunchToken = intent.getLaunchToken();
655 if (aInfo.launchToken == null && intentLaunchToken != null) {
656 aInfo.launchToken = intentLaunchToken;
657 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700658 }
659 return aInfo;
660 }
661
Patrick Baumann78380272018-04-04 10:41:01 -0700662 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
663 int filterCallingUid) {
Wale Ogunwale1f573382018-12-03 06:42:54 -0800664 try {
665 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
666 int modifiedFlags = flags
667 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
668 if (intent.isWebIntent()
669 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
670 modifiedFlags |= PackageManager.MATCH_INSTANT;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100671 }
Wale Ogunwale1f573382018-12-03 06:42:54 -0800672
673 // In order to allow cross-profile lookup, we clear the calling identity here.
674 // Note the binder identity won't affect the result, but filterCallingUid will.
675
676 // Cross-user/profile call check are done at the entry points
677 // (e.g. AMS.startActivityAsUser).
678 final long token = Binder.clearCallingIdentity();
679 try {
680 return mService.getPackageManagerInternalLocked().resolveIntent(
681 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
682 } finally {
683 Binder.restoreCallingIdentity(token);
684 }
685 } finally {
686 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Todd Kennedy7440f172015-12-09 14:31:22 -0800687 }
Todd Kennedy7440f172015-12-09 14:31:22 -0800688 }
689
690 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -0700691 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
692 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -0800693 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
694 }
695
Wale Ogunwaled32da472018-11-16 07:19:28 -0800696 boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800697 boolean andResume, boolean checkConfig) throws RemoteException {
698
Wale Ogunwaled32da472018-11-16 07:19:28 -0800699 if (!mRootActivityContainer.allPausedActivitiesComplete()) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800700 // While there are activities pausing we skipping starting any new activities until
701 // pauses are complete. NOTE: that we also do this for activities that are starting in
702 // the paused state because they will first be resumed then paused on the client side.
703 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
704 "realStartActivityLocked: Skipping start of r=" + r
705 + " some activities pausing...");
706 return false;
707 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700708
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800709 final TaskRecord task = r.getTaskRecord();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700710 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -0700711
712 beginDeferResume();
713
Craig Mautner2420ead2013-04-01 17:13:20 -0700714 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700715 r.startFreezingScreenLocked(proc, 0);
chaviw59b98852017-06-13 12:05:44 -0700716
717 // schedule launch ticks to collect information about slow apps.
718 r.startLaunchTickingLocked();
719
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700720 r.setProcess(proc);
chaviw59b98852017-06-13 12:05:44 -0700721
Louis Chang77ce34d2019-01-03 15:45:12 +0800722 // Ensure activity is allowed to be resumed after process has set.
723 if (andResume && !r.canResumeByCompat()) {
724 andResume = false;
725 }
726
Bryce Lee459c0622018-03-19 11:04:01 -0700727 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +0200728 r.notifyUnknownVisibilityLaunched();
729 }
730
chaviw59b98852017-06-13 12:05:44 -0700731 // Have the window manager re-evaluate the orientation of the screen based on the new
732 // activity order. Note that as a result of this, it can call back into the activity
733 // manager with a new orientation. We don't care about that, because the activity is
734 // not currently running so we are just restarting it anyway.
735 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -0700736 // Deferring resume here because we're going to launch new activity shortly.
737 // We don't want to perform a redundant launch of the same record while ensuring
738 // configurations and trying to resume top activity of focused stack.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800739 mRootActivityContainer.ensureVisibilityAndConfig(r, r.getDisplayId(),
Andrii Kulianf4479ee2018-05-23 17:52:48 -0700740 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -0700741 }
chaviw59b98852017-06-13 12:05:44 -0700742
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800743 if (r.getActivityStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
chaviw59b98852017-06-13 12:05:44 -0700744 true /* isTop */)) {
745 // We only set the visibility to true if the activity is allowed to be visible
746 // based on
747 // keyguard state. This avoids setting this into motion in window manager that is
748 // later cancelled due to later calls to ensure visible activities that set
749 // visibility back to false.
750 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700751 }
chaviw59b98852017-06-13 12:05:44 -0700752
chaviw59b98852017-06-13 12:05:44 -0700753 final int applicationInfoUid =
754 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800755 if ((r.mUserId != proc.mUserId) || (r.appInfo.uid != applicationInfoUid)) {
chaviw59b98852017-06-13 12:05:44 -0700756 Slog.wtf(TAG,
757 "User ID for activity changing for " + r
758 + " appInfo.uid=" + r.appInfo.uid
759 + " info.ai.uid=" + applicationInfoUid
Wale Ogunwale9c103022018-10-18 07:44:54 -0700760 + " old=" + r.app + " new=" + proc);
chaviw59b98852017-06-13 12:05:44 -0700761 }
762
Wale Ogunwale9c103022018-10-18 07:44:54 -0700763 proc.clearWaitingToKill();
chaviw59b98852017-06-13 12:05:44 -0700764 r.launchCount++;
765 r.lastLaunchTime = SystemClock.uptimeMillis();
766
767 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
768
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700769 proc.addActivityIfNeeded(r);
770 proc.updateProcessInfo(false, true, true, true);
chaviw59b98852017-06-13 12:05:44 -0700771
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700772 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +0100773 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
774 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
775 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -0700776 && lockTaskController.getLockTaskModeState()
777 == LOCK_TASK_MODE_LOCKED)) {
778 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -0700779 }
780
781 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700782 if (!proc.hasThread()) {
chaviw59b98852017-06-13 12:05:44 -0700783 throw new RemoteException();
784 }
785 List<ResultInfo> results = null;
786 List<ReferrerIntent> newIntents = null;
787 if (andResume) {
788 // We don't need to deliver new intents and/or set results if activity is going
789 // to pause immediately after launch.
790 results = r.results;
791 newIntents = r.newIntents;
792 }
793 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
794 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
795 + " newIntents=" + newIntents + " andResume=" + andResume);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800796 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.mUserId,
chaviw59b98852017-06-13 12:05:44 -0700797 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700798 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -0700799 // Home process is the root process of the task.
Louis Changdcdde952018-12-04 15:38:44 +0800800 updateHomeProcess(task.mActivities.get(0).app);
chaviw59b98852017-06-13 12:05:44 -0700801 }
Wale Ogunwale214f3482018-10-04 11:00:47 -0700802 mService.getPackageManagerInternalLocked().notifyPackageUse(
803 r.intent.getComponent().getPackageName(), NOTIFY_PACKAGE_USE_ACTIVITY);
chaviw59b98852017-06-13 12:05:44 -0700804 r.sleeping = false;
805 r.forceNewConfig = false;
Wale Ogunwale008163e2018-07-23 23:11:08 -0700806 mService.getAppWarningsLocked().onStartActivity(r);
Wale Ogunwale53783742018-09-16 10:21:51 -0700807 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Wale Ogunwale9c103022018-10-18 07:44:54 -0700808 ProfilerInfo profilerInfo = proc.onStartActivity(mService.mTopProcessState);
chaviw59b98852017-06-13 12:05:44 -0700809
chaviw59b98852017-06-13 12:05:44 -0700810 // Because we could be starting an Activity in the system process this may not go
811 // across a Binder interface which would create a new Configuration. Consequently
812 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -0800813
chaviw59b98852017-06-13 12:05:44 -0700814 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
Wale Ogunwale9c103022018-10-18 07:44:54 -0700815 proc.getConfiguration(), r.getMergedOverrideConfiguration());
chaviw59b98852017-06-13 12:05:44 -0700816 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -0800817
chaviw59b98852017-06-13 12:05:44 -0700818 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800819
820
821 // Create activity launch transaction.
Wale Ogunwale9c103022018-10-18 07:44:54 -0700822 final ClientTransaction clientTransaction = ClientTransaction.obtain(
823 proc.getThread(), r.appToken);
lumark588a3e82018-07-20 18:53:54 +0800824
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800825 final DisplayContent dc = r.getDisplay().mDisplayContent;
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800826 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -0700827 System.identityHashCode(r), r.info,
828 // TODO: Have this take the merged configuration instead of separate global
829 // and override configs.
830 mergedConfiguration.getGlobalConfiguration(),
831 mergedConfiguration.getOverrideConfiguration(), r.compat,
Wale Ogunwale9c103022018-10-18 07:44:54 -0700832 r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700833 r.icicle, r.persistentState, results, newIntents,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800834 dc.isNextTransitionForward(), profilerInfo));
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800835
836 // Set desired final state.
837 final ActivityLifecycleItem lifecycleItem;
838 if (andResume) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800839 lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800840 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -0700841 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800842 }
843 clientTransaction.setLifecycleStateRequest(lifecycleItem);
844
845 // Schedule transaction.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700846 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Andrii Kuliand70cdb92019-01-08 15:03:50 -0800847 mRootActivityContainer.updateTopResumedActivityIfNeeded();
Craig Mautner2420ead2013-04-01 17:13:20 -0700848
Wale Ogunwale9c103022018-10-18 07:44:54 -0700849 if ((proc.mInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
Wale Ogunwale214f3482018-10-04 11:00:47 -0700850 && mService.mHasHeavyWeightFeature) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700851 // This may be a heavy-weight process! Note that the package manager will ensure
852 // that only activity can run in the main process of the .apk, which is the only
853 // thing that will be considered heavy-weight.
Wale Ogunwale9c103022018-10-18 07:44:54 -0700854 if (proc.mName.equals(proc.mInfo.packageName)) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700855 if (mService.mHeavyWeightProcess != null
856 && mService.mHeavyWeightProcess != proc) {
857 Slog.w(TAG, "Starting new heavy weight process " + proc
chaviw59b98852017-06-13 12:05:44 -0700858 + " when already running "
Wale Ogunwale53783742018-09-16 10:21:51 -0700859 + mService.mHeavyWeightProcess);
chaviw59b98852017-06-13 12:05:44 -0700860 }
Wale Ogunwale53783742018-09-16 10:21:51 -0700861 mService.setHeavyWeightProcess(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700862 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700863 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700864
chaviw59b98852017-06-13 12:05:44 -0700865 } catch (RemoteException e) {
866 if (r.launchFailed) {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700867 // This is the second time we failed -- finish activity and give up.
chaviw59b98852017-06-13 12:05:44 -0700868 Slog.e(TAG, "Second failure launching "
Wale Ogunwale9c103022018-10-18 07:44:54 -0700869 + r.intent.getComponent().flattenToShortString() + ", giving up", e);
870 proc.appDied();
chaviw59b98852017-06-13 12:05:44 -0700871 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
872 "2nd-crash", false);
873 return false;
874 }
Craig Mautner2420ead2013-04-01 17:13:20 -0700875
chaviw59b98852017-06-13 12:05:44 -0700876 // This is the first time we failed -- restart process and
877 // retry.
878 r.launchFailed = true;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700879 proc.removeActivity(r);
chaviw59b98852017-06-13 12:05:44 -0700880 throw e;
881 }
882 } finally {
883 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -0700884 }
885
886 r.launchFailed = false;
887 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -0700888 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -0700889 }
890
Andrii Kulianb372da62018-01-18 10:46:24 -0800891 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
892 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -0700893 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -0700894 // As part of the process of launching, ActivityThread also performs
895 // a resume.
896 stack.minimalResumeActivityLocked(r);
897 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800898 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +0000899 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -0800900 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700901 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -0800902 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -0800903 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -0700904 }
905
906 // Launch the new version setup screen if needed. We do this -after-
907 // launching the initial activity (that is, home), so that it can have
908 // a chance to initialize itself while in the background, making the
909 // switch back to it faster and look better.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800910 if (mRootActivityContainer.isTopDisplayFocusedStack(stack)) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700911 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -0700912 }
913
Dianne Hackborn465fa392014-09-14 14:21:18 -0700914 // Update any services we are bound to that might care about whether
915 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -0700916 if (r.app != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700917 r.app.updateServiceConnectionActivities();
Wale Ogunwaled6ac7622016-05-26 09:02:25 -0700918 }
Dianne Hackborn465fa392014-09-14 14:21:18 -0700919
Craig Mautner2420ead2013-04-01 17:13:20 -0700920 return true;
921 }
922
Louis Changdcdde952018-12-04 15:38:44 +0800923 void updateHomeProcess(WindowProcessController app) {
924 if (app != null && mService.mHomeProcess != app) {
925 if (!mHandler.hasMessages(REPORT_HOME_CHANGED_MSG)) {
926 mHandler.sendEmptyMessage(REPORT_HOME_CHANGED_MSG);
927 }
928 mService.mHomeProcess = app;
929 }
930 }
931
Sudheer Shankafab200f2017-05-17 20:41:53 -0700932 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
933 int extrasSize = 0;
934 if (intent != null) {
935 final Bundle extras = intent.getExtras();
936 if (extras != null) {
937 extrasSize = extras.getSize();
938 }
939 }
940 int icicleSize = (icicle == null ? 0 : icicle.getSize());
941 if (extrasSize + icicleSize > 200000) {
942 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
943 + ", icicle size: " + icicleSize);
944 }
945 }
946
Wale Ogunwale9c103022018-10-18 07:44:54 -0700947 void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700948 // Is this activity's application already running?
Wale Ogunwale9c103022018-10-18 07:44:54 -0700949 final WindowProcessController wpc =
950 mService.getProcessController(r.processName, r.info.applicationInfo.uid);
Craig Mautnere79d42682013-04-01 19:01:53 -0700951
Chilun917173f2019-01-04 16:47:42 +0800952 boolean knownToBeDead = false;
Wale Ogunwale9c103022018-10-18 07:44:54 -0700953 if (wpc != null && wpc.hasThread()) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700954 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700955 if ((r.info.flags & ActivityInfo.FLAG_MULTIPROCESS) == 0
Dianne Hackborn237cefb2013-10-22 18:45:27 -0700956 || !"android".equals(r.info.packageName)) {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700957 // Don't add this if it is a platform component that is marked to run in
958 // multiple processes, because this is actually part of the framework so doesn't
959 // make sense to track as a separate apk in the process.
960 wpc.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode);
Dianne Hackborn237cefb2013-10-22 18:45:27 -0700961 }
Wale Ogunwale9c103022018-10-18 07:44:54 -0700962 realStartActivityLocked(r, wpc, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -0700963 return;
964 } catch (RemoteException e) {
965 Slog.w(TAG, "Exception when starting activity "
966 + r.intent.getComponent().flattenToShortString(), e);
967 }
968
969 // If a dead object exception was thrown -- fall through to
970 // restart the application.
Chilun917173f2019-01-04 16:47:42 +0800971 knownToBeDead = true;
Craig Mautnere79d42682013-04-01 19:01:53 -0700972 }
973
Issei Suzuki5205af72018-11-09 11:51:59 +0100974 // Suppress transition until the new activity becomes ready, otherwise the keyguard can
975 // appear for a short amount of time before the new process with the new activity had the
976 // ability to set its showWhenLocked flags.
977 if (getKeyguardController().isKeyguardLocked()) {
978 r.notifyUnknownVisibilityLaunched();
979 }
980
Ioannis Ilkos3501ca42019-02-06 11:43:48 +0000981 try {
982 if (Trace.isTagEnabled(TRACE_TAG_ACTIVITY_MANAGER)) {
983 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "dispatchingStartProcess:"
984 + r.processName);
985 }
986 // Post message to start process to avoid possible deadlock of calling into AMS with the
987 // ATMS lock held.
988 final Message msg = PooledLambda.obtainMessage(
989 ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,
990 r.info.applicationInfo, knownToBeDead, "activity", r.intent.getComponent());
991 mService.mH.sendMessage(msg);
992 } finally {
993 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
994 }
Craig Mautnere79d42682013-04-01 19:01:53 -0700995 }
996
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700997 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo, String resultWho,
998 int requestCode, int callingPid, int callingUid, String callingPackage,
999 boolean ignoreTargetSecurity, boolean launchingInTask,
1000 WindowProcessController callerApp, ActivityRecord resultRecord,
1001 ActivityStack resultStack) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001002 final boolean isCallerRecents = mService.getRecentTasks() != null
1003 && mService.getRecentTasks().isCallerRecents(callingUid);
Wale Ogunwalef6733932018-06-27 05:14:34 -07001004 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001005 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001006 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1007 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1008 // caller is the recents component and we are specifically starting an activity in an
1009 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001010 return true;
1011 }
1012 final int componentRestriction = getComponentRestrictionForCallingPackage(
1013 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1014 final int actionRestriction = getActionRestrictionForCallingPackage(
1015 intent.getAction(), callingPackage, callingPid, callingUid);
1016 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1017 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1018 if (resultRecord != null) {
1019 resultStack.sendActivityResultLocked(-1,
1020 resultRecord, resultWho, requestCode,
1021 Activity.RESULT_CANCELED, null);
1022 }
1023 final String msg;
1024 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1025 msg = "Permission Denial: starting " + intent.toString()
1026 + " from " + callerApp + " (pid=" + callingPid
1027 + ", uid=" + callingUid + ")" + " with revoked permission "
1028 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1029 } else if (!aInfo.exported) {
1030 msg = "Permission Denial: starting " + intent.toString()
1031 + " from " + callerApp + " (pid=" + callingPid
1032 + ", uid=" + callingUid + ")"
1033 + " not exported from uid " + aInfo.applicationInfo.uid;
1034 } else {
1035 msg = "Permission Denial: starting " + intent.toString()
1036 + " from " + callerApp + " (pid=" + callingPid
1037 + ", uid=" + callingUid + ")"
1038 + " requires " + aInfo.permission;
1039 }
1040 Slog.w(TAG, msg);
1041 throw new SecurityException(msg);
1042 }
1043
1044 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1045 final String message = "Appop Denial: starting " + intent.toString()
1046 + " from " + callerApp + " (pid=" + callingPid
1047 + ", uid=" + callingUid + ")"
1048 + " requires " + AppOpsManager.permissionToOp(
1049 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1050 Slog.w(TAG, message);
1051 return false;
1052 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1053 final String message = "Appop Denial: starting " + intent.toString()
1054 + " from " + callerApp + " (pid=" + callingPid
1055 + ", uid=" + callingUid + ")"
1056 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1057 Slog.w(TAG, message);
1058 return false;
1059 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001060
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001061 return true;
1062 }
1063
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001064 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001065 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1066 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001067 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1068 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1069
Andrii Kulian02689a72017-07-06 14:28:59 -07001070 if (callingPid == -1 && callingUid == -1) {
1071 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1072 return true;
1073 }
1074
Wale Ogunwaled32da472018-11-16 07:19:28 -08001075 final ActivityDisplay activityDisplay =
1076 mRootActivityContainer.getActivityDisplayOrCreate(launchDisplayId);
Louis Chang7d0037c2018-08-13 12:42:06 +08001077 if (activityDisplay == null || activityDisplay.isRemoved()) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001078 Slog.w(TAG, "Launch on display check: display not found");
1079 return false;
1080 }
1081
Andrii Kulian02689a72017-07-06 14:28:59 -07001082 // Check if the caller has enough privileges to embed activities and launch to private
1083 // displays.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001084 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001085 callingUid);
1086 if (startAnyPerm == PERMISSION_GRANTED) {
1087 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1088 + " allow launch any on display");
1089 return true;
1090 }
1091
Andrii Kulian8f070292017-09-12 22:56:49 -07001092 // Check if caller is already present on display
1093 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1094
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001095 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1096 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1097 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001098 // Limit launching on virtual displays, because their contents can be read from Surface
1099 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001100 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1101 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1102 + " disallow launch on virtual display for not-embedded activity.");
1103 return false;
1104 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001105 // Check if the caller is allowed to embed activities from other apps.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001106 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001107 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001108 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1109 + " disallow activity embedding without permission.");
1110 return false;
1111 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001112 }
1113
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001114 if (!activityDisplay.isPrivate()) {
1115 // Anyone can launch on a public display.
1116 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1117 + " allow launch on public display");
1118 return true;
1119 }
1120
1121 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001122 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001123 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1124 + " allow launch for owner of the display");
1125 return true;
1126 }
1127
Andrii Kulian8f070292017-09-12 22:56:49 -07001128 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001129 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1130 + " allow launch for caller present on the display");
1131 return true;
1132 }
1133
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001134 Slog.w(TAG, "Launch on display check: denied");
1135 return false;
1136 }
1137
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001138 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001139 final long identity = Binder.clearCallingIdentity();
1140 try {
1141 return UserManager.get(mService.mContext).getUserInfo(userId);
1142 } finally {
1143 Binder.restoreCallingIdentity(identity);
1144 }
1145 }
1146
Svet Ganov99b60432015-06-27 13:15:22 -07001147 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001148 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Wale Ogunwale214f3482018-10-04 11:00:47 -07001149 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001150 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001151 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001152 return ACTIVITY_RESTRICTION_PERMISSION;
1153 }
1154
Christopher Tateff7add02015-08-17 10:23:22 -07001155 if (activityInfo.permission == null) {
1156 return ACTIVITY_RESTRICTION_NONE;
1157 }
1158
Svet Ganov99b60432015-06-27 13:15:22 -07001159 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1160 if (opCode == AppOpsManager.OP_NONE) {
1161 return ACTIVITY_RESTRICTION_NONE;
1162 }
1163
Wale Ogunwalef6733932018-06-27 05:14:34 -07001164 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage)
1165 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001166 if (!ignoreTargetSecurity) {
1167 return ACTIVITY_RESTRICTION_APPOP;
1168 }
Svet Ganov99b60432015-06-27 13:15:22 -07001169 }
1170
1171 return ACTIVITY_RESTRICTION_NONE;
1172 }
1173
Svetoslav7008b512015-06-24 18:47:07 -07001174 private int getActionRestrictionForCallingPackage(String action,
1175 String callingPackage, int callingPid, int callingUid) {
1176 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001177 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001178 }
1179
1180 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1181 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001182 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001183 }
1184
1185 final PackageInfo packageInfo;
1186 try {
1187 packageInfo = mService.mContext.getPackageManager()
1188 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1189 } catch (PackageManager.NameNotFoundException e) {
1190 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001191 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001192 }
1193
1194 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001195 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001196 }
1197
Wale Ogunwalef6733932018-06-27 05:14:34 -07001198 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001199 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001200 }
1201
1202 final int opCode = AppOpsManager.permissionToOpCode(permission);
1203 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001204 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001205 }
1206
Wale Ogunwalef6733932018-06-27 05:14:34 -07001207 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage)
1208 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001209 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001210 }
1211
Svet Ganov99b60432015-06-27 13:15:22 -07001212 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001213 }
1214
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001215 void setLaunchSource(int uid) {
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001216 mLaunchingActivityWakeLock.setWorkSource(new WorkSource(uid));
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001217 }
1218
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001219 void acquireLaunchWakelock() {
1220 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1221 throw new IllegalStateException("Calling must be system uid");
1222 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001223 mLaunchingActivityWakeLock.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001224 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1225 // To be safe, don't allow the wake lock to be held for too long.
1226 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1227 }
1228 }
1229
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001230 /**
Louis Chang89f43fc2018-10-05 10:59:14 +08001231 * Called when all resumed tasks/stacks are idle.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001232 * @return the state of mService.mAm.mBooting before this was called.
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001233 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001234 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001235 private boolean checkFinishBootingLocked() {
Wale Ogunwale53783742018-09-16 10:21:51 -07001236 final boolean booting = mService.isBooting();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001237 boolean enableScreen = false;
Wale Ogunwale53783742018-09-16 10:21:51 -07001238 mService.setBooting(false);
1239 if (!mService.isBooted()) {
1240 mService.setBooted(true);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001241 enableScreen = true;
1242 }
1243 if (booting || enableScreen) {
Wale Ogunwale53783742018-09-16 10:21:51 -07001244 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001245 }
1246 return booting;
1247 }
1248
Craig Mautnerf3333272013-04-22 10:55:53 -07001249 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001250 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001251 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001252 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001253 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001254
Craig Mautnerf3333272013-04-22 10:55:53 -07001255 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001256 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001257 int NS = 0;
1258 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001259 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001260 boolean activityRemoved = false;
1261
Wale Ogunwale7d701172015-03-11 15:36:30 -07001262 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001263 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001264 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1265 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001266 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1267 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001268 if (fromTimeout) {
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -08001269 reportActivityLaunchedLocked(fromTimeout, r, INVALID_DELAY,
1270 -1 /* launchState */);
Craig Mautnerf3333272013-04-22 10:55:53 -07001271 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001272
1273 // This is a hack to semi-deal with a race condition
1274 // in the client where it can be constructed with a
1275 // newer configuration from when we asked it to launch.
1276 // We'll update with whatever configuration it now says
1277 // it used to launch.
1278 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001279 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001280 }
1281
1282 // We are now idle. If someone is waiting for a thumbnail from
1283 // us, we can now deliver.
1284 r.idle = true;
1285
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001286 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Louis Chang89f43fc2018-10-05 10:59:14 +08001287
Louis Chang1eff2482018-11-01 15:46:31 +08001288 // Check if able to finish booting when device is booting and all resumed activities
1289 // are idle.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001290 if ((mService.isBooting() && mRootActivityContainer.allResumedActivitiesIdle())
1291 || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001292 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001293 }
Garfield Tan2746ab52018-07-25 12:33:01 -07001294
1295 // When activity is idle, we consider the relaunch must be successful, so let's clear
1296 // the flag.
1297 r.mRelaunchReason = RELAUNCH_REASON_NONE;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001298 }
1299
Wale Ogunwaled32da472018-11-16 07:19:28 -08001300 if (mRootActivityContainer.allResumedActivitiesIdle()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001301 if (r != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001302 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001303 }
1304
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001305 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001306 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1307 if (VALIDATE_WAKE_LOCK_CALLER &&
1308 Binder.getCallingUid() != Process.myUid()) {
1309 throw new IllegalStateException("Calling must be system uid");
1310 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001311 mLaunchingActivityWakeLock.release();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001312 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001313 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001314 }
1315
1316 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001317 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1318 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001319 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001320 if ((NF = mFinishingActivities.size()) > 0) {
1321 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001322 mFinishingActivities.clear();
1323 }
1324
Craig Mautnerf3333272013-04-22 10:55:53 -07001325 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001326 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001327 mStartingUsers.clear();
1328 }
1329
Craig Mautnerf3333272013-04-22 10:55:53 -07001330 // Stop any activities that are scheduled to do so but have been
1331 // waiting for the next one to start.
1332 for (int i = 0; i < NS; i++) {
1333 r = stops.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001334 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001335 if (stack != null) {
1336 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08001337 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
1338 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07001339 } else {
1340 stack.stopActivityLocked(r);
1341 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001342 }
1343 }
1344
1345 // Finish any activities that are scheduled to do so but have been
1346 // waiting for the next one to start.
1347 for (int i = 0; i < NF; i++) {
1348 r = finishes.get(i);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001349 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001350 if (stack != null) {
1351 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1352 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001353 }
1354
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001355 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001356 // Complete user switch
1357 if (startingUsers != null) {
1358 for (int i = 0; i < startingUsers.size(); i++) {
Wale Ogunwale86b74462018-07-02 08:42:43 -07001359 mService.mAmInternal.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001360 }
1361 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001362 }
1363
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001364 mService.mH.post(() -> mService.mAmInternal.trimApplications());
Craig Mautnerf3333272013-04-22 10:55:53 -07001365 //dump();
1366 //mWindowManager.dump();
1367
Craig Mautnerf3333272013-04-22 10:55:53 -07001368 if (activityRemoved) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001369 mRootActivityContainer.resumeFocusedStacksTopActivities();
Craig Mautnerf3333272013-04-22 10:55:53 -07001370 }
1371
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001372 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001373 }
1374
Wale Ogunwaled32da472018-11-16 07:19:28 -08001375 /** This doesn't just find a task, it also moves the task to front. */
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001376 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
1377 boolean forceNonResizeable) {
Louis Changbd48dca2018-08-29 17:44:34 +08001378 ActivityStack currentStack = task.getStack();
Wale Ogunwale66e16852017-10-19 13:35:52 -07001379 if (currentStack == null) {
1380 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
1381 + task + " to front. Stack is null");
1382 return;
1383 }
1384
Craig Mautneraea74a52014-03-08 14:23:10 -08001385 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1386 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001387 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07001388
Louis Changc8b64112018-10-11 09:22:14 +08001389 reason = reason + " findTaskToMoveToFront";
1390 boolean reparented = false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001391 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08001392 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001393 task.updateOverrideConfiguration(bounds);
1394
Wale Ogunwaled32da472018-11-16 07:19:28 -08001395 ActivityStack stack =
1396 mRootActivityContainer.getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001397
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001398 if (stack != currentStack) {
Louis Changc8b64112018-10-11 09:22:14 +08001399 moveHomeStackToFrontIfNeeded(flags, stack.getDisplay(), reason);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001400 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Louis Changc8b64112018-10-11 09:22:14 +08001401 reason);
Louis Changbd48dca2018-08-29 17:44:34 +08001402 currentStack = stack;
Louis Changc8b64112018-10-11 09:22:14 +08001403 reparented = true;
1404 // task.reparent() should already placed the task on top,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001405 // still need moveTaskToFrontLocked() below for any transition settings.
1406 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001407 if (stack.resizeStackWithLaunchBounds()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001408 mRootActivityContainer.resizeStack(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001409 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
1410 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001411 } else {
1412 // WM resizeTask must be done after the task is moved to the correct stack,
1413 // because Task's setBounds() also updates dim layer's bounds, but that has
1414 // dependency on the stack.
1415 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001416 }
1417 }
1418
Louis Changc8b64112018-10-11 09:22:14 +08001419 if (!reparented) {
1420 moveHomeStackToFrontIfNeeded(flags, currentStack.getDisplay(), reason);
1421 }
1422
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001423 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07001424 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001425 r == null ? null : r.appTimeTracker, reason);
1426
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001427 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001428 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001429
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001430 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001431 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001432 }
1433
Louis Changc8b64112018-10-11 09:22:14 +08001434 private void moveHomeStackToFrontIfNeeded(int flags, ActivityDisplay display, String reason) {
1435 final ActivityStack focusedStack = display.getFocusedStack();
1436
1437 if ((display.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
1438 && (flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0)
1439 || (focusedStack != null && focusedStack.isActivityTypeRecents())) {
1440 // We move home stack to front when we are on a fullscreen display and caller has
1441 // requested the home activity to move with it. Or the previous stack is recents.
1442 display.moveHomeStackToFront(reason);
1443 }
1444 }
1445
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001446 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001447 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001448 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001449 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001450 return false;
1451 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001452 return (mService.mSupportsPictureInPicture
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001453 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001454 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001455 }
1456
Bryce Leeec55eb02017-12-05 20:51:27 -08001457 LaunchParamsController getLaunchParamsController() {
1458 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07001459 }
1460
Wale Ogunwaled32da472018-11-16 07:19:28 -08001461 private void deferUpdateRecentsHomeStackBounds() {
1462 mRootActivityContainer.deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
1463 mRootActivityContainer.deferUpdateBounds(ACTIVITY_TYPE_HOME);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001464 }
1465
Wale Ogunwaled32da472018-11-16 07:19:28 -08001466 private void continueUpdateRecentsHomeStackBounds() {
1467 mRootActivityContainer.continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
1468 mRootActivityContainer.continueUpdateBounds(ACTIVITY_TYPE_HOME);
Jorim Jaggi192086e2016-03-11 17:17:03 +01001469 }
1470
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001471 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08001472 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001473 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1474 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled32da472018-11-16 07:19:28 -08001475 final TaskRecord task =
1476 mRootActivityContainer.anyTaskForId(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001477 if (task != null) {
1478 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001479 }
1480 }
1481 mResizingTasksDuringAnimation.clear();
1482 }
1483
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001484 /**
1485 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001486 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001487 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001488 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001489 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07001490
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001491 mWindowManager.deferSurfaceLayout();
1492 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001493 final int windowingMode = fromStack.getWindowingMode();
1494 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
Wale Ogunwaled32da472018-11-16 07:19:28 -08001495 final ActivityDisplay toDisplay =
1496 mRootActivityContainer.getActivityDisplay(toDisplayId);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001497
1498 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001499 // Tell the display we are exiting split-screen mode.
1500 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001501 // We are moving all tasks from the docked stack to the fullscreen stack,
1502 // which is dismissing the docked stack, so resize all other stacks to
1503 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001504 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
1505 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07001506 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
1507 continue;
1508 }
Evan Rosky10475742018-09-05 19:02:48 -07001509 otherStack.setWindowingMode(WINDOWING_MODE_UNDEFINED);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001510 }
1511
1512 // Also disable docked stack resizing since we have manually adjusted the
1513 // size of other stacks above and we don't want to trigger a docked stack
1514 // resize when we remove task from it below and it is detached from the
1515 // display because it no longer contains any tasks.
1516 mAllowDockedStackResize = false;
1517 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001518
Winson Chung5af42fc2017-03-24 17:11:33 -07001519 // If we are moving from the pinned stack, then the animation takes care of updating
1520 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001521 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001522 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001523
Wale Ogunwale388945c2017-10-04 12:13:46 -07001524 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001525 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07001526 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001527 for (int i = 0; i < size; ++i) {
1528 final TaskRecord task = tasks.get(i);
1529 final ActivityStack toStack = toDisplay.getOrCreateStack(
1530 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07001531
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001532 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07001533 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07001534 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001535 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07001536 isTopTask /* animate */, DEFER_RESUME,
1537 schedulePictureInPictureModeChange,
1538 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08001539 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
1540 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001541 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07001542 // Position the tasks in the fullscreen stack in order at the bottom of the
1543 // stack. Also defer resume until all the tasks have been moved to the
1544 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001545 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07001546 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
1547 schedulePictureInPictureModeChange,
1548 "moveTasksToFullscreenStack - NOT_onTop");
1549 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001550 }
1551 }
Winson Chung74666102017-02-22 17:49:24 -08001552
Wale Ogunwaled32da472018-11-16 07:19:28 -08001553 mRootActivityContainer.ensureActivitiesVisible(null, 0, PRESERVE_WINDOWS);
1554 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001555 } finally {
1556 mAllowDockedStackResize = true;
1557 mWindowManager.continueSurfaceLayout();
1558 }
1559 }
1560
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001561 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001562 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
1563 }
1564
1565 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
1566 mWindowManager.inSurfaceTransaction(() ->
1567 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07001568 }
1569
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001570 void setSplitScreenResizing(boolean resizing) {
1571 if (resizing == mDockedStackResizing) {
1572 return;
1573 }
1574
1575 mDockedStackResizing = resizing;
1576 mWindowManager.setDockedStackResizing(resizing);
1577
1578 if (!resizing && mHasPendingDockedBounds) {
1579 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
1580 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
1581 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
1582
1583 mHasPendingDockedBounds = false;
1584 mPendingDockedBounds = null;
1585 mPendingTempDockedTaskBounds = null;
1586 mPendingTempDockedTaskInsetBounds = null;
1587 mPendingTempOtherTaskBounds = null;
1588 mPendingTempOtherTaskInsetBounds = null;
1589 }
1590 }
1591
Jorim Jaggidc249c42015-12-15 14:57:31 -08001592 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001593 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
1594 boolean preserveWindows) {
1595 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
1596 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
1597 false /* deferResume */);
1598 }
1599
Wale Ogunwaled32da472018-11-16 07:19:28 -08001600 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001601 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
1602 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001603
1604 if (!mAllowDockedStackResize) {
1605 // Docked stack resize currently disabled.
1606 return;
1607 }
1608
Wale Ogunwaled32da472018-11-16 07:19:28 -08001609 final ActivityStack stack =
1610 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001611 if (stack == null) {
1612 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1613 return;
1614 }
1615
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001616 if (mDockedStackResizing) {
1617 mHasPendingDockedBounds = true;
1618 mPendingDockedBounds = copyOrNull(dockedBounds);
1619 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
1620 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
1621 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
1622 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
1623 }
1624
Jorim Jaggidc249c42015-12-15 14:57:31 -08001625 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1626 mWindowManager.deferSurfaceLayout();
1627 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001628 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
1629 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001630 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001631 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001632
Andrii Kulian69fb5e42016-04-05 16:47:29 -07001633 // TODO: Checking for isAttached might not be needed as if the user passes in null
1634 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08001635 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
1636 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07001637 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08001638 // In this case we make all other static stacks fullscreen and move all
1639 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001640 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001641
1642 // stack shouldn't contain anymore activities, so nothing to resume.
1643 r = null;
1644 } else {
1645 // Docked stacks occupy a dedicated region on screen so the size of all other
1646 // static stacks need to be adjusted so they don't overlap with the docked stack.
1647 // We get the bounds to use from window manager which has been adjusted for any
1648 // screen controls and is also the same for all stacks.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001649 final ActivityDisplay display = mRootActivityContainer.getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08001650 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001651 for (int i = display.getChildCount() - 1; i >= 0; --i) {
1652 final ActivityStack current = display.getChildAt(i);
Evan Roskyc5abbd82018-10-05 16:02:19 -07001653 if (!current.inSplitScreenSecondaryWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07001654 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001655 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08001656 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07001657 continue;
1658 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001659 if (mDockedStackResizing && !current.isTopActivityVisible()) {
1660 // Non-visible stacks get resized once we're done with the resize
1661 // interaction.
1662 continue;
1663 }
Evan Roskyc5abbd82018-10-05 16:02:19 -07001664 current.getStackDockedModeBounds(dockedBounds,
Wale Ogunwale926aade2017-08-29 11:24:37 -07001665 tempOtherTaskBounds /* currentTempTaskBounds */,
1666 tempRect /* outStackBounds */,
Evan Roskyc5abbd82018-10-05 16:02:19 -07001667 otherTaskRect /* outTempTaskBounds */);
Wale Ogunwale926aade2017-08-29 11:24:37 -07001668
Wale Ogunwaled32da472018-11-16 07:19:28 -08001669 mRootActivityContainer.resizeStack(current,
1670 !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07001671 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
1672 tempOtherTaskInsetBounds, preserveWindows,
1673 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001674 }
1675 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001676 if (!deferResume) {
1677 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
1678 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08001679 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001680 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001681 mWindowManager.continueSurfaceLayout();
1682 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1683 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08001684 }
1685
Robert Carr0d00c2e2016-02-29 17:45:02 -08001686 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Yunfan Chen279f5582018-12-12 15:24:50 -08001687 // TODO(multi-display): The display containing the stack should be passed in.
1688 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001689 mRootActivityContainer.getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08001690 if (stack == null) {
1691 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
1692 return;
1693 }
Winson Chung19953ca2017-04-11 11:19:23 -07001694
1695 // It is possible for the bounds animation from the WM to call this but be delayed by
1696 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
1697 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
1698 // for the AMS lock to be freed. So check and make sure these bounds are still good.
Yunfan Chen279f5582018-12-12 15:24:50 -08001699 final TaskStack stackController = stack.getTaskStack();
Winson Chung8bca9e42017-04-16 15:59:43 -07001700 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07001701 return;
1702 }
1703
Robert Carr0d00c2e2016-02-29 17:45:02 -08001704 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
1705 mWindowManager.deferSurfaceLayout();
1706 try {
1707 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08001708 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02001709 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
1710 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08001711 insetBounds = tempRect;
1712 insetBounds.top = 0;
1713 insetBounds.left = 0;
1714 insetBounds.right = tempPinnedTaskBounds.width();
1715 insetBounds.bottom = tempPinnedTaskBounds.height();
1716 }
Winson Chung15036ca2018-05-31 15:51:47 -07001717 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
1718 // We have finished the animation into PiP, and are resizing the tasks to match the
1719 // stack bounds, while layouts are deferred, update any task state as a part of
1720 // transitioning it from fullscreen into a floating state.
1721 stack.onPipAnimationEndResize();
1722 }
Robert Carr7e4c90e2017-02-15 19:52:38 -08001723 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07001724 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08001725 } finally {
1726 mWindowManager.continueSurfaceLayout();
1727 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1728 }
1729 }
1730
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001731 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08001732 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001733 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07001734 /**
1735 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
1736 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
1737 * that the client receives onStop() before it is reparented. We do this by detaching
1738 * the stack from the display so that it will be considered invisible when
1739 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
1740 * invisible as well and added to the stopping list. After which we process the
1741 * stopping list by handling the idle.
1742 */
Yunfan Chen279f5582018-12-12 15:24:50 -08001743 stack.mForceHidden = true;
1744 stack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
1745 stack.mForceHidden = false;
Winson Chung47900652017-04-06 18:44:25 -07001746 activityIdleInternalLocked(null, false /* fromTimeout */,
1747 true /* processPausingActivites */, null /* configuration */);
1748
1749 // Move all the tasks to the bottom of the fullscreen stack
Yunfan Chen279f5582018-12-12 15:24:50 -08001750 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08001751 } else {
1752 for (int i = tasks.size() - 1; i >= 0; i--) {
1753 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07001754 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08001755 }
1756 }
1757 }
1758
1759 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001760 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07001761 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
1762 * instead moved back onto the fullscreen stack.
1763 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001764 void removeStack(ActivityStack stack) {
1765 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001766 }
1767
Wale Ogunwale68278562017-09-23 17:13:55 -07001768 /**
Winson Chung6954fc92017-03-24 16:22:12 -07001769 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
1770 */
Winson Chung0ec2a352017-10-26 11:38:30 -07001771 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
1772 String reason) {
1773 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
1774 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07001775 }
1776
1777 /**
Winson Chung010927a2016-12-15 16:12:35 -08001778 * Removes the task with the specified task id.
1779 *
1780 * @param taskId Identifier of the task to be removed.
1781 * @param killProcess Kill any process associated with the task if possible.
1782 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07001783 * @param pauseImmediately Pauses all task activities immediately without waiting for the
1784 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08001785 * @return Returns true if the given task was found and removed.
1786 */
Winson Chung6954fc92017-03-24 16:22:12 -07001787 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07001788 boolean pauseImmediately, String reason) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001789 final TaskRecord tr =
1790 mRootActivityContainer.anyTaskForId(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08001791 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07001792 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08001793 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001794 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08001795 if (tr.isPersistable) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001796 mService.notifyTaskPersisterLocked(null, true);
Winson Chung010927a2016-12-15 16:12:35 -08001797 }
1798 return true;
1799 }
1800 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
1801 return false;
1802 }
1803
1804 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
1805 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07001806 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08001807 }
1808 ComponentName component = tr.getBaseIntent().getComponent();
1809 if (component == null) {
1810 Slog.w(TAG, "No component for base intent of task: " + tr);
1811 return;
1812 }
1813
1814 // Find any running services associated with this app and stop if needed.
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001815 final Message msg = PooledLambda.obtainMessage(ActivityManagerInternal::cleanUpServices,
1816 mService.mAmInternal, tr.userId, component, new Intent(tr.getBaseIntent()));
1817 mService.mH.sendMessage(msg);
Winson Chung010927a2016-12-15 16:12:35 -08001818
1819 if (!killProcess) {
1820 return;
1821 }
1822
1823 // Determine if the process(es) for this task should be killed.
1824 final String pkg = component.getPackageName();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001825 ArrayList<Object> procsToKill = new ArrayList<>();
1826 ArrayMap<String, SparseArray<WindowProcessController>> pmap =
1827 mService.mProcessNames.getMap();
Winson Chung010927a2016-12-15 16:12:35 -08001828 for (int i = 0; i < pmap.size(); i++) {
1829
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001830 SparseArray<WindowProcessController> uids = pmap.valueAt(i);
Winson Chung010927a2016-12-15 16:12:35 -08001831 for (int j = 0; j < uids.size(); j++) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001832 WindowProcessController proc = uids.valueAt(j);
1833 if (proc.mUserId != tr.userId) {
Winson Chung010927a2016-12-15 16:12:35 -08001834 // Don't kill process for a different user.
1835 continue;
1836 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001837 if (proc == mService.mHomeProcess) {
Winson Chung010927a2016-12-15 16:12:35 -08001838 // Don't kill the home process along with tasks from the same package.
1839 continue;
1840 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001841 if (!proc.mPkgList.contains(pkg)) {
Winson Chung010927a2016-12-15 16:12:35 -08001842 // Don't kill process that is not associated with this task.
1843 continue;
1844 }
1845
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001846 if (!proc.shouldKillProcessForRemovedTask(tr)) {
1847 // Don't kill process(es) that has an activity in a different task that is also
Yunfan Chenafc15832018-07-26 16:34:28 +09001848 // in recents, or has an activity not stopped.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001849 return;
Winson Chung010927a2016-12-15 16:12:35 -08001850 }
1851
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001852 if (proc.hasForegroundServices()) {
Winson Chung010927a2016-12-15 16:12:35 -08001853 // Don't kill process(es) with foreground service.
1854 return;
1855 }
1856
1857 // Add process to kill list.
1858 procsToKill.add(proc);
1859 }
1860 }
1861
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001862 // Kill the running processes. Post on handle since we don't want to hold the service lock
1863 // while calling into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -07001864 final Message m = PooledLambda.obtainMessage(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001865 ActivityManagerInternal::killProcessesForRemovedTask, mService.mAmInternal,
1866 procsToKill);
Yohei Yukawae2fa39e2018-09-22 13:13:10 -07001867 mService.mH.sendMessage(m);
Winson Chung010927a2016-12-15 16:12:35 -08001868 }
1869
Chong Zhang5dcb2752015-08-18 13:50:26 -07001870 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07001871 * Called to restore the state of the task into the stack that it's supposed to go into.
1872 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07001873 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001874 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07001875 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001876 * @return true if the task has been restored successfully.
1877 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07001878 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001879 final ActivityStack stack =
1880 mRootActivityContainer.getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001881 final ActivityStack currentStack = task.getStack();
1882 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001883 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001884 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001885 // Nothing else to do since it is already restored in the right stack.
1886 return true;
1887 }
1888 // Remove current stack association, so we can re-associate the task with the
1889 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001890 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001891 }
1892
Wale Ogunwaleab5de372017-10-18 06:46:31 -07001893 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001894 // TODO: move call for creation here and other place into Stack.addTask()
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001895 task.createTask(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001896 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
1897 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08001898 final ArrayList<ActivityRecord> activities = task.mActivities;
1899 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001900 activities.get(activityNdx).createAppWindowToken();
Wale Ogunwale7de05352014-12-12 15:21:33 -08001901 }
1902 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07001903 }
1904
Winson Chung1dbc8112017-09-28 18:05:31 -07001905 @Override
1906 public void onRecentTaskAdded(TaskRecord task) {
1907 task.touchActiveTime();
1908 }
1909
1910 @Override
Winson Chunge6439102018-07-30 15:48:01 -07001911 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed, boolean killProcess) {
Winson Chung0ec2a352017-10-26 11:38:30 -07001912 if (wasTrimmed) {
1913 // Task was trimmed from the recent tasks list -- remove the active task record as well
1914 // since the user won't really be able to go back to it
Winson Chunge6439102018-07-30 15:48:01 -07001915 removeTaskByIdLocked(task.taskId, killProcess, false /* removeFromRecents */,
1916 !PAUSE_IMMEDIATELY, "recent-task-trimmed");
Winson Chung0ec2a352017-10-26 11:38:30 -07001917 }
Winson Chung1dbc8112017-09-28 18:05:31 -07001918 task.removedFromRecents();
1919 }
1920
Wale Ogunwale040b4702015-08-06 18:10:50 -07001921 /**
Winson Chung74666102017-02-22 17:49:24 -08001922 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
1923 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07001924 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001925 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001926 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001927 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001928 final int stackId = stack.mStackId;
1929 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07001930
Winson Chung74666102017-02-22 17:49:24 -08001931 // Check that we aren't reparenting to the same stack that the task is already in
1932 if (prevStack != null && prevStack.mStackId == stackId) {
1933 Slog.w(TAG, "Can not reparent to same stack, task=" + task
1934 + " already in stackId=" + stackId);
1935 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08001936 }
1937
Winson Chung74666102017-02-22 17:49:24 -08001938 // Ensure that we aren't trying to move into a multi-window stack without multi-window
1939 // support
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001940 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08001941 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001942 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08001943 }
1944
Andrii Kulianeafd9db2017-04-05 22:01:35 -07001945 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
1946 // multi-display.
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001947 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07001948 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
1949 + " reparent task=" + task + " to stackId=" + stackId);
1950 }
1951
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001952 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001953 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001954 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08001955 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
1956 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08001957 }
1958
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001959 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
1960 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001961 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001962 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
1963 + " Moving to a fullscreen stack instead.");
1964 if (prevStack != null) {
1965 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001966 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001967 stack = stack.getDisplay().createStack(
1968 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001969 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001970 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001971 }
1972
1973 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001974 scheduleSleepTimeout();
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001975 if (!mGoingToSleepWakeLock.isHeld()) {
1976 mGoingToSleepWakeLock.acquire();
1977 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001978 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1979 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07001980 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08001981 mLaunchingActivityWakeLock.release();
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001982 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001983 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001984 }
David Stevens9440dc82017-03-16 19:00:20 -07001985
Wale Ogunwaled32da472018-11-16 07:19:28 -08001986 mRootActivityContainer.applySleepTokens(false /* applyToStacks */);
David Stevens9440dc82017-03-16 19:00:20 -07001987
1988 checkReadyForSleepLocked(true /* allowDelay */);
1989 }
1990
Craig Mautner8d341ef2013-03-26 09:03:27 -07001991 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001992 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07001993
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001994 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07001995 final long endTime = System.currentTimeMillis() + timeout;
1996 while (true) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001997 if (!mRootActivityContainer.putStacksToSleep(
1998 true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001999 long timeRemaining = endTime - System.currentTimeMillis();
2000 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002001 try {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002002 mService.mGlobalLock.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002003 } catch (InterruptedException e) {
2004 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002005 } else {
2006 Slog.w(TAG, "Activity manager shutdown timed out");
2007 timedout = true;
2008 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002009 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002010 } else {
2011 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002012 }
2013 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002014
2015 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07002016 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002017
Craig Mautner8d341ef2013-03-26 09:03:27 -07002018 return timedout;
2019 }
2020
2021 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002022 removeSleepTimeouts();
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002023 if (mGoingToSleepWakeLock.isHeld()) {
2024 mGoingToSleepWakeLock.release();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002025 }
David Stevens9440dc82017-03-16 19:00:20 -07002026 }
2027
Craig Mautner0eea92c2013-05-16 13:35:39 -07002028 void activitySleptLocked(ActivityRecord r) {
2029 mGoingToSleepActivities.remove(r);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002030 final ActivityStack s = r.getActivityStack();
David Stevens9440dc82017-03-16 19:00:20 -07002031 if (s != null) {
2032 s.checkReadyForSleep();
2033 } else {
2034 checkReadyForSleepLocked(true);
2035 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002036 }
2037
David Stevens9440dc82017-03-16 19:00:20 -07002038 void checkReadyForSleepLocked(boolean allowDelay) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002039 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002040 // Do not care.
2041 return;
2042 }
2043
Wale Ogunwaled32da472018-11-16 07:19:28 -08002044 if (!mRootActivityContainer.putStacksToSleep(
2045 allowDelay, false /* shuttingDown */)) {
David Stevens18abd0e2017-08-17 14:55:47 -07002046 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002047 }
2048
Wei Wang65c7a152016-06-02 18:51:22 -07002049 // Send launch end powerhint before going sleep
Wale Ogunwaled32da472018-11-16 07:19:28 -08002050 mRootActivityContainer.sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07002051
Craig Mautner0eea92c2013-05-16 13:35:39 -07002052 removeSleepTimeouts();
2053
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002054 if (mGoingToSleepWakeLock.isHeld()) {
2055 mGoingToSleepWakeLock.release();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002056 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07002057 if (mService.mShuttingDown) {
2058 mService.mGlobalLock.notifyAll();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002059 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002060 }
2061
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002062 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07002063 // A resumed activity cannot be stopping. remove from list
2064 mStoppingActivities.remove(r);
2065
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002066 final ActivityStack stack = r.getActivityStack();
Riddle Hsubbb63c22018-10-03 12:28:29 +08002067 if (stack.getDisplay().allResumedActivitiesComplete()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002068 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
lumark588a3e82018-07-20 18:53:54 +08002069 // Make sure activity & window visibility should be identical
2070 // for all displays in this stage.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002071 mRootActivityContainer.executeAppTransitionForAllDisplay();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002072 return true;
2073 }
2074 return false;
2075 }
2076
Craig Mautnerbb742462014-07-07 15:28:55 -07002077 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002078 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002079 final TaskRecord task = r.getTaskRecord();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002080 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002081
2082 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07002083 mRecentTasks.add(task);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002084 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002085 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07002086
2087 // When launching tasks behind, update the last active time of the top task after the new
2088 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002089 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07002090 if (top != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002091 top.getTaskRecord().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07002092 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002093 }
2094
2095 void scheduleLaunchTaskBehindComplete(IBinder token) {
2096 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2097 }
2098
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002099 /** Checks whether the userid is a profile of the current user. */
2100 boolean isCurrentProfileLocked(int userId) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002101 if (userId == mRootActivityContainer.mCurrentUser) return true;
Wale Ogunwale86b74462018-07-02 08:42:43 -07002102 return mService.mAmInternal.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002103 }
2104
Bryce Leeb7c9b802017-05-02 14:20:24 -07002105 /**
2106 * Returns whether a stopping activity is present that should be stopped after visible, rather
2107 * than idle.
2108 * @return {@code true} if such activity is present. {@code false} otherwise.
2109 */
2110 boolean isStoppingNoHistoryActivity() {
2111 // Activities that are marked as nohistory should be stopped immediately after the resumed
2112 // activity has become visible.
2113 for (ActivityRecord record : mStoppingActivities) {
2114 if (record.isNoHistory()) {
2115 return true;
2116 }
2117 }
2118
2119 return false;
2120 }
2121
Winson Chung4dabf232017-01-25 13:25:22 -08002122 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
2123 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002124 ArrayList<ActivityRecord> stops = null;
2125
Wale Ogunwaled32da472018-11-16 07:19:28 -08002126 final boolean nowVisible = mRootActivityContainer.allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002127 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2128 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07002129 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002130 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002131 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2132 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07002133 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07002134 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002135 if (s.finishing) {
2136 // If this activity is finishing, it is sitting on top of
2137 // everyone else but we now know it is no longer needed...
2138 // so get rid of it. Otherwise, we need to go through the
2139 // normal flow and hide it once we determine that it is
2140 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002141 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002142 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002143 }
2144 }
David Stevens9440dc82017-03-16 19:00:20 -07002145 if (remove) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002146 final ActivityStack stack = s.getActivityStack();
David Stevens9440dc82017-03-16 19:00:20 -07002147 final boolean shouldSleepOrShutDown = stack != null
2148 ? stack.shouldSleepOrShutDownActivities()
Wale Ogunwalef6733932018-06-27 05:14:34 -07002149 : mService.isSleepingOrShuttingDownLocked();
David Stevens9440dc82017-03-16 19:00:20 -07002150 if (!waitingVisible || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08002151 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07002152 // Defer processing pausing activities in this iteration and reschedule
2153 // a delayed idle to reprocess it again
2154 removeTimeoutsForActivityLocked(idleActivity);
2155 scheduleIdleTimeoutLocked(idleActivity);
2156 continue;
2157 }
Winson Chung4dabf232017-01-25 13:25:22 -08002158
David Stevens9440dc82017-03-16 19:00:20 -07002159 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
2160 if (stops == null) {
2161 stops = new ArrayList<>();
2162 }
2163 stops.add(s);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02002164
2165 // Make sure to remove it in all cases in case we entered this block with
2166 // shouldSleepOrShutDown
2167 mActivitiesWaitingForVisibleActivity.remove(s);
David Stevens9440dc82017-03-16 19:00:20 -07002168 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002169 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002170 }
2171 }
2172
2173 return stops;
2174 }
2175
Craig Mautner27084302013-03-25 08:05:25 -07002176 public void dump(PrintWriter pw, String prefix) {
Riddle Hsu75016992018-09-20 20:37:14 +08002177 pw.println();
2178 pw.println("ActivityStackSupervisor state:");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002179 mRootActivityContainer.dump(pw, prefix);
Riddle Hsu75016992018-09-20 20:37:14 +08002180 pw.print(prefix);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002181 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Wale Ogunwaled32da472018-11-16 07:19:28 -08002182 pw.println(prefix + "mUserStackInFront=" + mRootActivityContainer.mUserStackInFront);
Bryce Lee4a194382017-04-04 14:32:48 -07002183 if (!mWaitingForActivityVisible.isEmpty()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002184 pw.println(prefix + "mWaitingForActivityVisible=");
Bryce Lee4a194382017-04-04 14:32:48 -07002185 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002186 pw.print(prefix + prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
Bryce Lee4a194382017-04-04 14:32:48 -07002187 }
2188 }
Winson Chungc1674272018-02-21 10:15:17 -08002189 pw.print(prefix); pw.print("isHomeRecentsComponent=");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002190 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mRootActivityContainer.mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07002191
Bryce Lee459c0622018-03-19 11:04:01 -07002192 getKeyguardController().dump(pw, prefix);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002193 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07002194 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002195
Dianne Hackborn390517b2013-05-30 15:03:32 -07002196 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2197 boolean needSep, String prefix) {
2198 if (activity != null) {
2199 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2200 if (needSep) {
2201 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002202 }
2203 pw.print(prefix);
2204 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002205 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002206 }
2207 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002208 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002209 }
2210
Dianne Hackborn390517b2013-05-30 15:03:32 -07002211 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002212 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07002213 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002214 String innerPrefix = null;
2215 String[] args = null;
2216 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002217 for (int i=list.size()-1; i>=0; i--) {
2218 final ActivityRecord r = list.get(i);
2219 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2220 continue;
2221 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002222 if (innerPrefix == null) {
2223 innerPrefix = prefix + " ";
2224 args = new String[0];
2225 }
2226 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002227 final boolean full = !brief && (complete || !r.isInHistory());
2228 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002229 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002230 needNL = false;
2231 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07002232 if (header != null) {
2233 pw.println(header);
2234 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002235 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002236 if (lastTask != r.getTaskRecord()) {
2237 lastTask = r.getTaskRecord();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002238 pw.print(prefix);
2239 pw.print(full ? "* " : " ");
2240 pw.println(lastTask);
2241 if (full) {
2242 lastTask.dump(pw, prefix + " ");
2243 } else if (complete) {
2244 // Complete + brief == give a summary. Isn't that obvious?!?
2245 if (lastTask.intent != null) {
2246 pw.print(prefix); pw.print(" ");
2247 pw.println(lastTask.intent.toInsecureStringWithClip());
2248 }
2249 }
2250 }
2251 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2252 pw.print(" #"); pw.print(i); pw.print(": ");
2253 pw.println(r);
2254 if (full) {
2255 r.dump(pw, innerPrefix);
2256 } else if (complete) {
2257 // Complete + brief == give a summary. Isn't that obvious?!?
2258 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2259 if (r.app != null) {
2260 pw.print(innerPrefix); pw.println(r.app);
2261 }
2262 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002263 if (client && r.attachedToProcess()) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002264 // flush anything that is already in the PrintWriter since the thread is going
2265 // to write to the file descriptor directly
2266 pw.flush();
2267 try {
2268 TransferPipe tp = new TransferPipe();
2269 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002270 r.app.getThread().dumpActivity(
2271 tp.getWriteFd(), r.appToken, innerPrefix, args);
2272 // Short timeout, since blocking here can deadlock with the application.
Craig Mautner8d341ef2013-03-26 09:03:27 -07002273 tp.go(fd, 2000);
2274 } finally {
2275 tp.kill();
2276 }
2277 } catch (IOException e) {
2278 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2279 } catch (RemoteException e) {
2280 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2281 }
2282 needNL = true;
2283 }
2284 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002285 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002286 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002287
Craig Mautnerf3333272013-04-22 10:55:53 -07002288 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002289 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2290 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002291 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2292 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002293 }
2294
2295 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002296 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002297 }
2298
2299 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002300 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2301 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002302 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2303 }
2304
Craig Mautner05d29032013-05-03 13:40:13 -07002305 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002306 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2307 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2308 }
Craig Mautner05d29032013-05-03 13:40:13 -07002309 }
2310
Craig Mautner0eea92c2013-05-16 13:35:39 -07002311 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002312 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2313 }
2314
2315 final void scheduleSleepTimeout() {
2316 removeSleepTimeouts();
2317 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2318 }
2319
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002320 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002321 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002322 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002323 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07002324 }
2325
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002326 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002327 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002328 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002329 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002330 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002331 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002332 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002333 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002334 return;
2335 }
2336
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002337 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002338 if (isSecondaryDisplayPreferred) {
2339 final int actualDisplayId = task.getStack().mDisplayId;
2340 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08002341 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002342 }
Jeff Chang78f54412019-02-14 20:42:27 +08002343
2344 final ActivityDisplay preferredDisplay =
2345 mRootActivityContainer.getActivityDisplay(preferredDisplayId);
2346
2347 final boolean singleTaskInstance = preferredDisplay != null
2348 && preferredDisplay.isSingleTaskInstance();
2349
2350 if (singleTaskInstance) {
2351 // Suppress the warning toast if the preferredDisplay was set to singleTask.
2352 // The singleTaskInstance displays will only contain one task and any attempt to
2353 // launch new task will re-route to the default display.
2354 return;
2355 }
2356
Andrii Kulianb850ea52017-12-12 23:49:10 -08002357 if (preferredDisplayId != actualDisplayId) {
Riddle Hsu16567132018-08-16 21:37:47 +08002358 Slog.w(TAG, "Failed to put " + task + " on display " + preferredDisplayId);
2359 // Display a warning toast that we failed to put a task on a secondary display.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002360 mService.getTaskChangeNotificationController()
Mark Renoufc808f062019-02-07 15:20:37 -05002361 .notifyActivityLaunchOnSecondaryDisplayFailed(task.getTaskInfo(),
2362 preferredDisplayId);
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002363 return;
Riddle Hsu16567132018-08-16 21:37:47 +08002364 } else if (!forceNonResizable && handleForcedResizableTask(task,
2365 FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY)) {
2366 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002367 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002368 }
2369
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002370 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002371 // Display a warning toast that we tried to put an app that doesn't support split-screen
2372 // in split-screen.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002373 mService.getTaskChangeNotificationController().notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07002374
Andrii Kulianc27916642016-04-12 17:59:27 -07002375 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
2376 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002377
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002378 final ActivityStack dockedStack =
2379 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002380 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002381 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002382 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002383 return;
2384 }
2385
Riddle Hsu16567132018-08-16 21:37:47 +08002386 handleForcedResizableTask(task, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN);
2387 }
2388
2389 /**
2390 * @return {@code true} if the top activity of the task is forced to be resizable and the user
2391 * was notified about activity being forced resized.
2392 */
2393 private boolean handleForcedResizableTask(TaskRecord task, int reason) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002394 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002395 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Riddle Hsu16567132018-08-16 21:37:47 +08002396 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002397 final String packageName = topActivity.appInfo.packageName;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002398 mService.getTaskChangeNotificationController().notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002399 task.taskId, reason, packageName);
Riddle Hsu16567132018-08-16 21:37:47 +08002400 return true;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002401 }
Riddle Hsu16567132018-08-16 21:37:47 +08002402 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -08002403 }
2404
Jorim Jaggife89d122015-12-22 16:28:44 +01002405 void activityRelaunchedLocked(IBinder token) {
2406 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07002407 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2408 if (r != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002409 if (r.getActivityStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07002410 r.setSleeping(true, true);
2411 }
2412 }
Jorim Jaggife89d122015-12-22 16:28:44 +01002413 }
2414
2415 void activityRelaunchingLocked(ActivityRecord r) {
2416 mWindowManager.notifyAppRelaunching(r.appToken);
2417 }
2418
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002419 void logStackState() {
2420 mActivityMetricsLogger.logWindowState();
2421 }
2422
Winson Chung5af42fc2017-03-24 17:11:33 -07002423 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07002424 // If the stack is animating in a way where we will be forcing a multi-mode change at the
2425 // end, then ensure that we defer all in between multi-window mode changes
2426 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
2427 return;
2428 }
2429
Wale Ogunwale22e25262016-02-01 10:32:02 -08002430 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
2431 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002432 if (r.attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002433 mMultiWindowModeChangedActivities.add(r);
2434 }
2435 }
2436
2437 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
2438 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
2439 }
2440 }
2441
Winson Chung5af42fc2017-03-24 17:11:33 -07002442 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002443 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002444 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002445 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002446 return;
2447 }
2448
Evan Roskydfe3da72018-10-26 17:21:06 -07002449 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getRequestedOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07002450 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08002451
Winson Chungab76bbc2017-08-14 13:33:51 -07002452 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
2453 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
2454 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002455 if (r.attachedToProcess()) {
Winson Chungab76bbc2017-08-14 13:33:51 -07002456 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08002457 // If we are scheduling pip change, then remove this activity from multi-window
2458 // change list as the processing of pip change will make sure multi-window changed
2459 // message is processed in the right order relative to pip changed.
2460 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07002461 }
Winson Chungab76bbc2017-08-14 13:33:51 -07002462 }
2463 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07002464
Winson Chungab76bbc2017-08-14 13:33:51 -07002465 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
2466 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
2467 }
2468 }
2469
2470 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
2471 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
2472 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
2473 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002474 if (r.attachedToProcess()) {
Winson Chungab76bbc2017-08-14 13:33:51 -07002475 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07002476 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08002477 }
2478 }
2479
chaviw59b98852017-06-13 12:05:44 -07002480 void wakeUp(String reason) {
Michael Wrighte3001042019-02-05 00:13:14 +00002481 mPowerManager.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_APPLICATION,
2482 "android.server.am:TURN_ON:" + reason);
chaviw59b98852017-06-13 12:05:44 -07002483 }
2484
2485 /**
2486 * Begin deferring resume to avoid duplicate resumes in one pass.
2487 */
Riddle Hsu402b4402018-11-06 17:23:15 +08002488 void beginDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07002489 mDeferResumeCount++;
2490 }
2491
2492 /**
2493 * End deferring resume and determine if resume can be called.
2494 */
Riddle Hsu402b4402018-11-06 17:23:15 +08002495 void endDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07002496 mDeferResumeCount--;
2497 }
2498
Wale Ogunwaled32da472018-11-16 07:19:28 -08002499 /** @return True if resume can be called. */
2500 boolean readyToResume() {
chaviw59b98852017-06-13 12:05:44 -07002501 return mDeferResumeCount == 0;
2502 }
2503
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002504 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002505
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002506 public ActivityStackSupervisorHandler(Looper looper) {
2507 super(looper);
2508 }
2509
Winson Chung4dabf232017-01-25 13:25:22 -08002510 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002511 synchronized (mService.mGlobalLock) {
Winson Chung4dabf232017-01-25 13:25:22 -08002512 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
2513 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002514 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002515 }
2516
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002517 @Override
2518 public void handleMessage(Message msg) {
2519 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002520 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002521 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002522 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
2523 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07002524 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08002525 }
2526 }
2527 } break;
2528 case REPORT_PIP_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002529 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08002530 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
2531 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07002532 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
2533 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08002534 }
2535 }
2536 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07002537 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002538 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2539 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002540 // We don't at this point know if the activity is fullscreen,
2541 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08002542 activityIdleInternal((ActivityRecord) msg.obj,
2543 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07002544 } break;
2545 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002546 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08002547 activityIdleInternal((ActivityRecord) msg.obj,
2548 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07002549 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07002550 case RESUME_TOP_ACTIVITY_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002551 synchronized (mService.mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002552 mRootActivityContainer.resumeFocusedStacksTopActivities();
Craig Mautner05d29032013-05-03 13:40:13 -07002553 }
2554 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002555 case SLEEP_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002556 synchronized (mService.mGlobalLock) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07002557 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002558 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07002559 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002560 }
2561 }
2562 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002563 case LAUNCH_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002564 synchronized (mService.mGlobalLock) {
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002565 if (mLaunchingActivityWakeLock.isHeld()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002566 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
2567 if (VALIDATE_WAKE_LOCK_CALLER
2568 && Binder.getCallingUid() != Process.myUid()) {
2569 throw new IllegalStateException("Calling must be system uid");
2570 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08002571 mLaunchingActivityWakeLock.release();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002572 }
2573 }
2574 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07002575 case LAUNCH_TASK_BEHIND_COMPLETE: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002576 synchronized (mService.mGlobalLock) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002577 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07002578 if (r != null) {
2579 handleLaunchTaskBehindCompleteLocked(r);
2580 }
2581 }
2582 } break;
Louis Changdcdde952018-12-04 15:38:44 +08002583 case REPORT_HOME_CHANGED_MSG: {
2584 synchronized (mService.mGlobalLock) {
2585 mHandler.removeMessages(REPORT_HOME_CHANGED_MSG);
Chong Zhangc806d902015-11-30 09:44:27 -08002586
Louis Changdcdde952018-12-04 15:38:44 +08002587 // Start home activities on displays with no activities.
2588 mRootActivityContainer.startHomeOnEmptyDisplays("homeChanged");
2589 }
2590 }
2591 break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002592 }
2593 }
2594 }
Craig Mautnered6649f2013-12-02 14:08:25 -08002595
Jorim Jaggic69bd222016-03-15 14:38:37 +01002596 /**
2597 * Puts a task into resizing mode during the next app transition.
2598 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002599 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01002600 */
Bryce Leed3624e12017-11-30 08:51:45 -08002601 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002602 mResizingTasksDuringAnimation.add(task.taskId);
2603 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01002604 }
2605
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002606 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
2607 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002608 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002609 final String callingPackage;
2610 final Intent intent;
2611 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002612 int activityType = ACTIVITY_TYPE_UNDEFINED;
2613 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002614 final ActivityOptions activityOptions = options != null
2615 ? options.getOptions(this)
2616 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002617 if (activityOptions != null) {
2618 activityType = activityOptions.getLaunchActivityType();
2619 windowingMode = activityOptions.getLaunchWindowingMode();
2620 }
2621 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002622 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002623 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002624 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07002625
Matthew Ng606dd802017-06-05 14:06:32 -07002626 mWindowManager.deferSurfaceLayout();
2627 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002628 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07002629 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07002630 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07002631
Matthew Ng606dd802017-06-05 14:06:32 -07002632 // Defer updating the stack in which recents is until the app transition is done, to
2633 // not run into issues where we still need to draw the task in recents but the
2634 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08002635 deferUpdateRecentsHomeStackBounds();
lumark588a3e82018-07-20 18:53:54 +08002636 // TODO(multi-display): currently recents animation only support default display.
Matthew Ng606dd802017-06-05 14:06:32 -07002637 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002638 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002639
Wale Ogunwaled32da472018-11-16 07:19:28 -08002640 task = mRootActivityContainer.anyTaskForId(taskId,
2641 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07002642 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08002643 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07002644 mWindowManager.executeAppTransition();
2645 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002646 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07002647 }
2648
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08002649 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
2650 // We always want to return to the home activity instead of the recents activity
2651 // from whatever is started from the recents activity, so move the home stack
2652 // forward.
Louis Changbd48dca2018-08-29 17:44:34 +08002653 // TODO (b/115289124): Multi-display supports for recents.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002654 mRootActivityContainer.getDefaultDisplay().moveHomeStackToFront(
2655 "startActivityFromRecents");
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08002656 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002657
Matthew Ng606dd802017-06-05 14:06:32 -07002658 // If the user must confirm credentials (e.g. when first launching a work app and the
2659 // Work Challenge is present) let startActivityInPackage handle the intercepting.
Wale Ogunwale86b74462018-07-02 08:42:43 -07002660 if (!mService.mAmInternal.shouldConfirmCredentials(task.userId)
Matthew Ng606dd802017-06-05 14:06:32 -07002661 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07002662 final ActivityRecord targetActivity = task.getTopActivity();
2663
Wale Ogunwaled32da472018-11-16 07:19:28 -08002664 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(
2665 true /* forceSend */, targetActivity);
Igor Murashkin212d06c2018-10-22 16:34:39 -07002666 mActivityMetricsLogger.notifyActivityLaunching(task.intent);
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002667 try {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002668 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002669 true /* fromRecents */);
lumark5e8aff42019-01-11 22:22:59 +08002670 // Apply options to prevent pendingOptions be taken by client to make sure
2671 // the override pending app transition will be applied immediately.
2672 targetActivity.applyOptionsLocked();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02002673 } finally {
2674 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
2675 targetActivity);
2676 }
Matthew Ng606dd802017-06-05 14:06:32 -07002677
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002678 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
Bryce Leed3624e12017-11-30 08:51:45 -08002679 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
2680 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07002681 return ActivityManager.START_TASK_TO_FRONT;
2682 }
Matthew Ng606dd802017-06-05 14:06:32 -07002683 callingPackage = task.mCallingPackage;
2684 intent = task.intent;
2685 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
2686 userId = task.userId;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002687 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002688 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07002689 null, 0, 0, options, userId, task, "startActivityFromRecents",
Michal Karpinskiac116df2018-12-10 17:51:42 +00002690 false /* validateIncomingUser */, null /* originatingPendingIntent */,
2691 false /* allowBackgroundActivityStart */);
Matthew Ng606dd802017-06-05 14:06:32 -07002692 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002693 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
2694 // If we are launching the task in the docked stack, put it into resizing mode so
2695 // the window renders full-screen with the background filling the void. Also only
2696 // call this at the end to make sure that tasks exists on the window manager side.
2697 setResizingDuringAnimation(task);
2698
2699 final ActivityDisplay display = task.getStack().getDisplay();
2700 final ActivityStack topSecondaryStack =
2701 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2702 if (topSecondaryStack.isActivityTypeHome()) {
Louis Changbd48dca2018-08-29 17:44:34 +08002703 // If the home activity is the top split-screen secondary stack, then the
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002704 // primary split-screen stack is in the minimized mode which means it can't
2705 // receive input keys, so we should move the focused app to the home app so that
2706 // window manager can correctly calculate the focus window that can receive
2707 // input keys.
Louis Changbd48dca2018-08-29 17:44:34 +08002708 display.moveHomeStackToFront(
2709 "startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08002710
2711 // Immediately update the minimized docked stack mode, the upcoming animation
2712 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
2713 // will do the animation to the target bounds
2714 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08002715 }
2716 }
Matthew Ng606dd802017-06-05 14:06:32 -07002717 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002718 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002719 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07002720
2721 /**
Bryce Lee4a194382017-04-04 14:32:48 -07002722 * Internal container to store a match qualifier alongside a WaitResult.
2723 */
2724 static class WaitInfo {
2725 private final ComponentName mTargetComponent;
2726 private final WaitResult mResult;
Vishnu Nair132ee832018-09-28 15:00:05 -07002727 /** Time stamp when we started to wait for {@link WaitResult}. */
2728 private final long mStartTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07002729
Vishnu Nair132ee832018-09-28 15:00:05 -07002730 WaitInfo(ComponentName targetComponent, WaitResult result, long startTimeMs) {
Bryce Lee4a194382017-04-04 14:32:48 -07002731 this.mTargetComponent = targetComponent;
2732 this.mResult = result;
Vishnu Nair132ee832018-09-28 15:00:05 -07002733 this.mStartTimeMs = startTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07002734 }
2735
Wale Ogunwale3270f172017-04-26 07:29:42 -07002736 public boolean matches(ComponentName targetComponent) {
2737 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07002738 }
2739
2740 public WaitResult getResult() {
2741 return mResult;
2742 }
2743
Vishnu Nair132ee832018-09-28 15:00:05 -07002744 public long getStartTime() {
2745 return mStartTimeMs;
2746 }
2747
Bryce Lee4a194382017-04-04 14:32:48 -07002748 public ComponentName getComponent() {
2749 return mTargetComponent;
2750 }
2751
2752 public void dump(PrintWriter pw, String prefix) {
2753 pw.println(prefix + "WaitInfo:");
2754 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
2755 pw.println(prefix + " mResult=");
2756 mResult.dump(pw, prefix);
2757 }
2758 }
Craig Mautner27084302013-03-25 08:05:25 -07002759}