blob: 6034f81f9cc87258b8bb3665f487ce85c5c90e55 [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;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070028import static android.app.ActivityTaskManager.INVALID_STACK_ID;
Louis Chang39ba54b2018-10-18 11:28:57 +080029import static android.app.ActivityTaskManager.INVALID_TASK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070030import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
31import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Vishnu Nair132ee832018-09-28 15:00:05 -070032import static android.app.WaitResult.INVALID_DELAY;
Wale Ogunwale68278562017-09-23 17:13:55 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070034import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070036import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
37import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070039import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070040import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
42import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070043import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070044import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070045import static android.app.WindowConfiguration.activityTypeToString;
46import static android.app.WindowConfiguration.windowingModeToString;
Louis Chang89f43fc2018-10-05 10:59:14 +080047import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Louis Changbd48dca2018-08-29 17:44:34 +080048import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
49import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale214f3482018-10-04 11:00:47 -070050import static android.content.pm.PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070051import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020052import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Adrian Roosa6d6aab2018-04-19 18:58:22 +020053import static android.graphics.Rect.copyOrNull;
chaviw59b98852017-06-13 12:05:44 -070054import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010055import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020056import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
57import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070058import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070059import static android.view.Display.TYPE_VIRTUAL;
Louis Chang7d0037c2018-08-13 12:42:06 +080060import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Louis Changceeb5062018-09-17 18:13:52 +080061
Louis Chang1eff2482018-11-01 15:46:31 +080062import static com.android.server.am.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
63import static com.android.server.am.ActivityStackSupervisorProto.DISPLAYS;
64import static com.android.server.am.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
65import static com.android.server.am.ActivityStackSupervisorProto.IS_HOME_RECENTS_COMPONENT;
66import static com.android.server.am.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
67import static com.android.server.am.ActivityStackSupervisorProto.PENDING_ACTIVITIES;
68import static com.android.server.am.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Wale Ogunwale59507092018-10-29 09:00:30 -070069import static com.android.server.wm.ActivityStack.ActivityState.DESTROYED;
70import static com.android.server.wm.ActivityStack.ActivityState.INITIALIZING;
71import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
72import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
73import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
74import static com.android.server.wm.ActivityStack.ActivityState.STOPPED;
75import static com.android.server.wm.ActivityStack.ActivityState.STOPPING;
76import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale59507092018-10-29 09:00:30 -070077import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ALL;
78import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_IDLE;
79import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_PAUSE;
80import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS;
81import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RELEASE;
82import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STACK;
83import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STATES;
84import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
85import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
86import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_IDLE;
87import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_PAUSE;
88import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RECENTS;
89import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RELEASE;
90import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STACK;
91import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STATES;
92import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SWITCH;
93import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TASKS;
94import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
95import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
96import static com.android.server.wm.ActivityTaskManagerService.ANIMATE;
97import static com.android.server.wm.ActivityTaskManagerService.H.FIRST_SUPERVISOR_STACK_MSG;
98import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE;
99import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
100import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
101import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
102import static com.android.server.wm.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
103import static com.android.server.wm.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
104import static com.android.server.wm.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Louis Changceeb5062018-09-17 18:13:52 +0800105
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800106import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200107
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800109import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700110import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700111import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100112import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700113import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800115import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800116import android.app.ActivityManager.StackInfo;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700117import android.app.ActivityManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.app.ActivityOptions;
Louis Chang89f43fc2018-10-05 10:59:14 +0800119import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700120import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700121import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700122import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700123import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700124import android.app.WindowConfiguration.ActivityType;
125import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800126import android.app.servertransaction.ActivityLifecycleItem;
127import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700128import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800129import android.app.servertransaction.PauseActivityItem;
130import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700132import android.content.Context;
133import android.content.Intent;
134import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700136import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137import android.content.pm.PackageManager;
138import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100139import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.content.res.Configuration;
Garfield Tan4a48a7f2018-10-02 14:23:55 -0700141import android.content.res.Resources;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800142import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143import android.hardware.display.DisplayManager;
144import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800145import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800146import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100148import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700149import android.os.Debug;
Louis Chang89f43fc2018-10-05 10:59:14 +0800150import android.os.FactoryTest;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700151import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700152import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700153import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700154import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700155import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700156import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700157import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700158import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700159import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700160import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100161import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700162import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700163import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700164import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700165import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700166import android.util.ArraySet;
Garfield Tan4a48a7f2018-10-02 14:23:55 -0700167import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700168import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800169import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700170import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700171import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800172import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800173import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700174import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700175import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800176import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800177
Andreas Gampea36dc622018-02-05 17:19:22 -0800178import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700179import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800180import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700181import com.android.internal.os.TransferPipe;
Louis Chang7d0037c2018-08-13 12:42:06 +0800182import com.android.internal.os.logging.MetricsLoggerWrapper;
Svetoslav7008b512015-06-24 18:47:07 -0700183import com.android.internal.util.ArrayUtils;
Louis Chang7d0037c2018-08-13 12:42:06 +0800184import com.android.internal.util.function.pooled.PooledLambda;
Jeff Brownca9bc702014-02-11 14:32:56 -0800185import com.android.server.LocalServices;
Wale Ogunwale59507092018-10-29 09:00:30 -0700186import com.android.server.am.ActivityManagerService;
187import com.android.server.am.AppTimeTracker;
188import com.android.server.am.EventLogTags;
189import com.android.server.am.UserState;
190import com.android.server.wm.ActivityStack.ActivityState;
Louis Chang7d0037c2018-08-13 12:42:06 +0800191import com.android.server.wm.ActivityTaskManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700192
Craig Mautner8d341ef2013-03-26 09:03:27 -0700193import java.io.FileDescriptor;
194import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700195import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800196import java.lang.annotation.Retention;
197import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700198import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700199import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700200import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700201import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700202
Winson Chung1dbc8112017-09-28 18:05:31 -0700203public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800204 RecentTasks.Callbacks, RootWindowContainerListener {
Wale Ogunwale98875612018-10-12 07:53:02 -0700205 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_ATM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700207 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700208 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700210 private static final String TAG_STACK = TAG + POSTFIX_STACK;
211 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Andrii Kulianab132ee2018-07-24 22:10:21 +0800212 static final String TAG_STATES = TAG + POSTFIX_STATES;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800213 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800214
Craig Mautnerf3333272013-04-22 10:55:53 -0700215 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700216 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700217
Craig Mautner0eea92c2013-05-16 13:35:39 -0700218 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700219 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700221 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700222 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223
Craig Mautner05d29032013-05-03 13:40:13 -0700224 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
225 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
226 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700227 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700228 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700229 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800230 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
231 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. stack) that we are trying to get
236 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800237 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700239 // Used to indicate that windows of activities should be preserved during the resize.
240 static final boolean PRESERVE_WINDOWS = true;
241
Wale Ogunwale040b4702015-08-06 18:10:50 -0700242 // Used to indicate if an object (e.g. task) should be moved/created
243 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700244 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700245
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700246 // Don't execute any calls to resume.
247 static final boolean DEFER_RESUME = true;
248
Winson Chung010927a2016-12-15 16:12:35 -0800249 // Used to indicate that a task is removed it should also be removed from recents.
250 static final boolean REMOVE_FROM_RECENTS = true;
251
Winson Chung6954fc92017-03-24 16:22:12 -0700252 // Used to indicate that pausing an activity should occur immediately without waiting for
253 // the activity callback indicating that it has completed pausing
254 static final boolean PAUSE_IMMEDIATELY = true;
255
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200256 /** True if the docked stack is currently being resized. */
257 private boolean mDockedStackResizing;
258
259 /**
260 * True if there are pending docked bounds that need to be applied after
261 * {@link #mDockedStackResizing} is reset to false.
262 */
263 private boolean mHasPendingDockedBounds;
264 private Rect mPendingDockedBounds;
265 private Rect mPendingTempDockedTaskBounds;
266 private Rect mPendingTempDockedTaskInsetBounds;
267 private Rect mPendingTempOtherTaskBounds;
268 private Rect mPendingTempOtherTaskInsetBounds;
269
Winson Chung7900bee2017-03-10 08:59:25 -0800270 /**
271 * The modes which affect which tasks are returned when calling
272 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
273 */
274 @Retention(RetentionPolicy.SOURCE)
275 @IntDef({
276 MATCH_TASK_IN_STACKS_ONLY,
277 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
278 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
279 })
280 public @interface AnyTaskForIdMatchTaskMode {}
281 // Match only tasks in the current stacks
282 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
283 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
284 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
285 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
286 // provided stack id
287 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
288
Svetoslav7008b512015-06-24 18:47:07 -0700289 // Activity actions an app cannot start if it uses a permission which is not granted.
290 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
291 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700292
Svetoslav7008b512015-06-24 18:47:07 -0700293 static {
294 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
295 Manifest.permission.CAMERA);
296 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
297 Manifest.permission.CAMERA);
298 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
299 Manifest.permission.CALL_PHONE);
300 }
301
Svet Ganov99b60432015-06-27 13:15:22 -0700302 /** Action restriction: launching the activity is not restricted. */
303 private static final int ACTIVITY_RESTRICTION_NONE = 0;
304 /** Action restriction: launching the activity is restricted by a permission. */
305 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
306 /** Action restriction: launching the activity is restricted by an app op. */
307 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700308
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700309 // For debugging to make sure the caller when acquiring/releasing our
310 // wake lock is the system process.
311 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800312 /** The number of distinct task ids that can be assigned to the tasks of a single user */
313 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700314
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700315 ActivityTaskManagerService mService;
Craig Mautner27084302013-03-25 08:05:25 -0700316
Winson Chung61c9e5a2017-10-11 10:39:32 -0700317 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700318 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800319
Winson Chung61c9e5a2017-10-11 10:39:32 -0700320 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700321 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700322
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700323 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000324 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325
326 /** Short cut */
327 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800328 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329
Bryce Leeec55eb02017-12-05 20:51:27 -0800330 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700331
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800332 /**
333 * Maps the task identifier that activities are currently being started in to the userId of the
334 * task. Each time a new task is created, the entry for the userId of the task is incremented
335 */
336 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700337
Craig Mautner2420ead2013-04-01 17:13:20 -0700338 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800339 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700340
Craig Mautnerde4ef022013-04-07 19:01:33 -0700341 /** List of activities that are waiting for a new activity to become visible before completing
342 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700343 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
344 // mStoppingActivities when something else comes up.
345 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346
Bryce Lee4a194382017-04-04 14:32:48 -0700347 /** List of processes waiting to find out when a specific activity becomes visible. */
348 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700349
350 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700351 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700352
Craig Mautnerde4ef022013-04-07 19:01:33 -0700353 /** List of activities that are ready to be stopped, but waiting for the next activity to
354 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800355 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700356
Craig Mautnerf3333272013-04-22 10:55:53 -0700357 /** List of activities that are ready to be finished, but waiting for the previous activity to
358 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800359 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700360
Craig Mautner0eea92c2013-05-16 13:35:39 -0700361 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800362 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363
Wale Ogunwale22e25262016-02-01 10:32:02 -0800364 /** List of activities whose multi-window mode changed that we need to report to the
365 * application */
366 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
367
368 /** List of activities whose picture-in-picture mode changed that we need to report to the
369 * application */
370 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
371
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100372 /**
373 * Animations that for the current transition have requested not to
374 * be considered for the transition animation.
375 */
376 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
377
Winson Chung5af42fc2017-03-24 17:11:33 -0700378 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
379 * the application */
380 Rect mPipModeChangedTargetStackBounds;
381
Craig Mautnerf3333272013-04-22 10:55:53 -0700382 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700383 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700384
Craig Mautnerde4ef022013-04-07 19:01:33 -0700385 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
386 * is being brought in front of us. */
387 boolean mUserLeaving = false;
388
Bryce Leed3624e12017-11-30 08:51:45 -0800389 /** Set when a power hint has started, but not ended. */
390 private boolean mPowerHintSent;
391
Craig Mautner0eea92c2013-05-16 13:35:39 -0700392 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700393 * We don't want to allow the device to go to sleep while in the process
394 * of launching an activity. This is primarily to allow alarm intent
395 * receivers to launch an activity and get that to run before the device
396 * goes back to sleep.
397 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700398 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700399
400 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700401 * Set when the system is going to sleep, until we have
402 * successfully paused the current activity and released our wake lock.
403 * At that point the system is allowed to actually sleep.
404 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700405 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700406
David Stevens9440dc82017-03-16 19:00:20 -0700407 /**
408 * A list of tokens that cause the top activity to be put to sleep.
409 * They are used by components that may hide and block interaction with underlying
410 * activities.
411 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700412 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700413
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700414 /** Stack id of the front stack when user switched, indexed by userId. */
415 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700416
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800417 /** Reference to default display so we can quickly look it up. */
418 private ActivityDisplay mDefaultDisplay;
419
420 /**
421 * List of displays which contain activities, sorted by z-order.
422 * The last entry in the list is the topmost.
423 */
424 private final ArrayList<ActivityDisplay> mActivityDisplays = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800426 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
427
428 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800429
Wale Ogunwaled046a012015-12-24 13:05:59 -0800430 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800431 boolean inResumeTopActivity;
432
Andrii Kulian1e32e022016-09-16 15:29:34 -0700433 /**
434 * Temporary rect used during docked stack resize calculation so we don't need to create a new
435 * object each time.
436 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700437 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700438 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700439
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700440 // The default minimal size that will be used if the activity doesn't specify its minimal size.
441 // It will be calculated when the default display gets added.
Garfield Tan4a48a7f2018-10-02 14:23:55 -0700442 int mDefaultMinSizeOfResizeableTaskDp = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700443
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700444 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
445 private boolean mTaskLayersChanged = true;
446
Bryce Lee2a3cc462017-10-27 10:57:35 -0700447 private ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800448
Jorim Jaggiea039a82017-08-02 14:37:49 +0200449 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
450
Andrii Kulian1779e612016-10-12 21:58:25 -0700451 @Override
452 protected int getChildCount() {
453 return mActivityDisplays.size();
454 }
455
456 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700457 protected ActivityDisplay getChildAt(int index) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800458 return mActivityDisplays.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -0700459 }
460
461 @Override
462 protected ConfigurationContainer getParent() {
463 return null;
464 }
465
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700466 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700467 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700468 if (activityDisplay == null) {
469 throw new IllegalArgumentException("No display found with id: " + displayId);
470 }
471
472 return activityDisplay.getOverrideConfiguration();
473 }
474
475 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700476 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700477 if (activityDisplay == null) {
478 throw new IllegalArgumentException("No display found with id: " + displayId);
479 }
480
481 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
482 }
483
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700484 /** Check if placing task or activity on specified display is allowed. */
Riddle Hsu16567132018-08-16 21:37:47 +0800485 boolean canPlaceEntityOnDisplay(int displayId, int callingPid, int callingUid,
486 ActivityInfo activityInfo) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700487 if (displayId == DEFAULT_DISPLAY) {
488 // No restrictions for the default display.
489 return true;
490 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700491 if (!mService.mSupportsMultiDisplay) {
Andrii Kulian02689a72017-07-06 14:28:59 -0700492 // Can't launch on secondary displays if feature is not supported.
493 return false;
494 }
Andrii Kulian02689a72017-07-06 14:28:59 -0700495 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
496 // Can't place activities to a display that has restricted launch rules.
497 // In this case the request should be made by explicitly adding target display id and
498 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
499 return false;
500 }
501 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700502 }
503
504 /**
505 * Check if configuration of specified display matches current global config.
506 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
507 * the same config as on the default display.
508 * @param displayId Id of the display to check.
509 * @return {@code true} if configuration matches.
510 */
511 private boolean displayConfigMatchesGlobal(int displayId) {
512 if (displayId == DEFAULT_DISPLAY) {
513 return true;
514 }
515 if (displayId == INVALID_DISPLAY) {
516 return false;
517 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700518 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700519 if (targetDisplay == null) {
520 throw new IllegalArgumentException("No display found with id: " + displayId);
521 }
522 return getConfiguration().equals(targetDisplay.getConfiguration());
523 }
524
Wale Ogunwale39381972015-12-17 17:15:29 -0800525 static class FindTaskResult {
Louis Changc85b1a32018-08-14 16:40:53 +0800526 ActivityRecord mRecord;
527 boolean mIdealMatch;
528
529 void clear() {
530 mRecord = null;
531 mIdealMatch = false;
532 }
533
534 void setTo(FindTaskResult result) {
535 mRecord = result.mRecord;
536 mIdealMatch = result.mIdealMatch;
537 }
Wale Ogunwale39381972015-12-17 17:15:29 -0800538 }
Louis Changc85b1a32018-08-14 16:40:53 +0800539
Wale Ogunwale39381972015-12-17 17:15:29 -0800540 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
541
Craig Mautneree36c772014-07-16 14:56:05 -0700542 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100543 * Used to keep track whether app visibilities got changed since the last pause. Useful to
544 * determine whether to invoke the task stack change listener after pausing.
545 */
546 boolean mAppVisibilitiesChangedSinceLastPause;
547
548 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100549 * Set of tasks that are in resizing mode during an app transition to fill the "void".
550 */
551 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
552
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700553
554 /**
555 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
556 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
557 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
558 * like the docked stack going empty.
559 */
560 private boolean mAllowDockedStackResize = true;
561
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100562 /**
Tony Mak853304c2016-04-18 15:17:41 +0100563 * Is dock currently minimized.
564 */
565 boolean mIsDockMinimized;
566
Bryce Lee2a3cc462017-10-27 10:57:35 -0700567 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200568
chaviw59b98852017-06-13 12:05:44 -0700569 private PowerManager mPowerManager;
570 private int mDeferResumeCount;
571
Bryce Lee2a3cc462017-10-27 10:57:35 -0700572 private boolean mInitialized;
573
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800574 private RootWindowContainerController mWindowContainerController;
575
Tony Mak853304c2016-04-18 15:17:41 +0100576 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700577 * Description of a request to start a new activity, which has been held
578 * due to app switches being disabled.
579 */
580 static class PendingActivityLaunch {
581 final ActivityRecord r;
582 final ActivityRecord sourceRecord;
583 final int startFlags;
584 final ActivityStack stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700585 final WindowProcessController callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700586
587 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700588 int _startFlags, ActivityStack _stack, WindowProcessController app) {
Craig Mautneree36c772014-07-16 14:56:05 -0700589 r = _r;
590 sourceRecord = _sourceRecord;
591 startFlags = _startFlags;
592 stack = _stack;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700593 callerApp = app;
Robert Carr13997f52015-10-23 13:13:39 -0700594 }
595
596 void sendErrorResult(String message) {
597 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700598 if (callerApp.hasThread()) {
599 callerApp.getThread().scheduleCrash(message);
Robert Carr13997f52015-10-23 13:13:39 -0700600 }
601 } catch (RemoteException e) {
602 Slog.e(TAG, "Exception scheduling crash of failed "
603 + "activity launcher sourceRecord=" + sourceRecord, e);
604 }
Craig Mautneree36c772014-07-16 14:56:05 -0700605 }
606 }
607
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700608 public ActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700609 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000610 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700611 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700612 }
613
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700614 @VisibleForTesting
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700615 void setService(ActivityTaskManagerService service) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700616 mService = service;
617 }
618
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800619 @VisibleForTesting
620 void setWindowContainerController(RootWindowContainerController controller) {
621 mWindowContainerController = controller;
622 }
623
Bryce Lee2a3cc462017-10-27 10:57:35 -0700624 public void initialize() {
625 if (mInitialized) {
626 return;
627 }
628
629 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700630 mRunningTasks = createRunningTasks();
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700631 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext, mHandler.getLooper());
Wale Ogunwalef6733932018-06-27 05:14:34 -0700632 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700633
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700634 mLaunchParamsController = new LaunchParamsController(mService);
Bryce Leeec55eb02017-12-05 20:51:27 -0800635 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700636 }
637
Bryce Lee2a3cc462017-10-27 10:57:35 -0700638
639 public ActivityMetricsLogger getActivityMetricsLogger() {
640 return mActivityMetricsLogger;
641 }
642
643 public KeyguardController getKeyguardController() {
644 return mKeyguardController;
645 }
646
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800647 void setRecentTasks(RecentTasks recentTasks) {
648 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700649 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800650 }
651
Winson Chung3f0e59a2017-10-25 10:19:05 -0700652 @VisibleForTesting
653 RunningTasks createRunningTasks() {
654 return new RunningTasks();
655 }
656
Jeff Brown2c43c332014-06-12 22:38:59 -0700657 /**
658 * At the time when the constructor runs, the power manager has not yet been
659 * initialized. So we initialize our wakelocks afterwards.
660 */
661 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700662 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
663 mGoingToSleep = mPowerManager
664 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
665 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700666 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700667 }
668
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700669 void setWindowManager(WindowManagerService wm) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700670 mWindowManager = wm;
671 getKeyguardController().setWindowManager(wm);
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800672 setWindowContainerController(new RootWindowContainerController(this));
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800674 mDisplayManager = mService.mContext.getSystemService(DisplayManager.class);
Riddle Hsuf53da812018-08-15 22:00:27 +0800675 mDisplayManager.registerDisplayListener(this, mHandler);
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700676 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800678 final Display[] displays = mDisplayManager.getDisplays();
Andrii Kulianc543add2018-09-19 19:02:20 -0700679 for (int displayNdx = 0; displayNdx < displays.length; ++displayNdx) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700680 final Display display = displays[displayNdx];
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800681 final ActivityDisplay activityDisplay = new ActivityDisplay(this, display);
682 if (activityDisplay.mDisplayId == DEFAULT_DISPLAY) {
683 mDefaultDisplay = activityDisplay;
684 }
685 addChild(activityDisplay, ActivityDisplay.POSITION_TOP);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 }
Garfield Tan4a48a7f2018-10-02 14:23:55 -0700687 calculateDefaultMinimalSizeOfResizeableTasks();
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700688
Andrii Kulian52d255c2018-07-13 11:32:19 -0700689 final ActivityDisplay defaultDisplay = getDefaultDisplay();
Louis Changbd48dca2018-08-29 17:44:34 +0800690
Riddle Hsubbb63c22018-10-03 12:28:29 +0800691 defaultDisplay.getOrCreateStack(WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800692 positionChildAt(defaultDisplay, ActivityDisplay.POSITION_TOP);
693 }
694
695 /** Change the z-order of the given display. */
696 private void positionChildAt(ActivityDisplay display, int position) {
697 if (position >= mActivityDisplays.size()) {
698 position = mActivityDisplays.size() - 1;
699 } else if (position < 0) {
700 position = 0;
701 }
702
703 if (mActivityDisplays.isEmpty()) {
704 mActivityDisplays.add(display);
705 } else if (mActivityDisplays.get(position) != display) {
706 mActivityDisplays.remove(display);
707 mActivityDisplays.add(position, display);
708 }
709 }
710
711 @Override
712 public void onChildPositionChanged(DisplayWindowController childController, int position) {
713 // Assume AM lock is held from positionChildAt of controller in each hierarchy.
714 final ActivityDisplay display = getActivityDisplay(childController.getDisplayId());
715 if (display != null) {
716 positionChildAt(display, position);
717 }
Craig Mautner27084302013-03-25 08:05:25 -0700718 }
719
Andrii Kulian5f750bc2018-07-17 08:57:23 -0700720 ActivityStack getTopDisplayFocusedStack() {
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800721 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
722 final ActivityStack focusedStack = mActivityDisplays.get(i).getFocusedStack();
Andrii Kulian52d255c2018-07-13 11:32:19 -0700723 if (focusedStack != null) {
724 return focusedStack;
725 }
726 }
727 return null;
728 }
729
730 ActivityRecord getTopResumedActivity() {
Andrii Kulian5f750bc2018-07-17 08:57:23 -0700731 final ActivityStack focusedStack = getTopDisplayFocusedStack();
Andrii Kulian52d255c2018-07-13 11:32:19 -0700732 if (focusedStack == null) {
733 return null;
734 }
735 final ActivityRecord resumedActivity = focusedStack.getResumedActivity();
736 if (resumedActivity != null && resumedActivity.app != null) {
737 return resumedActivity;
738 }
739 // The top focused stack might not have a resumed activity yet - look on all displays in
740 // focus order.
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800741 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
742 final ActivityDisplay display = mActivityDisplays.get(i);
Andrii Kulian52d255c2018-07-13 11:32:19 -0700743 final ActivityRecord resumedActivityOnDisplay = display.getResumedActivity();
744 if (resumedActivityOnDisplay != null) {
745 return resumedActivityOnDisplay;
746 }
747 }
748 return null;
Craig Mautner20e72272013-04-01 13:45:53 -0700749 }
750
Bryce Lee7b851cc2018-04-10 14:53:13 -0700751 boolean isFocusable(ConfigurationContainer container, boolean alwaysFocusable) {
752 if (container.inSplitScreenPrimaryWindowingMode() && mIsDockMinimized) {
753 return false;
754 }
755
756 return container.getWindowConfiguration().canReceiveKeys() || alwaysFocusable;
757 }
758
Andrii Kulian5f750bc2018-07-17 08:57:23 -0700759 boolean isTopDisplayFocusedStack(ActivityStack stack) {
760 return stack != null && stack == getTopDisplayFocusedStack();
Craig Mautner20e72272013-04-01 13:45:53 -0700761 }
762
Matthew Ng330757d2017-02-28 14:19:17 -0800763 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700764 final ActivityStack recentsStack = getDefaultDisplay().getStack(
765 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800766 if (recentsStack != null) {
767 recentsStack.moveToFront(reason);
768 }
769 }
770
Louis Changbd48dca2018-08-29 17:44:34 +0800771 boolean resumeHomeActivity(ActivityRecord prev, String reason, int displayId) {
Wale Ogunwale53783742018-09-16 10:21:51 -0700772 if (!mService.isBooting() && !mService.isBooted()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700773 // Not ready yet!
774 return false;
775 }
776
Louis Changbd48dca2018-08-29 17:44:34 +0800777 if (displayId == INVALID_DISPLAY) {
778 displayId = DEFAULT_DISPLAY;
779 }
780
781 final ActivityRecord r = getActivityDisplay(displayId).getHomeActivity();
782 final String myReason = reason + " resumeHomeActivity";
Wale Ogunwaled046a012015-12-24 13:05:59 -0800783
Mark Lua56ea122015-10-08 13:31:01 +0800784 // Only resume home activity if isn't finishing.
785 if (r != null && !r.finishing) {
Louis Chang19443452018-10-09 12:10:21 +0800786 r.moveFocusableActivityToTop(myReason);
Louis Changbd48dca2018-08-29 17:44:34 +0800787 return resumeFocusedStacksTopActivitiesLocked(r.getStack(), prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700788 }
Louis Chang89f43fc2018-10-05 10:59:14 +0800789 return startHomeOnDisplay(mCurrentUser, myReason, displayId);
Louis Changbd48dca2018-08-29 17:44:34 +0800790 }
791
Louis Changdd3592a2018-11-05 11:04:14 +0800792 /**
793 * Check if home activity start should be allowed on a display.
794 * @param homeInfo {@code ActivityInfo} of the home activity that is going to be launched.
795 * @param displayId The id of the target display.
796 * @param allowInstrumenting Whether launching home should be allowed if being instrumented.
797 * @return {@code true} if allow to launch, {@code false} otherwise.
798 */
799 boolean canStartHomeOnDisplay(ActivityInfo homeInfo, int displayId,
800 boolean allowInstrumenting) {
Louis Chang89f43fc2018-10-05 10:59:14 +0800801 if (mService.mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
802 && mService.mTopAction == null) {
803 // We are running in factory test mode, but unable to find the factory test app, so
804 // just sit around displaying the error message and don't try to start anything.
805 return false;
806 }
807
808 final WindowProcessController app =
809 mService.getProcessController(homeInfo.processName, homeInfo.applicationInfo.uid);
Louis Changdd3592a2018-11-05 11:04:14 +0800810 if (!allowInstrumenting && app != null && app.isInstrumenting()) {
Louis Chang89f43fc2018-10-05 10:59:14 +0800811 // Don't do this if the home app is currently being instrumented.
812 return false;
813 }
814
Louis Chang39ba54b2018-10-18 11:28:57 +0800815 if (displayId == DEFAULT_DISPLAY || (displayId != INVALID_DISPLAY
816 && displayId == mService.mVr2dDisplayId)) {
817 // No restrictions to default display or vr 2d display.
Louis Changbd48dca2018-08-29 17:44:34 +0800818 return true;
819 }
820
821 final ActivityDisplay display = getActivityDisplay(displayId);
822 if (display == null || display.isRemoved() || !display.supportsSystemDecorations()) {
823 // Can't launch home on display that doesn't support system decorations.
824 return false;
825 }
826
Louis Chang89f43fc2018-10-05 10:59:14 +0800827 final boolean supportMultipleInstance = homeInfo.launchMode != LAUNCH_SINGLE_TASK
828 && homeInfo.launchMode != LAUNCH_SINGLE_INSTANCE;
Louis Changbd48dca2018-08-29 17:44:34 +0800829 if (!supportMultipleInstance) {
830 // Can't launch home on other displays if it requested to be single instance.
831 return false;
832 }
833
834 return true;
Craig Mautner69ada552013-04-18 13:51:51 -0700835 }
836
Craig Mautner8d341ef2013-03-26 09:03:27 -0700837 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700838 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
839 }
840
841 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700842 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700843 }
844
845 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700846 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700847 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800848 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700849 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700850 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700851 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700852 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700853 @Nullable ActivityOptions aOptions, boolean onTop) {
854 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700855 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
856 throw new IllegalArgumentException("Should not specify activity options for non-restore"
857 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800858 }
859
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800862 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700863 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
864 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700865 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700866 if (task == null) {
867 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700869 if (aOptions != null) {
870 // Resolve the stack the task should be placed in now based on options
871 // and reparent if needed.
872 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
873 if (launchStack != null && stack != launchStack) {
874 final int reparentMode = onTop
875 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
876 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
877 "anyTaskForIdLocked");
878 }
879 }
880 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700881 }
882 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800883
Winson Chung7900bee2017-03-10 08:59:25 -0800884 // If we are matching stack tasks only, return now
885 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800886 return null;
887 }
888
Winson Chung7900bee2017-03-10 08:59:25 -0800889 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
890 // the task from recents
891 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700892 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700893
894 if (task == null) {
895 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800896 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
897 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700898
899 return null;
900 }
901
902 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700903 return task;
904 }
905
Winson Chung7900bee2017-03-10 08:59:25 -0800906 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700907 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700908 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
909 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800910 return null;
911 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700912 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800913 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700914 }
915
Craig Mautner6170f732013-04-02 13:05:23 -0700916 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800917 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800919 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700920 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
921 final ActivityStack stack = display.getChildAt(stackNdx);
922 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 if (r != null) {
924 return r;
925 }
Craig Mautner6170f732013-04-02 13:05:23 -0700926 }
927 }
928 return null;
929 }
930
Tony Mak853304c2016-04-18 15:17:41 +0100931 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000932 * Detects whether we should show a lock screen in front of this task for a locked user.
933 * <p>
934 * We'll do this if either of the following holds:
935 * <ul>
936 * <li>The top activity explicitly belongs to {@param userId}.</li>
937 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
938 * </ul>
939 *
940 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100941 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000942 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
943 // To handle the case that work app is in the task but just is not the top one.
944 final ActivityRecord activityRecord = task.getTopActivity();
945 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
946
947 return (activityRecord != null && activityRecord.userId == userId)
948 || (resultTo != null && resultTo.userId == userId);
949 }
950
951 /**
952 * Find all visible task stacks containing {@param userId} and intercept them with an activity
953 * to block out the contents and possibly start a credential-confirming intent.
954 *
955 * @param userId user handle for the locked managed profile.
956 */
957 void lockAllProfileTasks(@UserIdInt int userId) {
958 mWindowManager.deferSurfaceLayout();
959 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800961 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700962 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
963 final ActivityStack stack = display.getChildAt(stackNdx);
964 final List<TaskRecord> tasks = stack.getAllTasks();
965 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
966 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000967
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700968 // Check the task for a top activity belonging to userId, or returning a
969 // result to an activity belonging to userId. Example case: a document
970 // picker for personal files, opened by a work app, should still get locked.
971 if (taskTopActivityIsUser(task, userId)) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700972 mService.getTaskChangeNotificationController().notifyTaskProfileLocked(
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700973 task.taskId, userId);
974 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000975 }
Tony Mak853304c2016-04-18 15:17:41 +0100976 }
977 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000978 } finally {
979 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100980 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000981 }
982
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800983 void setNextTaskIdForUserLocked(int taskId, int userId) {
984 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
985 if (taskId > currentTaskId) {
986 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700987 }
988 }
989
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700990 static int nextTaskIdForUser(int taskId, int userId) {
991 int nextTaskId = taskId + 1;
992 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
993 // Wrap around as there will be smaller task ids that are available now.
994 nextTaskId -= MAX_TASK_IDS_PER_USER;
995 }
996 return nextTaskId;
997 }
998
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800999 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001000 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
1001 // for a userId u, a taskId can only be in the range
1002 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
1003 // 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 -07001004 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -07001005 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001006 || anyTaskForIdLocked(
1007 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001008 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001009 if (candidateTaskId == currentTaskId) {
1010 // Something wrong!
1011 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
1012 throw new IllegalStateException("Cannot get an available task id."
1013 + " Reached limit of " + MAX_TASK_IDS_PER_USER
1014 + " running tasks per user.");
1015 }
1016 }
1017 mCurTaskIdForUser.put(userId, candidateTaskId);
1018 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001019 }
1020
Wale Ogunwale31913b52018-10-13 08:29:31 -07001021 boolean attachApplicationLocked(WindowProcessController app) throws RemoteException {
1022 final String processName = app.mName;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001025 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001026 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1027 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001028 if (!isTopDisplayFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001029 continue;
1030 }
Jorim Jaggiea039a82017-08-02 14:37:49 +02001031 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
1032 final ActivityRecord top = stack.topRunningActivityLocked();
1033 final int size = mTmpActivityList.size();
1034 for (int i = 0; i < size; i++) {
1035 final ActivityRecord activity = mTmpActivityList.get(i);
Wale Ogunwale31913b52018-10-13 08:29:31 -07001036 if (activity.app == null && app.mUid == activity.info.applicationInfo.uid
Jorim Jaggiea039a82017-08-02 14:37:49 +02001037 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001038 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001039 if (realStartActivityLocked(activity, app,
Jorim Jaggiea039a82017-08-02 14:37:49 +02001040 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001041 didSomething = true;
1042 }
Dianne Hackbornff072722014-09-24 10:56:28 -07001043 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001044 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +02001045 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001046 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -07001047 }
Craig Mautner20e72272013-04-01 13:45:53 -07001048 }
Craig Mautner20e72272013-04-01 13:45:53 -07001049 }
1050 }
1051 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001052 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001053 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001054 }
Craig Mautner20e72272013-04-01 13:45:53 -07001055 return didSomething;
1056 }
1057
1058 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001059 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Louis Changd17c6572018-11-07 16:27:20 +08001060 // TODO(b/117135575): Check resumed activities on all visible stacks.
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001061 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Louis Changd17c6572018-11-07 16:27:20 +08001062 if (display.isSleeping()) {
1063 // No resumed activities while display is sleeping.
1064 continue;
1065 }
1066
1067 // If the focused stack is not null or not empty, there should have some activities
1068 // resuming or resumed. Make sure these activities are idle.
1069 final ActivityStack stack = display.getFocusedStack();
1070 if (stack == null || stack.numActivities() == 0) {
1071 continue;
1072 }
1073 final ActivityRecord resumedActivity = stack.getResumedActivity();
1074 if (resumedActivity == null || !resumedActivity.idle) {
1075 if (DEBUG_STATES) {
1076 Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
1077 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001078 }
Louis Changd17c6572018-11-07 16:27:20 +08001079 return false;
Craig Mautner20e72272013-04-01 13:45:53 -07001080 }
1081 }
Wei Wang65c7a152016-06-02 18:51:22 -07001082 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -08001083 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001084 return true;
1085 }
1086
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001087 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001088 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001089 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001090 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001091 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1092 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001093 final ActivityRecord r = stack.getResumedActivity();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001094 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001095 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001096 return false;
1097 }
1098 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001099 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001100 }
1101 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001102 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001103 }
1104
lumark588a3e82018-07-20 18:53:54 +08001105 private void executeAppTransitionForAllDisplay() {
1106 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1107 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
1108 display.getWindowContainerController().executeAppTransition();
1109 }
1110 }
1111
Craig Mautner2acc3892013-09-23 10:28:14 -07001112 /**
1113 * Pause all activities in either all of the stacks or just the back stacks.
1114 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001115 * @param resuming The resuming activity.
1116 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1117 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001118 * @return true if any activity was paused as a result of this call.
1119 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001120 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001121 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001122 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001123 someActivityPaused |= mActivityDisplays.get(displayNdx)
Andrii Kulianab132ee2018-07-24 22:10:21 +08001124 .pauseBackStacks(userLeaving, resuming, dontWait);
Craig Mautnercf910b02013-04-23 11:23:27 -07001125 }
1126 return someActivityPaused;
1127 }
1128
Craig Mautnerde4ef022013-04-07 19:01:33 -07001129 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001130 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001132 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001133 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1134 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001135 final ActivityRecord r = stack.mPausingActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08001136 if (r != null && !r.isState(PAUSED, STOPPED, STOPPING)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001137 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001138 Slog.d(TAG_STATES,
Bryce Lee7ace3952018-02-16 14:34:32 -08001139 "allPausedActivitiesComplete: r=" + r + " state=" + r.getState());
Craig Mautner4a1cb222013-12-04 16:14:06 -08001140 pausing = false;
1141 } else {
1142 return false;
1143 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001144 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001145 }
1146 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001147 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001148 }
1149
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001150 void cancelInitializingActivities() {
1151 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001152 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001153 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1154 final ActivityStack stack = display.getChildAt(stackNdx);
1155 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001156 }
1157 }
1158 }
1159
Vishnu Nair132ee832018-09-28 15:00:05 -07001160 void waitActivityVisible(ComponentName name, WaitResult result, long startTimeMs) {
1161 final WaitInfo waitInfo = new WaitInfo(name, result, startTimeMs);
Bryce Lee4a194382017-04-04 14:32:48 -07001162 mWaitingForActivityVisible.add(waitInfo);
1163 }
1164
1165 void cleanupActivity(ActivityRecord r) {
1166 // Make sure this record is no longer in the pending finishes list.
1167 // This could happen, for example, if we are trimming activities
1168 // down to the max limit while they are still waiting to finish.
1169 mFinishingActivities.remove(r);
1170 mActivitiesWaitingForVisibleActivity.remove(r);
1171
1172 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001173 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001174 mWaitingForActivityVisible.remove(i);
1175 }
1176 }
1177 }
1178
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001179 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001180 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001181 }
1182
1183 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1184 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001185 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1186 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001187 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001188 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001189 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001190 result.timeout = false;
1191 result.who = w.getComponent();
Vishnu Nair132ee832018-09-28 15:00:05 -07001192 result.totalTime = SystemClock.uptimeMillis() - w.getStartTime();
Bryce Lee4a194382017-04-04 14:32:48 -07001193 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001194 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001195 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001196 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07001197 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001198 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 }
1200
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001201 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1202 if (mWaitingActivityLaunched.isEmpty()) {
1203 return;
1204 }
1205
1206 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1207 return;
1208 }
1209
Chong Zhang5022da32016-06-21 16:31:37 -07001210 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001211
Chong Zhang5022da32016-06-21 16:31:37 -07001212 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1213 WaitResult w = mWaitingActivityLaunched.remove(i);
1214 if (w.who == null) {
1215 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001216 w.result = result;
1217
1218 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1219 // will be no followup launch signals. Assign the result and launched component.
1220 if (result == START_DELIVERED_TO_TOP) {
1221 w.who = r.realActivity;
1222 }
Chong Zhang5022da32016-06-21 16:31:37 -07001223 }
1224 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001225
Chong Zhang5022da32016-06-21 16:31:37 -07001226 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07001227 mService.mGlobalLock.notifyAll();
Chong Zhang5022da32016-06-21 16:31:37 -07001228 }
1229 }
1230
Vishnu Nair132ee832018-09-28 15:00:05 -07001231 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001232 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001233 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001234 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001235 if (w.who == null) {
1236 changed = true;
1237 w.timeout = timeout;
1238 if (r != null) {
1239 w.who = new ComponentName(r.info.packageName, r.info.name);
1240 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001241 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001242 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001243 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001244 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001245 if (changed) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07001246 mService.mGlobalLock.notifyAll();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001247 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001248 }
1249
Craig Mautner29219d92013-04-16 20:19:12 -07001250 ActivityRecord topRunningActivityLocked() {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001251 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsue10cea52018-10-16 23:33:23 +08001252 final ActivityRecord topActivity = mActivityDisplays.get(i).topRunningActivity();
1253 if (topActivity != null) {
Bryce Leec961e0a2018-04-13 17:58:02 -07001254 return topActivity;
Craig Mautner29219d92013-04-16 20:19:12 -07001255 }
1256 }
1257 return null;
1258 }
1259
Winson Chung61c9e5a2017-10-11 10:39:32 -07001260 @VisibleForTesting
1261 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1262 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1263 int callingUid, boolean allowed) {
1264 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1265 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001266 }
1267
Todd Kennedy7440f172015-12-09 14:31:22 -08001268 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1269 ProfilerInfo profilerInfo) {
1270 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001271 if (aInfo != null) {
1272 // Store the found target back into the intent, because now that
1273 // we have it we never want to do this again. For example, if the
1274 // user navigates back to this point in the history, we should
1275 // always restart the exact same activity.
1276 intent.setComponent(new ComponentName(
1277 aInfo.applicationInfo.packageName, aInfo.name));
1278
1279 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001280 if (!aInfo.processName.equals("system")) {
Wale Ogunwale7056a062018-10-18 15:02:50 -07001281 if ((startFlags & (START_FLAG_DEBUG | START_FLAG_NATIVE_DEBUGGING
1282 | START_FLAG_TRACK_ALLOCATION)) != 0 || profilerInfo != null) {
1283 /**
1284 * Assume safe to call into AMS synchronously because the call that set these
1285 * flags should have originated from AMS which will already have its lock held.
1286 * @see ActivityManagerService#startActivityAndWait(IApplicationThread, String,
1287 * Intent, String, IBinder, String, int, int, ProfilerInfo, Bundle, int)
1288 * TODO(b/80414790): Investigate a better way of untangling this.
1289 */
1290 mService.mAmInternal.setDebugFlagsForStartingActivity(
1291 aInfo, startFlags, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001292 }
1293 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001294 final String intentLaunchToken = intent.getLaunchToken();
1295 if (aInfo.launchToken == null && intentLaunchToken != null) {
1296 aInfo.launchToken = intentLaunchToken;
1297 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001298 }
1299 return aInfo;
1300 }
1301
Patrick Baumann78380272018-04-04 10:41:01 -07001302 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
1303 int filterCallingUid) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001304 synchronized (mService.mGlobalLock) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001305 try {
1306 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001307 int modifiedFlags = flags
1308 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001309 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001310 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1311 modifiedFlags |= PackageManager.MATCH_INSTANT;
1312 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001313
Makoto Onuki1a342742018-04-26 14:56:59 -07001314 // In order to allow cross-profile lookup, we clear the calling identity here.
1315 // Note the binder identity won't affect the result, but filterCallingUid will.
1316
1317 // Cross-user/profile call check are done at the entry points
1318 // (e.g. AMS.startActivityAsUser).
1319 final long token = Binder.clearCallingIdentity();
1320 try {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001321 return mService.getPackageManagerInternalLocked().resolveIntent(
Makoto Onuki1a342742018-04-26 14:56:59 -07001322 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
1323 } finally {
1324 Binder.restoreCallingIdentity(token);
1325 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001326 } finally {
1327 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1328 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001329 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001330 }
1331
1332 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -07001333 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
1334 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -08001335 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1336 }
1337
Wale Ogunwale9c103022018-10-18 07:44:54 -07001338 private boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001339 boolean andResume, boolean checkConfig) throws RemoteException {
1340
1341 if (!allPausedActivitiesComplete()) {
1342 // While there are activities pausing we skipping starting any new activities until
1343 // pauses are complete. NOTE: that we also do this for activities that are starting in
1344 // the paused state because they will first be resumed then paused on the client side.
1345 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1346 "realStartActivityLocked: Skipping start of r=" + r
1347 + " some activities pausing...");
1348 return false;
1349 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001350
Bryce Leeaf691c02017-03-20 14:20:22 -07001351 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001352 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001353
1354 beginDeferResume();
1355
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001357 r.startFreezingScreenLocked(proc, 0);
chaviw59b98852017-06-13 12:05:44 -07001358
1359 // schedule launch ticks to collect information about slow apps.
1360 r.startLaunchTickingLocked();
1361
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001362 r.setProcess(proc);
chaviw59b98852017-06-13 12:05:44 -07001363
Bryce Lee459c0622018-03-19 11:04:01 -07001364 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001365 r.notifyUnknownVisibilityLaunched();
1366 }
1367
chaviw59b98852017-06-13 12:05:44 -07001368 // Have the window manager re-evaluate the orientation of the screen based on the new
1369 // activity order. Note that as a result of this, it can call back into the activity
1370 // manager with a new orientation. We don't care about that, because the activity is
1371 // not currently running so we are just restarting it anyway.
1372 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -07001373 // Deferring resume here because we're going to launch new activity shortly.
1374 // We don't want to perform a redundant launch of the same record while ensuring
1375 // configurations and trying to resume top activity of focused stack.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001376 ensureVisibilityAndConfig(r, r.getDisplayId(),
1377 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 }
chaviw59b98852017-06-13 12:05:44 -07001379
1380 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1381 true /* isTop */)) {
1382 // We only set the visibility to true if the activity is allowed to be visible
1383 // based on
1384 // keyguard state. This avoids setting this into motion in window manager that is
1385 // later cancelled due to later calls to ensure visible activities that set
1386 // visibility back to false.
1387 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001388 }
chaviw59b98852017-06-13 12:05:44 -07001389
chaviw59b98852017-06-13 12:05:44 -07001390 final int applicationInfoUid =
1391 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
Wale Ogunwale9c103022018-10-18 07:44:54 -07001392 if ((r.userId != proc.mUserId) || (r.appInfo.uid != applicationInfoUid)) {
chaviw59b98852017-06-13 12:05:44 -07001393 Slog.wtf(TAG,
1394 "User ID for activity changing for " + r
1395 + " appInfo.uid=" + r.appInfo.uid
1396 + " info.ai.uid=" + applicationInfoUid
Wale Ogunwale9c103022018-10-18 07:44:54 -07001397 + " old=" + r.app + " new=" + proc);
chaviw59b98852017-06-13 12:05:44 -07001398 }
1399
Wale Ogunwale9c103022018-10-18 07:44:54 -07001400 proc.clearWaitingToKill();
chaviw59b98852017-06-13 12:05:44 -07001401 r.launchCount++;
1402 r.lastLaunchTime = SystemClock.uptimeMillis();
1403
1404 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1405
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001406 proc.addActivityIfNeeded(r);
1407 proc.updateProcessInfo(false, true, true, true);
chaviw59b98852017-06-13 12:05:44 -07001408
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001409 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +01001410 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1411 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1412 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -07001413 && lockTaskController.getLockTaskModeState()
1414 == LOCK_TASK_MODE_LOCKED)) {
1415 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001416 }
1417
1418 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001419 if (!proc.hasThread()) {
chaviw59b98852017-06-13 12:05:44 -07001420 throw new RemoteException();
1421 }
1422 List<ResultInfo> results = null;
1423 List<ReferrerIntent> newIntents = null;
1424 if (andResume) {
1425 // We don't need to deliver new intents and/or set results if activity is going
1426 // to pause immediately after launch.
1427 results = r.results;
1428 newIntents = r.newIntents;
1429 }
1430 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1431 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1432 + " newIntents=" + newIntents + " andResume=" + andResume);
1433 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1434 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001435 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001436 // Home process is the root process of the task.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001437 mService.mHomeProcess = task.mActivities.get(0).app;
chaviw59b98852017-06-13 12:05:44 -07001438 }
Wale Ogunwale214f3482018-10-04 11:00:47 -07001439 mService.getPackageManagerInternalLocked().notifyPackageUse(
1440 r.intent.getComponent().getPackageName(), NOTIFY_PACKAGE_USE_ACTIVITY);
chaviw59b98852017-06-13 12:05:44 -07001441 r.sleeping = false;
1442 r.forceNewConfig = false;
Wale Ogunwale008163e2018-07-23 23:11:08 -07001443 mService.getAppWarningsLocked().onStartActivity(r);
Wale Ogunwale53783742018-09-16 10:21:51 -07001444 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001445 ProfilerInfo profilerInfo = proc.onStartActivity(mService.mTopProcessState);
chaviw59b98852017-06-13 12:05:44 -07001446
chaviw59b98852017-06-13 12:05:44 -07001447 // Because we could be starting an Activity in the system process this may not go
1448 // across a Binder interface which would create a new Configuration. Consequently
1449 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001450
chaviw59b98852017-06-13 12:05:44 -07001451 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
Wale Ogunwale9c103022018-10-18 07:44:54 -07001452 proc.getConfiguration(), r.getMergedOverrideConfiguration());
chaviw59b98852017-06-13 12:05:44 -07001453 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001454
chaviw59b98852017-06-13 12:05:44 -07001455 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001456
1457
1458 // Create activity launch transaction.
Wale Ogunwale9c103022018-10-18 07:44:54 -07001459 final ClientTransaction clientTransaction = ClientTransaction.obtain(
1460 proc.getThread(), r.appToken);
lumark588a3e82018-07-20 18:53:54 +08001461
1462 final DisplayWindowController dwc = r.getDisplay().getWindowContainerController();
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001463 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001464 System.identityHashCode(r), r.info,
1465 // TODO: Have this take the merged configuration instead of separate global
1466 // and override configs.
1467 mergedConfiguration.getGlobalConfiguration(),
1468 mergedConfiguration.getOverrideConfiguration(), r.compat,
Wale Ogunwale9c103022018-10-18 07:44:54 -07001469 r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001470 r.icicle, r.persistentState, results, newIntents,
lumark588a3e82018-07-20 18:53:54 +08001471 dwc.isNextTransitionForward(), profilerInfo));
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001472
1473 // Set desired final state.
1474 final ActivityLifecycleItem lifecycleItem;
1475 if (andResume) {
lumark588a3e82018-07-20 18:53:54 +08001476 lifecycleItem = ResumeActivityItem.obtain(dwc.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001477 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001478 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001479 }
1480 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1481
1482 // Schedule transaction.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001483 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Craig Mautner2420ead2013-04-01 17:13:20 -07001484
Wale Ogunwale9c103022018-10-18 07:44:54 -07001485 if ((proc.mInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
Wale Ogunwale214f3482018-10-04 11:00:47 -07001486 && mService.mHasHeavyWeightFeature) {
Wale Ogunwale53783742018-09-16 10:21:51 -07001487 // This may be a heavy-weight process! Note that the package manager will ensure
1488 // that only activity can run in the main process of the .apk, which is the only
1489 // thing that will be considered heavy-weight.
Wale Ogunwale9c103022018-10-18 07:44:54 -07001490 if (proc.mName.equals(proc.mInfo.packageName)) {
Wale Ogunwale53783742018-09-16 10:21:51 -07001491 if (mService.mHeavyWeightProcess != null
1492 && mService.mHeavyWeightProcess != proc) {
1493 Slog.w(TAG, "Starting new heavy weight process " + proc
chaviw59b98852017-06-13 12:05:44 -07001494 + " when already running "
Wale Ogunwale53783742018-09-16 10:21:51 -07001495 + mService.mHeavyWeightProcess);
chaviw59b98852017-06-13 12:05:44 -07001496 }
Wale Ogunwale53783742018-09-16 10:21:51 -07001497 mService.setHeavyWeightProcess(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001498 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001499 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001500
chaviw59b98852017-06-13 12:05:44 -07001501 } catch (RemoteException e) {
1502 if (r.launchFailed) {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001503 // This is the second time we failed -- finish activity and give up.
chaviw59b98852017-06-13 12:05:44 -07001504 Slog.e(TAG, "Second failure launching "
Wale Ogunwale9c103022018-10-18 07:44:54 -07001505 + r.intent.getComponent().flattenToShortString() + ", giving up", e);
1506 proc.appDied();
chaviw59b98852017-06-13 12:05:44 -07001507 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1508 "2nd-crash", false);
1509 return false;
1510 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001511
chaviw59b98852017-06-13 12:05:44 -07001512 // This is the first time we failed -- restart process and
1513 // retry.
1514 r.launchFailed = true;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001515 proc.removeActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001516 throw e;
1517 }
1518 } finally {
1519 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001520 }
1521
1522 r.launchFailed = false;
1523 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001524 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001525 }
1526
Andrii Kulianb372da62018-01-18 10:46:24 -08001527 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1528 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001529 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001530 // As part of the process of launching, ActivityThread also performs
1531 // a resume.
1532 stack.minimalResumeActivityLocked(r);
1533 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001534 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001535 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001536 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001537 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001538 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001539 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -07001540 }
1541
1542 // Launch the new version setup screen if needed. We do this -after-
1543 // launching the initial activity (that is, home), so that it can have
1544 // a chance to initialize itself while in the background, making the
1545 // switch back to it faster and look better.
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001546 if (isTopDisplayFocusedStack(stack)) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001547 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001548 }
1549
Dianne Hackborn465fa392014-09-14 14:21:18 -07001550 // Update any services we are bound to that might care about whether
1551 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001552 if (r.app != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001553 r.app.updateServiceConnectionActivities();
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001554 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001555
Craig Mautner2420ead2013-04-01 17:13:20 -07001556 return true;
1557 }
1558
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001559 /**
1560 * Ensure all activities visibility, update orientation and configuration.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001561 *
1562 * @param starting The currently starting activity or {@code null} if there is none.
1563 * @param displayId The id of the display where operation is executed.
1564 * @param markFrozenIfConfigChanged Whether to set {@link ActivityRecord#frozenBeforeDestroy} to
1565 * {@code true} if config changed.
1566 * @param deferResume Whether to defer resume while updating config.
Andrii Kulian2f0d73c2018-06-15 14:32:04 -07001567 * @return 'true' if starting activity was kept or wasn't provided, 'false' if it was relaunched
1568 * because of configuration update.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001569 */
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001570 boolean ensureVisibilityAndConfig(ActivityRecord starting, int displayId,
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001571 boolean markFrozenIfConfigChanged, boolean deferResume) {
1572 // First ensure visibility without updating the config just yet. We need this to know what
1573 // activities are affecting configuration now.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001574 // Passing null here for 'starting' param value, so that visibility of actual starting
1575 // activity will be properly updated.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001576 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001577 false /* preserveWindows */, false /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001578
Andrii Kulian2f0d73c2018-06-15 14:32:04 -07001579 if (displayId == INVALID_DISPLAY) {
1580 // The caller didn't provide a valid display id, skip updating config.
1581 return true;
1582 }
1583
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001584 // Force-update the orientation from the WindowManager, since we need the true configuration
1585 // to send to the client now.
1586 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1587 getDisplayOverrideConfiguration(displayId),
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001588 starting != null && starting.mayFreezeScreenLocked(starting.app)
1589 ? starting.appToken : null,
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001590 displayId, true /* forceUpdate */);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001591 if (starting != null && markFrozenIfConfigChanged && config != null) {
1592 starting.frozenBeforeDestroy = true;
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001593 }
1594
1595 // Update the configuration of the activities on the display.
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001596 return mService.updateDisplayOverrideConfigurationLocked(config, starting, deferResume,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001597 displayId);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001598 }
1599
Sudheer Shankafab200f2017-05-17 20:41:53 -07001600 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1601 int extrasSize = 0;
1602 if (intent != null) {
1603 final Bundle extras = intent.getExtras();
1604 if (extras != null) {
1605 extrasSize = extras.getSize();
1606 }
1607 }
1608 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1609 if (extrasSize + icicleSize > 200000) {
1610 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1611 + ", icicle size: " + icicleSize);
1612 }
1613 }
1614
Wale Ogunwale9c103022018-10-18 07:44:54 -07001615 void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001616 // Is this activity's application already running?
Wale Ogunwale9c103022018-10-18 07:44:54 -07001617 final WindowProcessController wpc =
1618 mService.getProcessController(r.processName, r.info.applicationInfo.uid);
Craig Mautnere79d42682013-04-01 19:01:53 -07001619
Wale Ogunwale9c103022018-10-18 07:44:54 -07001620 if (wpc != null && wpc.hasThread()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001621 try {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001622 if ((r.info.flags & ActivityInfo.FLAG_MULTIPROCESS) == 0
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001623 || !"android".equals(r.info.packageName)) {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001624 // Don't add this if it is a platform component that is marked to run in
1625 // multiple processes, because this is actually part of the framework so doesn't
1626 // make sense to track as a separate apk in the process.
1627 wpc.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001628 }
Wale Ogunwale9c103022018-10-18 07:44:54 -07001629 realStartActivityLocked(r, wpc, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001630 return;
1631 } catch (RemoteException e) {
1632 Slog.w(TAG, "Exception when starting activity "
1633 + r.intent.getComponent().flattenToShortString(), e);
1634 }
1635
1636 // If a dead object exception was thrown -- fall through to
1637 // restart the application.
1638 }
1639
Wale Ogunwale5c918702018-10-18 11:06:33 -07001640 // Post message to start process to avoid possible deadlock of calling into AMS with the
1641 // ATMS lock held.
1642 final Message msg = PooledLambda.obtainMessage(
1643 ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,
1644 r.info.applicationInfo, true, "activity", r.intent.getComponent());
1645 mService.mH.sendMessage(msg);
Craig Mautnere79d42682013-04-01 19:01:53 -07001646 }
1647
Bryce Leed3624e12017-11-30 08:51:45 -08001648 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1649 boolean sendHint = forceSend;
1650
1651 if (!sendHint) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07001652 // Send power hint if we don't know what we're launching yet
1653 sendHint = targetActivity == null || targetActivity.app == null;
1654 }
1655
1656 if (!sendHint) { // targetActivity != null
1657 // Send power hint when the activity's process is different than the current resumed
1658 // activity on all displays, or if there are no resumed activities in the system.
1659 boolean noResumedActivities = true;
1660 boolean allFocusedProcessesDiffer = true;
1661 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001662 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayNdx);
Andrii Kulian52d255c2018-07-13 11:32:19 -07001663 final ActivityRecord resumedActivity = activityDisplay.getResumedActivity();
1664 final WindowProcessController resumedActivityProcess =
1665 resumedActivity == null ? null : resumedActivity.app;
1666
1667 noResumedActivities &= resumedActivityProcess == null;
1668 if (resumedActivityProcess != null) {
1669 allFocusedProcessesDiffer &= !resumedActivityProcess.equals(targetActivity.app);
1670 }
1671 }
1672 sendHint = noResumedActivities || allFocusedProcessesDiffer;
Bryce Leed3624e12017-11-30 08:51:45 -08001673 }
1674
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001675 if (sendHint && mService.mPowerManagerInternal != null) {
1676 mService.mPowerManagerInternal.powerHint(PowerHint.LAUNCH, 1);
Bryce Leed3624e12017-11-30 08:51:45 -08001677 mPowerHintSent = true;
1678 }
1679 }
1680
1681 void sendPowerHintForLaunchEndIfNeeded() {
1682 // Trigger launch power hint if activity is launched
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001683 if (mPowerHintSent && mService.mPowerManagerInternal != null) {
1684 mService.mPowerManagerInternal.powerHint(PowerHint.LAUNCH, 0);
Bryce Leed3624e12017-11-30 08:51:45 -08001685 mPowerHintSent = false;
1686 }
1687 }
1688
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001689 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo, String resultWho,
1690 int requestCode, int callingPid, int callingUid, String callingPackage,
1691 boolean ignoreTargetSecurity, boolean launchingInTask,
1692 WindowProcessController callerApp, ActivityRecord resultRecord,
1693 ActivityStack resultStack) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001694 final boolean isCallerRecents = mService.getRecentTasks() != null
1695 && mService.getRecentTasks().isCallerRecents(callingUid);
Wale Ogunwalef6733932018-06-27 05:14:34 -07001696 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001697 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001698 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1699 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1700 // caller is the recents component and we are specifically starting an activity in an
1701 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001702 return true;
1703 }
1704 final int componentRestriction = getComponentRestrictionForCallingPackage(
1705 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1706 final int actionRestriction = getActionRestrictionForCallingPackage(
1707 intent.getAction(), callingPackage, callingPid, callingUid);
1708 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1709 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1710 if (resultRecord != null) {
1711 resultStack.sendActivityResultLocked(-1,
1712 resultRecord, resultWho, requestCode,
1713 Activity.RESULT_CANCELED, null);
1714 }
1715 final String msg;
1716 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1717 msg = "Permission Denial: starting " + intent.toString()
1718 + " from " + callerApp + " (pid=" + callingPid
1719 + ", uid=" + callingUid + ")" + " with revoked permission "
1720 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1721 } else if (!aInfo.exported) {
1722 msg = "Permission Denial: starting " + intent.toString()
1723 + " from " + callerApp + " (pid=" + callingPid
1724 + ", uid=" + callingUid + ")"
1725 + " not exported from uid " + aInfo.applicationInfo.uid;
1726 } else {
1727 msg = "Permission Denial: starting " + intent.toString()
1728 + " from " + callerApp + " (pid=" + callingPid
1729 + ", uid=" + callingUid + ")"
1730 + " requires " + aInfo.permission;
1731 }
1732 Slog.w(TAG, msg);
1733 throw new SecurityException(msg);
1734 }
1735
1736 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1737 final String message = "Appop Denial: starting " + intent.toString()
1738 + " from " + callerApp + " (pid=" + callingPid
1739 + ", uid=" + callingUid + ")"
1740 + " requires " + AppOpsManager.permissionToOp(
1741 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1742 Slog.w(TAG, message);
1743 return false;
1744 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1745 final String message = "Appop Denial: starting " + intent.toString()
1746 + " from " + callerApp + " (pid=" + callingPid
1747 + ", uid=" + callingUid + ")"
1748 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1749 Slog.w(TAG, message);
1750 return false;
1751 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001752
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001753 return true;
1754 }
1755
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001756 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001757 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1758 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001759 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1760 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1761
Andrii Kulian02689a72017-07-06 14:28:59 -07001762 if (callingPid == -1 && callingUid == -1) {
1763 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1764 return true;
1765 }
1766
Andrii Kulian62e6f252017-05-30 22:46:53 -07001767 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Louis Chang7d0037c2018-08-13 12:42:06 +08001768 if (activityDisplay == null || activityDisplay.isRemoved()) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001769 Slog.w(TAG, "Launch on display check: display not found");
1770 return false;
1771 }
1772
Andrii Kulian02689a72017-07-06 14:28:59 -07001773 // Check if the caller has enough privileges to embed activities and launch to private
1774 // displays.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001775 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001776 callingUid);
1777 if (startAnyPerm == PERMISSION_GRANTED) {
1778 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1779 + " allow launch any on display");
1780 return true;
1781 }
1782
Andrii Kulian8f070292017-09-12 22:56:49 -07001783 // Check if caller is already present on display
1784 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1785
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001786 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1787 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1788 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001789 // Limit launching on virtual displays, because their contents can be read from Surface
1790 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001791 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1792 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1793 + " disallow launch on virtual display for not-embedded activity.");
1794 return false;
1795 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001796 // Check if the caller is allowed to embed activities from other apps.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001797 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001798 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001799 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1800 + " disallow activity embedding without permission.");
1801 return false;
1802 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001803 }
1804
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001805 if (!activityDisplay.isPrivate()) {
1806 // Anyone can launch on a public display.
1807 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1808 + " allow launch on public display");
1809 return true;
1810 }
1811
1812 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001813 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001814 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1815 + " allow launch for owner of the display");
1816 return true;
1817 }
1818
Andrii Kulian8f070292017-09-12 22:56:49 -07001819 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001820 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1821 + " allow launch for caller present on the display");
1822 return true;
1823 }
1824
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001825 Slog.w(TAG, "Launch on display check: denied");
1826 return false;
1827 }
1828
1829 /** Update lists of UIDs that are present on displays and have access to them. */
1830 void updateUIDsPresentOnDisplay() {
1831 mDisplayAccessUIDs.clear();
1832 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001833 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001834 // Only bother calculating the whitelist for private displays
1835 if (activityDisplay.isPrivate()) {
1836 mDisplayAccessUIDs.append(
1837 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1838 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001839 }
1840 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1841 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1842 }
1843
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001844 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001845 final long identity = Binder.clearCallingIdentity();
1846 try {
1847 return UserManager.get(mService.mContext).getUserInfo(userId);
1848 } finally {
1849 Binder.restoreCallingIdentity(identity);
1850 }
1851 }
1852
Svet Ganov99b60432015-06-27 13:15:22 -07001853 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001854 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Wale Ogunwale214f3482018-10-04 11:00:47 -07001855 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001856 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001857 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001858 return ACTIVITY_RESTRICTION_PERMISSION;
1859 }
1860
Christopher Tateff7add02015-08-17 10:23:22 -07001861 if (activityInfo.permission == null) {
1862 return ACTIVITY_RESTRICTION_NONE;
1863 }
1864
Svet Ganov99b60432015-06-27 13:15:22 -07001865 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1866 if (opCode == AppOpsManager.OP_NONE) {
1867 return ACTIVITY_RESTRICTION_NONE;
1868 }
1869
Wale Ogunwalef6733932018-06-27 05:14:34 -07001870 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage)
1871 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001872 if (!ignoreTargetSecurity) {
1873 return ACTIVITY_RESTRICTION_APPOP;
1874 }
Svet Ganov99b60432015-06-27 13:15:22 -07001875 }
1876
1877 return ACTIVITY_RESTRICTION_NONE;
1878 }
1879
Svetoslav7008b512015-06-24 18:47:07 -07001880 private int getActionRestrictionForCallingPackage(String action,
1881 String callingPackage, int callingPid, int callingUid) {
1882 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001883 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001884 }
1885
1886 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1887 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001888 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001889 }
1890
1891 final PackageInfo packageInfo;
1892 try {
1893 packageInfo = mService.mContext.getPackageManager()
1894 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1895 } catch (PackageManager.NameNotFoundException e) {
1896 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001897 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001898 }
1899
1900 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001901 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001902 }
1903
Wale Ogunwalef6733932018-06-27 05:14:34 -07001904 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001905 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001906 }
1907
1908 final int opCode = AppOpsManager.permissionToOpCode(permission);
1909 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001910 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001911 }
1912
Wale Ogunwalef6733932018-06-27 05:14:34 -07001913 if (mService.getAppOpsService().noteOperation(opCode, callingUid, callingPackage)
1914 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001915 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001916 }
1917
Svet Ganov99b60432015-06-27 13:15:22 -07001918 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001919 }
1920
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001921 void setLaunchSource(int uid) {
1922 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1923 }
1924
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001925 void acquireLaunchWakelock() {
1926 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1927 throw new IllegalStateException("Calling must be system uid");
1928 }
1929 mLaunchingActivity.acquire();
1930 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1931 // To be safe, don't allow the wake lock to be held for too long.
1932 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1933 }
1934 }
1935
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001936 /**
Louis Chang89f43fc2018-10-05 10:59:14 +08001937 * Called when all resumed tasks/stacks are idle.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001938 * @return the state of mService.mAm.mBooting before this was called.
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001939 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001940 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001941 private boolean checkFinishBootingLocked() {
Wale Ogunwale53783742018-09-16 10:21:51 -07001942 final boolean booting = mService.isBooting();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001943 boolean enableScreen = false;
Wale Ogunwale53783742018-09-16 10:21:51 -07001944 mService.setBooting(false);
1945 if (!mService.isBooted()) {
1946 mService.setBooted(true);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001947 enableScreen = true;
1948 }
1949 if (booting || enableScreen) {
Wale Ogunwale53783742018-09-16 10:21:51 -07001950 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001951 }
1952 return booting;
1953 }
1954
Craig Mautnerf3333272013-04-22 10:55:53 -07001955 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001956 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001957 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001958 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001959 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001960
Craig Mautnerf3333272013-04-22 10:55:53 -07001961 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001962 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001963 int NS = 0;
1964 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001965 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001966 boolean activityRemoved = false;
1967
Wale Ogunwale7d701172015-03-11 15:36:30 -07001968 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001969 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001970 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1971 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001972 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1973 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001974 if (fromTimeout) {
Vishnu Nair132ee832018-09-28 15:00:05 -07001975 reportActivityLaunchedLocked(fromTimeout, r, INVALID_DELAY);
Craig Mautnerf3333272013-04-22 10:55:53 -07001976 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001977
1978 // This is a hack to semi-deal with a race condition
1979 // in the client where it can be constructed with a
1980 // newer configuration from when we asked it to launch.
1981 // We'll update with whatever configuration it now says
1982 // it used to launch.
1983 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001984 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001985 }
1986
1987 // We are now idle. If someone is waiting for a thumbnail from
1988 // us, we can now deliver.
1989 r.idle = true;
1990
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001991 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Louis Chang89f43fc2018-10-05 10:59:14 +08001992
Louis Chang1eff2482018-11-01 15:46:31 +08001993 // Check if able to finish booting when device is booting and all resumed activities
1994 // are idle.
1995 if ((mService.isBooting() && allResumedActivitiesIdle()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001996 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001997 }
Garfield Tan2746ab52018-07-25 12:33:01 -07001998
1999 // When activity is idle, we consider the relaunch must be successful, so let's clear
2000 // the flag.
2001 r.mRelaunchReason = RELAUNCH_REASON_NONE;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002002 }
2003
2004 if (allResumedActivitiesIdle()) {
2005 if (r != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07002006 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002007 }
2008
2009 if (mLaunchingActivity.isHeld()) {
2010 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2011 if (VALIDATE_WAKE_LOCK_CALLER &&
2012 Binder.getCallingUid() != Process.myUid()) {
2013 throw new IllegalStateException("Calling must be system uid");
2014 }
2015 mLaunchingActivity.release();
2016 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002017 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002018 }
2019
2020 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08002021 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
2022 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002023 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002024 if ((NF = mFinishingActivities.size()) > 0) {
2025 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002026 mFinishingActivities.clear();
2027 }
2028
Craig Mautnerf3333272013-04-22 10:55:53 -07002029 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002030 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002031 mStartingUsers.clear();
2032 }
2033
Craig Mautnerf3333272013-04-22 10:55:53 -07002034 // Stop any activities that are scheduled to do so but have been
2035 // waiting for the next one to start.
2036 for (int i = 0; i < NS; i++) {
2037 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002038 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002039 if (stack != null) {
2040 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08002041 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
2042 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002043 } else {
2044 stack.stopActivityLocked(r);
2045 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002046 }
2047 }
2048
2049 // Finish any activities that are scheduled to do so but have been
2050 // waiting for the next one to start.
2051 for (int i = 0; i < NF; i++) {
2052 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002053 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002054 if (stack != null) {
2055 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2056 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002057 }
2058
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002059 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002060 // Complete user switch
2061 if (startingUsers != null) {
2062 for (int i = 0; i < startingUsers.size(); i++) {
Wale Ogunwale86b74462018-07-02 08:42:43 -07002063 mService.mAmInternal.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002064 }
2065 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002066 }
2067
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002068 mService.mH.post(() -> mService.mAmInternal.trimApplications());
Craig Mautnerf3333272013-04-22 10:55:53 -07002069 //dump();
2070 //mWindowManager.dump();
2071
Craig Mautnerf3333272013-04-22 10:55:53 -07002072 if (activityRemoved) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08002073 resumeFocusedStacksTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002074 }
2075
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002076 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002077 }
2078
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002079 boolean handleAppDiedLocked(WindowProcessController app) {
Craig Mautner19091252013-10-05 00:03:53 -07002080 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002081 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002082 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002083 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2084 final ActivityStack stack = display.getChildAt(stackNdx);
2085 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002086 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002087 }
Craig Mautner19091252013-10-05 00:03:53 -07002088 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002089 }
2090
2091 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002092 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002093 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002094 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2095 final ActivityStack stack = display.getChildAt(stackNdx);
2096 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002097 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002098 }
2099 }
2100
Craig Mautner93529a42013-10-04 15:03:13 -07002101 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002102 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002103 }
2104
Craig Mautner8d341ef2013-03-26 09:03:27 -07002105 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002106 * Update the last used stack id for non-current user (current user's last
2107 * used stack is the focused stack)
2108 */
2109 void updateUserStackLocked(int userId, ActivityStack stack) {
2110 if (userId != mCurrentUser) {
Louis Changbd48dca2018-08-29 17:44:34 +08002111 mUserStackInFront.put(userId, stack != null ? stack.getStackId()
2112 : getDefaultDisplay().getHomeStack().mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002113 }
2114 }
2115
2116 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002117 * @return true if some activity was finished (or would have finished if doit were true).
2118 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002119 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2120 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002121 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002122 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002123 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002124 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2125 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002126 if (stack.finishDisabledPackageActivitiesLocked(
2127 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002128 didSomething = true;
2129 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
2131 }
2132 return didSomething;
2133 }
2134
Dianne Hackborna413dc02013-07-12 12:02:55 -07002135 void updatePreviousProcessLocked(ActivityRecord r) {
2136 // Now that this process has stopped, we may want to consider
2137 // it to be the previous app to try to keep around in case
2138 // the user wants to return to it.
2139
2140 // First, found out what is currently the foreground app, so that
2141 // we don't blow away the previous app if this activity is being
2142 // hosted by the process that is actually still the foreground.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002143 WindowProcessController fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002144 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002145 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002146 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2147 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002148 if (isTopDisplayFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002149 final ActivityRecord resumedActivity = stack.getResumedActivity();
2150 if (resumedActivity != null) {
2151 fgApp = resumedActivity.app;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002152 } else if (stack.mPausingActivity != null) {
2153 fgApp = stack.mPausingActivity.app;
2154 }
2155 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002156 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002157 }
2158 }
2159
2160 // Now set this one as the previous process, only if that really
2161 // makes sense to.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002162 if (r.hasProcess() && fgApp != null && r.app != fgApp
2163 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
2164 && r.app != mService.mHomeProcess) {
2165 mService.mPreviousProcess = r.app;
2166 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002167 }
2168 }
2169
Andrii Kulianab132ee2018-07-24 22:10:21 +08002170 boolean resumeFocusedStacksTopActivitiesLocked() {
2171 return resumeFocusedStacksTopActivitiesLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002172 }
2173
Andrii Kulianab132ee2018-07-24 22:10:21 +08002174 boolean resumeFocusedStacksTopActivitiesLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002175 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002176
2177 if (!readyToResume()) {
2178 return false;
2179 }
2180
Riddle Hsu75016992018-09-20 20:37:14 +08002181 if (targetStack != null && (targetStack.isTopStackOnDisplay()
2182 || getTopDisplayFocusedStack() == targetStack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002183 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002184 }
chaviw59b98852017-06-13 12:05:44 -07002185
Andrii Kulianab132ee2018-07-24 22:10:21 +08002186 // Resume all top activities in focused stacks on all displays.
2187 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002188 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Andrii Kulianab132ee2018-07-24 22:10:21 +08002189 final ActivityStack focusedStack = display.getFocusedStack();
2190 if (focusedStack == null) {
2191 continue;
2192 }
2193 final ActivityRecord r = focusedStack.topRunningActivityLocked();
2194 if (r == null || !r.isState(RESUMED)) {
2195 focusedStack.resumeTopActivityUncheckedLocked(null, null);
2196 } else if (r.isState(RESUMED)) {
2197 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2198 focusedStack.executeAppTransition(targetOptions);
2199 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002200 }
chaviw59b98852017-06-13 12:05:44 -07002201
Wale Ogunwaled046a012015-12-24 13:05:59 -08002202 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002203 }
2204
Todd Kennedy39bfee52016-02-24 10:28:21 -08002205 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2206 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002207 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002208 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2209 final ActivityStack stack = display.getChildAt(stackNdx);
2210 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002211 }
2212 }
2213 }
2214
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002215 /**
2216 * Finish the topmost activities in all stacks that belong to the crashed app.
2217 * @param app The app that crashed.
2218 * @param reason Reason to perform this action.
Wale Ogunwale64258362018-10-16 15:13:37 -07002219 * @return The task id that was finished in this stack, or INVALID_TASK_ID if none was finished.
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002220 */
Wale Ogunwale64258362018-10-16 15:13:37 -07002221 int finishTopCrashedActivitiesLocked(WindowProcessController app, String reason) {
Adrian Roos20d7df32016-01-12 18:59:43 +01002222 TaskRecord finishedTask = null;
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002223 ActivityStack focusedStack = getTopDisplayFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002224 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002225 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002226 // It is possible that request to finish activity might also remove its task and stack,
2227 // so we need to be careful with indexes in the loop and check child count every time.
2228 for (int stackNdx = 0; stackNdx < display.getChildCount(); ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002229 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002230 final TaskRecord t = stack.finishTopCrashedActivityLocked(app, reason);
Adrian Roos20d7df32016-01-12 18:59:43 +01002231 if (stack == focusedStack || finishedTask == null) {
2232 finishedTask = t;
2233 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002234 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002235 }
Wale Ogunwale64258362018-10-16 15:13:37 -07002236 return finishedTask != null ? finishedTask.taskId : INVALID_TASK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002237 }
2238
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002239 void finishVoiceTask(IVoiceInteractionSession session) {
2240 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002241 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002242 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002243 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002244 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002245 stack.finishVoiceTask(session);
2246 }
2247 }
2248 }
2249
Evan Rosky9ba524e2018-01-03 16:27:56 -08002250 /**
2251 * This doesn't just find a task, it also moves the task to front.
2252 */
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002253 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2254 boolean forceNonResizeable) {
Louis Changbd48dca2018-08-29 17:44:34 +08002255 ActivityStack currentStack = task.getStack();
Wale Ogunwale66e16852017-10-19 13:35:52 -07002256 if (currentStack == null) {
2257 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2258 + task + " to front. Stack is null");
2259 return;
2260 }
2261
Craig Mautneraea74a52014-03-08 14:23:10 -08002262 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2263 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002264 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002265
Louis Changc8b64112018-10-11 09:22:14 +08002266 reason = reason + " findTaskToMoveToFront";
2267 boolean reparented = false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002268 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002269 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002270 task.updateOverrideConfiguration(bounds);
2271
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002272 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002273
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002274 if (stack != currentStack) {
Louis Changc8b64112018-10-11 09:22:14 +08002275 moveHomeStackToFrontIfNeeded(flags, stack.getDisplay(), reason);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002276 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Louis Changc8b64112018-10-11 09:22:14 +08002277 reason);
Louis Changbd48dca2018-08-29 17:44:34 +08002278 currentStack = stack;
Louis Changc8b64112018-10-11 09:22:14 +08002279 reparented = true;
2280 // task.reparent() should already placed the task on top,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002281 // still need moveTaskToFrontLocked() below for any transition settings.
2282 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002283 if (stack.resizeStackWithLaunchBounds()) {
2284 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002285 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2286 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002287 } else {
2288 // WM resizeTask must be done after the task is moved to the correct stack,
2289 // because Task's setBounds() also updates dim layer's bounds, but that has
2290 // dependency on the stack.
2291 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002292 }
2293 }
2294
Louis Changc8b64112018-10-11 09:22:14 +08002295 if (!reparented) {
2296 moveHomeStackToFrontIfNeeded(flags, currentStack.getDisplay(), reason);
2297 }
2298
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002299 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07002300 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002301 r == null ? null : r.appTimeTracker, reason);
2302
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002303 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002304 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002305
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002306 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002307 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002308 }
2309
Louis Changc8b64112018-10-11 09:22:14 +08002310 private void moveHomeStackToFrontIfNeeded(int flags, ActivityDisplay display, String reason) {
2311 final ActivityStack focusedStack = display.getFocusedStack();
2312
2313 if ((display.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2314 && (flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0)
2315 || (focusedStack != null && focusedStack.isActivityTypeRecents())) {
2316 // We move home stack to front when we are on a fullscreen display and caller has
2317 // requested the home activity to move with it. Or the previous stack is recents.
2318 display.moveHomeStackToFront(reason);
2319 }
2320 }
2321
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002322 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002323 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002324 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002325 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002326 return false;
2327 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002328 return (mService.mSupportsPictureInPicture
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002329 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002330 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002331 }
2332
Bryce Leeec55eb02017-12-05 20:51:27 -08002333 LaunchParamsController getLaunchParamsController() {
2334 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002335 }
2336
Winson Chung55893332017-02-17 17:13:10 -08002337 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002338 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002339 final T stack = mActivityDisplays.get(i).getStack(stackId);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002340 if (stack != null) {
2341 return stack;
2342 }
2343 }
2344 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002345 }
2346
Wale Ogunwale68278562017-09-23 17:13:55 -07002347 /** @see ActivityDisplay#getStack(int, int) */
2348 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2349 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002350 final T stack = mActivityDisplays.get(i).getStack(windowingMode, activityType);
Wale Ogunwale68278562017-09-23 17:13:55 -07002351 if (stack != null) {
2352 return stack;
2353 }
2354 }
2355 return null;
2356 }
2357
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002358 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002359 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002360 // Preference is given to the activity type for the activity then the task since the type
2361 // once set shouldn't change.
2362 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2363 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2364 activityType = task.getActivityType();
2365 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002366 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2367 return activityType;
2368 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002369 if (options != null) {
2370 activityType = options.getLaunchActivityType();
2371 }
2372 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002373 }
2374
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002375 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2376 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2377 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002378 }
2379
2380 /**
2381 * Returns the right stack to use for launching factoring in all the input parameters.
2382 *
2383 * @param r The activity we are trying to launch. Can be null.
2384 * @param options The activity options used to the launch. Can be null.
2385 * @param candidateTask The possible task the activity might be launched in. Can be null.
2386 *
2387 * @return The stack to use for the launch or INVALID_STACK_ID.
2388 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002389 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2390 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2391 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002392 int taskId = INVALID_TASK_ID;
2393 int displayId = INVALID_DISPLAY;
2394 //Rect bounds = null;
2395
2396 // We give preference to the launch preference in activity options.
2397 if (options != null) {
2398 taskId = options.getLaunchTaskId();
2399 displayId = options.getLaunchDisplayId();
2400 // TODO: Need to work this into the equation...
2401 //bounds = options.getLaunchBounds();
2402 }
2403
2404 // First preference for stack goes to the task Id set in the activity options. Use the stack
2405 // associated with that if possible.
2406 if (taskId != INVALID_TASK_ID) {
2407 // Temporarily set the task id to invalid in case in re-entry.
2408 options.setLaunchTaskId(INVALID_TASK_ID);
2409 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002410 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002411 options.setLaunchTaskId(taskId);
2412 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002413 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002414 }
2415 }
2416
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002417 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002418 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002419
2420 // Next preference for stack goes to the display Id set in the activity options or the
2421 // candidate display.
2422 if (displayId == INVALID_DISPLAY) {
2423 displayId = candidateDisplayId;
2424 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002425 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002426 if (r != null) {
Riddle Hsufd4a0502018-10-16 01:05:16 +08002427 stack = (T) getValidLaunchStackOnDisplay(displayId, r, candidateTask, options);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002428 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002429 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002430 }
2431 }
2432 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2433 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002434 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002435 if (stack != null) {
2436 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002437 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002438 }
2439 }
2440
2441 // Give preference to the stack and display of the input task and activity if they match the
2442 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002443 stack = null;
2444 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002445 if (candidateTask != null) {
2446 stack = candidateTask.getStack();
2447 }
2448 if (stack == null && r != null) {
2449 stack = r.getStack();
2450 }
2451 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002452 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002453 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002454 final int windowingMode =
2455 display.resolveWindowingMode(r, options, candidateTask, activityType);
2456 if (stack.isCompatible(windowingMode, activityType)) {
2457 return stack;
2458 }
Andrii Kulianec7d4c42018-04-26 18:31:58 -07002459 if (windowingMode == WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY
2460 && display.getSplitScreenPrimaryStack() == stack
2461 && candidateTask == stack.topTask()) {
2462 // This is a special case when we try to launch an activity that is currently on
2463 // top of split-screen primary stack, but is targeting split-screen secondary.
2464 // In this case we don't want to move it to another stack.
2465 // TODO(b/78788972): Remove after differentiating between preferred and required
2466 // launch options.
2467 return stack;
2468 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002469 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002470 }
2471
Louis Chang6322c422018-07-18 18:37:28 +08002472 if (display == null || !canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002473 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002474 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002475
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002476 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002477 }
2478
Andrii Kulianb850ea52017-12-12 23:49:10 -08002479 /** @return true if activity record is null or can be launched on provided display. */
2480 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2481 if (r == null) {
2482 return true;
2483 }
2484 return r.canBeLaunchedOnDisplay(displayId);
2485 }
2486
Andrii Kulian16802aa2016-11-02 12:21:33 -07002487 /**
2488 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2489 * If there is no such stack, new dynamic stack can be created.
2490 * @param displayId Target display.
2491 * @param r Activity that should be launched there.
Riddle Hsufd4a0502018-10-16 01:05:16 +08002492 * @param candidateTask The possible task the activity might be put in.
Andrii Kulian16802aa2016-11-02 12:21:33 -07002493 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2494 */
Louis Chang6322c422018-07-18 18:37:28 +08002495 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r,
Riddle Hsufd4a0502018-10-16 01:05:16 +08002496 @Nullable TaskRecord candidateTask, @Nullable ActivityOptions options) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002497 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002498 if (activityDisplay == null) {
2499 throw new IllegalArgumentException(
2500 "Display with displayId=" + displayId + " not found.");
2501 }
2502
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002503 if (!r.canBeLaunchedOnDisplay(displayId)) {
2504 return null;
2505 }
2506
Riddle Hsufd4a0502018-10-16 01:05:16 +08002507 // If {@code r} is already in target display and its task is the same as the candidate task,
2508 // the intention should be getting a launch stack for the reusable activity, so we can use
2509 // the existing stack.
2510 if (r.getDisplayId() == displayId && r.getTask() == candidateTask) {
2511 return candidateTask.getStack();
2512 }
2513
Andrii Kulian16802aa2016-11-02 12:21:33 -07002514 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002515 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2516 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002517 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002518 return stack;
2519 }
2520 }
2521
2522 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002523 if (displayId != DEFAULT_DISPLAY) {
2524 return activityDisplay.createStack(
Louis Chang6322c422018-07-18 18:37:28 +08002525 options != null ? options.getLaunchWindowingMode() : r.getWindowingMode(),
2526 options != null ? options.getLaunchActivityType() : r.getActivityType(),
2527 true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002528 }
2529
2530 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2531 return null;
2532 }
2533
Riddle Hsufd4a0502018-10-16 01:05:16 +08002534 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r,
2535 @Nullable ActivityOptions options) {
2536 return getValidLaunchStackOnDisplay(displayId, r, null /* candidateTask */, options);
2537 }
2538
Wale Ogunwale68278562017-09-23 17:13:55 -07002539 // TODO: Can probably be consolidated into getLaunchStack()...
2540 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2541 switch (stack.getActivityType()) {
2542 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2543 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2544 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002545 }
Evan Rosky9ba524e2018-01-03 16:27:56 -08002546 // There is a 1-to-1 relationship between stack and task when not in
2547 // primary split-windowing mode.
2548 if (stack.getWindowingMode() != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
2549 return false;
2550 } else {
2551 return r.supportsSplitScreenWindowingMode();
Wale Ogunwale68278562017-09-23 17:13:55 -07002552 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002553 }
2554
Andrii Kulian7fc22812016-12-28 13:04:11 -08002555 /**
Andrii Kulianab132ee2018-07-24 22:10:21 +08002556 * Get next focusable stack in the system. This will search through the stack on the same
2557 * display as the current focused stack, looking for a focusable and visible stack, different
2558 * from the target stack. If no valid candidates will be found, it will then go through all
2559 * displays and stacks in last-focused order.
Andrii Kulian7fc22812016-12-28 13:04:11 -08002560 *
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002561 * @param currentFocus The stack that previously had focus.
2562 * @param ignoreCurrent If we should ignore {@param currentFocus} when searching for next
2563 * candidate.
Andrii Kulianab132ee2018-07-24 22:10:21 +08002564 * @return Next focusable {@link ActivityStack}, {@code null} if not found.
Andrii Kulian7fc22812016-12-28 13:04:11 -08002565 */
Andrii Kulianab132ee2018-07-24 22:10:21 +08002566 ActivityStack getNextFocusableStackLocked(@NonNull ActivityStack currentFocus,
2567 boolean ignoreCurrent) {
2568 // First look for next focusable stack on the same display
2569 final ActivityDisplay preferredDisplay = currentFocus.getDisplay();
2570 final ActivityStack preferredFocusableStack = preferredDisplay.getNextFocusableStack(
2571 currentFocus, ignoreCurrent);
2572 if (preferredFocusableStack != null) {
2573 return preferredFocusableStack;
2574 }
Louis Changbd48dca2018-08-29 17:44:34 +08002575 if (preferredDisplay.supportsSystemDecorations()) {
2576 // Stop looking for focusable stack on other displays because the preferred display
2577 // supports system decorations. Home activity would be launched on the same display if
2578 // no focusable stack found.
2579 return null;
2580 }
Andrii Kulian7fc22812016-12-28 13:04:11 -08002581
Andrii Kulianab132ee2018-07-24 22:10:21 +08002582 // Now look through all displays
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002583 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2584 final ActivityDisplay display = mActivityDisplays.get(i);
2585 if (display == preferredDisplay) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08002586 // We've already checked this one
2587 continue;
2588 }
Andrii Kulianab132ee2018-07-24 22:10:21 +08002589 final ActivityStack nextFocusableStack = display.getNextFocusableStack(currentFocus,
2590 ignoreCurrent);
2591 if (nextFocusableStack != null) {
2592 return nextFocusableStack;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002593 }
2594 }
2595
Andrii Kulianab132ee2018-07-24 22:10:21 +08002596 return null;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002597 }
2598
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002599 /**
2600 * Get next valid stack for launching provided activity in the system. This will search across
2601 * displays and stacks in last-focused order for a focusable and visible stack, except those
2602 * that are on a currently focused display.
2603 *
2604 * @param r The activity that is being launched.
2605 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2606 * searching for the next candidate.
2607 * @return Next valid {@link ActivityStack}, null if not found.
2608 */
2609 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002610 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2611 final ActivityDisplay display = mActivityDisplays.get(i);
2612 if (display.mDisplayId == currentFocus) {
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002613 continue;
2614 }
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002615 final ActivityStack stack = getValidLaunchStackOnDisplay(display.mDisplayId, r,
Louis Chang6322c422018-07-18 18:37:28 +08002616 null /* options */);
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002617 if (stack != null) {
2618 return stack;
2619 }
2620 }
2621 return null;
2622 }
2623
Louis Changbd48dca2018-08-29 17:44:34 +08002624 ActivityRecord getDefaultDisplayHomeActivity() {
2625 return getDefaultDisplayHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002626 }
2627
Louis Changbd48dca2018-08-29 17:44:34 +08002628 ActivityRecord getDefaultDisplayHomeActivityForUser(int userId) {
Martijn Coenen2a5c8392018-10-01 10:17:06 +02002629 return getActivityDisplay(DEFAULT_DISPLAY).getHomeActivityForUser(userId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002630 }
2631
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002632 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2633 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2634 boolean deferResume) {
2635
2636 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002637 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002638 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002639 return;
2640 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002641
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002642 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002643 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002644 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002645 // If the docked stack exists, don't resize non-floating stacks independently of the
2646 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002647 return;
2648 }
2649
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002650 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002651 mWindowManager.deferSurfaceLayout();
2652 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002653 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002654 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2655 // null bounds = fullscreen windowing mode...at least for now.
2656 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2657 } else if (splitScreenActive) {
2658 // If we are in split-screen mode and this stack support split-screen, then
2659 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2660 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2661 }
2662 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002663 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002664 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002665 stack.ensureVisibleActivitiesConfigurationLocked(
2666 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002667 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002668 } finally {
2669 mWindowManager.continueSurfaceLayout();
2670 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002671 }
2672 }
2673
Winson Chungc1674272018-02-21 10:15:17 -08002674 void deferUpdateRecentsHomeStackBounds() {
2675 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
2676 deferUpdateBounds(ACTIVITY_TYPE_HOME);
2677 }
2678
Bryce Leed3624e12017-11-30 08:51:45 -08002679 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002680 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002681 if (stack != null) {
2682 stack.deferUpdateBounds();
2683 }
2684 }
2685
Winson Chungc1674272018-02-21 10:15:17 -08002686 void continueUpdateRecentsHomeStackBounds() {
2687 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
2688 continueUpdateBounds(ACTIVITY_TYPE_HOME);
2689 }
2690
Bryce Leed3624e12017-11-30 08:51:45 -08002691 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002692 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002693 if (stack != null) {
2694 stack.continueUpdateBounds();
2695 }
2696 }
2697
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002698 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08002699 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002700 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2701 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002702 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002703 if (task != null) {
2704 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002705 }
2706 }
2707 mResizingTasksDuringAnimation.clear();
2708 }
2709
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002710 /**
2711 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002712 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002713 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002714 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002715 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002716
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002717 mWindowManager.deferSurfaceLayout();
2718 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002719 final int windowingMode = fromStack.getWindowingMode();
2720 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2721 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2722
2723 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002724 // Tell the display we are exiting split-screen mode.
2725 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002726 // We are moving all tasks from the docked stack to the fullscreen stack,
2727 // which is dismissing the docked stack, so resize all other stacks to
2728 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002729 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2730 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002731 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2732 continue;
2733 }
Evan Rosky10475742018-09-05 19:02:48 -07002734 otherStack.setWindowingMode(WINDOWING_MODE_UNDEFINED);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002735 }
2736
2737 // Also disable docked stack resizing since we have manually adjusted the
2738 // size of other stacks above and we don't want to trigger a docked stack
2739 // resize when we remove task from it below and it is detached from the
2740 // display because it no longer contains any tasks.
2741 mAllowDockedStackResize = false;
2742 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002743
Winson Chung5af42fc2017-03-24 17:11:33 -07002744 // If we are moving from the pinned stack, then the animation takes care of updating
2745 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002746 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002747 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002748
Wale Ogunwale388945c2017-10-04 12:13:46 -07002749 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002750 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002751 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002752 for (int i = 0; i < size; ++i) {
2753 final TaskRecord task = tasks.get(i);
2754 final ActivityStack toStack = toDisplay.getOrCreateStack(
2755 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002756
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002757 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002758 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002759 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002760 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002761 isTopTask /* animate */, DEFER_RESUME,
2762 schedulePictureInPictureModeChange,
2763 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08002764 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
2765 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002766 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002767 // Position the tasks in the fullscreen stack in order at the bottom of the
2768 // stack. Also defer resume until all the tasks have been moved to the
2769 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002770 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002771 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2772 schedulePictureInPictureModeChange,
2773 "moveTasksToFullscreenStack - NOT_onTop");
2774 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002775 }
2776 }
Winson Chung74666102017-02-22 17:49:24 -08002777
2778 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
Andrii Kulianab132ee2018-07-24 22:10:21 +08002779 resumeFocusedStacksTopActivitiesLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002780 } finally {
2781 mAllowDockedStackResize = true;
2782 mWindowManager.continueSurfaceLayout();
2783 }
2784 }
2785
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002786 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002787 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2788 }
2789
2790 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2791 mWindowManager.inSurfaceTransaction(() ->
2792 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002793 }
2794
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002795 void setSplitScreenResizing(boolean resizing) {
2796 if (resizing == mDockedStackResizing) {
2797 return;
2798 }
2799
2800 mDockedStackResizing = resizing;
2801 mWindowManager.setDockedStackResizing(resizing);
2802
2803 if (!resizing && mHasPendingDockedBounds) {
2804 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
2805 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
2806 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
2807
2808 mHasPendingDockedBounds = false;
2809 mPendingDockedBounds = null;
2810 mPendingTempDockedTaskBounds = null;
2811 mPendingTempDockedTaskInsetBounds = null;
2812 mPendingTempOtherTaskBounds = null;
2813 mPendingTempOtherTaskInsetBounds = null;
2814 }
2815 }
2816
Jorim Jaggidc249c42015-12-15 14:57:31 -08002817 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002818 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2819 boolean preserveWindows) {
2820 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2821 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2822 false /* deferResume */);
2823 }
2824
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002825 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002826 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2827 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002828
2829 if (!mAllowDockedStackResize) {
2830 // Docked stack resize currently disabled.
2831 return;
2832 }
2833
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002834 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002835 if (stack == null) {
2836 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2837 return;
2838 }
2839
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002840 if (mDockedStackResizing) {
2841 mHasPendingDockedBounds = true;
2842 mPendingDockedBounds = copyOrNull(dockedBounds);
2843 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
2844 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
2845 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
2846 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
2847 }
2848
Jorim Jaggidc249c42015-12-15 14:57:31 -08002849 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2850 mWindowManager.deferSurfaceLayout();
2851 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002852 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2853 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002854 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002855 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002856
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002857 // TODO: Checking for isAttached might not be needed as if the user passes in null
2858 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002859 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2860 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002861 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002862 // In this case we make all other static stacks fullscreen and move all
2863 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002864 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002865
2866 // stack shouldn't contain anymore activities, so nothing to resume.
2867 r = null;
2868 } else {
2869 // Docked stacks occupy a dedicated region on screen so the size of all other
2870 // static stacks need to be adjusted so they don't overlap with the docked stack.
2871 // We get the bounds to use from window manager which has been adjusted for any
2872 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002873 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002874 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002875 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2876 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002877 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002878 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002879 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002880 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002881 continue;
2882 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002883 if (mDockedStackResizing && !current.isTopActivityVisible()) {
2884 // Non-visible stacks get resized once we're done with the resize
2885 // interaction.
2886 continue;
2887 }
Wale Ogunwale926aade2017-08-29 11:24:37 -07002888 // Need to set windowing mode here before we try to get the dock bounds.
2889 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2890 current.getStackDockedModeBounds(
2891 tempOtherTaskBounds /* currentTempTaskBounds */,
2892 tempRect /* outStackBounds */,
2893 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2894
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002895 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002896 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2897 tempOtherTaskInsetBounds, preserveWindows,
2898 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002899 }
2900 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002901 if (!deferResume) {
2902 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2903 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002904 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002905 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002906 mWindowManager.continueSurfaceLayout();
2907 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2908 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002909 }
2910
Robert Carr0d00c2e2016-02-29 17:45:02 -08002911 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002912 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002913 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002914 if (stack == null) {
2915 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2916 return;
2917 }
Winson Chung19953ca2017-04-11 11:19:23 -07002918
2919 // It is possible for the bounds animation from the WM to call this but be delayed by
2920 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2921 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2922 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2923 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002924 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002925 return;
2926 }
2927
Robert Carr0d00c2e2016-02-29 17:45:02 -08002928 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2929 mWindowManager.deferSurfaceLayout();
2930 try {
2931 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002932 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02002933 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
2934 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08002935 insetBounds = tempRect;
2936 insetBounds.top = 0;
2937 insetBounds.left = 0;
2938 insetBounds.right = tempPinnedTaskBounds.width();
2939 insetBounds.bottom = tempPinnedTaskBounds.height();
2940 }
Winson Chung15036ca2018-05-31 15:51:47 -07002941 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
2942 // We have finished the animation into PiP, and are resizing the tasks to match the
2943 // stack bounds, while layouts are deferred, update any task state as a part of
2944 // transitioning it from fullscreen into a floating state.
2945 stack.onPipAnimationEndResize();
2946 }
Robert Carr7e4c90e2017-02-15 19:52:38 -08002947 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002948 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002949 } finally {
2950 mWindowManager.continueSurfaceLayout();
2951 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2952 }
2953 }
2954
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002955 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002956 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002957 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002958 /**
2959 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2960 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2961 * that the client receives onStop() before it is reparented. We do this by detaching
2962 * the stack from the display so that it will be considered invisible when
2963 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2964 * invisible as well and added to the stopping list. After which we process the
2965 * stopping list by handling the idle.
2966 */
2967 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2968 pinnedStack.mForceHidden = true;
2969 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2970 pinnedStack.mForceHidden = false;
2971 activityIdleInternalLocked(null, false /* fromTimeout */,
2972 true /* processPausingActivites */, null /* configuration */);
2973
2974 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002975 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002976 } else {
2977 for (int i = tasks.size() - 1; i >= 0; i--) {
2978 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07002979 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08002980 }
2981 }
2982 }
2983
2984 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002985 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002986 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2987 * instead moved back onto the fullscreen stack.
2988 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002989 void removeStack(ActivityStack stack) {
2990 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002991 }
2992
Wale Ogunwale68278562017-09-23 17:13:55 -07002993 /**
2994 * Removes stacks in the input windowing modes from the system if they are of activity type
2995 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2996 */
2997 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002998 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08002999 mActivityDisplays.get(i).removeStacksInWindowingModes(windowingModes);
Wale Ogunwale68278562017-09-23 17:13:55 -07003000 }
3001 }
3002
3003 void removeStacksWithActivityTypes(int... activityTypes) {
3004 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003005 mActivityDisplays.get(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003006 }
Robert Carr6914f082017-03-20 19:04:30 -07003007 }
3008
3009 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003010 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
3011 */
Winson Chung0ec2a352017-10-26 11:38:30 -07003012 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
3013 String reason) {
3014 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
3015 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07003016 }
3017
3018 /**
Winson Chung010927a2016-12-15 16:12:35 -08003019 * Removes the task with the specified task id.
3020 *
3021 * @param taskId Identifier of the task to be removed.
3022 * @param killProcess Kill any process associated with the task if possible.
3023 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07003024 * @param pauseImmediately Pauses all task activities immediately without waiting for the
3025 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08003026 * @return Returns true if the given task was found and removed.
3027 */
Winson Chung6954fc92017-03-24 16:22:12 -07003028 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07003029 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003030 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08003031 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003032 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08003033 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07003034 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003035 if (tr.isPersistable) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07003036 mService.notifyTaskPersisterLocked(null, true);
Winson Chung010927a2016-12-15 16:12:35 -08003037 }
3038 return true;
3039 }
3040 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
3041 return false;
3042 }
3043
3044 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
3045 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07003046 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003047 }
3048 ComponentName component = tr.getBaseIntent().getComponent();
3049 if (component == null) {
3050 Slog.w(TAG, "No component for base intent of task: " + tr);
3051 return;
3052 }
3053
3054 // Find any running services associated with this app and stop if needed.
Wale Ogunwale342fbe92018-10-09 08:44:10 -07003055 final Message msg = PooledLambda.obtainMessage(ActivityManagerInternal::cleanUpServices,
3056 mService.mAmInternal, tr.userId, component, new Intent(tr.getBaseIntent()));
3057 mService.mH.sendMessage(msg);
Winson Chung010927a2016-12-15 16:12:35 -08003058
3059 if (!killProcess) {
3060 return;
3061 }
3062
3063 // Determine if the process(es) for this task should be killed.
3064 final String pkg = component.getPackageName();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003065 ArrayList<Object> procsToKill = new ArrayList<>();
3066 ArrayMap<String, SparseArray<WindowProcessController>> pmap =
3067 mService.mProcessNames.getMap();
Winson Chung010927a2016-12-15 16:12:35 -08003068 for (int i = 0; i < pmap.size(); i++) {
3069
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003070 SparseArray<WindowProcessController> uids = pmap.valueAt(i);
Winson Chung010927a2016-12-15 16:12:35 -08003071 for (int j = 0; j < uids.size(); j++) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003072 WindowProcessController proc = uids.valueAt(j);
3073 if (proc.mUserId != tr.userId) {
Winson Chung010927a2016-12-15 16:12:35 -08003074 // Don't kill process for a different user.
3075 continue;
3076 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003077 if (proc == mService.mHomeProcess) {
Winson Chung010927a2016-12-15 16:12:35 -08003078 // Don't kill the home process along with tasks from the same package.
3079 continue;
3080 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003081 if (!proc.mPkgList.contains(pkg)) {
Winson Chung010927a2016-12-15 16:12:35 -08003082 // Don't kill process that is not associated with this task.
3083 continue;
3084 }
3085
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003086 if (!proc.shouldKillProcessForRemovedTask(tr)) {
3087 // Don't kill process(es) that has an activity in a different task that is also
Yunfan Chenafc15832018-07-26 16:34:28 +09003088 // in recents, or has an activity not stopped.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003089 return;
Winson Chung010927a2016-12-15 16:12:35 -08003090 }
3091
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003092 if (proc.hasForegroundServices()) {
Winson Chung010927a2016-12-15 16:12:35 -08003093 // Don't kill process(es) with foreground service.
3094 return;
3095 }
3096
3097 // Add process to kill list.
3098 procsToKill.add(proc);
3099 }
3100 }
3101
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003102 // Kill the running processes. Post on handle since we don't want to hold the service lock
3103 // while calling into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -07003104 final Message m = PooledLambda.obtainMessage(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003105 ActivityManagerInternal::killProcessesForRemovedTask, mService.mAmInternal,
3106 procsToKill);
Yohei Yukawae2fa39e2018-09-22 13:13:10 -07003107 mService.mH.sendMessage(m);
Winson Chung010927a2016-12-15 16:12:35 -08003108 }
3109
Chong Zhang5dcb2752015-08-18 13:50:26 -07003110 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07003111 * Called to restore the state of the task into the stack that it's supposed to go into.
3112 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07003113 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003114 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003115 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003116 * @return true if the task has been restored successfully.
3117 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003118 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
3119 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07003120 final ActivityStack currentStack = task.getStack();
3121 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003122 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003123 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003124 // Nothing else to do since it is already restored in the right stack.
3125 return true;
3126 }
3127 // Remove current stack association, so we can re-associate the task with the
3128 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003129 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003130 }
3131
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003132 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003133 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003134 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003135 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3136 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003137 final ArrayList<ActivityRecord> activities = task.mActivities;
3138 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003139 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003140 }
3141 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003142 }
3143
Winson Chung1dbc8112017-09-28 18:05:31 -07003144 @Override
3145 public void onRecentTaskAdded(TaskRecord task) {
3146 task.touchActiveTime();
3147 }
3148
3149 @Override
Winson Chunge6439102018-07-30 15:48:01 -07003150 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed, boolean killProcess) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003151 if (wasTrimmed) {
3152 // Task was trimmed from the recent tasks list -- remove the active task record as well
3153 // since the user won't really be able to go back to it
Winson Chunge6439102018-07-30 15:48:01 -07003154 removeTaskByIdLocked(task.taskId, killProcess, false /* removeFromRecents */,
3155 !PAUSE_IMMEDIATELY, "recent-task-trimmed");
Winson Chung0ec2a352017-10-26 11:38:30 -07003156 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003157 task.removedFromRecents();
3158 }
3159
Wale Ogunwale040b4702015-08-06 18:10:50 -07003160 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003161 * Move stack with all its existing content to specified display.
3162 * @param stackId Id of stack to move.
3163 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003164 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003165 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003166 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003167 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003168 if (activityDisplay == null) {
3169 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3170 + displayId);
3171 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003172 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003173 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003174 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3175 + stackId);
3176 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003177
3178 final ActivityDisplay currentDisplay = stack.getDisplay();
3179 if (currentDisplay == null) {
3180 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3181 + " is not attached to any display.");
3182 }
3183
3184 if (currentDisplay.mDisplayId == displayId) {
3185 throw new IllegalArgumentException("Trying to move stack=" + stack
3186 + " to its current displayId=" + displayId);
3187 }
3188
Riddle Hsu402b4402018-11-06 17:23:15 +08003189 stack.reparent(activityDisplay, onTop, false /* displayRemoved */);
Andrii Kulian839def92016-11-02 10:58:58 -07003190 // TODO(multi-display): resize stacks properly if moved from split-screen.
3191 }
3192
3193 /**
Winson Chung74666102017-02-22 17:49:24 -08003194 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3195 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003196 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003197 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003198 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003199 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003200 final int stackId = stack.mStackId;
3201 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003202
Winson Chung74666102017-02-22 17:49:24 -08003203 // Check that we aren't reparenting to the same stack that the task is already in
3204 if (prevStack != null && prevStack.mStackId == stackId) {
3205 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3206 + " already in stackId=" + stackId);
3207 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003208 }
3209
Winson Chung74666102017-02-22 17:49:24 -08003210 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3211 // support
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003212 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003213 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003214 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003215 }
3216
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003217 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3218 // multi-display.
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003219 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003220 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3221 + " reparent task=" + task + " to stackId=" + stackId);
3222 }
3223
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003224 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003225 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003226 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003227 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3228 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003229 }
3230
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003231 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3232 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003233 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003234 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3235 + " Moving to a fullscreen stack instead.");
3236 if (prevStack != null) {
3237 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003238 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003239 stack = stack.getDisplay().createStack(
3240 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003241 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003242 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243 }
3244
Winson Chung08f81892017-03-02 15:40:51 -08003245 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003246 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003247 if (stack == null) {
3248 throw new IllegalArgumentException(
3249 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3250 }
3251
3252 final ActivityRecord r = stack.topRunningActivityLocked();
3253 if (r == null) {
3254 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3255 + " in stack=" + stack);
3256 return false;
3257 }
3258
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003259 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003260 Slog.w(TAG,
3261 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003262 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003263 return false;
3264 }
3265
Winson Chung3a682872017-04-10 14:38:05 -07003266 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003267 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003268 return true;
3269 }
3270
Winson Chung8bca9e42017-04-16 15:59:43 -07003271 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003272 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003273
Wale Ogunwale480dca02016-02-06 13:58:29 -08003274 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003275
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003276 final ActivityDisplay display = r.getStack().getDisplay();
3277 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003278
Bryce Lee04ab3462017-04-10 15:06:33 -07003279 // This will clear the pinned stack by moving an existing task to the full screen stack,
3280 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003281 if (stack != null) {
3282 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3283 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003284
Wale Ogunwale1666e312016-12-16 11:27:18 -08003285 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003286 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003287
Winson Chunge55c0192017-08-24 14:50:48 -07003288 // Calculate the target bounds here before the task is reparented back into pinned windowing
3289 // mode (which will reset the saved bounds)
3290 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3291
Wale Ogunwale480dca02016-02-06 13:58:29 -08003292 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003293 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003294 // Resize the pinned stack to match the current size of the task the activity we are
3295 // going to be moving is currently contained in. We do this to have the right starting
3296 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003297 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003298 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003299 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003300
3301 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003302 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003303 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3304 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003305 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003306 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003307 // reveal/leave the other activities in their original task.
3308
3309 // Currently, we don't support reparenting activities across tasks in two different
3310 // stacks, so instead, just create a new task in the same stack, reparent the
3311 // activity into that task, and then reparent the whole task to the new stack. This
3312 // ensures that all the necessary work to migrate states in the old and new stacks
3313 // is also done.
3314 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003315 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003316 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3317
Winson Chung5af42fc2017-03-24 17:11:33 -07003318 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003319 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003320 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003321 }
Winson Chungc2baac02017-01-11 13:34:47 -08003322
3323 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3324 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003325 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003326 } finally {
3327 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003328 }
3329
Winson Chunge7ba6862017-05-24 12:13:33 -07003330 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3331 true /* fromFullscreen */);
3332
3333 // Update the visibility of all activities after the they have been reparented to the new
3334 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3335 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3336 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003337 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Andrii Kulianab132ee2018-07-24 22:10:21 +08003338 resumeFocusedStacksTopActivitiesLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003339
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07003340 mService.getTaskChangeNotificationController().notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003341 }
3342
Louis Changc85b1a32018-08-14 16:40:53 +08003343 ActivityRecord findTaskLocked(ActivityRecord r, int preferredDisplayId) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003344 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Louis Changc85b1a32018-08-14 16:40:53 +08003345 mTmpFindTaskResult.clear();
3346
3347 // Looking up task on preferred display first
3348 final ActivityDisplay preferredDisplay = getActivityDisplay(preferredDisplayId);
3349 if (preferredDisplay != null) {
3350 preferredDisplay.findTaskLocked(r, true /* isPreferredDisplay */, mTmpFindTaskResult);
3351 if (mTmpFindTaskResult.mIdealMatch) {
3352 return mTmpFindTaskResult.mRecord;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003353 }
3354 }
Winson Chung5b895b72017-05-01 13:46:25 -07003355
Louis Changc85b1a32018-08-14 16:40:53 +08003356 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3357 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
3358 if (display.mDisplayId == preferredDisplayId) {
3359 continue;
3360 }
3361
3362 display.findTaskLocked(r, false /* isPreferredDisplay */, mTmpFindTaskResult);
3363 if (mTmpFindTaskResult.mIdealMatch) {
3364 return mTmpFindTaskResult.mRecord;
3365 }
3366 }
3367
3368 if (DEBUG_TASKS && mTmpFindTaskResult.mRecord == null) Slog.d(TAG_TASKS, "No task found");
3369 return mTmpFindTaskResult.mRecord;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003370 }
3371
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003372 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003373 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003374 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003375 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003376 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3377 final ActivityStack stack = display.getChildAt(stackNdx);
3378 final ActivityRecord ar = stack.findActivityLocked(
3379 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003380 if (ar != null) {
3381 return ar;
3382 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003383 }
3384 }
3385 return null;
3386 }
3387
David Stevens9440dc82017-03-16 19:00:20 -07003388 boolean hasAwakeDisplay() {
3389 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003390 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003391 if (!display.shouldSleep()) {
3392 return true;
3393 }
3394 }
3395 return false;
3396 }
3397
Craig Mautner8d341ef2013-03-26 09:03:27 -07003398 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003399 scheduleSleepTimeout();
3400 if (!mGoingToSleep.isHeld()) {
3401 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003402 if (mLaunchingActivity.isHeld()) {
3403 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3404 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003405 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003406 mLaunchingActivity.release();
Wale Ogunwale342fbe92018-10-09 08:44:10 -07003407 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003408 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003409 }
David Stevens9440dc82017-03-16 19:00:20 -07003410
3411 applySleepTokensLocked(false /* applyToStacks */);
3412
3413 checkReadyForSleepLocked(true /* allowDelay */);
3414 }
3415
3416 void prepareForShutdownLocked() {
3417 for (int i = 0; i < mActivityDisplays.size(); i++) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003418 createSleepTokenLocked("shutdown", mActivityDisplays.get(i).mDisplayId);
David Stevens9440dc82017-03-16 19:00:20 -07003419 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003420 }
3421
3422 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003423 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003424
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003425 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003426 final long endTime = System.currentTimeMillis() + timeout;
3427 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003428 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003429 long timeRemaining = endTime - System.currentTimeMillis();
3430 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003431 try {
Wale Ogunwalef6733932018-06-27 05:14:34 -07003432 mService.mGlobalLock.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003433 } catch (InterruptedException e) {
3434 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003435 } else {
3436 Slog.w(TAG, "Activity manager shutdown timed out");
3437 timedout = true;
3438 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003439 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003440 } else {
3441 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003442 }
3443 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003444
3445 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003446 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003447
Craig Mautner8d341ef2013-03-26 09:03:27 -07003448 return timedout;
3449 }
3450
3451 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003452 removeSleepTimeouts();
3453 if (mGoingToSleep.isHeld()) {
3454 mGoingToSleep.release();
3455 }
David Stevens9440dc82017-03-16 19:00:20 -07003456 }
3457
3458 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003459 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003460 // Set the sleeping state of the display.
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003461 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003462 final boolean displayShouldSleep = display.shouldSleep();
3463 if (displayShouldSleep == display.isSleeping()) {
3464 continue;
3465 }
3466 display.setIsSleeping(displayShouldSleep);
3467
3468 if (!applyToStacks) {
3469 continue;
3470 }
3471
3472 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003473 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3474 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003475 if (displayShouldSleep) {
3476 stack.goToSleepIfPossible(false /* shuttingDown */);
3477 } else {
3478 stack.awakeFromSleepingLocked();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003479 if (stack.isFocusedStackOnDisplay() && !getKeyguardController()
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003480 .isKeyguardOrAodShowing(display.mDisplayId)) {
Bryce Leed939cf02018-03-12 09:04:44 -07003481 // If the keyguard is unlocked - resume immediately.
3482 // It is possible that the display will not be awake at the time we
3483 // process the keyguard going away, which can happen before the sleep token
3484 // is released. As a result, it is important we resume the activity here.
Andrii Kulianab132ee2018-07-24 22:10:21 +08003485 resumeFocusedStacksTopActivitiesLocked();
David Stevens9440dc82017-03-16 19:00:20 -07003486 }
3487 }
3488 }
3489
3490 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3491 continue;
3492 }
3493 // The display is awake now, so clean up the going to sleep list.
3494 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3495 final ActivityRecord r = it.next();
3496 if (r.getDisplayId() == display.mDisplayId) {
3497 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003498 }
Craig Mautner5314a402013-09-26 12:40:16 -07003499 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003500 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003501 }
3502
3503 void activitySleptLocked(ActivityRecord r) {
3504 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003505 final ActivityStack s = r.getStack();
3506 if (s != null) {
3507 s.checkReadyForSleep();
3508 } else {
3509 checkReadyForSleepLocked(true);
3510 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003511 }
3512
David Stevens9440dc82017-03-16 19:00:20 -07003513 void checkReadyForSleepLocked(boolean allowDelay) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07003514 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003515 // Do not care.
3516 return;
3517 }
3518
David Stevens18abd0e2017-08-17 14:55:47 -07003519 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3520 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003521 }
3522
Wei Wang65c7a152016-06-02 18:51:22 -07003523 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003524 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003525
Craig Mautner0eea92c2013-05-16 13:35:39 -07003526 removeSleepTimeouts();
3527
3528 if (mGoingToSleep.isHeld()) {
3529 mGoingToSleep.release();
3530 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07003531 if (mService.mShuttingDown) {
3532 mService.mGlobalLock.notifyAll();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003533 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003534 }
3535
David Stevens18abd0e2017-08-17 14:55:47 -07003536 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3537 // successfully put to sleep.
3538 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3539 boolean allSleep = true;
3540 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003541 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003542 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3543 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003544 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003545 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003546 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003547 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003548 }
3549 }
3550 }
3551 return allSleep;
3552 }
3553
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003554 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003555 // A resumed activity cannot be stopping. remove from list
3556 mStoppingActivities.remove(r);
3557
Andrii Kulian02b7a832016-10-06 23:11:56 -07003558 final ActivityStack stack = r.getStack();
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003559 if (isTopDisplayFocusedStack(stack)) {
Wale Ogunwale53783742018-09-16 10:21:51 -07003560 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003561 }
Riddle Hsubbb63c22018-10-03 12:28:29 +08003562 if (stack.getDisplay().allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003563 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
lumark588a3e82018-07-20 18:53:54 +08003564 // Make sure activity & window visibility should be identical
3565 // for all displays in this stage.
3566 executeAppTransitionForAllDisplay();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003567 return true;
3568 }
3569 return false;
3570 }
3571
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003572 void handleAppCrashLocked(WindowProcessController app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003573 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003574 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003575 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3576 final ActivityStack stack = display.getChildAt(stackNdx);
3577 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003579 }
3580 }
3581
Craig Mautnerbb742462014-07-07 15:28:55 -07003582 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003583 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003584 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003585 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003586
3587 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003588 mRecentTasks.add(task);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07003589 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003590 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003591
3592 // When launching tasks behind, update the last active time of the top task after the new
3593 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003594 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003595 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003596 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003597 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003598 }
3599
3600 void scheduleLaunchTaskBehindComplete(IBinder token) {
3601 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3602 }
3603
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003604 /**
3605 * Make sure that all activities that need to be visible in the system actually are and update
3606 * their configuration.
3607 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003608 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3609 boolean preserveWindows) {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003610 ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003611 true /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003612 }
3613
3614 /**
3615 * @see #ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
3616 */
3617 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003618 boolean preserveWindows, boolean notifyClients) {
Bryce Lee459c0622018-03-19 11:04:01 -07003619 getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02003620 try {
3621 // First the front stacks. In case any are not fullscreen and are in front of home.
3622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003623 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003624 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3625 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003626 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003627 notifyClients);
Jorim Jaggife762342016-10-13 14:33:27 +02003628 }
Craig Mautner580ea812013-04-25 12:58:38 -07003629 }
Jorim Jaggife762342016-10-13 14:33:27 +02003630 } finally {
Bryce Lee459c0622018-03-19 11:04:01 -07003631 getKeyguardController().endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003632 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003633 }
3634
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003635 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3636 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003637 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003638 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3639 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003640 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3641 }
3642 }
3643 }
3644
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003645 void invalidateTaskLayers() {
3646 mTaskLayersChanged = true;
3647 }
3648
3649 void rankTaskLayersIfNeeded() {
3650 if (!mTaskLayersChanged) {
3651 return;
3652 }
3653 mTaskLayersChanged = false;
3654 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003655 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003656 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003657 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3658 final ActivityStack stack = display.getChildAt(stackNdx);
3659 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003660 }
3661 }
3662 }
3663
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003664 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3665 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003666 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003667 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3668 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003669 stack.clearOtherAppTimeTrackers(except);
3670 }
3671 }
3672 }
3673
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003674 void scheduleDestroyAllActivities(WindowProcessController app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003675 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003676 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003677 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3678 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003679 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003680 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003681 }
3682 }
3683
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003684 void releaseSomeActivitiesLocked(WindowProcessController app, String reason) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003685 // Tasks is non-null only if two or more tasks are found.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003686 ArraySet<TaskRecord> tasks = app.getReleaseSomeActivitiesTasks();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003687 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003688 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003689 return;
3690 }
3691 // If we have activities in multiple tasks that are in a position to be destroyed,
3692 // let's iterate through the tasks and release the oldest one.
3693 final int numDisplays = mActivityDisplays.size();
3694 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003695 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003696 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003697 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003698 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3699 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003700 // Try to release activities in this stack; if we manage to, we are done.
3701 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3702 return;
3703 }
3704 }
3705 }
3706 }
3707
Amith Yamasani37a40c22015-06-17 13:25:42 -07003708 boolean switchUserLocked(int userId, UserState uss) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003709 final int focusStackId = getTopDisplayFocusedStack().getStackId();
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003710 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003711 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003712 if (dockedStack != null) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07003713 moveTasksToFullscreenStackLocked(dockedStack, dockedStack.isFocusedStackOnDisplay());
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003714 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003715 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3716 // also cause all tasks to be moved to the fullscreen stack at a position that is
3717 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003718 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003719
3720 mUserStackInFront.put(mCurrentUser, focusStackId);
Louis Changbd48dca2018-08-29 17:44:34 +08003721 final int restoreStackId =
3722 mUserStackInFront.get(userId, getDefaultDisplay().getHomeStack().mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003723 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003724
Craig Mautner858d8a62013-04-23 17:08:34 -07003725 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003727 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003728 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3729 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003730 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003731 TaskRecord task = stack.topTask();
3732 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003733 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003734 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003736 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003737
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003738 ActivityStack stack = getStack(restoreStackId);
3739 if (stack == null) {
Louis Changbd48dca2018-08-29 17:44:34 +08003740 stack = getDefaultDisplay().getHomeStack();
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003741 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003742 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003743 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003744 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003745 } else {
3746 // Stack was moved to another display while user was swapped out.
Louis Changbd48dca2018-08-29 17:44:34 +08003747 resumeHomeActivity(null, "switchUserOnOtherDisplay", DEFAULT_DISPLAY);
Craig Mautnere0a38842013-12-16 16:14:02 -08003748 }
Craig Mautner93529a42013-10-04 15:03:13 -07003749 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003750 }
3751
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003752 /** Checks whether the userid is a profile of the current user. */
3753 boolean isCurrentProfileLocked(int userId) {
3754 if (userId == mCurrentUser) return true;
Wale Ogunwale86b74462018-07-02 08:42:43 -07003755 return mService.mAmInternal.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003756 }
3757
Bryce Leeb7c9b802017-05-02 14:20:24 -07003758 /**
3759 * Returns whether a stopping activity is present that should be stopped after visible, rather
3760 * than idle.
3761 * @return {@code true} if such activity is present. {@code false} otherwise.
3762 */
3763 boolean isStoppingNoHistoryActivity() {
3764 // Activities that are marked as nohistory should be stopped immediately after the resumed
3765 // activity has become visible.
3766 for (ActivityRecord record : mStoppingActivities) {
3767 if (record.isNoHistory()) {
3768 return true;
3769 }
3770 }
3771
3772 return false;
3773 }
3774
Winson Chung4dabf232017-01-25 13:25:22 -08003775 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3776 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003777 ArrayList<ActivityRecord> stops = null;
3778
3779 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003780 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3781 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003782 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003783 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003784 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3785 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003786 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003787 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003788 if (s.finishing) {
3789 // If this activity is finishing, it is sitting on top of
3790 // everyone else but we now know it is no longer needed...
3791 // so get rid of it. Otherwise, we need to go through the
3792 // normal flow and hide it once we determine that it is
3793 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003794 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003795 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003796 }
3797 }
David Stevens9440dc82017-03-16 19:00:20 -07003798 if (remove) {
3799 final ActivityStack stack = s.getStack();
3800 final boolean shouldSleepOrShutDown = stack != null
3801 ? stack.shouldSleepOrShutDownActivities()
Wale Ogunwalef6733932018-06-27 05:14:34 -07003802 : mService.isSleepingOrShuttingDownLocked();
David Stevens9440dc82017-03-16 19:00:20 -07003803 if (!waitingVisible || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08003804 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07003805 // Defer processing pausing activities in this iteration and reschedule
3806 // a delayed idle to reprocess it again
3807 removeTimeoutsForActivityLocked(idleActivity);
3808 scheduleIdleTimeoutLocked(idleActivity);
3809 continue;
3810 }
Winson Chung4dabf232017-01-25 13:25:22 -08003811
David Stevens9440dc82017-03-16 19:00:20 -07003812 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3813 if (stops == null) {
3814 stops = new ArrayList<>();
3815 }
3816 stops.add(s);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02003817
3818 // Make sure to remove it in all cases in case we entered this block with
3819 // shouldSleepOrShutDown
3820 mActivitiesWaitingForVisibleActivity.remove(s);
David Stevens9440dc82017-03-16 19:00:20 -07003821 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003823 }
3824 }
3825
3826 return stops;
3827 }
3828
Craig Mautnercf910b02013-04-23 11:23:27 -07003829 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003831 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003832 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3833 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003834 final ActivityRecord r = stack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08003835 final ActivityState state = r == null ? DESTROYED : r.getState();
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003836 if (isTopDisplayFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003837 if (r == null) Slog.e(TAG,
3838 "validateTop...: null top activity, stack=" + stack);
3839 else {
3840 final ActivityRecord pausing = stack.mPausingActivity;
3841 if (pausing != null && pausing == r) Slog.e(TAG,
3842 "validateTop...: top stack has pausing activity r=" + r
3843 + " state=" + state);
3844 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3845 "validateTop...: activity in front not resumed r=" + r
3846 + " state=" + state);
3847 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003848 } else {
Bryce Leec4ab62a2018-03-05 14:19:26 -08003849 final ActivityRecord resumed = stack.getResumedActivity();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003850 if (resumed != null && resumed == r) Slog.e(TAG,
3851 "validateTop...: back stack has resumed activity r=" + r
3852 + " state=" + state);
3853 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3854 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003855 }
3856 }
3857 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003858 }
3859
Bryce Lee77a7dd62018-01-22 15:47:09 -08003860 public void dumpDisplays(PrintWriter pw) {
3861 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003862 final ActivityDisplay display = mActivityDisplays.get(i);
Bryce Lee77a7dd62018-01-22 15:47:09 -08003863 pw.print("[id:" + display.mDisplayId + " stacks:");
3864 display.dumpStacks(pw);
3865 pw.print("]");
3866 }
3867 }
3868
Craig Mautner27084302013-03-25 08:05:25 -07003869 public void dump(PrintWriter pw, String prefix) {
Riddle Hsu75016992018-09-20 20:37:14 +08003870 pw.println();
3871 pw.println("ActivityStackSupervisor state:");
3872 pw.print(prefix);
3873 pw.println("topDisplayFocusedStack=" + getTopDisplayFocusedStack());
3874 pw.print(prefix);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003875 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003876 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003877 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003878 final ActivityDisplay display = mActivityDisplays.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003879 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003880 }
Bryce Lee4a194382017-04-04 14:32:48 -07003881 if (!mWaitingForActivityVisible.isEmpty()) {
3882 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3883 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3884 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3885 }
3886 }
Winson Chungc1674272018-02-21 10:15:17 -08003887 pw.print(prefix); pw.print("isHomeRecentsComponent=");
3888 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07003889
Bryce Lee459c0622018-03-19 11:04:01 -07003890 getKeyguardController().dump(pw, prefix);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07003891 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003892 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003893
Yi Jin2b30f322018-02-20 15:41:47 -08003894 public void writeToProto(ProtoOutputStream proto, long fieldId) {
3895 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02003896 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003897 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003898 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayNdx);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003899 activityDisplay.writeToProto(proto, DISPLAYS);
3900 }
Bryce Lee459c0622018-03-19 11:04:01 -07003901 getKeyguardController().writeToProto(proto, KEYGUARD_CONTROLLER);
Andrii Kulian52d255c2018-07-13 11:32:19 -07003902 // TODO(b/111541062): Update tests to look for resumed activities on all displays
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003903 final ActivityStack focusedStack = getTopDisplayFocusedStack();
Andrii Kulian52d255c2018-07-13 11:32:19 -07003904 if (focusedStack != null) {
3905 proto.write(FOCUSED_STACK_ID, focusedStack.mStackId);
3906 final ActivityRecord focusedActivity = focusedStack.getDisplay().getResumedActivity();
Steven Timotius4346f0a2017-09-12 11:07:21 -07003907 if (focusedActivity != null) {
3908 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3909 }
3910 } else {
3911 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3912 }
Winson Chungc1674272018-02-21 10:15:17 -08003913 proto.write(IS_HOME_RECENTS_COMPONENT,
3914 mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
wilsonshih468b7c02018-06-29 10:21:40 +08003915 mService.getActivityStartController().writeToProto(proto, PENDING_ACTIVITIES);
Yi Jin2b30f322018-02-20 15:41:47 -08003916 proto.end(token);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003917 }
3918
Winson43d1f262016-06-14 16:05:55 -07003919 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003920 * Dump all connected displays' configurations.
3921 * @param prefix Prefix to apply to each line of the dump.
3922 */
3923 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3924 pw.print(prefix); pw.println("Display override configurations:");
3925 final int displayCount = mActivityDisplays.size();
3926 for (int i = 0; i < displayCount; i++) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003927 final ActivityDisplay activityDisplay = mActivityDisplays.get(i);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003928 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3929 pw.println(activityDisplay.getOverrideConfiguration());
3930 }
3931 }
3932
3933 /**
Winson43d1f262016-06-14 16:05:55 -07003934 * Dumps the activities matching the given {@param name} in the either the focused stack
3935 * or all visible stacks if {@param dumpVisibleStacks} is true.
3936 */
Winson Chung6998bc42017-02-28 17:07:05 -08003937 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3938 boolean dumpFocusedStackOnly) {
3939 if (dumpFocusedStackOnly) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003940 return getTopDisplayFocusedStack().getDumpActivitiesLocked(name);
Winson Chung6998bc42017-02-28 17:07:05 -08003941 } else {
Winson43d1f262016-06-14 16:05:55 -07003942 ArrayList<ActivityRecord> activities = new ArrayList<>();
3943 int numDisplays = mActivityDisplays.size();
3944 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003945 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003946 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3947 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003948 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003949 activities.addAll(stack.getDumpActivitiesLocked(name));
3950 }
3951 }
3952 }
3953 return activities;
Winson43d1f262016-06-14 16:05:55 -07003954 }
Craig Mautner20e72272013-04-01 13:45:53 -07003955 }
3956
Dianne Hackborn390517b2013-05-30 15:03:32 -07003957 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3958 boolean needSep, String prefix) {
3959 if (activity != null) {
3960 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3961 if (needSep) {
3962 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003963 }
3964 pw.print(prefix);
3965 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003966 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003967 }
3968 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003969 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003970 }
3971
Craig Mautner8d341ef2013-03-26 09:03:27 -07003972 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3973 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003974 boolean printed = false;
3975 boolean needSep = false;
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003976 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3977 ActivityDisplay activityDisplay = mActivityDisplays.get(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003978 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003979 pw.println(" (activities from top to bottom):");
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003980 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003981 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3982 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003983 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003984 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003985 + ": type=" + activityTypeToString(stack.getActivityType())
3986 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003987 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08003988 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07003989
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003990 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3991 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003992
Craig Mautner4a1cb222013-12-04 16:14:06 -08003993 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3994 !dumpAll, false, dumpPackage, true,
3995 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003996
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 needSep = printed;
3998 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3999 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004000 if (pr) {
4001 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004003 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08004004 pr = printThisActivity(pw, stack.getResumedActivity(), dumpPackage, needSep,
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 " mResumedActivity: ");
4006 if (pr) {
4007 printed = true;
4008 needSep = false;
4009 }
4010 if (dumpAll) {
4011 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4012 " mLastPausedActivity: ");
4013 if (pr) {
4014 printed = true;
4015 needSep = true;
4016 }
4017 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4018 needSep, " mLastNoHistoryActivity: ");
4019 }
4020 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004021 }
Andrii Kulian52d255c2018-07-13 11:32:19 -07004022 printThisActivity(pw, activityDisplay.getResumedActivity(), dumpPackage, needSep,
4023 " ResumedActivity:");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004024 }
4025
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004026 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4027 false, dumpPackage, true, " Activities waiting to finish:", null);
4028 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4029 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07004030 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
4031 false, !dumpAll, false, dumpPackage, true,
4032 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004033 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4034 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004035
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004036 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004037 }
4038
Dianne Hackborn390517b2013-05-30 15:03:32 -07004039 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004040 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004041 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004042 String innerPrefix = null;
4043 String[] args = null;
4044 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004045 for (int i=list.size()-1; i>=0; i--) {
4046 final ActivityRecord r = list.get(i);
4047 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4048 continue;
4049 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004050 if (innerPrefix == null) {
4051 innerPrefix = prefix + " ";
4052 args = new String[0];
4053 }
4054 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004055 final boolean full = !brief && (complete || !r.isInHistory());
4056 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004057 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004058 needNL = false;
4059 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004060 if (header != null) {
4061 pw.println(header);
4062 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004063 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004064 if (lastTask != r.getTask()) {
4065 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07004066 pw.print(prefix);
4067 pw.print(full ? "* " : " ");
4068 pw.println(lastTask);
4069 if (full) {
4070 lastTask.dump(pw, prefix + " ");
4071 } else if (complete) {
4072 // Complete + brief == give a summary. Isn't that obvious?!?
4073 if (lastTask.intent != null) {
4074 pw.print(prefix); pw.print(" ");
4075 pw.println(lastTask.intent.toInsecureStringWithClip());
4076 }
4077 }
4078 }
4079 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4080 pw.print(" #"); pw.print(i); pw.print(": ");
4081 pw.println(r);
4082 if (full) {
4083 r.dump(pw, innerPrefix);
4084 } else if (complete) {
4085 // Complete + brief == give a summary. Isn't that obvious?!?
4086 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4087 if (r.app != null) {
4088 pw.print(innerPrefix); pw.println(r.app);
4089 }
4090 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004091 if (client && r.attachedToProcess()) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004092 // flush anything that is already in the PrintWriter since the thread is going
4093 // to write to the file descriptor directly
4094 pw.flush();
4095 try {
4096 TransferPipe tp = new TransferPipe();
4097 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004098 r.app.getThread().dumpActivity(
4099 tp.getWriteFd(), r.appToken, innerPrefix, args);
4100 // Short timeout, since blocking here can deadlock with the application.
Craig Mautner8d341ef2013-03-26 09:03:27 -07004101 tp.go(fd, 2000);
4102 } finally {
4103 tp.kill();
4104 }
4105 } catch (IOException e) {
4106 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4107 } catch (RemoteException e) {
4108 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4109 }
4110 needNL = true;
4111 }
4112 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004113 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004114 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004115
Craig Mautnerf3333272013-04-22 10:55:53 -07004116 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004117 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4118 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004119 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4120 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004121 }
4122
4123 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004124 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004125 }
4126
4127 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004128 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4129 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004130 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4131 }
4132
Craig Mautner05d29032013-05-03 13:40:13 -07004133 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004134 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4135 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4136 }
Craig Mautner05d29032013-05-03 13:40:13 -07004137 }
4138
Craig Mautner0eea92c2013-05-16 13:35:39 -07004139 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004140 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4141 }
4142
4143 final void scheduleSleepTimeout() {
4144 removeSleepTimeouts();
4145 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4146 }
4147
Craig Mautner4a1cb222013-12-04 16:14:06 -08004148 @Override
4149 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004150 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Riddle Hsuf53da812018-08-15 22:00:27 +08004151 synchronized (mService.mGlobalLock) {
4152 getActivityDisplayOrCreateLocked(displayId);
Louis Chang1eff2482018-11-01 15:46:31 +08004153 // Do not start home before booting, or it may accidentally finish booting before it
4154 // starts. Instead, we expect home activities to be launched when the system is ready
4155 // (ActivityManagerService#systemReady).
4156 if (mService.isBooted() || mService.isBooting()) {
4157 startHomeOnDisplay(mCurrentUser, "displayAdded", displayId);
4158 }
Riddle Hsuf53da812018-08-15 22:00:27 +08004159 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 }
4161
4162 @Override
4163 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004164 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Riddle Hsuf53da812018-08-15 22:00:27 +08004165 if (displayId == DEFAULT_DISPLAY) {
4166 throw new IllegalArgumentException("Can't remove the primary display.");
4167 }
4168
4169 synchronized (mService.mGlobalLock) {
4170 final ActivityDisplay activityDisplay = getActivityDisplay(displayId);
4171 if (activityDisplay == null) {
4172 return;
4173 }
4174
4175 activityDisplay.remove();
4176 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004177 }
4178
4179 @Override
4180 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004181 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004182 synchronized (mService.mGlobalLock) {
Riddle Hsuf53da812018-08-15 22:00:27 +08004183 final ActivityDisplay activityDisplay = getActivityDisplay(displayId);
4184 if (activityDisplay != null) {
4185 activityDisplay.onDisplayChanged();
4186 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004187 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 }
4189
Andrii Kulianca007a62016-11-22 16:33:28 -08004190 /** Check if display with specified id is added to the list. */
4191 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004192 return getActivityDisplayOrCreateLocked(displayId) != null;
4193 }
4194
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004195 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004196 ActivityDisplay getActivityDisplay(int displayId) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004197 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
4198 final ActivityDisplay activityDisplay = mActivityDisplays.get(i);
4199 if (activityDisplay.mDisplayId == displayId) {
4200 return activityDisplay;
4201 }
4202 }
4203 return null;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004204 }
4205
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004206 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4207 ActivityDisplay getDefaultDisplay() {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004208 return mDefaultDisplay;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004209 }
4210
Andrii Kulian62e6f252017-05-30 22:46:53 -07004211 /**
4212 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4213 * corresponding record in display manager.
4214 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004215 // TODO: Look into consolidating with getActivityDisplay()
4216 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004217 ActivityDisplay activityDisplay = getActivityDisplay(displayId);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004218 if (activityDisplay != null) {
4219 return activityDisplay;
4220 }
4221 if (mDisplayManager == null) {
4222 // The system isn't fully initialized yet.
4223 return null;
4224 }
4225 final Display display = mDisplayManager.getDisplay(displayId);
4226 if (display == null) {
4227 // The display is not registered in DisplayManager.
4228 return null;
4229 }
4230 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale45477b52018-03-06 12:24:19 -08004231 activityDisplay = new ActivityDisplay(this, display);
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004232 addChild(activityDisplay, ActivityDisplay.POSITION_BOTTOM);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004233 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004234 }
4235
Louis Chang89f43fc2018-10-05 10:59:14 +08004236 boolean startHomeOnAllDisplays(int userId, String reason) {
4237 boolean homeStarted = false;
4238 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
4239 final int displayId = mActivityDisplays.get(i).mDisplayId;
4240 homeStarted |= startHomeOnDisplay(userId, reason, displayId);
4241 }
4242 return homeStarted;
4243 }
4244
4245 /**
4246 * This starts home activity on displays that can have system decorations and only if the
4247 * home activity can have multiple instances.
4248 */
4249 boolean startHomeOnDisplay(int userId, String reason, int displayId) {
4250 final Intent homeIntent = mService.getHomeIntent();
4251 final ActivityInfo aInfo = resolveHomeActivity(userId, homeIntent);
4252 if (aInfo == null) {
4253 return false;
4254 }
4255
Louis Changdd3592a2018-11-05 11:04:14 +08004256 if (!canStartHomeOnDisplay(aInfo, displayId, false /* allowInstrumenting */)) {
Louis Chang89f43fc2018-10-05 10:59:14 +08004257 return false;
4258 }
4259
4260 // Update the reason for ANR debugging to verify if the user activity is the one that
4261 // actually launched.
4262 final String myReason = reason + ":" + userId + ":" + UserHandle.getUserId(
4263 aInfo.applicationInfo.uid);
4264 mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason,
4265 displayId);
4266 return true;
4267 }
4268
4269 /**
4270 * This resolves the home activity info and updates the home component of the given intent.
4271 * @return the home activity info if any.
4272 */
4273 private ActivityInfo resolveHomeActivity(int userId, Intent homeIntent) {
4274 final int flags = ActivityManagerService.STOCK_PM_FLAGS;
4275 final ComponentName comp = homeIntent.getComponent();
4276 ActivityInfo aInfo = null;
4277 try {
4278 if (comp != null) {
4279 // Factory test.
4280 aInfo = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId);
4281 } else {
4282 final String resolvedType =
4283 homeIntent.resolveTypeIfNeeded(mService.mContext.getContentResolver());
4284 final ResolveInfo info = AppGlobals.getPackageManager()
4285 .resolveIntent(homeIntent, resolvedType, flags, userId);
4286 if (info != null) {
4287 aInfo = info.activityInfo;
4288 }
4289 }
4290 } catch (RemoteException e) {
4291 // ignore
4292 }
4293
4294 if (aInfo == null) {
4295 Slog.wtf(TAG, "No home screen found for " + homeIntent, new Throwable());
4296 return null;
4297 }
4298
4299 homeIntent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
4300 aInfo = new ActivityInfo(aInfo);
4301 aInfo.applicationInfo = mService.getAppInfoForUser(aInfo.applicationInfo, userId);
4302 homeIntent.setFlags(homeIntent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
4303 return aInfo;
4304 }
4305
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004306 @VisibleForTesting
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004307 void addChild(ActivityDisplay activityDisplay, int position) {
4308 positionChildAt(activityDisplay, position);
4309 mWindowContainerController.positionChildAt(
4310 activityDisplay.getWindowContainerController(), position);
4311 }
4312
4313 void removeChild(ActivityDisplay activityDisplay) {
4314 // The caller must tell the controller of {@link ActivityDisplay} to release its container
4315 // {@link DisplayContent}. That is done in {@link ActivityDisplay#releaseSelfIfNeeded}).
4316 mActivityDisplays.remove(activityDisplay);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004317 }
4318
Garfield Tan4a48a7f2018-10-02 14:23:55 -07004319 private void calculateDefaultMinimalSizeOfResizeableTasks() {
4320 final Resources res = mService.mContext.getResources();
4321 final float minimalSize = res.getDimension(
4322 com.android.internal.R.dimen.default_minimal_size_resizable_task);
4323 final DisplayMetrics dm = res.getDisplayMetrics();
4324
4325 mDefaultMinSizeOfResizeableTaskDp = (int) (minimalSize / dm.density);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004326 }
4327
David Stevens9440dc82017-03-16 19:00:20 -07004328 SleepToken createSleepTokenLocked(String tag, int displayId) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004329 final ActivityDisplay display = getActivityDisplay(displayId);
David Stevens9440dc82017-03-16 19:00:20 -07004330 if (display == null) {
4331 throw new IllegalArgumentException("Invalid display: " + displayId);
4332 }
4333
4334 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4335 mSleepTokens.add(token);
4336 display.mAllSleepTokens.add(token);
4337 return token;
4338 }
4339
4340 private void removeSleepTokenLocked(SleepTokenImpl token) {
4341 mSleepTokens.remove(token);
4342
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004343 final ActivityDisplay display = getActivityDisplay(token.mDisplayId);
David Stevens9440dc82017-03-16 19:00:20 -07004344 if (display != null) {
4345 display.mAllSleepTokens.remove(token);
4346 if (display.mAllSleepTokens.isEmpty()) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07004347 mService.updateSleepIfNeededLocked();
David Stevens9440dc82017-03-16 19:00:20 -07004348 }
4349 }
4350 }
4351
Wale Ogunwale68278562017-09-23 17:13:55 -07004352 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004353 final int displayId = stack.mDisplayId;
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004354 final ActivityDisplay display = getActivityDisplay(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004355 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004356 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004357 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004358 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004359 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004360 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004361 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004362 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004363 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004364
4365 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4366 final int numTasks = tasks.size();
4367 int[] taskIds = new int[numTasks];
4368 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004369 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004370 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 for (int i = 0; i < numTasks; ++i) {
4372 final TaskRecord task = tasks.get(i);
4373 taskIds[i] = task.taskId;
4374 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4375 : task.realActivity != null ? task.realActivity.flattenToString()
4376 : task.getTopActivity() != null ? task.getTopActivity().packageName
4377 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004378 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004379 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004380 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004381 }
4382 info.taskIds = taskIds;
4383 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004384 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004385 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004386
4387 final ActivityRecord top = stack.topRunningActivityLocked();
4388 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004389 return info;
4390 }
4391
Wale Ogunwale68278562017-09-23 17:13:55 -07004392 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004393 ActivityStack stack = getStack(stackId);
4394 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004395 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004396 }
4397 return null;
4398 }
4399
Wale Ogunwale68278562017-09-23 17:13:55 -07004400 StackInfo getStackInfo(int windowingMode, int activityType) {
4401 final ActivityStack stack = getStack(windowingMode, activityType);
4402 return (stack != null) ? getStackInfo(stack) : null;
4403 }
4404
Craig Mautner4a1cb222013-12-04 16:14:06 -08004405 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004406 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004407 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004408 final ActivityDisplay display = mActivityDisplays.get(displayNdx);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004409 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4410 final ActivityStack stack = display.getChildAt(stackNdx);
4411 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004412 }
4413 }
4414 return list;
4415 }
4416
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004417 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004418 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004419 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004420 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004421 }
4422
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004423 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004424 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004425 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004426 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004427 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004428 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004429 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004430 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004431 return;
4432 }
4433
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004434 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004435 if (isSecondaryDisplayPreferred) {
4436 final int actualDisplayId = task.getStack().mDisplayId;
4437 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004438 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004439 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004440 if (preferredDisplayId != actualDisplayId) {
Riddle Hsu16567132018-08-16 21:37:47 +08004441 Slog.w(TAG, "Failed to put " + task + " on display " + preferredDisplayId);
4442 // Display a warning toast that we failed to put a task on a secondary display.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004443 mService.getTaskChangeNotificationController()
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004444 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004445 return;
Riddle Hsu16567132018-08-16 21:37:47 +08004446 } else if (!forceNonResizable && handleForcedResizableTask(task,
4447 FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY)) {
4448 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004449 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004450 }
4451
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004452 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004453 // Display a warning toast that we tried to put an app that doesn't support split-screen
4454 // in split-screen.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004455 mService.getTaskChangeNotificationController().notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004456
Andrii Kulianc27916642016-04-12 17:59:27 -07004457 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4458 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004459
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004460 final ActivityStack dockedStack =
4461 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004462 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004463 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004464 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004465 return;
4466 }
4467
Riddle Hsu16567132018-08-16 21:37:47 +08004468 handleForcedResizableTask(task, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN);
4469 }
4470
4471 /**
4472 * @return {@code true} if the top activity of the task is forced to be resizable and the user
4473 * was notified about activity being forced resized.
4474 */
4475 private boolean handleForcedResizableTask(TaskRecord task, int reason) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004476 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004477 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Riddle Hsu16567132018-08-16 21:37:47 +08004478 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004479 final String packageName = topActivity.appInfo.packageName;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004480 mService.getTaskChangeNotificationController().notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004481 task.taskId, reason, packageName);
Riddle Hsu16567132018-08-16 21:37:47 +08004482 return true;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004483 }
Riddle Hsu16567132018-08-16 21:37:47 +08004484 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -08004485 }
4486
Jorim Jaggife89d122015-12-22 16:28:44 +01004487 void activityRelaunchedLocked(IBinder token) {
4488 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004489 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4490 if (r != null) {
4491 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004492 r.setSleeping(true, true);
4493 }
4494 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004495 }
4496
4497 void activityRelaunchingLocked(ActivityRecord r) {
4498 mWindowManager.notifyAppRelaunching(r.appToken);
4499 }
4500
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004501 void logStackState() {
4502 mActivityMetricsLogger.logWindowState();
4503 }
4504
Winson Chung5af42fc2017-03-24 17:11:33 -07004505 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004506 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4507 // end, then ensure that we defer all in between multi-window mode changes
4508 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4509 return;
4510 }
4511
Wale Ogunwale22e25262016-02-01 10:32:02 -08004512 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4513 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004514 if (r.attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004515 mMultiWindowModeChangedActivities.add(r);
4516 }
4517 }
4518
4519 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4520 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4521 }
4522 }
4523
Winson Chung5af42fc2017-03-24 17:11:33 -07004524 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004525 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004526 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004527 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004528 return;
4529 }
4530
Bryce Leef3c6a472017-11-14 14:53:06 -08004531 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004532 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004533
Winson Chungab76bbc2017-08-14 13:33:51 -07004534 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4535 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4536 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004537 if (r.attachedToProcess()) {
Winson Chungab76bbc2017-08-14 13:33:51 -07004538 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004539 // If we are scheduling pip change, then remove this activity from multi-window
4540 // change list as the processing of pip change will make sure multi-window changed
4541 // message is processed in the right order relative to pip changed.
4542 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004543 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004544 }
4545 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004546
Winson Chungab76bbc2017-08-14 13:33:51 -07004547 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4548 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4549 }
4550 }
4551
4552 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4553 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4554 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4555 final ActivityRecord r = task.mActivities.get(i);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004556 if (r.attachedToProcess()) {
Winson Chungab76bbc2017-08-14 13:33:51 -07004557 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004558 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004559 }
4560 }
4561
Tony Mak853304c2016-04-18 15:17:41 +01004562 void setDockedStackMinimized(boolean minimized) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07004563 // Get currently focused stack before setting mIsDockMinimized. We do this because if
4564 // split-screen is active, primary stack will not be focusable (see #isFocusable) while
Andrii Kulian5f750bc2018-07-17 08:57:23 -07004565 // still occluding other stacks. This will cause getTopDisplayFocusedStack() to return null.
4566 final ActivityStack current = getTopDisplayFocusedStack();
Tony Mak853304c2016-04-18 15:17:41 +01004567 mIsDockMinimized = minimized;
Wale Ogunwale17696de2018-03-15 16:41:05 -07004568 if (mIsDockMinimized) {
Wale Ogunwale17696de2018-03-15 16:41:05 -07004569 if (current.inSplitScreenPrimaryWindowingMode()) {
4570 // The primary split-screen stack can't be focused while it is minimize, so move
4571 // focus to something else.
4572 current.adjustFocusToNextFocusableStack("setDockedStackMinimized");
4573 }
4574 }
Tony Mak853304c2016-04-18 15:17:41 +01004575 }
4576
chaviw59b98852017-06-13 12:05:44 -07004577 void wakeUp(String reason) {
4578 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4579 }
4580
4581 /**
4582 * Begin deferring resume to avoid duplicate resumes in one pass.
4583 */
Riddle Hsu402b4402018-11-06 17:23:15 +08004584 void beginDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07004585 mDeferResumeCount++;
4586 }
4587
4588 /**
4589 * End deferring resume and determine if resume can be called.
4590 */
Riddle Hsu402b4402018-11-06 17:23:15 +08004591 void endDeferResume() {
chaviw59b98852017-06-13 12:05:44 -07004592 mDeferResumeCount--;
4593 }
4594
4595 /**
4596 * @return True if resume can be called.
4597 */
4598 private boolean readyToResume() {
4599 return mDeferResumeCount == 0;
4600 }
4601
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004602 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004603
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004604 public ActivityStackSupervisorHandler(Looper looper) {
4605 super(looper);
4606 }
4607
Winson Chung4dabf232017-01-25 13:25:22 -08004608 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004609 synchronized (mService.mGlobalLock) {
Winson Chung4dabf232017-01-25 13:25:22 -08004610 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4611 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004612 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004613 }
4614
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004615 @Override
4616 public void handleMessage(Message msg) {
4617 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004618 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004619 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004620 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4621 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004622 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004623 }
4624 }
4625 } break;
4626 case REPORT_PIP_MODE_CHANGED_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004627 synchronized (mService.mGlobalLock) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004628 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4629 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004630 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4631 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004632 }
4633 }
4634 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004635 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004636 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4637 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004638 // We don't at this point know if the activity is fullscreen,
4639 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004640 activityIdleInternal((ActivityRecord) msg.obj,
4641 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004642 } break;
4643 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004644 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004645 activityIdleInternal((ActivityRecord) msg.obj,
4646 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004647 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004648 case RESUME_TOP_ACTIVITY_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004649 synchronized (mService.mGlobalLock) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08004650 resumeFocusedStacksTopActivitiesLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004651 }
4652 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004653 case SLEEP_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004654 synchronized (mService.mGlobalLock) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07004655 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004656 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004657 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004658 }
4659 }
4660 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004661 case LAUNCH_TIMEOUT_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004662 synchronized (mService.mGlobalLock) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004663 if (mLaunchingActivity.isHeld()) {
4664 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4665 if (VALIDATE_WAKE_LOCK_CALLER
4666 && Binder.getCallingUid() != Process.myUid()) {
4667 throw new IllegalStateException("Calling must be system uid");
4668 }
4669 mLaunchingActivity.release();
4670 }
4671 }
4672 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004673 case LAUNCH_TASK_BEHIND_COMPLETE: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004674 synchronized (mService.mGlobalLock) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004675 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004676 if (r != null) {
4677 handleLaunchTaskBehindCompleteLocked(r);
4678 }
4679 }
4680 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004681
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004682 }
4683 }
4684 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004685
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004686 ActivityStack findStackBehind(ActivityStack stack) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004687 final ActivityDisplay display = getActivityDisplay(stack.mDisplayId);
4688 if (display != null) {
4689 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4690 if (display.getChildAt(i) == stack && i > 0) {
4691 return display.getChildAt(i - 1);
4692 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004693 }
4694 }
4695 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004696 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004697 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004698
Jorim Jaggic69bd222016-03-15 14:38:37 +01004699 /**
4700 * Puts a task into resizing mode during the next app transition.
4701 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004702 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004703 */
Bryce Leed3624e12017-11-30 08:51:45 -08004704 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004705 mResizingTasksDuringAnimation.add(task.taskId);
4706 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004707 }
4708
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004709 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4710 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004711 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004712 final String callingPackage;
4713 final Intent intent;
4714 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004715 int activityType = ACTIVITY_TYPE_UNDEFINED;
4716 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004717 final ActivityOptions activityOptions = options != null
4718 ? options.getOptions(this)
4719 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004720 if (activityOptions != null) {
4721 activityType = activityOptions.getLaunchActivityType();
4722 windowingMode = activityOptions.getLaunchWindowingMode();
4723 }
4724 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004725 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004726 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004727 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004728
Matthew Ng606dd802017-06-05 14:06:32 -07004729 mWindowManager.deferSurfaceLayout();
4730 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004731 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004732 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004733 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004734
Matthew Ng606dd802017-06-05 14:06:32 -07004735 // Defer updating the stack in which recents is until the app transition is done, to
4736 // not run into issues where we still need to draw the task in recents but the
4737 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08004738 deferUpdateRecentsHomeStackBounds();
lumark588a3e82018-07-20 18:53:54 +08004739 // TODO(multi-display): currently recents animation only support default display.
Matthew Ng606dd802017-06-05 14:06:32 -07004740 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004741 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004742
Matthew Ng606dd802017-06-05 14:06:32 -07004743 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004744 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004745 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08004746 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004747 mWindowManager.executeAppTransition();
4748 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004749 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004750 }
4751
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004752 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4753 // We always want to return to the home activity instead of the recents activity
4754 // from whatever is started from the recents activity, so move the home stack
4755 // forward.
Louis Changbd48dca2018-08-29 17:44:34 +08004756 // TODO (b/115289124): Multi-display supports for recents.
4757 getDefaultDisplay().moveHomeStackToFront("startActivityFromRecents");
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004758 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004759
Matthew Ng606dd802017-06-05 14:06:32 -07004760 // If the user must confirm credentials (e.g. when first launching a work app and the
4761 // Work Challenge is present) let startActivityInPackage handle the intercepting.
Wale Ogunwale86b74462018-07-02 08:42:43 -07004762 if (!mService.mAmInternal.shouldConfirmCredentials(task.userId)
Matthew Ng606dd802017-06-05 14:06:32 -07004763 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004764 final ActivityRecord targetActivity = task.getTopActivity();
4765
Bryce Leed3624e12017-11-30 08:51:45 -08004766 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Igor Murashkin212d06c2018-10-22 16:34:39 -07004767 mActivityMetricsLogger.notifyActivityLaunching(task.intent);
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004768 try {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004769 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004770 true /* fromRecents */);
4771 } finally {
4772 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4773 targetActivity);
4774 }
Matthew Ng606dd802017-06-05 14:06:32 -07004775
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004776 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
Bryce Leed3624e12017-11-30 08:51:45 -08004777 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4778 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004779 return ActivityManager.START_TASK_TO_FRONT;
4780 }
Matthew Ng606dd802017-06-05 14:06:32 -07004781 callingPackage = task.mCallingPackage;
4782 intent = task.intent;
4783 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4784 userId = task.userId;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004785 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004786 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07004787 null, 0, 0, options, userId, task, "startActivityFromRecents",
Michal Karpinski201bc0c2018-07-20 15:32:00 +01004788 false /* validateIncomingUser */, null /* originatingPendingIntent */);
Matthew Ng606dd802017-06-05 14:06:32 -07004789 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004790 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
4791 // If we are launching the task in the docked stack, put it into resizing mode so
4792 // the window renders full-screen with the background filling the void. Also only
4793 // call this at the end to make sure that tasks exists on the window manager side.
4794 setResizingDuringAnimation(task);
4795
4796 final ActivityDisplay display = task.getStack().getDisplay();
4797 final ActivityStack topSecondaryStack =
4798 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
4799 if (topSecondaryStack.isActivityTypeHome()) {
Louis Changbd48dca2018-08-29 17:44:34 +08004800 // If the home activity is the top split-screen secondary stack, then the
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004801 // primary split-screen stack is in the minimized mode which means it can't
4802 // receive input keys, so we should move the focused app to the home app so that
4803 // window manager can correctly calculate the focus window that can receive
4804 // input keys.
Louis Changbd48dca2018-08-29 17:44:34 +08004805 display.moveHomeStackToFront(
4806 "startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08004807
4808 // Immediately update the minimized docked stack mode, the upcoming animation
4809 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
4810 // will do the animation to the target bounds
4811 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004812 }
4813 }
Matthew Ng606dd802017-06-05 14:06:32 -07004814 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004815 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004816 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004817
4818 /**
4819 * @return a list of activities which are the top ones in each visible stack. The first
4820 * entry will be the focused activity.
4821 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004822 List<IBinder> getTopVisibleActivities() {
4823 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
Andrii Kulian5f750bc2018-07-17 08:57:23 -07004824 final ActivityStack topFocusedStack = getTopDisplayFocusedStack();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004825 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004826 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +08004827 final ActivityDisplay display = mActivityDisplays.get(i);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004828 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004829 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4830 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004831 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004832 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004833 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004834 if (top != null) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07004835 if (stack == topFocusedStack) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004836 topActivityTokens.add(0, top.appToken);
4837 } else {
4838 topActivityTokens.add(top.appToken);
4839 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004840 }
4841 }
4842 }
4843 }
4844 return topActivityTokens;
4845 }
Bryce Lee4a194382017-04-04 14:32:48 -07004846
4847 /**
4848 * Internal container to store a match qualifier alongside a WaitResult.
4849 */
4850 static class WaitInfo {
4851 private final ComponentName mTargetComponent;
4852 private final WaitResult mResult;
Vishnu Nair132ee832018-09-28 15:00:05 -07004853 /** Time stamp when we started to wait for {@link WaitResult}. */
4854 private final long mStartTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07004855
Vishnu Nair132ee832018-09-28 15:00:05 -07004856 WaitInfo(ComponentName targetComponent, WaitResult result, long startTimeMs) {
Bryce Lee4a194382017-04-04 14:32:48 -07004857 this.mTargetComponent = targetComponent;
4858 this.mResult = result;
Vishnu Nair132ee832018-09-28 15:00:05 -07004859 this.mStartTimeMs = startTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07004860 }
4861
Wale Ogunwale3270f172017-04-26 07:29:42 -07004862 public boolean matches(ComponentName targetComponent) {
4863 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004864 }
4865
4866 public WaitResult getResult() {
4867 return mResult;
4868 }
4869
Vishnu Nair132ee832018-09-28 15:00:05 -07004870 public long getStartTime() {
4871 return mStartTimeMs;
4872 }
4873
Bryce Lee4a194382017-04-04 14:32:48 -07004874 public ComponentName getComponent() {
4875 return mTargetComponent;
4876 }
4877
4878 public void dump(PrintWriter pw, String prefix) {
4879 pw.println(prefix + "WaitInfo:");
4880 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4881 pw.println(prefix + " mResult=");
4882 mResult.dump(pw, prefix);
4883 }
4884 }
David Stevens9440dc82017-03-16 19:00:20 -07004885
4886 private final class SleepTokenImpl extends SleepToken {
4887 private final String mTag;
4888 private final long mAcquireTime;
4889 private final int mDisplayId;
4890
4891 public SleepTokenImpl(String tag, int displayId) {
4892 mTag = tag;
4893 mDisplayId = displayId;
4894 mAcquireTime = SystemClock.uptimeMillis();
4895 }
4896
4897 @Override
4898 public void release() {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07004899 synchronized (mService.mGlobalLock) {
David Stevens9440dc82017-03-16 19:00:20 -07004900 removeSleepTokenLocked(this);
4901 }
4902 }
4903
4904 @Override
4905 public String toString() {
4906 return "{\"" + mTag + "\", display " + mDisplayId
4907 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4908 }
4909 }
4910
Craig Mautner27084302013-03-25 08:05:25 -07004911}