blob: cbf30bdd0cbb4c146d4e6f9b88a299674c15a69c [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
17package com.android.server.am;
18
Andrii Kulian3c9ad072017-08-01 11:45:22 -070019import static android.Manifest.permission.ACTIVITY_EMBEDDING;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010020import static android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS;
Andrii Kulian3a95edc2017-06-28 16:21:07 -070021import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Jorim Jaggife762342016-10-13 14:33:27 +020022import static android.Manifest.permission.START_ANY_ACTIVITY;
23import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Charles Heff9b4dff2017-09-22 10:18:37 +010024import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
Bryce Lee5f0e28f2018-01-30 16:00:03 -080025import static android.app.ActivityManager.START_DELIVERED_TO_TOP;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070028import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
29import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070030import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070032import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
34import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
39import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070040import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070042import static android.app.WindowConfiguration.activityTypeToString;
43import static android.app.WindowConfiguration.windowingModeToString;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070044import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020045import static android.content.pm.PackageManager.PERMISSION_GRANTED;
chaviw59b98852017-06-13 12:05:44 -070046import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010047import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020048import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
49import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070050import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070051import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070052
Winson Chung47900652017-04-06 18:44:25 -070053import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020054import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020057import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020066import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020073import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
74import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
75import static com.android.server.am.ActivityManagerService.ANIMATE;
76import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070086import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020087import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Charles Heff9b4dff2017-09-22 10:18:37 +010089import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080090import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
91import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
92import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070093import static com.android.server.am.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
94import static com.android.server.am.ActivityStackSupervisorProto.DISPLAYS;
95import static com.android.server.am.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
96import static com.android.server.am.ActivityStackSupervisorProto.IS_HOME_RECENTS_COMPONENT;
97import static com.android.server.am.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
98import static com.android.server.am.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010099import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -0700100
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800101import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200102
Svetoslav7008b512015-06-24 18:47:07 -0700103import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800104import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700105import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700106import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100107import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800110import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800111import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700112import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700114import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700115import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700116import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700117import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700118import android.app.WindowConfiguration.ActivityType;
119import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800120import android.app.servertransaction.ActivityLifecycleItem;
121import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700122import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800123import android.app.servertransaction.PauseActivityItem;
124import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700126import android.content.Context;
127import android.content.Intent;
128import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700130import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.content.pm.PackageManager;
132import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100133import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800135import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800136import android.hardware.display.DisplayManager;
137import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800138import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800139import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100141import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700142import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700143import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700145import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700146import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700147import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700148import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700149import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700150import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700151import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700152import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100153import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700154import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700155import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700156import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700157import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700158import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700159import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800160import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700161import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700162import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700165import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700166import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800167import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800168
Andreas Gampea36dc622018-02-05 17:19:22 -0800169import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700170import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800171import com.android.internal.content.ReferrerIntent;
Chenjie Yu52cacc62017-12-08 18:11:45 -0800172import com.android.internal.os.logging.MetricsLoggerWrapper;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700173import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700174import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800175import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700176import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700177import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700178import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700179import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700180
Craig Mautner8d341ef2013-03-26 09:03:27 -0700181import java.io.FileDescriptor;
182import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700183import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800184import java.lang.annotation.Retention;
185import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700186import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700187import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700188import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700189import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700190
Winson Chung1dbc8112017-09-28 18:05:31 -0700191public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
192 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700194 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800202 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800203
Craig Mautnerf3333272013-04-22 10:55:53 -0700204 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700206
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700209
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Craig Mautner05d29032013-05-03 13:40:13 -0700213 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
214 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
215 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700216 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800218 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
219 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
220 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700221 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800222 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
223 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700226
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227 // Used to indicate if an object (e.g. stack) that we are trying to get
228 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800229 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700231 // Used to indicate that windows of activities should be preserved during the resize.
232 static final boolean PRESERVE_WINDOWS = true;
233
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234 // Used to indicate if an object (e.g. task) should be moved/created
235 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700236 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700238 // Don't execute any calls to resume.
239 static final boolean DEFER_RESUME = true;
240
Winson Chung010927a2016-12-15 16:12:35 -0800241 // Used to indicate that a task is removed it should also be removed from recents.
242 static final boolean REMOVE_FROM_RECENTS = true;
243
Winson Chung6954fc92017-03-24 16:22:12 -0700244 // Used to indicate that pausing an activity should occur immediately without waiting for
245 // the activity callback indicating that it has completed pausing
246 static final boolean PAUSE_IMMEDIATELY = true;
247
Winson Chung7900bee2017-03-10 08:59:25 -0800248 /**
249 * The modes which affect which tasks are returned when calling
250 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
251 */
252 @Retention(RetentionPolicy.SOURCE)
253 @IntDef({
254 MATCH_TASK_IN_STACKS_ONLY,
255 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
256 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
257 })
258 public @interface AnyTaskForIdMatchTaskMode {}
259 // Match only tasks in the current stacks
260 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
261 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
262 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
263 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
264 // provided stack id
265 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
266
Svetoslav7008b512015-06-24 18:47:07 -0700267 // Activity actions an app cannot start if it uses a permission which is not granted.
268 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
269 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700270
Svetoslav7008b512015-06-24 18:47:07 -0700271 static {
272 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
273 Manifest.permission.CAMERA);
274 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
275 Manifest.permission.CAMERA);
276 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
277 Manifest.permission.CALL_PHONE);
278 }
279
Svet Ganov99b60432015-06-27 13:15:22 -0700280 /** Action restriction: launching the activity is not restricted. */
281 private static final int ACTIVITY_RESTRICTION_NONE = 0;
282 /** Action restriction: launching the activity is restricted by a permission. */
283 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
284 /** Action restriction: launching the activity is restricted by an app op. */
285 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700286
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700287 // For debugging to make sure the caller when acquiring/releasing our
288 // wake lock is the system process.
289 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800290 /** The number of distinct task ids that can be assigned to the tasks of a single user */
291 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700292
Craig Mautner27084302013-03-25 08:05:25 -0700293 final ActivityManagerService mService;
294
Winson Chung61c9e5a2017-10-11 10:39:32 -0700295 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700296 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800297
Winson Chung61c9e5a2017-10-11 10:39:32 -0700298 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700299 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700300
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700301 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000302 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700303
304 /** Short cut */
305 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700307
Bryce Leeec55eb02017-12-05 20:51:27 -0800308 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700309
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800310 /**
311 * Maps the task identifier that activities are currently being started in to the userId of the
312 * task. Each time a new task is created, the entry for the userId of the task is incremented
313 */
314 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700315
Craig Mautner2420ead2013-04-01 17:13:20 -0700316 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800317 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700318
Craig Mautnere0a38842013-12-16 16:14:02 -0800319 /** The stack containing the launcher app. Assumed to always be attached to
320 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800321 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700322
Craig Mautnere0a38842013-12-16 16:14:02 -0800323 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800324 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700325
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
327 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800328 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800329 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330
331 /** List of activities that are waiting for a new activity to become visible before completing
332 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700333 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
334 // mStoppingActivities when something else comes up.
335 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336
Bryce Lee4a194382017-04-04 14:32:48 -0700337 /** List of processes waiting to find out when a specific activity becomes visible. */
338 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700339
340 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700341 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700342
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343 /** List of activities that are ready to be stopped, but waiting for the next activity to
344 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800345 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346
Craig Mautnerf3333272013-04-22 10:55:53 -0700347 /** List of activities that are ready to be finished, but waiting for the previous activity to
348 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800349 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700350
Craig Mautner0eea92c2013-05-16 13:35:39 -0700351 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800352 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700353
Wale Ogunwale22e25262016-02-01 10:32:02 -0800354 /** List of activities whose multi-window mode changed that we need to report to the
355 * application */
356 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
357
358 /** List of activities whose picture-in-picture mode changed that we need to report to the
359 * application */
360 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
361
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100362 /**
363 * Animations that for the current transition have requested not to
364 * be considered for the transition animation.
365 */
366 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
367
Winson Chung5af42fc2017-03-24 17:11:33 -0700368 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
369 * the application */
370 Rect mPipModeChangedTargetStackBounds;
371
Craig Mautnerf3333272013-04-22 10:55:53 -0700372 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700373 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700374
Craig Mautnerde4ef022013-04-07 19:01:33 -0700375 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
376 * is being brought in front of us. */
377 boolean mUserLeaving = false;
378
Bryce Leed3624e12017-11-30 08:51:45 -0800379 /** Set when a power hint has started, but not ended. */
380 private boolean mPowerHintSent;
381
Craig Mautner0eea92c2013-05-16 13:35:39 -0700382 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700383 * We don't want to allow the device to go to sleep while in the process
384 * of launching an activity. This is primarily to allow alarm intent
385 * receivers to launch an activity and get that to run before the device
386 * goes back to sleep.
387 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700388 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700389
390 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700391 * Set when the system is going to sleep, until we have
392 * successfully paused the current activity and released our wake lock.
393 * At that point the system is allowed to actually sleep.
394 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700395 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700396
David Stevens9440dc82017-03-16 19:00:20 -0700397 /**
398 * A list of tokens that cause the top activity to be put to sleep.
399 * They are used by components that may hide and block interaction with underlying
400 * activities.
401 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700402 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700403
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700404 /** Stack id of the front stack when user switched, indexed by userId. */
405 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700406
Bryce Leeaf3a4002017-03-20 10:37:12 -0700407 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800408 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700409 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800411 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
412
413 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800414
Wale Ogunwaled046a012015-12-24 13:05:59 -0800415 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800416 boolean inResumeTopActivity;
417
Andrii Kulian1e32e022016-09-16 15:29:34 -0700418 /**
419 * Temporary rect used during docked stack resize calculation so we don't need to create a new
420 * object each time.
421 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700422 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700423 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700424
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700425 // The default minimal size that will be used if the activity doesn't specify its minimal size.
426 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700427 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700428
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700429 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
430 private boolean mTaskLayersChanged = true;
431
Bryce Lee2a3cc462017-10-27 10:57:35 -0700432 private ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800433
Jorim Jaggiea039a82017-08-02 14:37:49 +0200434 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
435
Andrii Kulian1779e612016-10-12 21:58:25 -0700436 @Override
437 protected int getChildCount() {
438 return mActivityDisplays.size();
439 }
440
441 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700442 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700443 return mActivityDisplays.valueAt(index);
444 }
445
446 @Override
447 protected ConfigurationContainer getParent() {
448 return null;
449 }
450
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700451 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700452 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700453 if (activityDisplay == null) {
454 throw new IllegalArgumentException("No display found with id: " + displayId);
455 }
456
457 return activityDisplay.getOverrideConfiguration();
458 }
459
460 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700461 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700462 if (activityDisplay == null) {
463 throw new IllegalArgumentException("No display found with id: " + displayId);
464 }
465
466 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
467 }
468
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700469 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700470 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
471 int callingUid, ActivityInfo activityInfo) {
472 if (displayId == DEFAULT_DISPLAY) {
473 // No restrictions for the default display.
474 return true;
475 }
476 if (!mService.mSupportsMultiDisplay) {
477 // Can't launch on secondary displays if feature is not supported.
478 return false;
479 }
480 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
481 // Can't apply wrong configuration to non-resizeable activities.
482 return false;
483 }
484 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
485 // Can't place activities to a display that has restricted launch rules.
486 // In this case the request should be made by explicitly adding target display id and
487 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
488 return false;
489 }
490 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700491 }
492
493 /**
494 * Check if configuration of specified display matches current global config.
495 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
496 * the same config as on the default display.
497 * @param displayId Id of the display to check.
498 * @return {@code true} if configuration matches.
499 */
500 private boolean displayConfigMatchesGlobal(int displayId) {
501 if (displayId == DEFAULT_DISPLAY) {
502 return true;
503 }
504 if (displayId == INVALID_DISPLAY) {
505 return false;
506 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700507 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700508 if (targetDisplay == null) {
509 throw new IllegalArgumentException("No display found with id: " + displayId);
510 }
511 return getConfiguration().equals(targetDisplay.getConfiguration());
512 }
513
Wale Ogunwale39381972015-12-17 17:15:29 -0800514 static class FindTaskResult {
515 ActivityRecord r;
516 boolean matchedByRootAffinity;
517 }
518 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
519
Craig Mautneree36c772014-07-16 14:56:05 -0700520 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800521 * Temp storage for display ids sorted in focus order.
522 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
523 * it's more efficient, as the number of displays is usually small.
524 */
525 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
526
527 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100528 * Used to keep track whether app visibilities got changed since the last pause. Useful to
529 * determine whether to invoke the task stack change listener after pausing.
530 */
531 boolean mAppVisibilitiesChangedSinceLastPause;
532
533 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100534 * Set of tasks that are in resizing mode during an app transition to fill the "void".
535 */
536 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
537
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700538
539 /**
540 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
541 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
542 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
543 * like the docked stack going empty.
544 */
545 private boolean mAllowDockedStackResize = true;
546
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100547 /**
Tony Mak853304c2016-04-18 15:17:41 +0100548 * Is dock currently minimized.
549 */
550 boolean mIsDockMinimized;
551
Bryce Lee2a3cc462017-10-27 10:57:35 -0700552 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200553
chaviw59b98852017-06-13 12:05:44 -0700554 private PowerManager mPowerManager;
555 private int mDeferResumeCount;
556
Bryce Lee2a3cc462017-10-27 10:57:35 -0700557 private boolean mInitialized;
558
Tony Mak853304c2016-04-18 15:17:41 +0100559 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700560 * Description of a request to start a new activity, which has been held
561 * due to app switches being disabled.
562 */
563 static class PendingActivityLaunch {
564 final ActivityRecord r;
565 final ActivityRecord sourceRecord;
566 final int startFlags;
567 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700568 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700569
570 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700571 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700572 r = _r;
573 sourceRecord = _sourceRecord;
574 startFlags = _startFlags;
575 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700576 callerApp = _callerApp;
577 }
578
579 void sendErrorResult(String message) {
580 try {
581 if (callerApp.thread != null) {
582 callerApp.thread.scheduleCrash(message);
583 }
584 } catch (RemoteException e) {
585 Slog.e(TAG, "Exception scheduling crash of failed "
586 + "activity launcher sourceRecord=" + sourceRecord, e);
587 }
Craig Mautneree36c772014-07-16 14:56:05 -0700588 }
589 }
590
Bryce Leeaf691c02017-03-20 14:20:22 -0700591 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700592 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000593 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700594 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700595 }
596
597 public void initialize() {
598 if (mInitialized) {
599 return;
600 }
601
602 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700603 mRunningTasks = createRunningTasks();
Bryce Lee2a3cc462017-10-27 10:57:35 -0700604 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext,
605 mHandler.getLooper());
606 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700607
Bryce Leeec55eb02017-12-05 20:51:27 -0800608 mLaunchParamsController = new LaunchParamsController(mService);
609 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700610 }
611
Bryce Lee2a3cc462017-10-27 10:57:35 -0700612
613 public ActivityMetricsLogger getActivityMetricsLogger() {
614 return mActivityMetricsLogger;
615 }
616
617 public KeyguardController getKeyguardController() {
618 return mKeyguardController;
619 }
620
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800621 void setRecentTasks(RecentTasks recentTasks) {
622 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700623 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800624 }
625
Winson Chung3f0e59a2017-10-25 10:19:05 -0700626 @VisibleForTesting
627 RunningTasks createRunningTasks() {
628 return new RunningTasks();
629 }
630
Jeff Brown2c43c332014-06-12 22:38:59 -0700631 /**
632 * At the time when the constructor runs, the power manager has not yet been
633 * initialized. So we initialize our wakelocks afterwards.
634 */
635 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700636 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
637 mGoingToSleep = mPowerManager
638 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
639 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700640 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700641 }
642
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700643 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 synchronized (mService) {
645 mWindowManager = wm;
Bryce Lee459c0622018-03-19 11:04:01 -0700646 getKeyguardController().setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647
648 mDisplayManager =
649 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
650 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800651 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652
653 Display[] displays = mDisplayManager.getDisplays();
654 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwale45477b52018-03-06 12:24:19 -0800655 final Display display = displays[displayNdx];
656 ActivityDisplay activityDisplay = new ActivityDisplay(this, display);
657 mActivityDisplays.put(display.getDisplayId(), activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700658 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 }
660
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700661 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
662 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 }
Craig Mautner27084302013-03-25 08:05:25 -0700664 }
665
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700666 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700668 }
669
Bryce Lee7b851cc2018-04-10 14:53:13 -0700670 boolean isFocusable(ConfigurationContainer container, boolean alwaysFocusable) {
671 if (container.inSplitScreenPrimaryWindowingMode() && mIsDockMinimized) {
672 return false;
673 }
674
675 return container.getWindowConfiguration().canReceiveKeys() || alwaysFocusable;
676 }
677
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700680 }
681
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700682 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700683 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700684 }
685
Wale Ogunwaled046a012015-12-24 13:05:59 -0800686 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800687 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
688 if (!focusCandidate.isFocusable()) {
689 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -0800690 focusCandidate = getNextFocusableStackLocked(focusCandidate, false /* ignoreCurrent */);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800691 }
692
693 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800694 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800695 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800696
Wale Ogunwaled046a012015-12-24 13:05:59 -0800697 EventLogTags.writeAmFocusedStack(
698 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
699 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
700 }
701
702 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800703 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800704 if (r != null && r.idle) {
705 checkFinishBootingLocked();
706 }
707 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700708 }
709
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700710 void moveHomeStackToFront(String reason) {
711 mHomeStack.moveToFront(reason);
712 }
713
Matthew Ng330757d2017-02-28 14:19:17 -0800714 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700715 final ActivityStack recentsStack = getDefaultDisplay().getStack(
716 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800717 if (recentsStack != null) {
718 recentsStack.moveToFront(reason);
719 }
720 }
721
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700722 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800723 boolean moveHomeStackTaskToTop(String reason) {
724 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700725
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700726 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700727 if (top == null) {
728 return false;
729 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700730 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700731 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700732 }
733
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800734 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700735 if (!mService.mBooting && !mService.mBooted) {
736 // Not ready yet!
737 return false;
738 }
739
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800740 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700741 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800742 final String myReason = reason + " resumeHomeStackTask";
743
Mark Lua56ea122015-10-08 13:31:01 +0800744 // Only resume home activity if isn't finishing.
745 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700746 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800747 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700748 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800749 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700750 }
751
Craig Mautner8d341ef2013-03-26 09:03:27 -0700752 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700753 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
754 }
755
756 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700757 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700758 }
759
760 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700761 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700762 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800763 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700764 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700765 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700766 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700767 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700768 @Nullable ActivityOptions aOptions, boolean onTop) {
769 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700770 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
771 throw new IllegalArgumentException("Should not specify activity options for non-restore"
772 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800773 }
774
Craig Mautnere0a38842013-12-16 16:14:02 -0800775 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700777 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
778 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
779 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700780 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700781 if (task == null) {
782 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700784 if (aOptions != null) {
785 // Resolve the stack the task should be placed in now based on options
786 // and reparent if needed.
787 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
788 if (launchStack != null && stack != launchStack) {
789 final int reparentMode = onTop
790 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
791 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
792 "anyTaskForIdLocked");
793 }
794 }
795 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700796 }
797 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800798
Winson Chung7900bee2017-03-10 08:59:25 -0800799 // If we are matching stack tasks only, return now
800 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800801 return null;
802 }
803
Winson Chung7900bee2017-03-10 08:59:25 -0800804 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
805 // the task from recents
806 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700807 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700808
809 if (task == null) {
810 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800811 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
812 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700813
814 return null;
815 }
816
817 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700818 return task;
819 }
820
Winson Chung7900bee2017-03-10 08:59:25 -0800821 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700822 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700823 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
824 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800825 return null;
826 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700827 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800828 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700829 }
830
Craig Mautner6170f732013-04-02 13:05:23 -0700831 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800832 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700834 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
835 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
836 final ActivityStack stack = display.getChildAt(stackNdx);
837 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 if (r != null) {
839 return r;
840 }
Craig Mautner6170f732013-04-02 13:05:23 -0700841 }
842 }
843 return null;
844 }
845
Tony Mak853304c2016-04-18 15:17:41 +0100846 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000847 * Detects whether we should show a lock screen in front of this task for a locked user.
848 * <p>
849 * We'll do this if either of the following holds:
850 * <ul>
851 * <li>The top activity explicitly belongs to {@param userId}.</li>
852 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
853 * </ul>
854 *
855 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100856 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000857 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
858 // To handle the case that work app is in the task but just is not the top one.
859 final ActivityRecord activityRecord = task.getTopActivity();
860 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
861
862 return (activityRecord != null && activityRecord.userId == userId)
863 || (resultTo != null && resultTo.userId == userId);
864 }
865
866 /**
867 * Find all visible task stacks containing {@param userId} and intercept them with an activity
868 * to block out the contents and possibly start a credential-confirming intent.
869 *
870 * @param userId user handle for the locked managed profile.
871 */
872 void lockAllProfileTasks(@UserIdInt int userId) {
873 mWindowManager.deferSurfaceLayout();
874 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700875 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
876 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
877 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
878 final ActivityStack stack = display.getChildAt(stackNdx);
879 final List<TaskRecord> tasks = stack.getAllTasks();
880 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
881 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000882
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700883 // Check the task for a top activity belonging to userId, or returning a
884 // result to an activity belonging to userId. Example case: a document
885 // picker for personal files, opened by a work app, should still get locked.
886 if (taskTopActivityIsUser(task, userId)) {
887 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
888 task.taskId, userId);
889 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000890 }
Tony Mak853304c2016-04-18 15:17:41 +0100891 }
892 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000893 } finally {
894 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100895 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000896 }
897
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800898 void setNextTaskIdForUserLocked(int taskId, int userId) {
899 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
900 if (taskId > currentTaskId) {
901 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700902 }
903 }
904
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700905 static int nextTaskIdForUser(int taskId, int userId) {
906 int nextTaskId = taskId + 1;
907 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
908 // Wrap around as there will be smaller task ids that are available now.
909 nextTaskId -= MAX_TASK_IDS_PER_USER;
910 }
911 return nextTaskId;
912 }
913
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800914 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800915 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
916 // for a userId u, a taskId can only be in the range
917 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
918 // 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 -0700919 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700920 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700921 || anyTaskForIdLocked(
922 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700923 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800924 if (candidateTaskId == currentTaskId) {
925 // Something wrong!
926 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
927 throw new IllegalStateException("Cannot get an available task id."
928 + " Reached limit of " + MAX_TASK_IDS_PER_USER
929 + " running tasks per user.");
930 }
931 }
932 mCurTaskIdForUser.put(userId, candidateTaskId);
933 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700934 }
935
Chong Zhang6cda19c2016-06-14 19:07:56 -0700936 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800937 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700938 if (stack == null) {
939 return null;
940 }
Bryce Leec4ab62a2018-03-05 14:19:26 -0800941 ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700942 if (resumedActivity == null || resumedActivity.app == null) {
943 resumedActivity = stack.mPausingActivity;
944 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700945 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700946 }
947 }
948 return resumedActivity;
949 }
950
Dianne Hackbornff072722014-09-24 10:56:28 -0700951 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700952 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800953 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700955 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
956 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
957 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700958 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 continue;
960 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200961 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
962 final ActivityRecord top = stack.topRunningActivityLocked();
963 final int size = mTmpActivityList.size();
964 for (int i = 0; i < size; i++) {
965 final ActivityRecord activity = mTmpActivityList.get(i);
966 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
967 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200969 if (realStartActivityLocked(activity, app,
970 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 didSomething = true;
972 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700973 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200975 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700977 }
Craig Mautner20e72272013-04-01 13:45:53 -0700978 }
Craig Mautner20e72272013-04-01 13:45:53 -0700979 }
980 }
981 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700982 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700983 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700984 }
Craig Mautner20e72272013-04-01 13:45:53 -0700985 return didSomething;
986 }
987
988 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700990 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
991 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
992 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700993 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 continue;
995 }
Bryce Leec4ab62a2018-03-05 14:19:26 -0800996 final ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800997 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700998 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800999 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 return false;
1001 }
Craig Mautner20e72272013-04-01 13:45:53 -07001002 }
1003 }
Wei Wang65c7a152016-06-02 18:51:22 -07001004 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -08001005 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001006 return true;
1007 }
1008
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001011 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1012 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1013 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001014 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08001015 final ActivityRecord r = stack.getResumedActivity();
Bryce Lee7ace3952018-02-16 14:34:32 -08001016 if (r != null && !r.isState(RESUMED)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 return false;
1018 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001019 }
1020 }
1021 }
1022 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001023 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001024 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1025 mLastFocusedStack + " to=" + mFocusedStack);
1026 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001027 return true;
1028 }
1029
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001030 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001031 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001033 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1034 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1035 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001036 final ActivityRecord r = stack.getResumedActivity();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001037 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001038 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001039 return false;
1040 }
1041 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001042 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 }
1044 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001045 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001046 }
1047
Craig Mautner2acc3892013-09-23 10:28:14 -07001048 /**
1049 * Pause all activities in either all of the stacks or just the back stacks.
1050 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001051 * @param resuming The resuming activity.
1052 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1053 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001054 * @return true if any activity was paused as a result of this call.
1055 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001056 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001057 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001058 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001059 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1060 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1061 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001062 if (!isFocusedStack(stack) && stack.getResumedActivity() != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001063 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Bryce Leec4ab62a2018-03-05 14:19:26 -08001064 " mResumedActivity=" + stack.getResumedActivity());
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001065 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1066 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001067 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001068 }
1069 }
1070 return someActivityPaused;
1071 }
1072
Craig Mautnerde4ef022013-04-07 19:01:33 -07001073 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001074 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001075 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001076 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1077 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1078 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001079 final ActivityRecord r = stack.mPausingActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08001080 if (r != null && !r.isState(PAUSED, STOPPED, STOPPING)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001081 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001082 Slog.d(TAG_STATES,
Bryce Lee7ace3952018-02-16 14:34:32 -08001083 "allPausedActivitiesComplete: r=" + r + " state=" + r.getState());
Craig Mautner4a1cb222013-12-04 16:14:06 -08001084 pausing = false;
1085 } else {
1086 return false;
1087 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001088 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001089 }
1090 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001091 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001092 }
1093
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001094 void cancelInitializingActivities() {
1095 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001096 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1097 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1098 final ActivityStack stack = display.getChildAt(stackNdx);
1099 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001100 }
1101 }
1102 }
1103
Bryce Lee4a194382017-04-04 14:32:48 -07001104 void waitActivityVisible(ComponentName name, WaitResult result) {
1105 final WaitInfo waitInfo = new WaitInfo(name, result);
1106 mWaitingForActivityVisible.add(waitInfo);
1107 }
1108
1109 void cleanupActivity(ActivityRecord r) {
1110 // Make sure this record is no longer in the pending finishes list.
1111 // This could happen, for example, if we are trimming activities
1112 // down to the max limit while they are still waiting to finish.
1113 mFinishingActivities.remove(r);
1114 mActivitiesWaitingForVisibleActivity.remove(r);
1115
1116 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001117 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001118 mWaitingForActivityVisible.remove(i);
1119 }
1120 }
1121 }
1122
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001125 }
1126
1127 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1128 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001129 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1130 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001131 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001132 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001133 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001134 result.timeout = false;
1135 result.who = w.getComponent();
1136 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1137 result.thisTime = result.totalTime;
1138 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001139 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001140 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001141 if (changed) {
1142 mService.notifyAll();
1143 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001144 }
1145
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001146 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1147 if (mWaitingActivityLaunched.isEmpty()) {
1148 return;
1149 }
1150
1151 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1152 return;
1153 }
1154
Chong Zhang5022da32016-06-21 16:31:37 -07001155 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001156
Chong Zhang5022da32016-06-21 16:31:37 -07001157 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1158 WaitResult w = mWaitingActivityLaunched.remove(i);
1159 if (w.who == null) {
1160 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001161 w.result = result;
1162
1163 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1164 // will be no followup launch signals. Assign the result and launched component.
1165 if (result == START_DELIVERED_TO_TOP) {
1166 w.who = r.realActivity;
1167 }
Chong Zhang5022da32016-06-21 16:31:37 -07001168 }
1169 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001170
Chong Zhang5022da32016-06-21 16:31:37 -07001171 if (changed) {
1172 mService.notifyAll();
1173 }
1174 }
1175
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001176 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1177 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001178 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001179 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001180 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001181 if (w.who == null) {
1182 changed = true;
1183 w.timeout = timeout;
1184 if (r != null) {
1185 w.who = new ComponentName(r.info.packageName, r.info.name);
1186 }
1187 w.thisTime = thisTime;
1188 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001189 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001190 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001191 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001192 if (changed) {
1193 mService.notifyAll();
1194 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001195 }
1196
Craig Mautner29219d92013-04-16 20:19:12 -07001197 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001198 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001199 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001200 if (r != null) {
1201 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001202 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001203
Andrii Kulian7318d632016-07-20 18:59:28 -07001204 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001205 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1206
1207 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1208 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001209 final ActivityDisplay display = mActivityDisplays.get(displayId);
Bryce Lee4bc5ba32018-03-20 13:58:53 -07001210
1211 // If WindowManagerService has encountered the display before we have, ignore as there
1212 // will be no stacks present and therefore no activities.
1213 if (display == null) {
1214 continue;
1215 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001216 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1217 final ActivityStack stack = display.getChildAt(j);
1218 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001219 r = stack.topRunningActivityLocked();
1220 if (r != null) {
1221 return r;
1222 }
Craig Mautner29219d92013-04-16 20:19:12 -07001223 }
1224 }
1225 }
1226 return null;
1227 }
1228
Winson Chung61c9e5a2017-10-11 10:39:32 -07001229 @VisibleForTesting
1230 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1231 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1232 int callingUid, boolean allowed) {
1233 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1234 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001235 }
1236
Todd Kennedy7440f172015-12-09 14:31:22 -08001237 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1238 ProfilerInfo profilerInfo) {
1239 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001240 if (aInfo != null) {
1241 // Store the found target back into the intent, because now that
1242 // we have it we never want to do this again. For example, if the
1243 // user navigates back to this point in the history, we should
1244 // always restart the exact same activity.
1245 intent.setComponent(new ComponentName(
1246 aInfo.applicationInfo.packageName, aInfo.name));
1247
1248 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001249 if (!aInfo.processName.equals("system")) {
1250 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001251 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001252 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001253
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001254 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1255 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1256 }
1257
Man Caocfa78b22015-06-11 20:14:34 -07001258 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1259 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1260 }
1261
1262 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001263 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001264 }
1265 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001266 final String intentLaunchToken = intent.getLaunchToken();
1267 if (aInfo.launchToken == null && intentLaunchToken != null) {
1268 aInfo.launchToken = intentLaunchToken;
1269 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001270 }
1271 return aInfo;
1272 }
1273
Todd Kennedy7440f172015-12-09 14:31:22 -08001274 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Patrick Baumann78380272018-04-04 10:41:01 -07001275 return resolveIntent(intent, resolvedType, userId, 0, Binder.getCallingUid());
Kenny Guyb1b30262016-02-09 16:02:35 +00001276 }
1277
Patrick Baumann78380272018-04-04 10:41:01 -07001278 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
1279 int filterCallingUid) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001280 synchronized (mService) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001281 try {
1282 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001283 int modifiedFlags = flags
1284 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001285 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001286 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1287 modifiedFlags |= PackageManager.MATCH_INSTANT;
1288 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001289 return mService.getPackageManagerInternalLocked().resolveIntent(
Patrick Baumann78380272018-04-04 10:41:01 -07001290 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001291
1292 } finally {
1293 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1294 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001295 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001296 }
1297
1298 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -07001299 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
1300 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -08001301 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1302 }
1303
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001304 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1305 boolean andResume, boolean checkConfig) throws RemoteException {
1306
1307 if (!allPausedActivitiesComplete()) {
1308 // While there are activities pausing we skipping starting any new activities until
1309 // pauses are complete. NOTE: that we also do this for activities that are starting in
1310 // the paused state because they will first be resumed then paused on the client side.
1311 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1312 "realStartActivityLocked: Skipping start of r=" + r
1313 + " some activities pausing...");
1314 return false;
1315 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001316
Bryce Leeaf691c02017-03-20 14:20:22 -07001317 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001318 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001319
1320 beginDeferResume();
1321
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 try {
chaviw59b98852017-06-13 12:05:44 -07001323 r.startFreezingScreenLocked(app, 0);
1324
1325 // schedule launch ticks to collect information about slow apps.
1326 r.startLaunchTickingLocked();
1327
Dianne Hackborn68a06332017-11-15 17:54:18 -08001328 r.setProcess(app);
chaviw59b98852017-06-13 12:05:44 -07001329
Bryce Lee459c0622018-03-19 11:04:01 -07001330 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001331 r.notifyUnknownVisibilityLaunched();
1332 }
1333
chaviw59b98852017-06-13 12:05:44 -07001334 // Have the window manager re-evaluate the orientation of the screen based on the new
1335 // activity order. Note that as a result of this, it can call back into the activity
1336 // manager with a new orientation. We don't care about that, because the activity is
1337 // not currently running so we are just restarting it anyway.
1338 if (checkConfig) {
1339 final int displayId = r.getDisplayId();
1340 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1341 getDisplayOverrideConfiguration(displayId),
1342 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1343 // Deferring resume here because we're going to launch new activity shortly.
1344 // We don't want to perform a redundant launch of the same record while ensuring
1345 // configurations and trying to resume top activity of focused stack.
1346 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1347 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001348 }
chaviw59b98852017-06-13 12:05:44 -07001349
1350 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1351 true /* isTop */)) {
1352 // We only set the visibility to true if the activity is allowed to be visible
1353 // based on
1354 // keyguard state. This avoids setting this into motion in window manager that is
1355 // later cancelled due to later calls to ensure visible activities that set
1356 // visibility back to false.
1357 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 }
chaviw59b98852017-06-13 12:05:44 -07001359
chaviw59b98852017-06-13 12:05:44 -07001360 final int applicationInfoUid =
1361 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1362 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1363 Slog.wtf(TAG,
1364 "User ID for activity changing for " + r
1365 + " appInfo.uid=" + r.appInfo.uid
1366 + " info.ai.uid=" + applicationInfoUid
1367 + " old=" + r.app + " new=" + app);
1368 }
1369
1370 app.waitingToKill = null;
1371 r.launchCount++;
1372 r.lastLaunchTime = SystemClock.uptimeMillis();
1373
1374 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1375
1376 int idx = app.activities.indexOf(r);
1377 if (idx < 0) {
1378 app.activities.add(r);
1379 }
1380 mService.updateLruProcessLocked(app, true, null);
1381 mService.updateOomAdjLocked();
1382
Bryce Lee2b8e0372018-04-05 17:01:37 -07001383 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +01001384 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1385 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1386 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -07001387 && lockTaskController.getLockTaskModeState()
1388 == LOCK_TASK_MODE_LOCKED)) {
1389 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001390 }
1391
1392 try {
1393 if (app.thread == null) {
1394 throw new RemoteException();
1395 }
1396 List<ResultInfo> results = null;
1397 List<ReferrerIntent> newIntents = null;
1398 if (andResume) {
1399 // We don't need to deliver new intents and/or set results if activity is going
1400 // to pause immediately after launch.
1401 results = r.results;
1402 newIntents = r.newIntents;
1403 }
1404 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1405 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1406 + " newIntents=" + newIntents + " andResume=" + andResume);
1407 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1408 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001409 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001410 // Home process is the root process of the task.
1411 mService.mHomeProcess = task.mActivities.get(0).app;
1412 }
1413 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1414 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1415 r.sleeping = false;
1416 r.forceNewConfig = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05001417 mService.getAppWarningsLocked().onStartActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001418 mService.showAskCompatModeDialogLocked(r);
1419 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1420 ProfilerInfo profilerInfo = null;
1421 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1422 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1423 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001424 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1425 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1426 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001427 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001428 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001429 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001430 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001431 }
1432 }
chaviw59b98852017-06-13 12:05:44 -07001433
Andreas Gampe2b073a02017-06-22 17:28:57 -07001434 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001435 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001436 }
1437 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001438
chaviw59b98852017-06-13 12:05:44 -07001439 app.hasShownUi = true;
1440 app.pendingUiClean = true;
1441 app.forceProcessStateUpTo(mService.mTopProcessState);
1442 // Because we could be starting an Activity in the system process this may not go
1443 // across a Binder interface which would create a new Configuration. Consequently
1444 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001445
chaviw59b98852017-06-13 12:05:44 -07001446 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1447 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1448 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001449
chaviw59b98852017-06-13 12:05:44 -07001450 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001451
1452
1453 // Create activity launch transaction.
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001454 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001455 r.appToken);
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001456 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001457 System.identityHashCode(r), r.info,
1458 // TODO: Have this take the merged configuration instead of separate global
1459 // and override configs.
1460 mergedConfiguration.getGlobalConfiguration(),
1461 mergedConfiguration.getOverrideConfiguration(), r.compat,
1462 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001463 r.persistentState, results, newIntents, mService.isNextTransitionForward(),
1464 profilerInfo));
1465
1466 // Set desired final state.
1467 final ActivityLifecycleItem lifecycleItem;
1468 if (andResume) {
Bryce Leed946f862018-01-16 15:59:47 -08001469 lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward())
1470 .setDescription(r.getLifecycleDescription("realStartActivityLocked"));
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001471 } else {
Bryce Leef52974c2018-02-14 15:12:01 -08001472 lifecycleItem = PauseActivityItem.obtain()
1473 .setDescription(r.getLifecycleDescription("realStartActivityLocked"));
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001474 }
1475 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1476
1477 // Schedule transaction.
Bryce Leeb0f993f2018-03-02 15:38:01 -08001478 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001479
Craig Mautner2420ead2013-04-01 17:13:20 -07001480
chaviw59b98852017-06-13 12:05:44 -07001481 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1482 // This may be a heavy-weight process! Note that the package
1483 // manager will ensure that only activity can run in the main
1484 // process of the .apk, which is the only thing that will be
1485 // considered heavy-weight.
1486 if (app.processName.equals(app.info.packageName)) {
1487 if (mService.mHeavyWeightProcess != null
1488 && mService.mHeavyWeightProcess != app) {
1489 Slog.w(TAG, "Starting new heavy weight process " + app
1490 + " when already running "
1491 + mService.mHeavyWeightProcess);
1492 }
1493 mService.mHeavyWeightProcess = app;
1494 Message msg = mService.mHandler.obtainMessage(
1495 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1496 msg.obj = r;
1497 mService.mHandler.sendMessage(msg);
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) {
1503 // This is the second time we failed -- finish activity
1504 // and give up.
1505 Slog.e(TAG, "Second failure launching "
1506 + r.intent.getComponent().flattenToShortString()
1507 + ", giving up", e);
1508 mService.appDiedLocked(app);
1509 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1510 "2nd-crash", false);
1511 return false;
1512 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001513
chaviw59b98852017-06-13 12:05:44 -07001514 // This is the first time we failed -- restart process and
1515 // retry.
1516 r.launchFailed = true;
1517 app.activities.remove(r);
1518 throw e;
1519 }
1520 } finally {
1521 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001522 }
1523
1524 r.launchFailed = false;
1525 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001526 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001527 }
1528
Andrii Kulianb372da62018-01-18 10:46:24 -08001529 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1530 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001531 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001532 // As part of the process of launching, ActivityThread also performs
1533 // a resume.
1534 stack.minimalResumeActivityLocked(r);
1535 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001536 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001537 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001538 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001539 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001540 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001541 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -07001542 }
1543
1544 // Launch the new version setup screen if needed. We do this -after-
1545 // launching the initial activity (that is, home), so that it can have
1546 // a chance to initialize itself while in the background, making the
1547 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001548 if (isFocusedStack(stack)) {
Bryce Leed3624e12017-11-30 08:51:45 -08001549 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001550 }
1551
Dianne Hackborn465fa392014-09-14 14:21:18 -07001552 // Update any services we are bound to that might care about whether
1553 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001554 if (r.app != null) {
1555 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1556 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001557
Craig Mautner2420ead2013-04-01 17:13:20 -07001558 return true;
1559 }
1560
Sudheer Shankafab200f2017-05-17 20:41:53 -07001561 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1562 int extrasSize = 0;
1563 if (intent != null) {
1564 final Bundle extras = intent.getExtras();
1565 if (extras != null) {
1566 extrasSize = extras.getSize();
1567 }
1568 }
1569 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1570 if (extrasSize + icicleSize > 200000) {
1571 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1572 + ", icicle size: " + icicleSize);
1573 }
1574 }
1575
Craig Mautnere79d42682013-04-01 19:01:53 -07001576 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001577 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001578 // Is this activity's application already running?
1579 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001580 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001581
Andrii Kulian02b7a832016-10-06 23:11:56 -07001582 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001583
1584 if (app != null && app.thread != null) {
1585 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001586 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1587 || !"android".equals(r.info.packageName)) {
1588 // Don't add this if it is a platform component that is marked
1589 // to run in multiple processes, because this is actually
1590 // part of the framework so doesn't make sense to track as a
1591 // separate apk in the process.
Patrick Baumannc2def582018-04-04 12:14:15 -07001592 app.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode,
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001593 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001594 }
George Mount2c92c972014-03-20 09:38:23 -07001595 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001596 return;
1597 } catch (RemoteException e) {
1598 Slog.w(TAG, "Exception when starting activity "
1599 + r.intent.getComponent().flattenToShortString(), e);
1600 }
1601
1602 // If a dead object exception was thrown -- fall through to
1603 // restart the application.
1604 }
1605
1606 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001607 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001608 }
1609
Bryce Leed3624e12017-11-30 08:51:45 -08001610 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1611 boolean sendHint = forceSend;
1612
1613 if (!sendHint) {
1614 // If not forced, send power hint when the activity's process is different than the
1615 // current resumed activity.
1616 final ActivityRecord resumedActivity = getResumedActivityLocked();
1617 sendHint = resumedActivity == null
1618 || resumedActivity.app == null
1619 || !resumedActivity.app.equals(targetActivity.app);
1620 }
1621
1622 if (sendHint && mService.mLocalPowerManager != null) {
1623 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 1);
1624 mPowerHintSent = true;
1625 }
1626 }
1627
1628 void sendPowerHintForLaunchEndIfNeeded() {
1629 // Trigger launch power hint if activity is launched
1630 if (mPowerHintSent && mService.mLocalPowerManager != null) {
1631 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 0);
1632 mPowerHintSent = false;
1633 }
1634 }
1635
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001636 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001637 String resultWho, int requestCode, int callingPid, int callingUid,
1638 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001639 ActivityRecord resultRecord, ActivityStack resultStack) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001640 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1641 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001642 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001643 return true;
1644 }
1645 final int componentRestriction = getComponentRestrictionForCallingPackage(
1646 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1647 final int actionRestriction = getActionRestrictionForCallingPackage(
1648 intent.getAction(), callingPackage, callingPid, callingUid);
1649 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1650 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1651 if (resultRecord != null) {
1652 resultStack.sendActivityResultLocked(-1,
1653 resultRecord, resultWho, requestCode,
1654 Activity.RESULT_CANCELED, null);
1655 }
1656 final String msg;
1657 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1658 msg = "Permission Denial: starting " + intent.toString()
1659 + " from " + callerApp + " (pid=" + callingPid
1660 + ", uid=" + callingUid + ")" + " with revoked permission "
1661 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1662 } else if (!aInfo.exported) {
1663 msg = "Permission Denial: starting " + intent.toString()
1664 + " from " + callerApp + " (pid=" + callingPid
1665 + ", uid=" + callingUid + ")"
1666 + " not exported from uid " + aInfo.applicationInfo.uid;
1667 } else {
1668 msg = "Permission Denial: starting " + intent.toString()
1669 + " from " + callerApp + " (pid=" + callingPid
1670 + ", uid=" + callingUid + ")"
1671 + " requires " + aInfo.permission;
1672 }
1673 Slog.w(TAG, msg);
1674 throw new SecurityException(msg);
1675 }
1676
1677 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1678 final String message = "Appop Denial: starting " + intent.toString()
1679 + " from " + callerApp + " (pid=" + callingPid
1680 + ", uid=" + callingUid + ")"
1681 + " requires " + AppOpsManager.permissionToOp(
1682 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1683 Slog.w(TAG, message);
1684 return false;
1685 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1686 final String message = "Appop Denial: starting " + intent.toString()
1687 + " from " + callerApp + " (pid=" + callingPid
1688 + ", uid=" + callingUid + ")"
1689 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1690 Slog.w(TAG, message);
1691 return false;
1692 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001693
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001694 return true;
1695 }
1696
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001697 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001698 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1699 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001700 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1701 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1702
Andrii Kulian02689a72017-07-06 14:28:59 -07001703 if (callingPid == -1 && callingUid == -1) {
1704 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1705 return true;
1706 }
1707
Andrii Kulian62e6f252017-05-30 22:46:53 -07001708 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001709 if (activityDisplay == null) {
1710 Slog.w(TAG, "Launch on display check: display not found");
1711 return false;
1712 }
1713
Andrii Kulian02689a72017-07-06 14:28:59 -07001714 // Check if the caller has enough privileges to embed activities and launch to private
1715 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001716 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001717 callingUid);
1718 if (startAnyPerm == PERMISSION_GRANTED) {
1719 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1720 + " allow launch any on display");
1721 return true;
1722 }
1723
Andrii Kulian8f070292017-09-12 22:56:49 -07001724 // Check if caller is already present on display
1725 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1726
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001727 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1728 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1729 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001730 // Limit launching on virtual displays, because their contents can be read from Surface
1731 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001732 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1733 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1734 + " disallow launch on virtual display for not-embedded activity.");
1735 return false;
1736 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001737 // Check if the caller is allowed to embed activities from other apps.
1738 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001739 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001740 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1741 + " disallow activity embedding without permission.");
1742 return false;
1743 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001744 }
1745
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001746 if (!activityDisplay.isPrivate()) {
1747 // Anyone can launch on a public display.
1748 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1749 + " allow launch on public display");
1750 return true;
1751 }
1752
1753 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001754 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001755 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1756 + " allow launch for owner of the display");
1757 return true;
1758 }
1759
Andrii Kulian8f070292017-09-12 22:56:49 -07001760 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001761 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1762 + " allow launch for caller present on the display");
1763 return true;
1764 }
1765
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001766 Slog.w(TAG, "Launch on display check: denied");
1767 return false;
1768 }
1769
1770 /** Update lists of UIDs that are present on displays and have access to them. */
1771 void updateUIDsPresentOnDisplay() {
1772 mDisplayAccessUIDs.clear();
1773 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1774 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001775 // Only bother calculating the whitelist for private displays
1776 if (activityDisplay.isPrivate()) {
1777 mDisplayAccessUIDs.append(
1778 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1779 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001780 }
1781 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1782 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1783 }
1784
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001785 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001786 final long identity = Binder.clearCallingIdentity();
1787 try {
1788 return UserManager.get(mService.mContext).getUserInfo(userId);
1789 } finally {
1790 Binder.restoreCallingIdentity(identity);
1791 }
1792 }
1793
Svet Ganov99b60432015-06-27 13:15:22 -07001794 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001795 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001796 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1797 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001798 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001799 return ACTIVITY_RESTRICTION_PERMISSION;
1800 }
1801
Christopher Tateff7add02015-08-17 10:23:22 -07001802 if (activityInfo.permission == null) {
1803 return ACTIVITY_RESTRICTION_NONE;
1804 }
1805
Svet Ganov99b60432015-06-27 13:15:22 -07001806 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1807 if (opCode == AppOpsManager.OP_NONE) {
1808 return ACTIVITY_RESTRICTION_NONE;
1809 }
1810
1811 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1812 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001813 if (!ignoreTargetSecurity) {
1814 return ACTIVITY_RESTRICTION_APPOP;
1815 }
Svet Ganov99b60432015-06-27 13:15:22 -07001816 }
1817
1818 return ACTIVITY_RESTRICTION_NONE;
1819 }
1820
Svetoslav7008b512015-06-24 18:47:07 -07001821 private int getActionRestrictionForCallingPackage(String action,
1822 String callingPackage, int callingPid, int callingUid) {
1823 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001824 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001825 }
1826
1827 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1828 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001829 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001830 }
1831
1832 final PackageInfo packageInfo;
1833 try {
1834 packageInfo = mService.mContext.getPackageManager()
1835 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1836 } catch (PackageManager.NameNotFoundException e) {
1837 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001838 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001839 }
1840
1841 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001842 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001843 }
1844
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001845 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001846 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001847 }
1848
1849 final int opCode = AppOpsManager.permissionToOpCode(permission);
1850 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001851 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001852 }
1853
1854 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1855 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001856 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001857 }
1858
Svet Ganov99b60432015-06-27 13:15:22 -07001859 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001860 }
1861
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001862 void setLaunchSource(int uid) {
1863 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1864 }
1865
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001866 void acquireLaunchWakelock() {
1867 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1868 throw new IllegalStateException("Calling must be system uid");
1869 }
1870 mLaunchingActivity.acquire();
1871 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1872 // To be safe, don't allow the wake lock to be held for too long.
1873 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1874 }
1875 }
1876
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001877 /**
1878 * Called when the frontmost task is idle.
1879 * @return the state of mService.mBooting before this was called.
1880 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001881 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001882 private boolean checkFinishBootingLocked() {
1883 final boolean booting = mService.mBooting;
1884 boolean enableScreen = false;
1885 mService.mBooting = false;
1886 if (!mService.mBooted) {
1887 mService.mBooted = true;
1888 enableScreen = true;
1889 }
1890 if (booting || enableScreen) {
1891 mService.postFinishBooting(booting, enableScreen);
1892 }
1893 return booting;
1894 }
1895
Craig Mautnerf3333272013-04-22 10:55:53 -07001896 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001897 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001899 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001900 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001901
Craig Mautnerf3333272013-04-22 10:55:53 -07001902 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001903 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001904 int NS = 0;
1905 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001907 boolean activityRemoved = false;
1908
Wale Ogunwale7d701172015-03-11 15:36:30 -07001909 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001910 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001911 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1912 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001913 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1914 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001915 if (fromTimeout) {
1916 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001917 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001918
1919 // This is a hack to semi-deal with a race condition
1920 // in the client where it can be constructed with a
1921 // newer configuration from when we asked it to launch.
1922 // We'll update with whatever configuration it now says
1923 // it used to launch.
1924 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001925 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001926 }
1927
1928 // We are now idle. If someone is waiting for a thumbnail from
1929 // us, we can now deliver.
1930 r.idle = true;
1931
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001932 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001933 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001934 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001935 }
1936 }
1937
1938 if (allResumedActivitiesIdle()) {
1939 if (r != null) {
1940 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001941 }
1942
1943 if (mLaunchingActivity.isHeld()) {
1944 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1945 if (VALIDATE_WAKE_LOCK_CALLER &&
1946 Binder.getCallingUid() != Process.myUid()) {
1947 throw new IllegalStateException("Calling must be system uid");
1948 }
1949 mLaunchingActivity.release();
1950 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001951 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001952 }
1953
1954 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001955 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1956 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001957 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001958 if ((NF = mFinishingActivities.size()) > 0) {
1959 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001960 mFinishingActivities.clear();
1961 }
1962
Craig Mautnerf3333272013-04-22 10:55:53 -07001963 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001964 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001965 mStartingUsers.clear();
1966 }
1967
Craig Mautnerf3333272013-04-22 10:55:53 -07001968 // Stop any activities that are scheduled to do so but have been
1969 // waiting for the next one to start.
1970 for (int i = 0; i < NS; i++) {
1971 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001972 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001973 if (stack != null) {
1974 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08001975 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
1976 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07001977 } else {
1978 stack.stopActivityLocked(r);
1979 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001980 }
1981 }
1982
1983 // Finish any activities that are scheduled to do so but have been
1984 // waiting for the next one to start.
1985 for (int i = 0; i < NF; i++) {
1986 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001987 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001988 if (stack != null) {
1989 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1990 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001991 }
1992
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001993 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001994 // Complete user switch
1995 if (startingUsers != null) {
1996 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001997 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001998 }
1999 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002000 }
2001
2002 mService.trimApplications();
2003 //dump();
2004 //mWindowManager.dump();
2005
Craig Mautnerf3333272013-04-22 10:55:53 -07002006 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002007 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002008 }
2009
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002010 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002011 }
2012
Craig Mautner8e569572013-10-11 17:36:59 -07002013 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002014 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002015 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002016 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2017 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2018 final ActivityStack stack = display.getChildAt(stackNdx);
2019 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002020 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002021 }
Craig Mautner19091252013-10-05 00:03:53 -07002022 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002023 }
2024
2025 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002026 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002027 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2028 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2029 final ActivityStack stack = display.getChildAt(stackNdx);
2030 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002031 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002032 }
2033 }
2034
Craig Mautner93529a42013-10-04 15:03:13 -07002035 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002036 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002037 }
2038
Craig Mautner8d341ef2013-03-26 09:03:27 -07002039 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002040 * Update the last used stack id for non-current user (current user's last
2041 * used stack is the focused stack)
2042 */
2043 void updateUserStackLocked(int userId, ActivityStack stack) {
2044 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002045 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002046 }
2047 }
2048
2049 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002050 * @return true if some activity was finished (or would have finished if doit were true).
2051 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002052 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2053 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002054 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002055 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002056 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2057 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2058 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002059 if (stack.finishDisabledPackageActivitiesLocked(
2060 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002061 didSomething = true;
2062 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002063 }
2064 }
2065 return didSomething;
2066 }
2067
Dianne Hackborna413dc02013-07-12 12:02:55 -07002068 void updatePreviousProcessLocked(ActivityRecord r) {
2069 // Now that this process has stopped, we may want to consider
2070 // it to be the previous app to try to keep around in case
2071 // the user wants to return to it.
2072
2073 // First, found out what is currently the foreground app, so that
2074 // we don't blow away the previous app if this activity is being
2075 // hosted by the process that is actually still the foreground.
2076 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002078 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2079 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2080 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002081 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002082 final ActivityRecord resumedActivity = stack.getResumedActivity();
2083 if (resumedActivity != null) {
2084 fgApp = resumedActivity.app;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002085 } else if (stack.mPausingActivity != null) {
2086 fgApp = stack.mPausingActivity.app;
2087 }
2088 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002089 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002090 }
2091 }
2092
2093 // Now set this one as the previous process, only if that really
2094 // makes sense to.
2095 if (r.app != null && fgApp != null && r.app != fgApp
2096 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002097 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002098 mService.mPreviousProcess = r.app;
2099 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2100 }
2101 }
2102
Wale Ogunwaled046a012015-12-24 13:05:59 -08002103 boolean resumeFocusedStackTopActivityLocked() {
2104 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002105 }
2106
Wale Ogunwaled046a012015-12-24 13:05:59 -08002107 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002108 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002109
2110 if (!readyToResume()) {
2111 return false;
2112 }
2113
Wale Ogunwaled046a012015-12-24 13:05:59 -08002114 if (targetStack != null && isFocusedStack(targetStack)) {
2115 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002116 }
chaviw59b98852017-06-13 12:05:44 -07002117
Wale Ogunwale06579d62016-04-30 15:29:06 -07002118 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08002119 if (r == null || !r.isState(RESUMED)) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07002120 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
Bryce Lee7ace3952018-02-16 14:34:32 -08002121 } else if (r.isState(RESUMED)) {
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002122 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2123 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002124 }
chaviw59b98852017-06-13 12:05:44 -07002125
Wale Ogunwaled046a012015-12-24 13:05:59 -08002126 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002127 }
2128
Todd Kennedy39bfee52016-02-24 10:28:21 -08002129 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2130 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002131 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2132 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2133 final ActivityStack stack = display.getChildAt(stackNdx);
2134 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002135 }
2136 }
2137 }
2138
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002139 /**
2140 * Finish the topmost activities in all stacks that belong to the crashed app.
2141 * @param app The app that crashed.
2142 * @param reason Reason to perform this action.
2143 * @return The task that was finished in this stack, {@code null} if haven't found any.
2144 */
2145 TaskRecord finishTopCrashedActivitiesLocked(ProcessRecord app, String reason) {
Adrian Roos20d7df32016-01-12 18:59:43 +01002146 TaskRecord finishedTask = null;
2147 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002148 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002149 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002150 // It is possible that request to finish activity might also remove its task and stack,
2151 // so we need to be careful with indexes in the loop and check child count every time.
2152 for (int stackNdx = 0; stackNdx < display.getChildCount(); ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002153 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002154 final TaskRecord t = stack.finishTopCrashedActivityLocked(app, reason);
Adrian Roos20d7df32016-01-12 18:59:43 +01002155 if (stack == focusedStack || finishedTask == null) {
2156 finishedTask = t;
2157 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002158 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002159 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002160 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002161 }
2162
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002163 void finishVoiceTask(IVoiceInteractionSession session) {
2164 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002165 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2166 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002167 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002168 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002169 stack.finishVoiceTask(session);
2170 }
2171 }
2172 }
2173
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002174 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2175 boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002176 final ActivityStack currentStack = task.getStack();
2177 if (currentStack == null) {
2178 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2179 + task + " to front. Stack is null");
2180 return;
2181 }
2182
Craig Mautneraea74a52014-03-08 14:23:10 -08002183 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2184 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002185 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002186
2187 final ActivityRecord prev = topRunningActivityLocked();
2188
2189 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2190 || (prev != null && prev.isActivityTypeRecents())) {
2191 // Caller wants the home activity moved with it or the previous task is recents in which
2192 // case we always return home from the task we are moving to the front.
2193 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002194 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002195
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002196 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002197 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002198 task.updateOverrideConfiguration(bounds);
2199
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002200 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002201
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002202 if (stack != currentStack) {
2203 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002204 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002205 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002206 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2207 // still need moveTaskToFrontLocked() below for any transition settings.
2208 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002209 if (stack.resizeStackWithLaunchBounds()) {
2210 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002211 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2212 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002213 } else {
2214 // WM resizeTask must be done after the task is moved to the correct stack,
2215 // because Task's setBounds() also updates dim layer's bounds, but that has
2216 // dependency on the stack.
2217 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002218 }
2219 }
2220
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002221 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07002222 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002223 r == null ? null : r.appTimeTracker, reason);
2224
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002225 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002226 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002227
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002228 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002229 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002230 }
2231
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002232 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002233 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002234 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002235 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002236 return false;
2237 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002238 return (mService.mSupportsPictureInPicture
2239 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002240 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002241 }
2242
Bryce Leeec55eb02017-12-05 20:51:27 -08002243 LaunchParamsController getLaunchParamsController() {
2244 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002245 }
2246
Winson Chung55893332017-02-17 17:13:10 -08002247 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002248 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2249 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2250 if (stack != null) {
2251 return stack;
2252 }
2253 }
2254 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002255 }
2256
Wale Ogunwale68278562017-09-23 17:13:55 -07002257 /** @see ActivityDisplay#getStack(int, int) */
2258 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2259 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2260 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2261 if (stack != null) {
2262 return stack;
2263 }
2264 }
2265 return null;
2266 }
2267
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002268 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002269 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002270 // Preference is given to the activity type for the activity then the task since the type
2271 // once set shouldn't change.
2272 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2273 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2274 activityType = task.getActivityType();
2275 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002276 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2277 return activityType;
2278 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002279 if (options != null) {
2280 activityType = options.getLaunchActivityType();
2281 }
2282 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002283 }
2284
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002285 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2286 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2287 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002288 }
2289
2290 /**
2291 * Returns the right stack to use for launching factoring in all the input parameters.
2292 *
2293 * @param r The activity we are trying to launch. Can be null.
2294 * @param options The activity options used to the launch. Can be null.
2295 * @param candidateTask The possible task the activity might be launched in. Can be null.
2296 *
2297 * @return The stack to use for the launch or INVALID_STACK_ID.
2298 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002299 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2300 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2301 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002302 int taskId = INVALID_TASK_ID;
2303 int displayId = INVALID_DISPLAY;
2304 //Rect bounds = null;
2305
2306 // We give preference to the launch preference in activity options.
2307 if (options != null) {
2308 taskId = options.getLaunchTaskId();
2309 displayId = options.getLaunchDisplayId();
2310 // TODO: Need to work this into the equation...
2311 //bounds = options.getLaunchBounds();
2312 }
2313
2314 // First preference for stack goes to the task Id set in the activity options. Use the stack
2315 // associated with that if possible.
2316 if (taskId != INVALID_TASK_ID) {
2317 // Temporarily set the task id to invalid in case in re-entry.
2318 options.setLaunchTaskId(INVALID_TASK_ID);
2319 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002320 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002321 options.setLaunchTaskId(taskId);
2322 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002323 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002324 }
2325 }
2326
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002327 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002328 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002329
2330 // Next preference for stack goes to the display Id set in the activity options or the
2331 // candidate display.
2332 if (displayId == INVALID_DISPLAY) {
2333 displayId = candidateDisplayId;
2334 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002335 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002336 if (r != null) {
2337 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002338 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002339 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002340 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002341 }
2342 }
2343 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2344 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002345 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002346 if (stack != null) {
2347 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002348 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002349 }
2350 }
2351
2352 // Give preference to the stack and display of the input task and activity if they match the
2353 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002354 stack = null;
2355 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002356 if (candidateTask != null) {
2357 stack = candidateTask.getStack();
2358 }
2359 if (stack == null && r != null) {
2360 stack = r.getStack();
2361 }
2362 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002363 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002364 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002365 final int windowingMode =
2366 display.resolveWindowingMode(r, options, candidateTask, activityType);
2367 if (stack.isCompatible(windowingMode, activityType)) {
2368 return stack;
2369 }
2370 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002371 }
2372
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002373 if (display == null
Andrii Kulianb850ea52017-12-12 23:49:10 -08002374 || !canLaunchOnDisplay(r, display.mDisplayId)
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002375 // TODO: Can be removed once we figure-out how non-standard types should launch
2376 // outside the default display.
2377 || (activityType != ACTIVITY_TYPE_STANDARD
2378 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2379 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002380 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002381
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002382 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002383 }
2384
Andrii Kulianb850ea52017-12-12 23:49:10 -08002385 /** @return true if activity record is null or can be launched on provided display. */
2386 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2387 if (r == null) {
2388 return true;
2389 }
2390 return r.canBeLaunchedOnDisplay(displayId);
2391 }
2392
Andrii Kulian16802aa2016-11-02 12:21:33 -07002393 /**
2394 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2395 * If there is no such stack, new dynamic stack can be created.
2396 * @param displayId Target display.
2397 * @param r Activity that should be launched there.
2398 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2399 */
2400 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002401 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002402 if (activityDisplay == null) {
2403 throw new IllegalArgumentException(
2404 "Display with displayId=" + displayId + " not found.");
2405 }
2406
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002407 if (!r.canBeLaunchedOnDisplay(displayId)) {
2408 return null;
2409 }
2410
Andrii Kulian16802aa2016-11-02 12:21:33 -07002411 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002412 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2413 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002414 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002415 return stack;
2416 }
2417 }
2418
2419 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002420 if (displayId != DEFAULT_DISPLAY) {
2421 return activityDisplay.createStack(
2422 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002423 }
2424
2425 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2426 return null;
2427 }
2428
Wale Ogunwale68278562017-09-23 17:13:55 -07002429 // TODO: Can probably be consolidated into getLaunchStack()...
2430 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2431 switch (stack.getActivityType()) {
2432 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2433 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2434 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002435 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002436 switch (stack.getWindowingMode()) {
2437 case WINDOWING_MODE_FULLSCREEN: return true;
2438 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2439 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2440 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2441 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2442 }
2443
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002444 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002445 return r.canBeLaunchedOnDisplay(displayId);
2446 }
2447 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2448 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002449 }
2450
Andrii Kulian7fc22812016-12-28 13:04:11 -08002451 /**
2452 * Get next focusable stack in the system. This will search across displays and stacks
2453 * in last-focused order for a focusable and visible stack, different from the target stack.
2454 *
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002455 * @param currentFocus The stack that previously had focus.
2456 * @param ignoreCurrent If we should ignore {@param currentFocus} when searching for next
2457 * candidate.
Andrii Kulian7fc22812016-12-28 13:04:11 -08002458 * @return Next focusable {@link ActivityStack}, null if not found.
2459 */
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002460 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus, boolean ignoreCurrent) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002461 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2462
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002463 final int currentWindowingMode = currentFocus != null
2464 ? currentFocus.getWindowingMode() : WINDOWING_MODE_UNDEFINED;
2465 ActivityStack candidate = null;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002466 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2467 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002468 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002469 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2470 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2471 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002472 if (ignoreCurrent && stack == currentFocus) {
2473 continue;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002474 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002475 if (!stack.isFocusable() || !stack.shouldBeVisible(null)) {
2476 continue;
2477 }
2478
2479 if (currentWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
2480 && candidate == null && stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale17696de2018-03-15 16:41:05 -07002481 // If the currently focused stack is in split-screen secondary we save off the
2482 // top primary split-screen stack as a candidate for focus because we might
2483 // prefer focus to move to an other stack to avoid primary split-screen stack
2484 // overlapping with a fullscreen stack when a fullscreen stack is higher in z
2485 // than the next split-screen stack. Assistant stack, I am looking at you...
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002486 // We only move the focus to the primary-split screen stack if there isn't a
2487 // better alternative.
2488 candidate = stack;
2489 continue;
2490 }
Wale Ogunwale17696de2018-03-15 16:41:05 -07002491 if (candidate != null && stack.inSplitScreenSecondaryWindowingMode()) {
2492 // Use the candidate stack since we are now at the secondary split-screen.
2493 return candidate;
2494 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002495 return stack;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002496 }
2497 }
2498
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002499 return candidate;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002500 }
2501
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002502 /**
2503 * Get next valid stack for launching provided activity in the system. This will search across
2504 * displays and stacks in last-focused order for a focusable and visible stack, except those
2505 * that are on a currently focused display.
2506 *
2507 * @param r The activity that is being launched.
2508 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2509 * searching for the next candidate.
2510 * @return Next valid {@link ActivityStack}, null if not found.
2511 */
2512 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2513 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2514 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2515 final int displayId = mTmpOrderedDisplayIds.get(i);
2516 if (displayId == currentFocus) {
2517 continue;
2518 }
2519 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2520 if (stack != null) {
2521 return stack;
2522 }
2523 }
2524 return null;
2525 }
2526
Craig Mautneree2e45a2014-06-27 12:10:03 -07002527 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002528 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002529 }
2530
2531 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002532 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2533 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2534 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002535 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002536 final ArrayList<ActivityRecord> activities = task.mActivities;
2537 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2538 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002539 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002540 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002541 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002542 }
2543 }
2544 }
2545 }
2546 return null;
2547 }
2548
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002549 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2550 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2551 boolean deferResume) {
2552
2553 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002554 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002555 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002556 return;
2557 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002558
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002559 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002560 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002561 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002562 // If the docked stack exists, don't resize non-floating stacks independently of the
2563 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002564 return;
2565 }
2566
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002567 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002568 mWindowManager.deferSurfaceLayout();
2569 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002570 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002571 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2572 // null bounds = fullscreen windowing mode...at least for now.
2573 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2574 } else if (splitScreenActive) {
2575 // If we are in split-screen mode and this stack support split-screen, then
2576 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2577 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2578 }
2579 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002580 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002581 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002582 stack.ensureVisibleActivitiesConfigurationLocked(
2583 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002584 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002585 } finally {
2586 mWindowManager.continueSurfaceLayout();
2587 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002588 }
2589 }
2590
Winson Chungc1674272018-02-21 10:15:17 -08002591 void deferUpdateRecentsHomeStackBounds() {
2592 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
2593 deferUpdateBounds(ACTIVITY_TYPE_HOME);
2594 }
2595
Bryce Leed3624e12017-11-30 08:51:45 -08002596 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002597 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002598 if (stack != null) {
2599 stack.deferUpdateBounds();
2600 }
2601 }
2602
Winson Chungc1674272018-02-21 10:15:17 -08002603 void continueUpdateRecentsHomeStackBounds() {
2604 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
2605 continueUpdateBounds(ACTIVITY_TYPE_HOME);
2606 }
2607
Bryce Leed3624e12017-11-30 08:51:45 -08002608 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002609 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002610 if (stack != null) {
2611 stack.continueUpdateBounds();
2612 }
2613 }
2614
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002615 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08002616 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002617 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2618 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002619 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002620 if (task != null) {
2621 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002622 }
2623 }
2624 mResizingTasksDuringAnimation.clear();
2625 }
2626
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002627 /**
2628 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002629 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002630 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002631 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002632 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002633
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002634 mWindowManager.deferSurfaceLayout();
2635 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002636 final int windowingMode = fromStack.getWindowingMode();
2637 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2638 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2639
2640 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002641 // Tell the display we are exiting split-screen mode.
2642 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002643 // We are moving all tasks from the docked stack to the fullscreen stack,
2644 // which is dismissing the docked stack, so resize all other stacks to
2645 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002646 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2647 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002648 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2649 continue;
2650 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002651 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002652 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002653 }
2654
2655 // Also disable docked stack resizing since we have manually adjusted the
2656 // size of other stacks above and we don't want to trigger a docked stack
2657 // resize when we remove task from it below and it is detached from the
2658 // display because it no longer contains any tasks.
2659 mAllowDockedStackResize = false;
2660 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002661
Winson Chung5af42fc2017-03-24 17:11:33 -07002662 // If we are moving from the pinned stack, then the animation takes care of updating
2663 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002664 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002665 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002666
Wale Ogunwale388945c2017-10-04 12:13:46 -07002667 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002668 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002669 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002670 for (int i = 0; i < size; ++i) {
2671 final TaskRecord task = tasks.get(i);
2672 final ActivityStack toStack = toDisplay.getOrCreateStack(
2673 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002674
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002675 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002676 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002677 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002678 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002679 isTopTask /* animate */, DEFER_RESUME,
2680 schedulePictureInPictureModeChange,
2681 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08002682 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
2683 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002684 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002685 // Position the tasks in the fullscreen stack in order at the bottom of the
2686 // stack. Also defer resume until all the tasks have been moved to the
2687 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002688 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002689 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2690 schedulePictureInPictureModeChange,
2691 "moveTasksToFullscreenStack - NOT_onTop");
2692 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002693 }
2694 }
Winson Chung74666102017-02-22 17:49:24 -08002695
2696 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2697 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002698 } finally {
2699 mAllowDockedStackResize = true;
2700 mWindowManager.continueSurfaceLayout();
2701 }
2702 }
2703
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002704 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002705 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2706 }
2707
2708 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2709 mWindowManager.inSurfaceTransaction(() ->
2710 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002711 }
2712
Jorim Jaggidc249c42015-12-15 14:57:31 -08002713 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002714 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2715 boolean preserveWindows) {
2716 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2717 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2718 false /* deferResume */);
2719 }
2720
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002721 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002722 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2723 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002724
2725 if (!mAllowDockedStackResize) {
2726 // Docked stack resize currently disabled.
2727 return;
2728 }
2729
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002730 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002731 if (stack == null) {
2732 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2733 return;
2734 }
2735
2736 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2737 mWindowManager.deferSurfaceLayout();
2738 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002739 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2740 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002741 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002742 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002743
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002744 // TODO: Checking for isAttached might not be needed as if the user passes in null
2745 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002746 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2747 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002748 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002749 // In this case we make all other static stacks fullscreen and move all
2750 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002751 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002752
2753 // stack shouldn't contain anymore activities, so nothing to resume.
2754 r = null;
2755 } else {
2756 // Docked stacks occupy a dedicated region on screen so the size of all other
2757 // static stacks need to be adjusted so they don't overlap with the docked stack.
2758 // We get the bounds to use from window manager which has been adjusted for any
2759 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002760 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002761 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002762 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2763 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002764 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002765 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002766 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002767 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002768 continue;
2769 }
2770 // Need to set windowing mode here before we try to get the dock bounds.
2771 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2772 current.getStackDockedModeBounds(
2773 tempOtherTaskBounds /* currentTempTaskBounds */,
2774 tempRect /* outStackBounds */,
2775 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2776
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002777 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002778 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2779 tempOtherTaskInsetBounds, preserveWindows,
2780 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002781 }
2782 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002783 if (!deferResume) {
2784 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2785 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002786 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002787 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002788 mWindowManager.continueSurfaceLayout();
2789 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2790 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002791 }
2792
Robert Carr0d00c2e2016-02-29 17:45:02 -08002793 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002794 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002795 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002796 if (stack == null) {
2797 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2798 return;
2799 }
Winson Chung19953ca2017-04-11 11:19:23 -07002800
2801 // It is possible for the bounds animation from the WM to call this but be delayed by
2802 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2803 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2804 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2805 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002806 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002807 return;
2808 }
2809
Robert Carr0d00c2e2016-02-29 17:45:02 -08002810 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2811 mWindowManager.deferSurfaceLayout();
2812 try {
2813 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002814 Rect insetBounds = null;
2815 if (tempPinnedTaskBounds != null) {
2816 // We always use 0,0 as the position for the inset rect because
2817 // if we are getting insets at all in the pinned stack it must mean
2818 // we are headed for fullscreen.
2819 insetBounds = tempRect;
2820 insetBounds.top = 0;
2821 insetBounds.left = 0;
2822 insetBounds.right = tempPinnedTaskBounds.width();
2823 insetBounds.bottom = tempPinnedTaskBounds.height();
2824 }
2825 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002826 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002827 } finally {
2828 mWindowManager.continueSurfaceLayout();
2829 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2830 }
2831 }
2832
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002833 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002834 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002835 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002836 /**
2837 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2838 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2839 * that the client receives onStop() before it is reparented. We do this by detaching
2840 * the stack from the display so that it will be considered invisible when
2841 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2842 * invisible as well and added to the stopping list. After which we process the
2843 * stopping list by handling the idle.
2844 */
2845 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2846 pinnedStack.mForceHidden = true;
2847 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2848 pinnedStack.mForceHidden = false;
2849 activityIdleInternalLocked(null, false /* fromTimeout */,
2850 true /* processPausingActivites */, null /* configuration */);
2851
2852 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002853 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002854 } else {
2855 for (int i = tasks.size() - 1; i >= 0; i--) {
2856 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07002857 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08002858 }
2859 }
2860 }
2861
2862 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002863 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002864 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2865 * instead moved back onto the fullscreen stack.
2866 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002867 void removeStack(ActivityStack stack) {
2868 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002869 }
2870
Wale Ogunwale68278562017-09-23 17:13:55 -07002871 /**
2872 * Removes stacks in the input windowing modes from the system if they are of activity type
2873 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2874 */
2875 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002876 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002877 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2878 }
2879 }
2880
2881 void removeStacksWithActivityTypes(int... activityTypes) {
2882 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2883 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002884 }
Robert Carr6914f082017-03-20 19:04:30 -07002885 }
2886
2887 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002888 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2889 */
Winson Chung0ec2a352017-10-26 11:38:30 -07002890 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2891 String reason) {
2892 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
2893 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07002894 }
2895
2896 /**
Winson Chung010927a2016-12-15 16:12:35 -08002897 * Removes the task with the specified task id.
2898 *
2899 * @param taskId Identifier of the task to be removed.
2900 * @param killProcess Kill any process associated with the task if possible.
2901 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002902 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2903 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002904 * @return Returns true if the given task was found and removed.
2905 */
Winson Chung6954fc92017-03-24 16:22:12 -07002906 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07002907 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002908 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002909 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07002910 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08002911 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Bryce Lee2b8e0372018-04-05 17:01:37 -07002912 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002913 if (tr.isPersistable) {
2914 mService.notifyTaskPersisterLocked(null, true);
2915 }
2916 return true;
2917 }
2918 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2919 return false;
2920 }
2921
2922 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2923 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002924 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002925 }
2926 ComponentName component = tr.getBaseIntent().getComponent();
2927 if (component == null) {
2928 Slog.w(TAG, "No component for base intent of task: " + tr);
2929 return;
2930 }
2931
2932 // Find any running services associated with this app and stop if needed.
2933 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2934
2935 if (!killProcess) {
2936 return;
2937 }
2938
2939 // Determine if the process(es) for this task should be killed.
2940 final String pkg = component.getPackageName();
2941 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2942 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2943 for (int i = 0; i < pmap.size(); i++) {
2944
2945 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2946 for (int j = 0; j < uids.size(); j++) {
2947 ProcessRecord proc = uids.valueAt(j);
2948 if (proc.userId != tr.userId) {
2949 // Don't kill process for a different user.
2950 continue;
2951 }
2952 if (proc == mService.mHomeProcess) {
2953 // Don't kill the home process along with tasks from the same package.
2954 continue;
2955 }
2956 if (!proc.pkgList.containsKey(pkg)) {
2957 // Don't kill process that is not associated with this task.
2958 continue;
2959 }
2960
2961 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002962 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002963 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2964 // Don't kill process(es) that has an activity in a different task that is
2965 // also in recents.
2966 return;
2967 }
2968 }
2969
2970 if (proc.foregroundServices) {
2971 // Don't kill process(es) with foreground service.
2972 return;
2973 }
2974
2975 // Add process to kill list.
2976 procsToKill.add(proc);
2977 }
2978 }
2979
2980 // Kill the running processes.
2981 for (int i = 0; i < procsToKill.size(); i++) {
2982 ProcessRecord pr = procsToKill.get(i);
2983 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2984 && pr.curReceivers.isEmpty()) {
2985 pr.kill("remove task", true);
2986 } else {
2987 // We delay killing processes that are not in the background or running a receiver.
2988 pr.waitingToKill = "remove task";
2989 }
2990 }
2991 }
2992
Chong Zhang5dcb2752015-08-18 13:50:26 -07002993 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002994 * Called to restore the state of the task into the stack that it's supposed to go into.
2995 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002996 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002997 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002998 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002999 * @return true if the task has been restored successfully.
3000 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003001 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
3002 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07003003 final ActivityStack currentStack = task.getStack();
3004 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003005 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003006 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003007 // Nothing else to do since it is already restored in the right stack.
3008 return true;
3009 }
3010 // Remove current stack association, so we can re-associate the task with the
3011 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003012 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003013 }
3014
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003015 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003016 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003017 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003018 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3019 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003020 final ArrayList<ActivityRecord> activities = task.mActivities;
3021 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003022 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003023 }
3024 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003025 }
3026
Winson Chung1dbc8112017-09-28 18:05:31 -07003027 @Override
3028 public void onRecentTaskAdded(TaskRecord task) {
3029 task.touchActiveTime();
3030 }
3031
3032 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003033 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003034 if (wasTrimmed) {
3035 // Task was trimmed from the recent tasks list -- remove the active task record as well
3036 // since the user won't really be able to go back to it
3037 removeTaskByIdLocked(task.taskId, false /* killProcess */,
3038 false /* removeFromRecents */, !PAUSE_IMMEDIATELY, "recent-task-trimmed");
3039 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003040 task.removedFromRecents();
3041 }
3042
Wale Ogunwale040b4702015-08-06 18:10:50 -07003043 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003044 * Move stack with all its existing content to specified display.
3045 * @param stackId Id of stack to move.
3046 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003047 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003048 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003049 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003050 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003051 if (activityDisplay == null) {
3052 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3053 + displayId);
3054 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003055 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003056 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003057 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3058 + stackId);
3059 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003060
3061 final ActivityDisplay currentDisplay = stack.getDisplay();
3062 if (currentDisplay == null) {
3063 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3064 + " is not attached to any display.");
3065 }
3066
3067 if (currentDisplay.mDisplayId == displayId) {
3068 throw new IllegalArgumentException("Trying to move stack=" + stack
3069 + " to its current displayId=" + displayId);
3070 }
3071
3072 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003073 // TODO(multi-display): resize stacks properly if moved from split-screen.
3074 }
3075
3076 /**
Winson Chung74666102017-02-22 17:49:24 -08003077 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3078 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003079 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003080 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003081 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003082 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003083 final int stackId = stack.mStackId;
3084 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003085
Winson Chung74666102017-02-22 17:49:24 -08003086 // Check that we aren't reparenting to the same stack that the task is already in
3087 if (prevStack != null && prevStack.mStackId == stackId) {
3088 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3089 + " already in stackId=" + stackId);
3090 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003091 }
3092
Winson Chung74666102017-02-22 17:49:24 -08003093 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3094 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003095 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003096 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003097 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003098 }
3099
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003100 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3101 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003102 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003103 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3104 + " reparent task=" + task + " to stackId=" + stackId);
3105 }
3106
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003107 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003108 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3109 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003110 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3111 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003112 }
3113
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003114 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3115 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003116 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003117 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3118 + " Moving to a fullscreen stack instead.");
3119 if (prevStack != null) {
3120 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003121 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003122 stack = stack.getDisplay().createStack(
3123 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003124 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003125 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003126 }
3127
Winson Chung08f81892017-03-02 15:40:51 -08003128 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003129 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003130 if (stack == null) {
3131 throw new IllegalArgumentException(
3132 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3133 }
3134
3135 final ActivityRecord r = stack.topRunningActivityLocked();
3136 if (r == null) {
3137 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3138 + " in stack=" + stack);
3139 return false;
3140 }
3141
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003142 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003143 Slog.w(TAG,
3144 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003145 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003146 return false;
3147 }
3148
Winson Chung3a682872017-04-10 14:38:05 -07003149 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003150 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003151 return true;
3152 }
3153
Winson Chung8bca9e42017-04-16 15:59:43 -07003154 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003155 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003156
Wale Ogunwale480dca02016-02-06 13:58:29 -08003157 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003158
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003159 final ActivityDisplay display = r.getStack().getDisplay();
3160 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003161
Bryce Lee04ab3462017-04-10 15:06:33 -07003162 // This will clear the pinned stack by moving an existing task to the full screen stack,
3163 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003164 if (stack != null) {
3165 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3166 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003167
Wale Ogunwale1666e312016-12-16 11:27:18 -08003168 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003169 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003170
Winson Chunge55c0192017-08-24 14:50:48 -07003171 // Calculate the target bounds here before the task is reparented back into pinned windowing
3172 // mode (which will reset the saved bounds)
3173 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3174
Wale Ogunwale480dca02016-02-06 13:58:29 -08003175 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003176 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003177 // Resize the pinned stack to match the current size of the task the activity we are
3178 // going to be moving is currently contained in. We do this to have the right starting
3179 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003180 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003181 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003182 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003183
3184 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003185 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003186 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3187 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003188 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003189 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003190 // reveal/leave the other activities in their original task.
3191
3192 // Currently, we don't support reparenting activities across tasks in two different
3193 // stacks, so instead, just create a new task in the same stack, reparent the
3194 // activity into that task, and then reparent the whole task to the new stack. This
3195 // ensures that all the necessary work to migrate states in the old and new stacks
3196 // is also done.
3197 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003198 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003199 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3200
Winson Chung5af42fc2017-03-24 17:11:33 -07003201 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003202 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003203 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003204 }
Winson Chungc2baac02017-01-11 13:34:47 -08003205
3206 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3207 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003208 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003209 } finally {
3210 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003211 }
3212
Winson Chunge7ba6862017-05-24 12:13:33 -07003213 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3214 true /* fromFullscreen */);
3215
3216 // Update the visibility of all activities after the they have been reparented to the new
3217 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3218 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3219 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003220 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003221 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003222
Wale Ogunwale89be5762017-10-04 13:27:49 -07003223 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003224 }
3225
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003226 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003227 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3228 if (r == null || !r.isFocusable()) {
3229 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3230 "moveActivityStackToFront: unfocusable r=" + r);
3231 return false;
3232 }
3233
Bryce Leeaf691c02017-03-20 14:20:22 -07003234 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003235 final ActivityStack stack = r.getStack();
3236 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003237 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3238 + r + " task=" + task);
3239 return false;
3240 }
3241
Chong Zhang6cda19c2016-06-14 19:07:56 -07003242 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3243 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3244 "moveActivityStackToFront: already on top, r=" + r);
3245 return false;
3246 }
3247
3248 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3249 "moveActivityStackToFront: r=" + r);
3250
3251 stack.moveToFront(reason, task);
3252 return true;
3253 }
3254
David Stevensc6b91c62017-02-08 14:23:58 -08003255 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003256 mTmpFindTaskResult.r = null;
3257 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003258 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003259 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003260 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003261 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3262 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3263 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003264 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003265 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3266 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003267 continue;
3268 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003269 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003270 // It is possible to have tasks in multiple stacks with the same root affinity, so
3271 // we should keep looking after finding an affinity match to see if there is a
3272 // better match in another stack. Also, task affinity isn't a good enough reason
3273 // to target a display which isn't the source of the intent, so skip any affinity
3274 // matches not on the specified display.
3275 if (mTmpFindTaskResult.r != null) {
3276 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3277 return mTmpFindTaskResult.r;
3278 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003279 // Note: since the traversing through the stacks is top down, the floating
3280 // tasks should always have lower priority than any affinity-matching tasks
3281 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003282 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003283 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3284 Slog.d(TAG_TASKS, "Skipping match on different display "
3285 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003286 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003287 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003288 }
3289 }
Winson Chung5b895b72017-05-01 13:46:25 -07003290
David Stevensc6b91c62017-02-08 14:23:58 -08003291 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3292 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003293 }
3294
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003295 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003296 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003297 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003298 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3299 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3300 final ActivityStack stack = display.getChildAt(stackNdx);
3301 final ActivityRecord ar = stack.findActivityLocked(
3302 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003303 if (ar != null) {
3304 return ar;
3305 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003306 }
3307 }
3308 return null;
3309 }
3310
David Stevens9440dc82017-03-16 19:00:20 -07003311 boolean hasAwakeDisplay() {
3312 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3313 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3314 if (!display.shouldSleep()) {
3315 return true;
3316 }
3317 }
3318 return false;
3319 }
3320
Craig Mautner8d341ef2013-03-26 09:03:27 -07003321 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003322 scheduleSleepTimeout();
3323 if (!mGoingToSleep.isHeld()) {
3324 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003325 if (mLaunchingActivity.isHeld()) {
3326 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3327 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003328 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003329 mLaunchingActivity.release();
3330 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003331 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003332 }
David Stevens9440dc82017-03-16 19:00:20 -07003333
3334 applySleepTokensLocked(false /* applyToStacks */);
3335
3336 checkReadyForSleepLocked(true /* allowDelay */);
3337 }
3338
3339 void prepareForShutdownLocked() {
3340 for (int i = 0; i < mActivityDisplays.size(); i++) {
3341 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3342 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003343 }
3344
3345 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003346 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003347
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003348 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003349 final long endTime = System.currentTimeMillis() + timeout;
3350 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003351 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003352 long timeRemaining = endTime - System.currentTimeMillis();
3353 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003355 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003356 } catch (InterruptedException e) {
3357 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003358 } else {
3359 Slog.w(TAG, "Activity manager shutdown timed out");
3360 timedout = true;
3361 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003362 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003363 } else {
3364 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003365 }
3366 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003367
3368 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003369 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003370
Craig Mautner8d341ef2013-03-26 09:03:27 -07003371 return timedout;
3372 }
3373
3374 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003375 removeSleepTimeouts();
3376 if (mGoingToSleep.isHeld()) {
3377 mGoingToSleep.release();
3378 }
David Stevens9440dc82017-03-16 19:00:20 -07003379 }
3380
3381 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003382 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003383 // Set the sleeping state of the display.
3384 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3385 final boolean displayShouldSleep = display.shouldSleep();
3386 if (displayShouldSleep == display.isSleeping()) {
3387 continue;
3388 }
3389 display.setIsSleeping(displayShouldSleep);
3390
3391 if (!applyToStacks) {
3392 continue;
3393 }
3394
3395 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003396 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3397 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003398 if (displayShouldSleep) {
3399 stack.goToSleepIfPossible(false /* shuttingDown */);
3400 } else {
3401 stack.awakeFromSleepingLocked();
Lucas Dupin47a65c72018-02-15 14:16:18 -08003402 if (isFocusedStack(stack) && !getKeyguardController().isKeyguardOrAodShowing(
3403 display.mDisplayId)) {
Bryce Leed939cf02018-03-12 09:04:44 -07003404 // If the keyguard is unlocked - resume immediately.
3405 // It is possible that the display will not be awake at the time we
3406 // process the keyguard going away, which can happen before the sleep token
3407 // is released. As a result, it is important we resume the activity here.
David Stevens9440dc82017-03-16 19:00:20 -07003408 resumeFocusedStackTopActivityLocked();
3409 }
3410 }
3411 }
3412
3413 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3414 continue;
3415 }
3416 // The display is awake now, so clean up the going to sleep list.
3417 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3418 final ActivityRecord r = it.next();
3419 if (r.getDisplayId() == display.mDisplayId) {
3420 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 }
Craig Mautner5314a402013-09-26 12:40:16 -07003422 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003423 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003424 }
3425
3426 void activitySleptLocked(ActivityRecord r) {
3427 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003428 final ActivityStack s = r.getStack();
3429 if (s != null) {
3430 s.checkReadyForSleep();
3431 } else {
3432 checkReadyForSleepLocked(true);
3433 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003434 }
3435
David Stevens9440dc82017-03-16 19:00:20 -07003436 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003437 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003438 // Do not care.
3439 return;
3440 }
3441
David Stevens18abd0e2017-08-17 14:55:47 -07003442 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3443 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003444 }
3445
Wei Wang65c7a152016-06-02 18:51:22 -07003446 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003447 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003448
Craig Mautner0eea92c2013-05-16 13:35:39 -07003449 removeSleepTimeouts();
3450
3451 if (mGoingToSleep.isHeld()) {
3452 mGoingToSleep.release();
3453 }
3454 if (mService.mShuttingDown) {
3455 mService.notifyAll();
3456 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003457 }
3458
David Stevens18abd0e2017-08-17 14:55:47 -07003459 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3460 // successfully put to sleep.
3461 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3462 boolean allSleep = true;
3463 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003464 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3465 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3466 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003467 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003468 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003469 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003470 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003471 }
3472 }
3473 }
3474 return allSleep;
3475 }
3476
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003477 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003478 // A resumed activity cannot be stopping. remove from list
3479 mStoppingActivities.remove(r);
3480
Andrii Kulian02b7a832016-10-06 23:11:56 -07003481 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003482 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003483 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003484 }
3485 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003486 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003487 mWindowManager.executeAppTransition();
3488 return true;
3489 }
3490 return false;
3491 }
3492
Craig Mautner8d341ef2013-03-26 09:03:27 -07003493 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003495 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3496 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3497 final ActivityStack stack = display.getChildAt(stackNdx);
3498 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003500 }
3501 }
3502
Craig Mautnerbb742462014-07-07 15:28:55 -07003503 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003504 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003505 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003506 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003507
3508 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003509 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003510 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003511 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003512
3513 // When launching tasks behind, update the last active time of the top task after the new
3514 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003515 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003516 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003517 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003518 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003519 }
3520
3521 void scheduleLaunchTaskBehindComplete(IBinder token) {
3522 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3523 }
3524
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003525 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3526 boolean preserveWindows) {
Bryce Lee459c0622018-03-19 11:04:01 -07003527 getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02003528 try {
3529 // First the front stacks. In case any are not fullscreen and are in front of home.
3530 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003531 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3532 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3533 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003534 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3535 }
Craig Mautner580ea812013-04-25 12:58:38 -07003536 }
Jorim Jaggife762342016-10-13 14:33:27 +02003537 } finally {
Bryce Lee459c0622018-03-19 11:04:01 -07003538 getKeyguardController().endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003540 }
3541
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003542 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003544 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3545 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3546 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003547 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3548 }
3549 }
3550 }
3551
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003552 void invalidateTaskLayers() {
3553 mTaskLayersChanged = true;
3554 }
3555
3556 void rankTaskLayersIfNeeded() {
3557 if (!mTaskLayersChanged) {
3558 return;
3559 }
3560 mTaskLayersChanged = false;
3561 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003562 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003563 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003564 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3565 final ActivityStack stack = display.getChildAt(stackNdx);
3566 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003567 }
3568 }
3569 }
3570
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003571 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3572 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003573 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3574 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3575 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003576 stack.clearOtherAppTimeTrackers(except);
3577 }
3578 }
3579 }
3580
Craig Mautner8d341ef2013-03-26 09:03:27 -07003581 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003582 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003583 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3584 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3585 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003586 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003588 }
3589 }
3590
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003591 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3592 // Examine all activities currently running in the process.
3593 TaskRecord firstTask = null;
3594 // Tasks is non-null only if two or more tasks are found.
3595 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003596 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3597 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003598 ActivityRecord r = app.activities.get(i);
3599 // First, if we find an activity that is in the process of being destroyed,
3600 // then we just aren't going to do anything for now; we want things to settle
3601 // down before we try to prune more activities.
Bryce Lee7ace3952018-02-16 14:34:32 -08003602 if (r.finishing || r.isState(DESTROYING, DESTROYED)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003603 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003604 return;
3605 }
3606 // Don't consider any activies that are currently not in a state where they
3607 // can be destroyed.
Bryce Lee7ace3952018-02-16 14:34:32 -08003608 if (r.visible || !r.stopped || !r.haveState
3609 || r.isState(RESUMED, PAUSING, PAUSED, STOPPING)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003610 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003611 continue;
3612 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003613
3614 final TaskRecord task = r.getTask();
3615 if (task != null) {
3616 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003617 + " from " + r);
3618 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003619 firstTask = task;
3620 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003621 if (tasks == null) {
3622 tasks = new ArraySet<>();
3623 tasks.add(firstTask);
3624 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003625 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003626 }
3627 }
3628 }
3629 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003630 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003631 return;
3632 }
3633 // If we have activities in multiple tasks that are in a position to be destroyed,
3634 // let's iterate through the tasks and release the oldest one.
3635 final int numDisplays = mActivityDisplays.size();
3636 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003637 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3638 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003639 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003640 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3641 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003642 // Try to release activities in this stack; if we manage to, we are done.
3643 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3644 return;
3645 }
3646 }
3647 }
3648 }
3649
Amith Yamasani37a40c22015-06-17 13:25:42 -07003650 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003651 final int focusStackId = mFocusedStack.getStackId();
3652 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003653 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003654 if (dockedStack != null) {
3655 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3656 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003657 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3658 // also cause all tasks to be moved to the fullscreen stack at a position that is
3659 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003660 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003661
3662 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003663 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003664 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003665
Craig Mautner858d8a62013-04-23 17:08:34 -07003666 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003667 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003668 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3669 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3670 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003671 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003672 TaskRecord task = stack.topTask();
3673 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003674 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003675 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003677 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003678
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003679 ActivityStack stack = getStack(restoreStackId);
3680 if (stack == null) {
3681 stack = mHomeStack;
3682 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003683 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003684 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003685 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003686 } else {
3687 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003688 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003689 }
Craig Mautner93529a42013-10-04 15:03:13 -07003690 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003691 }
3692
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003693 /** Checks whether the userid is a profile of the current user. */
3694 boolean isCurrentProfileLocked(int userId) {
3695 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003696 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003697 }
3698
Bryce Leeb7c9b802017-05-02 14:20:24 -07003699 /**
3700 * Returns whether a stopping activity is present that should be stopped after visible, rather
3701 * than idle.
3702 * @return {@code true} if such activity is present. {@code false} otherwise.
3703 */
3704 boolean isStoppingNoHistoryActivity() {
3705 // Activities that are marked as nohistory should be stopped immediately after the resumed
3706 // activity has become visible.
3707 for (ActivityRecord record : mStoppingActivities) {
3708 if (record.isNoHistory()) {
3709 return true;
3710 }
3711 }
3712
3713 return false;
3714 }
3715
Winson Chung4dabf232017-01-25 13:25:22 -08003716 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3717 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003718 ArrayList<ActivityRecord> stops = null;
3719
3720 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003721 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3722 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003723 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003724 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003725 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3726 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003727 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003728 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003729 if (s.finishing) {
3730 // If this activity is finishing, it is sitting on top of
3731 // everyone else but we now know it is no longer needed...
3732 // so get rid of it. Otherwise, we need to go through the
3733 // normal flow and hide it once we determine that it is
3734 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003735 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003736 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003737 }
3738 }
David Stevens9440dc82017-03-16 19:00:20 -07003739 if (remove) {
3740 final ActivityStack stack = s.getStack();
3741 final boolean shouldSleepOrShutDown = stack != null
3742 ? stack.shouldSleepOrShutDownActivities()
3743 : mService.isSleepingOrShuttingDownLocked();
3744 if (!waitingVisible || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08003745 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07003746 // Defer processing pausing activities in this iteration and reschedule
3747 // a delayed idle to reprocess it again
3748 removeTimeoutsForActivityLocked(idleActivity);
3749 scheduleIdleTimeoutLocked(idleActivity);
3750 continue;
3751 }
Winson Chung4dabf232017-01-25 13:25:22 -08003752
David Stevens9440dc82017-03-16 19:00:20 -07003753 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3754 if (stops == null) {
3755 stops = new ArrayList<>();
3756 }
3757 stops.add(s);
3758 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003759 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003760 }
3761 }
3762
3763 return stops;
3764 }
3765
Craig Mautnercf910b02013-04-23 11:23:27 -07003766 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003768 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3769 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3770 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003771 final ActivityRecord r = stack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08003772 final ActivityState state = r == null ? DESTROYED : r.getState();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003773 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003774 if (r == null) Slog.e(TAG,
3775 "validateTop...: null top activity, stack=" + stack);
3776 else {
3777 final ActivityRecord pausing = stack.mPausingActivity;
3778 if (pausing != null && pausing == r) Slog.e(TAG,
3779 "validateTop...: top stack has pausing activity r=" + r
3780 + " state=" + state);
3781 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3782 "validateTop...: activity in front not resumed r=" + r
3783 + " state=" + state);
3784 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003785 } else {
Bryce Leec4ab62a2018-03-05 14:19:26 -08003786 final ActivityRecord resumed = stack.getResumedActivity();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003787 if (resumed != null && resumed == r) Slog.e(TAG,
3788 "validateTop...: back stack has resumed activity r=" + r
3789 + " state=" + state);
3790 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3791 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003792 }
3793 }
3794 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003795 }
3796
Bryce Lee77a7dd62018-01-22 15:47:09 -08003797 public void dumpDisplays(PrintWriter pw) {
3798 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3799 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3800 pw.print("[id:" + display.mDisplayId + " stacks:");
3801 display.dumpStacks(pw);
3802 pw.print("]");
3803 }
3804 }
3805
Craig Mautner27084302013-03-25 08:05:25 -07003806 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003807 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003808 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003809 pw.print(prefix);
3810 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003811 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003812 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3813 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003814 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003815 }
Bryce Lee4a194382017-04-04 14:32:48 -07003816 if (!mWaitingForActivityVisible.isEmpty()) {
3817 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3818 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3819 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3820 }
3821 }
Winson Chungc1674272018-02-21 10:15:17 -08003822 pw.print(prefix); pw.print("isHomeRecentsComponent=");
3823 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07003824
Bryce Lee459c0622018-03-19 11:04:01 -07003825 getKeyguardController().dump(pw, prefix);
Bryce Lee2b8e0372018-04-05 17:01:37 -07003826 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003827 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003828
Yi Jin2b30f322018-02-20 15:41:47 -08003829 public void writeToProto(ProtoOutputStream proto, long fieldId) {
3830 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02003831 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003832 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3833 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3834 activityDisplay.writeToProto(proto, DISPLAYS);
3835 }
Bryce Lee459c0622018-03-19 11:04:01 -07003836 getKeyguardController().writeToProto(proto, KEYGUARD_CONTROLLER);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003837 if (mFocusedStack != null) {
3838 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3839 ActivityRecord focusedActivity = getResumedActivityLocked();
3840 if (focusedActivity != null) {
3841 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3842 }
3843 } else {
3844 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3845 }
Winson Chungc1674272018-02-21 10:15:17 -08003846 proto.write(IS_HOME_RECENTS_COMPONENT,
3847 mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Yi Jin2b30f322018-02-20 15:41:47 -08003848 proto.end(token);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003849 }
3850
Winson43d1f262016-06-14 16:05:55 -07003851 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003852 * Dump all connected displays' configurations.
3853 * @param prefix Prefix to apply to each line of the dump.
3854 */
3855 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3856 pw.print(prefix); pw.println("Display override configurations:");
3857 final int displayCount = mActivityDisplays.size();
3858 for (int i = 0; i < displayCount; i++) {
3859 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3860 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3861 pw.println(activityDisplay.getOverrideConfiguration());
3862 }
3863 }
3864
3865 /**
Winson43d1f262016-06-14 16:05:55 -07003866 * Dumps the activities matching the given {@param name} in the either the focused stack
3867 * or all visible stacks if {@param dumpVisibleStacks} is true.
3868 */
Winson Chung6998bc42017-02-28 17:07:05 -08003869 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3870 boolean dumpFocusedStackOnly) {
3871 if (dumpFocusedStackOnly) {
3872 return mFocusedStack.getDumpActivitiesLocked(name);
3873 } else {
Winson43d1f262016-06-14 16:05:55 -07003874 ArrayList<ActivityRecord> activities = new ArrayList<>();
3875 int numDisplays = mActivityDisplays.size();
3876 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003877 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3878 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3879 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003880 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003881 activities.addAll(stack.getDumpActivitiesLocked(name));
3882 }
3883 }
3884 }
3885 return activities;
Winson43d1f262016-06-14 16:05:55 -07003886 }
Craig Mautner20e72272013-04-01 13:45:53 -07003887 }
3888
Dianne Hackborn390517b2013-05-30 15:03:32 -07003889 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3890 boolean needSep, String prefix) {
3891 if (activity != null) {
3892 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3893 if (needSep) {
3894 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003895 }
3896 pw.print(prefix);
3897 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003898 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003899 }
3900 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003901 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003902 }
3903
Craig Mautner8d341ef2013-03-26 09:03:27 -07003904 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3905 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003906 boolean printed = false;
3907 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003908 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3909 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003910 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003911 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003912 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3913 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3914 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003915 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003916 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003917 + ": type=" + activityTypeToString(stack.getActivityType())
3918 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003919 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08003920 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07003921
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003922 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3923 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003924
Craig Mautner4a1cb222013-12-04 16:14:06 -08003925 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3926 !dumpAll, false, dumpPackage, true,
3927 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003928
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929 needSep = printed;
3930 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3931 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003932 if (pr) {
3933 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003935 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08003936 pr = printThisActivity(pw, stack.getResumedActivity(), dumpPackage, needSep,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 " mResumedActivity: ");
3938 if (pr) {
3939 printed = true;
3940 needSep = false;
3941 }
3942 if (dumpAll) {
3943 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3944 " mLastPausedActivity: ");
3945 if (pr) {
3946 printed = true;
3947 needSep = true;
3948 }
3949 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3950 needSep, " mLastNoHistoryActivity: ");
3951 }
3952 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003953 }
3954 }
3955
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003956 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3957 false, dumpPackage, true, " Activities waiting to finish:", null);
3958 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3959 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003960 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3961 false, !dumpAll, false, dumpPackage, true,
3962 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003963 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3964 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003965
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003966 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003967 }
3968
Dianne Hackborn390517b2013-05-30 15:03:32 -07003969 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003971 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003972 String innerPrefix = null;
3973 String[] args = null;
3974 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003975 for (int i=list.size()-1; i>=0; i--) {
3976 final ActivityRecord r = list.get(i);
3977 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3978 continue;
3979 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003980 if (innerPrefix == null) {
3981 innerPrefix = prefix + " ";
3982 args = new String[0];
3983 }
3984 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003985 final boolean full = !brief && (complete || !r.isInHistory());
3986 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003987 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003988 needNL = false;
3989 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003990 if (header != null) {
3991 pw.println(header);
3992 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003993 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003994 if (lastTask != r.getTask()) {
3995 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003996 pw.print(prefix);
3997 pw.print(full ? "* " : " ");
3998 pw.println(lastTask);
3999 if (full) {
4000 lastTask.dump(pw, prefix + " ");
4001 } else if (complete) {
4002 // Complete + brief == give a summary. Isn't that obvious?!?
4003 if (lastTask.intent != null) {
4004 pw.print(prefix); pw.print(" ");
4005 pw.println(lastTask.intent.toInsecureStringWithClip());
4006 }
4007 }
4008 }
4009 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4010 pw.print(" #"); pw.print(i); pw.print(": ");
4011 pw.println(r);
4012 if (full) {
4013 r.dump(pw, innerPrefix);
4014 } else if (complete) {
4015 // Complete + brief == give a summary. Isn't that obvious?!?
4016 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4017 if (r.app != null) {
4018 pw.print(innerPrefix); pw.println(r.app);
4019 }
4020 }
4021 if (client && r.app != null && r.app.thread != null) {
4022 // flush anything that is already in the PrintWriter since the thread is going
4023 // to write to the file descriptor directly
4024 pw.flush();
4025 try {
4026 TransferPipe tp = new TransferPipe();
4027 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004028 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004029 // Short timeout, since blocking here can
4030 // deadlock with the application.
4031 tp.go(fd, 2000);
4032 } finally {
4033 tp.kill();
4034 }
4035 } catch (IOException e) {
4036 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4037 } catch (RemoteException e) {
4038 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4039 }
4040 needNL = true;
4041 }
4042 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004043 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004044 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004045
Craig Mautnerf3333272013-04-22 10:55:53 -07004046 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004047 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4048 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004049 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4050 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004051 }
4052
4053 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004054 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004055 }
4056
4057 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004058 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4059 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004060 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4061 }
4062
Craig Mautner05d29032013-05-03 13:40:13 -07004063 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004064 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4065 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4066 }
Craig Mautner05d29032013-05-03 13:40:13 -07004067 }
4068
Craig Mautner0eea92c2013-05-16 13:35:39 -07004069 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004070 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4071 }
4072
4073 final void scheduleSleepTimeout() {
4074 removeSleepTimeouts();
4075 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4076 }
4077
Craig Mautner4a1cb222013-12-04 16:14:06 -08004078 @Override
4079 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004080 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004081 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4082 }
4083
4084 @Override
4085 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004086 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004087 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4088 }
4089
4090 @Override
4091 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004092 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4094 }
4095
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004096 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004098 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004099 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 }
4101
Andrii Kulianca007a62016-11-22 16:33:28 -08004102 /** Check if display with specified id is added to the list. */
4103 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004104 return getActivityDisplayOrCreateLocked(displayId) != null;
4105 }
4106
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004107 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004108 ActivityDisplay getActivityDisplay(int displayId) {
4109 return mActivityDisplays.get(displayId);
4110 }
4111
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004112 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4113 ActivityDisplay getDefaultDisplay() {
4114 return mActivityDisplays.get(DEFAULT_DISPLAY);
4115 }
4116
Andrii Kulian62e6f252017-05-30 22:46:53 -07004117 /**
4118 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4119 * corresponding record in display manager.
4120 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004121 // TODO: Look into consolidating with getActivityDisplay()
4122 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004123 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4124 if (activityDisplay != null) {
4125 return activityDisplay;
4126 }
4127 if (mDisplayManager == null) {
4128 // The system isn't fully initialized yet.
4129 return null;
4130 }
4131 final Display display = mDisplayManager.getDisplay(displayId);
4132 if (display == null) {
4133 // The display is not registered in DisplayManager.
4134 return null;
4135 }
4136 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale45477b52018-03-06 12:24:19 -08004137 activityDisplay = new ActivityDisplay(this, display);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004138 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004139 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4140 mWindowManager.onDisplayAdded(displayId);
4141 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004142 }
4143
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004144 @VisibleForTesting
4145 void attachDisplay(ActivityDisplay display) {
4146 mActivityDisplays.put(display.mDisplayId, display);
4147 }
4148
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004149 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004150 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004151 mService.mContext.getResources().getDimensionPixelSize(
4152 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004153 }
4154
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004155 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004156 if (displayId == DEFAULT_DISPLAY) {
4157 throw new IllegalArgumentException("Can't remove the primary display.");
4158 }
4159
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004161 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4162 if (activityDisplay == null) {
4163 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004164 }
Bryce Leef19cbe22018-02-02 15:09:21 -08004165
4166 activityDisplay.remove();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004167
4168 releaseSleepTokens(activityDisplay);
4169
4170 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004172 }
4173
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004174 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004175 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004176 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08004177 // TODO: The following code block should be moved into {@link ActivityDisplay}.
Craig Mautnere0a38842013-12-16 16:14:02 -08004178 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004179 // The window policy is responsible for stopping activities on the default display
4180 if (displayId != Display.DEFAULT_DISPLAY) {
4181 int displayState = activityDisplay.mDisplay.getState();
4182 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4183 activityDisplay.mOffToken =
4184 mService.acquireSleepToken("Display-off", displayId);
4185 } else if (displayState == Display.STATE_ON
4186 && activityDisplay.mOffToken != null) {
4187 activityDisplay.mOffToken.release();
4188 activityDisplay.mOffToken = null;
4189 }
4190 }
Bryce Leef3c6a472017-11-14 14:53:06 -08004191
4192 activityDisplay.updateBounds();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004193 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004194 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004196 }
4197
David Stevens9440dc82017-03-16 19:00:20 -07004198 SleepToken createSleepTokenLocked(String tag, int displayId) {
4199 ActivityDisplay display = mActivityDisplays.get(displayId);
4200 if (display == null) {
4201 throw new IllegalArgumentException("Invalid display: " + displayId);
4202 }
4203
4204 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4205 mSleepTokens.add(token);
4206 display.mAllSleepTokens.add(token);
4207 return token;
4208 }
4209
4210 private void removeSleepTokenLocked(SleepTokenImpl token) {
4211 mSleepTokens.remove(token);
4212
4213 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4214 if (display != null) {
4215 display.mAllSleepTokens.remove(token);
4216 if (display.mAllSleepTokens.isEmpty()) {
4217 mService.updateSleepIfNeededLocked();
4218 }
4219 }
4220 }
4221
4222 private void releaseSleepTokens(ActivityDisplay display) {
4223 if (display.mAllSleepTokens.isEmpty()) {
4224 return;
4225 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004226 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004227 mSleepTokens.remove(token);
4228 }
4229 display.mAllSleepTokens.clear();
4230
4231 mService.updateSleepIfNeededLocked();
4232 }
4233
Wale Ogunwale68278562017-09-23 17:13:55 -07004234 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004235 final int displayId = stack.mDisplayId;
4236 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004238 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004239 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004240 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004241 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004242 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004243 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004244 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004245 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004246
4247 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4248 final int numTasks = tasks.size();
4249 int[] taskIds = new int[numTasks];
4250 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004251 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004252 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004253 for (int i = 0; i < numTasks; ++i) {
4254 final TaskRecord task = tasks.get(i);
4255 taskIds[i] = task.taskId;
4256 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4257 : task.realActivity != null ? task.realActivity.flattenToString()
4258 : task.getTopActivity() != null ? task.getTopActivity().packageName
4259 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004260 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004261 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004262 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004263 }
4264 info.taskIds = taskIds;
4265 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004266 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004267 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004268
4269 final ActivityRecord top = stack.topRunningActivityLocked();
4270 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004271 return info;
4272 }
4273
Wale Ogunwale68278562017-09-23 17:13:55 -07004274 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004275 ActivityStack stack = getStack(stackId);
4276 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004277 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004278 }
4279 return null;
4280 }
4281
Wale Ogunwale68278562017-09-23 17:13:55 -07004282 StackInfo getStackInfo(int windowingMode, int activityType) {
4283 final ActivityStack stack = getStack(windowingMode, activityType);
4284 return (stack != null) ? getStackInfo(stack) : null;
4285 }
4286
Craig Mautner4a1cb222013-12-04 16:14:06 -08004287 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004288 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004289 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004290 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4291 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4292 final ActivityStack stack = display.getChildAt(stackNdx);
4293 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004294 }
4295 }
4296 return list;
4297 }
4298
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004299 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004300 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004301 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004302 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004303 }
4304
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004305 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004306 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004307 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004308 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004309 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004310 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004311 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004312 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004313 return;
4314 }
4315
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004316 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004317 if (isSecondaryDisplayPreferred) {
4318 final int actualDisplayId = task.getStack().mDisplayId;
4319 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004320 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004321 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004322 // The task might have landed on a display different from requested.
4323 // TODO(multi-display): Find proper stack for the task on the default display.
4324 mService.setTaskWindowingMode(task.taskId,
4325 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
4326 if (preferredDisplayId != actualDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004327 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4328 // display with config different from global config.
4329 mService.mTaskChangeNotificationController
4330 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004331 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004332 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004333 }
4334
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004335 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004336 // Display a warning toast that we tried to put an app that doesn't support split-screen
4337 // in split-screen.
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004338 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004339
Andrii Kulianc27916642016-04-12 17:59:27 -07004340 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4341 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004342
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004343 final ActivityStack dockedStack =
4344 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004345 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004346 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004347 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004348 return;
4349 }
4350
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004351 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004352 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
4353 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004354 final String packageName = topActivity.appInfo.packageName;
4355 final int reason = isSecondaryDisplayPreferred
4356 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4357 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004358 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004359 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004360 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004361 }
4362
Jorim Jaggife89d122015-12-22 16:28:44 +01004363 void activityRelaunchedLocked(IBinder token) {
4364 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004365 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4366 if (r != null) {
4367 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004368 r.setSleeping(true, true);
4369 }
4370 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004371 }
4372
4373 void activityRelaunchingLocked(ActivityRecord r) {
4374 mWindowManager.notifyAppRelaunching(r.appToken);
4375 }
4376
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004377 void logStackState() {
4378 mActivityMetricsLogger.logWindowState();
4379 }
4380
Winson Chung5af42fc2017-03-24 17:11:33 -07004381 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004382 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4383 // end, then ensure that we defer all in between multi-window mode changes
4384 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4385 return;
4386 }
4387
Wale Ogunwale22e25262016-02-01 10:32:02 -08004388 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4389 final ActivityRecord r = task.mActivities.get(i);
4390 if (r.app != null && r.app.thread != null) {
4391 mMultiWindowModeChangedActivities.add(r);
4392 }
4393 }
4394
4395 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4396 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4397 }
4398 }
4399
Winson Chung5af42fc2017-03-24 17:11:33 -07004400 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004401 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004402 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004403 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004404 return;
4405 }
4406
Bryce Leef3c6a472017-11-14 14:53:06 -08004407 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004408 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004409
Winson Chungab76bbc2017-08-14 13:33:51 -07004410 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4411 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4412 final ActivityRecord r = task.mActivities.get(i);
4413 if (r.app != null && r.app.thread != null) {
4414 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004415 // If we are scheduling pip change, then remove this activity from multi-window
4416 // change list as the processing of pip change will make sure multi-window changed
4417 // message is processed in the right order relative to pip changed.
4418 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004419 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004420 }
4421 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004422
Winson Chungab76bbc2017-08-14 13:33:51 -07004423 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4424 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4425 }
4426 }
4427
4428 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4429 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4430 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4431 final ActivityRecord r = task.mActivities.get(i);
4432 if (r.app != null && r.app.thread != null) {
4433 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004434 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004435 }
4436 }
4437
Tony Mak853304c2016-04-18 15:17:41 +01004438 void setDockedStackMinimized(boolean minimized) {
4439 mIsDockMinimized = minimized;
Wale Ogunwale17696de2018-03-15 16:41:05 -07004440 if (mIsDockMinimized) {
4441 final ActivityStack current = getFocusedStack();
4442 if (current.inSplitScreenPrimaryWindowingMode()) {
4443 // The primary split-screen stack can't be focused while it is minimize, so move
4444 // focus to something else.
4445 current.adjustFocusToNextFocusableStack("setDockedStackMinimized");
4446 }
4447 }
Tony Mak853304c2016-04-18 15:17:41 +01004448 }
4449
chaviw59b98852017-06-13 12:05:44 -07004450 void wakeUp(String reason) {
4451 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4452 }
4453
4454 /**
4455 * Begin deferring resume to avoid duplicate resumes in one pass.
4456 */
4457 private void beginDeferResume() {
4458 mDeferResumeCount++;
4459 }
4460
4461 /**
4462 * End deferring resume and determine if resume can be called.
4463 */
4464 private void endDeferResume() {
4465 mDeferResumeCount--;
4466 }
4467
4468 /**
4469 * @return True if resume can be called.
4470 */
4471 private boolean readyToResume() {
4472 return mDeferResumeCount == 0;
4473 }
4474
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004475 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004476
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004477 public ActivityStackSupervisorHandler(Looper looper) {
4478 super(looper);
4479 }
4480
Winson Chung4dabf232017-01-25 13:25:22 -08004481 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004482 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004483 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4484 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004485 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004486 }
4487
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004488 @Override
4489 public void handleMessage(Message msg) {
4490 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004491 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4492 synchronized (mService) {
4493 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4494 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004495 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004496 }
4497 }
4498 } break;
4499 case REPORT_PIP_MODE_CHANGED_MSG: {
4500 synchronized (mService) {
4501 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4502 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004503 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4504 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004505 }
4506 }
4507 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004508 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004509 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4510 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004511 // We don't at this point know if the activity is fullscreen,
4512 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004513 activityIdleInternal((ActivityRecord) msg.obj,
4514 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004515 } break;
4516 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004517 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004518 activityIdleInternal((ActivityRecord) msg.obj,
4519 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004520 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004521 case RESUME_TOP_ACTIVITY_MSG: {
4522 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004523 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004524 }
4525 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004526 case SLEEP_TIMEOUT_MSG: {
4527 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004528 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004529 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004530 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004531 }
4532 }
4533 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004534 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004535 synchronized (mService) {
4536 if (mLaunchingActivity.isHeld()) {
4537 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4538 if (VALIDATE_WAKE_LOCK_CALLER
4539 && Binder.getCallingUid() != Process.myUid()) {
4540 throw new IllegalStateException("Calling must be system uid");
4541 }
4542 mLaunchingActivity.release();
4543 }
4544 }
4545 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004547 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004548 } break;
4549 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004550 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004551 } break;
4552 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004553 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004554 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004555 case LAUNCH_TASK_BEHIND_COMPLETE: {
4556 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004557 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004558 if (r != null) {
4559 handleLaunchTaskBehindCompleteLocked(r);
4560 }
4561 }
4562 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004563
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004564 }
4565 }
4566 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004567
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004568 ActivityStack findStackBehind(ActivityStack stack) {
4569 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004570 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004571 if (display == null) {
4572 return null;
4573 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004574 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4575 if (display.getChildAt(i) == stack && i > 0) {
4576 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004577 }
4578 }
4579 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004580 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004581 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004582
Jorim Jaggic69bd222016-03-15 14:38:37 +01004583 /**
4584 * Puts a task into resizing mode during the next app transition.
4585 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004586 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004587 */
Bryce Leed3624e12017-11-30 08:51:45 -08004588 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004589 mResizingTasksDuringAnimation.add(task.taskId);
4590 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004591 }
4592
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004593 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4594 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004595 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004596 final String callingPackage;
4597 final Intent intent;
4598 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004599 int activityType = ACTIVITY_TYPE_UNDEFINED;
4600 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004601 final ActivityOptions activityOptions = options != null
4602 ? options.getOptions(this)
4603 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004604 if (activityOptions != null) {
4605 activityType = activityOptions.getLaunchActivityType();
4606 windowingMode = activityOptions.getLaunchWindowingMode();
4607 }
4608 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004609 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004610 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004611 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004612
Matthew Ng606dd802017-06-05 14:06:32 -07004613 mWindowManager.deferSurfaceLayout();
4614 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004615 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004616 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004617 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004618
Matthew Ng606dd802017-06-05 14:06:32 -07004619 // Defer updating the stack in which recents is until the app transition is done, to
4620 // not run into issues where we still need to draw the task in recents but the
4621 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08004622 deferUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004623 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004624 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004625
Matthew Ng606dd802017-06-05 14:06:32 -07004626 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004627 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004628 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08004629 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004630 mWindowManager.executeAppTransition();
4631 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004632 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004633 }
4634
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004635 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4636 // We always want to return to the home activity instead of the recents activity
4637 // from whatever is started from the recents activity, so move the home stack
4638 // forward.
4639 moveHomeStackToFront("startActivityFromRecents");
4640 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004641
Matthew Ng606dd802017-06-05 14:06:32 -07004642 // If the user must confirm credentials (e.g. when first launching a work app and the
4643 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4644 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4645 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004646 final ActivityRecord targetActivity = task.getTopActivity();
4647
Bryce Leed3624e12017-11-30 08:51:45 -08004648 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004649 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004650 try {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004651 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004652 true /* fromRecents */);
4653 } finally {
4654 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4655 targetActivity);
4656 }
Matthew Ng606dd802017-06-05 14:06:32 -07004657
Bryce Leed3624e12017-11-30 08:51:45 -08004658 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
4659 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4660 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004661 return ActivityManager.START_TASK_TO_FRONT;
4662 }
Matthew Ng606dd802017-06-05 14:06:32 -07004663 callingPackage = task.mCallingPackage;
4664 intent = task.intent;
4665 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4666 userId = task.userId;
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004667 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004668 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004669 null, 0, 0, options, userId, task, "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004670 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004671 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
4672 // If we are launching the task in the docked stack, put it into resizing mode so
4673 // the window renders full-screen with the background filling the void. Also only
4674 // call this at the end to make sure that tasks exists on the window manager side.
4675 setResizingDuringAnimation(task);
4676
4677 final ActivityDisplay display = task.getStack().getDisplay();
4678 final ActivityStack topSecondaryStack =
4679 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
4680 if (topSecondaryStack.isActivityTypeHome()) {
4681 // If the home activity if the top split-screen secondary stack, then the
4682 // primary split-screen stack is in the minimized mode which means it can't
4683 // receive input keys, so we should move the focused app to the home app so that
4684 // window manager can correctly calculate the focus window that can receive
4685 // input keys.
4686 moveHomeStackToFront("startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08004687
4688 // Immediately update the minimized docked stack mode, the upcoming animation
4689 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
4690 // will do the animation to the target bounds
4691 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004692 }
4693 }
Matthew Ng606dd802017-06-05 14:06:32 -07004694 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004695 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004696 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004697
4698 /**
4699 * @return a list of activities which are the top ones in each visible stack. The first
4700 * entry will be the focused activity.
4701 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004702 List<IBinder> getTopVisibleActivities() {
4703 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4704 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004705 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004706 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4707 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004708 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4709 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004710 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004711 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004712 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004713 if (top != null) {
4714 if (stack == mFocusedStack) {
4715 topActivityTokens.add(0, top.appToken);
4716 } else {
4717 topActivityTokens.add(top.appToken);
4718 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004719 }
4720 }
4721 }
4722 }
4723 return topActivityTokens;
4724 }
Bryce Lee4a194382017-04-04 14:32:48 -07004725
4726 /**
4727 * Internal container to store a match qualifier alongside a WaitResult.
4728 */
4729 static class WaitInfo {
4730 private final ComponentName mTargetComponent;
4731 private final WaitResult mResult;
4732
4733 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4734 this.mTargetComponent = targetComponent;
4735 this.mResult = result;
4736 }
4737
Wale Ogunwale3270f172017-04-26 07:29:42 -07004738 public boolean matches(ComponentName targetComponent) {
4739 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004740 }
4741
4742 public WaitResult getResult() {
4743 return mResult;
4744 }
4745
4746 public ComponentName getComponent() {
4747 return mTargetComponent;
4748 }
4749
4750 public void dump(PrintWriter pw, String prefix) {
4751 pw.println(prefix + "WaitInfo:");
4752 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4753 pw.println(prefix + " mResult=");
4754 mResult.dump(pw, prefix);
4755 }
4756 }
David Stevens9440dc82017-03-16 19:00:20 -07004757
4758 private final class SleepTokenImpl extends SleepToken {
4759 private final String mTag;
4760 private final long mAcquireTime;
4761 private final int mDisplayId;
4762
4763 public SleepTokenImpl(String tag, int displayId) {
4764 mTag = tag;
4765 mDisplayId = displayId;
4766 mAcquireTime = SystemClock.uptimeMillis();
4767 }
4768
4769 @Override
4770 public void release() {
4771 synchronized (mService) {
4772 removeSleepTokenLocked(this);
4773 }
4774 }
4775
4776 @Override
4777 public String toString() {
4778 return "{\"" + mTag + "\", display " + mDisplayId
4779 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4780 }
4781 }
4782
Craig Mautner27084302013-03-25 08:05:25 -07004783}