blob: a82facd174a3b7263e905c6cb09c6ccd3a03960f [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;
Winson Chung1dbc8112017-09-28 18:05:31 -070093import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Steven Timotius4346f0a2017-09-12 11:07:21 -070094import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
95import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
96import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
97import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010098import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -070099
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800100import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200101
Svetoslav7008b512015-06-24 18:47:07 -0700102import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800103import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700104import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700105import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100106import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800109import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800110import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700111import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700113import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700114import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700116import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700117import android.app.WindowConfiguration.ActivityType;
118import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800119import android.app.servertransaction.ActivityLifecycleItem;
120import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700121import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800122import android.app.servertransaction.PauseActivityItem;
123import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700125import android.content.Context;
126import android.content.Intent;
127import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700129import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.content.pm.PackageManager;
131import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100132import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800134import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800135import android.hardware.display.DisplayManager;
136import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800137import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800138import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100140import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700141import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700142import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700143import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700144import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700145import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700146import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700147import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700148import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700149import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700150import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700151import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100152import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700153import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700154import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700155import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700157import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700158import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800159import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700160import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700161import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700164import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700165import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800166import android.view.Display;
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100167import android.view.RemoteAnimationAdapter;
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;
Jorim Jaggife762342016-10-13 14:33:27 +0200646 mKeyguardController.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) {
655 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700656 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800657 mActivityDisplays.put(displayId, 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
Craig Mautnerde4ef022013-04-07 19:01:33 -0700670 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700672 }
673
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700674 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700675 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700676 }
677
Wale Ogunwaled046a012015-12-24 13:05:59 -0800678 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800679 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
680 if (!focusCandidate.isFocusable()) {
681 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800682 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800683 }
684
685 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800686 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800687 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800688
Wale Ogunwaled046a012015-12-24 13:05:59 -0800689 EventLogTags.writeAmFocusedStack(
690 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
691 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
692 }
693
694 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800695 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800696 if (r != null && r.idle) {
697 checkFinishBootingLocked();
698 }
699 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700702 void moveHomeStackToFront(String reason) {
703 mHomeStack.moveToFront(reason);
704 }
705
Matthew Ng330757d2017-02-28 14:19:17 -0800706 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700707 final ActivityStack recentsStack = getDefaultDisplay().getStack(
708 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800709 if (recentsStack != null) {
710 recentsStack.moveToFront(reason);
711 }
712 }
713
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700714 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800715 boolean moveHomeStackTaskToTop(String reason) {
716 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700717
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700718 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700719 if (top == null) {
720 return false;
721 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700722 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700723 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700724 }
725
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800726 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700727 if (!mService.mBooting && !mService.mBooted) {
728 // Not ready yet!
729 return false;
730 }
731
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800732 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700733 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800734 final String myReason = reason + " resumeHomeStackTask";
735
Mark Lua56ea122015-10-08 13:31:01 +0800736 // Only resume home activity if isn't finishing.
737 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700738 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800739 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700740 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800741 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700742 }
743
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700745 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
746 }
747
748 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700749 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700750 }
751
752 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700753 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700754 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800755 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700756 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700757 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700758 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700759 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700760 @Nullable ActivityOptions aOptions, boolean onTop) {
761 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700762 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
763 throw new IllegalArgumentException("Should not specify activity options for non-restore"
764 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800765 }
766
Craig Mautnere0a38842013-12-16 16:14:02 -0800767 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700769 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
770 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
771 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700772 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700773 if (task == null) {
774 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800775 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700776 if (aOptions != null) {
777 // Resolve the stack the task should be placed in now based on options
778 // and reparent if needed.
779 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
780 if (launchStack != null && stack != launchStack) {
781 final int reparentMode = onTop
782 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
783 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
784 "anyTaskForIdLocked");
785 }
786 }
787 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700788 }
789 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800790
Winson Chung7900bee2017-03-10 08:59:25 -0800791 // If we are matching stack tasks only, return now
792 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800793 return null;
794 }
795
Winson Chung7900bee2017-03-10 08:59:25 -0800796 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
797 // the task from recents
798 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700799 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700800
801 if (task == null) {
802 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800803 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
804 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700805
806 return null;
807 }
808
809 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700810 return task;
811 }
812
Winson Chung7900bee2017-03-10 08:59:25 -0800813 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700814 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700815 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
816 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800817 return null;
818 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700819 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800820 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700821 }
822
Craig Mautner6170f732013-04-02 13:05:23 -0700823 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700826 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
827 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = display.getChildAt(stackNdx);
829 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 if (r != null) {
831 return r;
832 }
Craig Mautner6170f732013-04-02 13:05:23 -0700833 }
834 }
835 return null;
836 }
837
Tony Mak853304c2016-04-18 15:17:41 +0100838 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000839 * Detects whether we should show a lock screen in front of this task for a locked user.
840 * <p>
841 * We'll do this if either of the following holds:
842 * <ul>
843 * <li>The top activity explicitly belongs to {@param userId}.</li>
844 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
845 * </ul>
846 *
847 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100848 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000849 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
850 // To handle the case that work app is in the task but just is not the top one.
851 final ActivityRecord activityRecord = task.getTopActivity();
852 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
853
854 return (activityRecord != null && activityRecord.userId == userId)
855 || (resultTo != null && resultTo.userId == userId);
856 }
857
858 /**
859 * Find all visible task stacks containing {@param userId} and intercept them with an activity
860 * to block out the contents and possibly start a credential-confirming intent.
861 *
862 * @param userId user handle for the locked managed profile.
863 */
864 void lockAllProfileTasks(@UserIdInt int userId) {
865 mWindowManager.deferSurfaceLayout();
866 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700867 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
868 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
869 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
870 final ActivityStack stack = display.getChildAt(stackNdx);
871 final List<TaskRecord> tasks = stack.getAllTasks();
872 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
873 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000874
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700875 // Check the task for a top activity belonging to userId, or returning a
876 // result to an activity belonging to userId. Example case: a document
877 // picker for personal files, opened by a work app, should still get locked.
878 if (taskTopActivityIsUser(task, userId)) {
879 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
880 task.taskId, userId);
881 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000882 }
Tony Mak853304c2016-04-18 15:17:41 +0100883 }
884 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000885 } finally {
886 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100887 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000888 }
889
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800890 void setNextTaskIdForUserLocked(int taskId, int userId) {
891 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
892 if (taskId > currentTaskId) {
893 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700894 }
895 }
896
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700897 static int nextTaskIdForUser(int taskId, int userId) {
898 int nextTaskId = taskId + 1;
899 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
900 // Wrap around as there will be smaller task ids that are available now.
901 nextTaskId -= MAX_TASK_IDS_PER_USER;
902 }
903 return nextTaskId;
904 }
905
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800906 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800907 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
908 // for a userId u, a taskId can only be in the range
909 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
910 // 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 -0700911 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700912 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700913 || anyTaskForIdLocked(
914 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700915 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800916 if (candidateTaskId == currentTaskId) {
917 // Something wrong!
918 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
919 throw new IllegalStateException("Cannot get an available task id."
920 + " Reached limit of " + MAX_TASK_IDS_PER_USER
921 + " running tasks per user.");
922 }
923 }
924 mCurTaskIdForUser.put(userId, candidateTaskId);
925 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700926 }
927
Chong Zhang6cda19c2016-06-14 19:07:56 -0700928 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800929 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700930 if (stack == null) {
931 return null;
932 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700933 ActivityRecord resumedActivity = stack.mResumedActivity;
934 if (resumedActivity == null || resumedActivity.app == null) {
935 resumedActivity = stack.mPausingActivity;
936 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700937 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 }
939 }
940 return resumedActivity;
941 }
942
Dianne Hackbornff072722014-09-24 10:56:28 -0700943 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700944 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700947 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
948 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
949 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700950 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800951 continue;
952 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200953 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
954 final ActivityRecord top = stack.topRunningActivityLocked();
955 final int size = mTmpActivityList.size();
956 for (int i = 0; i < size; i++) {
957 final ActivityRecord activity = mTmpActivityList.get(i);
958 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
959 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200961 if (realStartActivityLocked(activity, app,
962 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 didSomething = true;
964 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700965 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200967 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700969 }
Craig Mautner20e72272013-04-01 13:45:53 -0700970 }
Craig Mautner20e72272013-04-01 13:45:53 -0700971 }
972 }
973 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700974 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700975 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700976 }
Craig Mautner20e72272013-04-01 13:45:53 -0700977 return didSomething;
978 }
979
980 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800981 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700982 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
983 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
984 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700985 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 continue;
987 }
988 final ActivityRecord resumedActivity = stack.mResumedActivity;
989 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700990 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800991 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 return false;
993 }
Craig Mautner20e72272013-04-01 13:45:53 -0700994 }
995 }
Wei Wang65c7a152016-06-02 18:51:22 -0700996 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -0800997 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700998 return true;
999 }
1000
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001003 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1004 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1005 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001006 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001007 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001008 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001009 return false;
1010 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 }
1012 }
1013 }
1014 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001015 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1017 mLastFocusedStack + " to=" + mFocusedStack);
1018 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001019 return true;
1020 }
1021
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001022 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001023 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001025 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1026 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1027 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001028 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001029 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001030 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001031 return false;
1032 }
1033 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 }
1036 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001037 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001038 }
1039
Craig Mautner2acc3892013-09-23 10:28:14 -07001040 /**
1041 * Pause all activities in either all of the stacks or just the back stacks.
1042 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001043 * @param resuming The resuming activity.
1044 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1045 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001046 * @return true if any activity was paused as a result of this call.
1047 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001048 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001049 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001050 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001051 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1052 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1053 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001054 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001055 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001056 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001057 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1058 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001059 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001060 }
1061 }
1062 return someActivityPaused;
1063 }
1064
Craig Mautnerde4ef022013-04-07 19:01:33 -07001065 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001066 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001067 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001068 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1069 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1070 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001071 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001072 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001074 Slog.d(TAG_STATES,
1075 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001076 pausing = false;
1077 } else {
1078 return false;
1079 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001080 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 }
1082 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001083 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001084 }
1085
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001086 void cancelInitializingActivities() {
1087 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001088 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1089 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1090 final ActivityStack stack = display.getChildAt(stackNdx);
1091 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001092 }
1093 }
1094 }
1095
Bryce Lee4a194382017-04-04 14:32:48 -07001096 void waitActivityVisible(ComponentName name, WaitResult result) {
1097 final WaitInfo waitInfo = new WaitInfo(name, result);
1098 mWaitingForActivityVisible.add(waitInfo);
1099 }
1100
1101 void cleanupActivity(ActivityRecord r) {
1102 // Make sure this record is no longer in the pending finishes list.
1103 // This could happen, for example, if we are trimming activities
1104 // down to the max limit while they are still waiting to finish.
1105 mFinishingActivities.remove(r);
1106 mActivitiesWaitingForVisibleActivity.remove(r);
1107
1108 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001109 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001110 mWaitingForActivityVisible.remove(i);
1111 }
1112 }
1113 }
1114
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001115 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001117 }
1118
1119 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1120 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001121 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1122 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001123 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001124 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001125 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001126 result.timeout = false;
1127 result.who = w.getComponent();
1128 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1129 result.thisTime = result.totalTime;
1130 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001131 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001132 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001133 if (changed) {
1134 mService.notifyAll();
1135 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001136 }
1137
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001138 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1139 if (mWaitingActivityLaunched.isEmpty()) {
1140 return;
1141 }
1142
1143 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1144 return;
1145 }
1146
Chong Zhang5022da32016-06-21 16:31:37 -07001147 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001148
Chong Zhang5022da32016-06-21 16:31:37 -07001149 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1150 WaitResult w = mWaitingActivityLaunched.remove(i);
1151 if (w.who == null) {
1152 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001153 w.result = result;
1154
1155 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1156 // will be no followup launch signals. Assign the result and launched component.
1157 if (result == START_DELIVERED_TO_TOP) {
1158 w.who = r.realActivity;
1159 }
Chong Zhang5022da32016-06-21 16:31:37 -07001160 }
1161 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001162
Chong Zhang5022da32016-06-21 16:31:37 -07001163 if (changed) {
1164 mService.notifyAll();
1165 }
1166 }
1167
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001168 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1169 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001170 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001171 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001172 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001173 if (w.who == null) {
1174 changed = true;
1175 w.timeout = timeout;
1176 if (r != null) {
1177 w.who = new ComponentName(r.info.packageName, r.info.name);
1178 }
1179 w.thisTime = thisTime;
1180 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001181 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001182 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001183 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001184 if (changed) {
1185 mService.notifyAll();
1186 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 }
1188
Craig Mautner29219d92013-04-16 20:19:12 -07001189 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001190 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001191 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001192 if (r != null) {
1193 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001194 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001195
Andrii Kulian7318d632016-07-20 18:59:28 -07001196 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001197 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1198
1199 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1200 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001201 final ActivityDisplay display = mActivityDisplays.get(displayId);
1202 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1203 final ActivityStack stack = display.getChildAt(j);
1204 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001205 r = stack.topRunningActivityLocked();
1206 if (r != null) {
1207 return r;
1208 }
Craig Mautner29219d92013-04-16 20:19:12 -07001209 }
1210 }
1211 }
1212 return null;
1213 }
1214
Winson Chung61c9e5a2017-10-11 10:39:32 -07001215 @VisibleForTesting
1216 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1217 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1218 int callingUid, boolean allowed) {
1219 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1220 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001221 }
1222
Todd Kennedy7440f172015-12-09 14:31:22 -08001223 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1224 ProfilerInfo profilerInfo) {
1225 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001226 if (aInfo != null) {
1227 // Store the found target back into the intent, because now that
1228 // we have it we never want to do this again. For example, if the
1229 // user navigates back to this point in the history, we should
1230 // always restart the exact same activity.
1231 intent.setComponent(new ComponentName(
1232 aInfo.applicationInfo.packageName, aInfo.name));
1233
1234 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001235 if (!aInfo.processName.equals("system")) {
1236 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001237 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001238 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001239
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001240 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1241 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1242 }
1243
Man Caocfa78b22015-06-11 20:14:34 -07001244 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1245 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1246 }
1247
1248 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001249 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001250 }
1251 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001252 final String intentLaunchToken = intent.getLaunchToken();
1253 if (aInfo.launchToken == null && intentLaunchToken != null) {
1254 aInfo.launchToken = intentLaunchToken;
1255 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001256 }
1257 return aInfo;
1258 }
1259
Todd Kennedy7440f172015-12-09 14:31:22 -08001260 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001261 return resolveIntent(intent, resolvedType, userId, 0);
1262 }
1263
1264 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001265 synchronized (mService) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001266 try {
1267 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001268 int modifiedFlags = flags
1269 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001270 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001271 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1272 modifiedFlags |= PackageManager.MATCH_INSTANT;
1273 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001274 return mService.getPackageManagerInternalLocked().resolveIntent(
Patrick Baumann577d4022018-01-31 16:55:10 +00001275 intent, resolvedType, modifiedFlags, userId, true);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001276
1277 } finally {
1278 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1279 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001280 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001281 }
1282
1283 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1284 ProfilerInfo profilerInfo, int userId) {
1285 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1286 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1287 }
1288
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001289 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1290 boolean andResume, boolean checkConfig) throws RemoteException {
1291
1292 if (!allPausedActivitiesComplete()) {
1293 // While there are activities pausing we skipping starting any new activities until
1294 // pauses are complete. NOTE: that we also do this for activities that are starting in
1295 // the paused state because they will first be resumed then paused on the client side.
1296 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1297 "realStartActivityLocked: Skipping start of r=" + r
1298 + " some activities pausing...");
1299 return false;
1300 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001301
Bryce Leeaf691c02017-03-20 14:20:22 -07001302 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001303 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001304
1305 beginDeferResume();
1306
Craig Mautner2420ead2013-04-01 17:13:20 -07001307 try {
chaviw59b98852017-06-13 12:05:44 -07001308 r.startFreezingScreenLocked(app, 0);
1309
1310 // schedule launch ticks to collect information about slow apps.
1311 r.startLaunchTickingLocked();
1312
Dianne Hackborn68a06332017-11-15 17:54:18 -08001313 r.setProcess(app);
chaviw59b98852017-06-13 12:05:44 -07001314
Jorim Jaggi838c2452017-08-28 15:44:43 +02001315 if (mKeyguardController.isKeyguardLocked()) {
1316 r.notifyUnknownVisibilityLaunched();
1317 }
1318
chaviw59b98852017-06-13 12:05:44 -07001319 // Have the window manager re-evaluate the orientation of the screen based on the new
1320 // activity order. Note that as a result of this, it can call back into the activity
1321 // manager with a new orientation. We don't care about that, because the activity is
1322 // not currently running so we are just restarting it anyway.
1323 if (checkConfig) {
1324 final int displayId = r.getDisplayId();
1325 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1326 getDisplayOverrideConfiguration(displayId),
1327 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1328 // Deferring resume here because we're going to launch new activity shortly.
1329 // We don't want to perform a redundant launch of the same record while ensuring
1330 // configurations and trying to resume top activity of focused stack.
1331 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1332 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 }
chaviw59b98852017-06-13 12:05:44 -07001334
1335 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1336 true /* isTop */)) {
1337 // We only set the visibility to true if the activity is allowed to be visible
1338 // based on
1339 // keyguard state. This avoids setting this into motion in window manager that is
1340 // later cancelled due to later calls to ensure visible activities that set
1341 // visibility back to false.
1342 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 }
chaviw59b98852017-06-13 12:05:44 -07001344
chaviw59b98852017-06-13 12:05:44 -07001345 final int applicationInfoUid =
1346 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1347 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1348 Slog.wtf(TAG,
1349 "User ID for activity changing for " + r
1350 + " appInfo.uid=" + r.appInfo.uid
1351 + " info.ai.uid=" + applicationInfoUid
1352 + " old=" + r.app + " new=" + app);
1353 }
1354
1355 app.waitingToKill = null;
1356 r.launchCount++;
1357 r.lastLaunchTime = SystemClock.uptimeMillis();
1358
1359 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1360
1361 int idx = app.activities.indexOf(r);
1362 if (idx < 0) {
1363 app.activities.add(r);
1364 }
1365 mService.updateLruProcessLocked(app, true, null);
1366 mService.updateOomAdjLocked();
1367
Charles Heff9b4dff2017-09-22 10:18:37 +01001368 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1369 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1370 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
1371 && mService.mLockTaskController.getLockTaskModeState()
1372 == LOCK_TASK_MODE_LOCKED)) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001373 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001374 }
1375
1376 try {
1377 if (app.thread == null) {
1378 throw new RemoteException();
1379 }
1380 List<ResultInfo> results = null;
1381 List<ReferrerIntent> newIntents = null;
1382 if (andResume) {
1383 // We don't need to deliver new intents and/or set results if activity is going
1384 // to pause immediately after launch.
1385 results = r.results;
1386 newIntents = r.newIntents;
1387 }
1388 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1389 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1390 + " newIntents=" + newIntents + " andResume=" + andResume);
1391 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1392 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001393 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001394 // Home process is the root process of the task.
1395 mService.mHomeProcess = task.mActivities.get(0).app;
1396 }
1397 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1398 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1399 r.sleeping = false;
1400 r.forceNewConfig = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05001401 mService.getAppWarningsLocked().onStartActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001402 mService.showAskCompatModeDialogLocked(r);
1403 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1404 ProfilerInfo profilerInfo = null;
1405 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1406 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1407 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001408 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1409 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1410 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001411 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001412 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001413 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001414 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001415 }
1416 }
chaviw59b98852017-06-13 12:05:44 -07001417
Andreas Gampe2b073a02017-06-22 17:28:57 -07001418 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001419 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001420 }
1421 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001422
chaviw59b98852017-06-13 12:05:44 -07001423 app.hasShownUi = true;
1424 app.pendingUiClean = true;
1425 app.forceProcessStateUpTo(mService.mTopProcessState);
1426 // Because we could be starting an Activity in the system process this may not go
1427 // across a Binder interface which would create a new Configuration. Consequently
1428 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001429
chaviw59b98852017-06-13 12:05:44 -07001430 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1431 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1432 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001433
chaviw59b98852017-06-13 12:05:44 -07001434 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001435
1436
1437 // Create activity launch transaction.
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001438 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001439 r.appToken);
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001440 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001441 System.identityHashCode(r), r.info,
1442 // TODO: Have this take the merged configuration instead of separate global
1443 // and override configs.
1444 mergedConfiguration.getGlobalConfiguration(),
1445 mergedConfiguration.getOverrideConfiguration(), r.compat,
1446 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001447 r.persistentState, results, newIntents, mService.isNextTransitionForward(),
1448 profilerInfo));
1449
1450 // Set desired final state.
1451 final ActivityLifecycleItem lifecycleItem;
1452 if (andResume) {
Bryce Leed946f862018-01-16 15:59:47 -08001453 lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward())
1454 .setDescription(r.getLifecycleDescription("realStartActivityLocked"));
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001455 } else {
Bryce Leef52974c2018-02-14 15:12:01 -08001456 lifecycleItem = PauseActivityItem.obtain()
1457 .setDescription(r.getLifecycleDescription("realStartActivityLocked"));
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001458 }
1459 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1460
1461 // Schedule transaction.
1462 mService.mLifecycleManager.scheduleTransaction(clientTransaction);
1463
Craig Mautner2420ead2013-04-01 17:13:20 -07001464
chaviw59b98852017-06-13 12:05:44 -07001465 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1466 // This may be a heavy-weight process! Note that the package
1467 // manager will ensure that only activity can run in the main
1468 // process of the .apk, which is the only thing that will be
1469 // considered heavy-weight.
1470 if (app.processName.equals(app.info.packageName)) {
1471 if (mService.mHeavyWeightProcess != null
1472 && mService.mHeavyWeightProcess != app) {
1473 Slog.w(TAG, "Starting new heavy weight process " + app
1474 + " when already running "
1475 + mService.mHeavyWeightProcess);
1476 }
1477 mService.mHeavyWeightProcess = app;
1478 Message msg = mService.mHandler.obtainMessage(
1479 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1480 msg.obj = r;
1481 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001482 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001483 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001484
chaviw59b98852017-06-13 12:05:44 -07001485 } catch (RemoteException e) {
1486 if (r.launchFailed) {
1487 // This is the second time we failed -- finish activity
1488 // and give up.
1489 Slog.e(TAG, "Second failure launching "
1490 + r.intent.getComponent().flattenToShortString()
1491 + ", giving up", e);
1492 mService.appDiedLocked(app);
1493 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1494 "2nd-crash", false);
1495 return false;
1496 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001497
chaviw59b98852017-06-13 12:05:44 -07001498 // This is the first time we failed -- restart process and
1499 // retry.
1500 r.launchFailed = true;
1501 app.activities.remove(r);
1502 throw e;
1503 }
1504 } finally {
1505 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001506 }
1507
1508 r.launchFailed = false;
1509 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001510 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001511 }
1512
Andrii Kulianb372da62018-01-18 10:46:24 -08001513 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1514 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001515 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001516 // As part of the process of launching, ActivityThread also performs
1517 // a resume.
1518 stack.minimalResumeActivityLocked(r);
1519 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001520 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001521 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001522 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001523 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001524 "Moving to PAUSED: " + r + " (starting in paused state)");
1525 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001526 }
1527
1528 // Launch the new version setup screen if needed. We do this -after-
1529 // launching the initial activity (that is, home), so that it can have
1530 // a chance to initialize itself while in the background, making the
1531 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001532 if (isFocusedStack(stack)) {
Bryce Leed3624e12017-11-30 08:51:45 -08001533 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001534 }
1535
Dianne Hackborn465fa392014-09-14 14:21:18 -07001536 // Update any services we are bound to that might care about whether
1537 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001538 if (r.app != null) {
1539 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1540 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001541
Craig Mautner2420ead2013-04-01 17:13:20 -07001542 return true;
1543 }
1544
Sudheer Shankafab200f2017-05-17 20:41:53 -07001545 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1546 int extrasSize = 0;
1547 if (intent != null) {
1548 final Bundle extras = intent.getExtras();
1549 if (extras != null) {
1550 extrasSize = extras.getSize();
1551 }
1552 }
1553 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1554 if (extrasSize + icicleSize > 200000) {
1555 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1556 + ", icicle size: " + icicleSize);
1557 }
1558 }
1559
Craig Mautnere79d42682013-04-01 19:01:53 -07001560 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001561 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001562 // Is this activity's application already running?
1563 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001564 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001565
Andrii Kulian02b7a832016-10-06 23:11:56 -07001566 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001567
1568 if (app != null && app.thread != null) {
1569 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001570 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1571 || !"android".equals(r.info.packageName)) {
1572 // Don't add this if it is a platform component that is marked
1573 // to run in multiple processes, because this is actually
1574 // part of the framework so doesn't make sense to track as a
1575 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001576 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1577 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001578 }
George Mount2c92c972014-03-20 09:38:23 -07001579 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001580 return;
1581 } catch (RemoteException e) {
1582 Slog.w(TAG, "Exception when starting activity "
1583 + r.intent.getComponent().flattenToShortString(), e);
1584 }
1585
1586 // If a dead object exception was thrown -- fall through to
1587 // restart the application.
1588 }
1589
1590 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001591 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001592 }
1593
Bryce Leed3624e12017-11-30 08:51:45 -08001594 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1595 boolean sendHint = forceSend;
1596
1597 if (!sendHint) {
1598 // If not forced, send power hint when the activity's process is different than the
1599 // current resumed activity.
1600 final ActivityRecord resumedActivity = getResumedActivityLocked();
1601 sendHint = resumedActivity == null
1602 || resumedActivity.app == null
1603 || !resumedActivity.app.equals(targetActivity.app);
1604 }
1605
1606 if (sendHint && mService.mLocalPowerManager != null) {
1607 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 1);
1608 mPowerHintSent = true;
1609 }
1610 }
1611
1612 void sendPowerHintForLaunchEndIfNeeded() {
1613 // Trigger launch power hint if activity is launched
1614 if (mPowerHintSent && mService.mLocalPowerManager != null) {
1615 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 0);
1616 mPowerHintSent = false;
1617 }
1618 }
1619
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001620 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001621 String resultWho, int requestCode, int callingPid, int callingUid,
1622 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001623 ActivityRecord resultRecord, ActivityStack resultStack) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001624 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1625 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001626 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001627 return true;
1628 }
1629 final int componentRestriction = getComponentRestrictionForCallingPackage(
1630 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1631 final int actionRestriction = getActionRestrictionForCallingPackage(
1632 intent.getAction(), callingPackage, callingPid, callingUid);
1633 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1634 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1635 if (resultRecord != null) {
1636 resultStack.sendActivityResultLocked(-1,
1637 resultRecord, resultWho, requestCode,
1638 Activity.RESULT_CANCELED, null);
1639 }
1640 final String msg;
1641 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1642 msg = "Permission Denial: starting " + intent.toString()
1643 + " from " + callerApp + " (pid=" + callingPid
1644 + ", uid=" + callingUid + ")" + " with revoked permission "
1645 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1646 } else if (!aInfo.exported) {
1647 msg = "Permission Denial: starting " + intent.toString()
1648 + " from " + callerApp + " (pid=" + callingPid
1649 + ", uid=" + callingUid + ")"
1650 + " not exported from uid " + aInfo.applicationInfo.uid;
1651 } else {
1652 msg = "Permission Denial: starting " + intent.toString()
1653 + " from " + callerApp + " (pid=" + callingPid
1654 + ", uid=" + callingUid + ")"
1655 + " requires " + aInfo.permission;
1656 }
1657 Slog.w(TAG, msg);
1658 throw new SecurityException(msg);
1659 }
1660
1661 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1662 final String message = "Appop Denial: starting " + intent.toString()
1663 + " from " + callerApp + " (pid=" + callingPid
1664 + ", uid=" + callingUid + ")"
1665 + " requires " + AppOpsManager.permissionToOp(
1666 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1667 Slog.w(TAG, message);
1668 return false;
1669 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1670 final String message = "Appop Denial: starting " + intent.toString()
1671 + " from " + callerApp + " (pid=" + callingPid
1672 + ", uid=" + callingUid + ")"
1673 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1674 Slog.w(TAG, message);
1675 return false;
1676 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001677
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001678 return true;
1679 }
1680
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001681 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001682 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1683 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001684 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1685 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1686
Andrii Kulian02689a72017-07-06 14:28:59 -07001687 if (callingPid == -1 && callingUid == -1) {
1688 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1689 return true;
1690 }
1691
Andrii Kulian62e6f252017-05-30 22:46:53 -07001692 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001693 if (activityDisplay == null) {
1694 Slog.w(TAG, "Launch on display check: display not found");
1695 return false;
1696 }
1697
Andrii Kulian02689a72017-07-06 14:28:59 -07001698 // Check if the caller has enough privileges to embed activities and launch to private
1699 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001700 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001701 callingUid);
1702 if (startAnyPerm == PERMISSION_GRANTED) {
1703 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1704 + " allow launch any on display");
1705 return true;
1706 }
1707
Andrii Kulian8f070292017-09-12 22:56:49 -07001708 // Check if caller is already present on display
1709 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1710
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001711 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1712 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1713 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001714 // Limit launching on virtual displays, because their contents can be read from Surface
1715 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001716 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1717 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1718 + " disallow launch on virtual display for not-embedded activity.");
1719 return false;
1720 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001721 // Check if the caller is allowed to embed activities from other apps.
1722 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001723 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001724 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1725 + " disallow activity embedding without permission.");
1726 return false;
1727 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001728 }
1729
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001730 if (!activityDisplay.isPrivate()) {
1731 // Anyone can launch on a public display.
1732 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1733 + " allow launch on public display");
1734 return true;
1735 }
1736
1737 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001738 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001739 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1740 + " allow launch for owner of the display");
1741 return true;
1742 }
1743
Andrii Kulian8f070292017-09-12 22:56:49 -07001744 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001745 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1746 + " allow launch for caller present on the display");
1747 return true;
1748 }
1749
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001750 Slog.w(TAG, "Launch on display check: denied");
1751 return false;
1752 }
1753
1754 /** Update lists of UIDs that are present on displays and have access to them. */
1755 void updateUIDsPresentOnDisplay() {
1756 mDisplayAccessUIDs.clear();
1757 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1758 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001759 // Only bother calculating the whitelist for private displays
1760 if (activityDisplay.isPrivate()) {
1761 mDisplayAccessUIDs.append(
1762 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1763 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001764 }
1765 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1766 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1767 }
1768
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001769 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001770 final long identity = Binder.clearCallingIdentity();
1771 try {
1772 return UserManager.get(mService.mContext).getUserInfo(userId);
1773 } finally {
1774 Binder.restoreCallingIdentity(identity);
1775 }
1776 }
1777
Svet Ganov99b60432015-06-27 13:15:22 -07001778 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001779 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001780 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1781 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001782 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001783 return ACTIVITY_RESTRICTION_PERMISSION;
1784 }
1785
Christopher Tateff7add02015-08-17 10:23:22 -07001786 if (activityInfo.permission == null) {
1787 return ACTIVITY_RESTRICTION_NONE;
1788 }
1789
Svet Ganov99b60432015-06-27 13:15:22 -07001790 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1791 if (opCode == AppOpsManager.OP_NONE) {
1792 return ACTIVITY_RESTRICTION_NONE;
1793 }
1794
1795 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1796 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001797 if (!ignoreTargetSecurity) {
1798 return ACTIVITY_RESTRICTION_APPOP;
1799 }
Svet Ganov99b60432015-06-27 13:15:22 -07001800 }
1801
1802 return ACTIVITY_RESTRICTION_NONE;
1803 }
1804
Svetoslav7008b512015-06-24 18:47:07 -07001805 private int getActionRestrictionForCallingPackage(String action,
1806 String callingPackage, int callingPid, int callingUid) {
1807 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001808 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001809 }
1810
1811 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1812 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001813 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001814 }
1815
1816 final PackageInfo packageInfo;
1817 try {
1818 packageInfo = mService.mContext.getPackageManager()
1819 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1820 } catch (PackageManager.NameNotFoundException e) {
1821 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001822 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001823 }
1824
1825 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001826 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001827 }
1828
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001829 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001830 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001831 }
1832
1833 final int opCode = AppOpsManager.permissionToOpCode(permission);
1834 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001835 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001836 }
1837
1838 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1839 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001840 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001841 }
1842
Svet Ganov99b60432015-06-27 13:15:22 -07001843 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001844 }
1845
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001846 void setLaunchSource(int uid) {
1847 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1848 }
1849
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001850 void acquireLaunchWakelock() {
1851 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1852 throw new IllegalStateException("Calling must be system uid");
1853 }
1854 mLaunchingActivity.acquire();
1855 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1856 // To be safe, don't allow the wake lock to be held for too long.
1857 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1858 }
1859 }
1860
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001861 /**
1862 * Called when the frontmost task is idle.
1863 * @return the state of mService.mBooting before this was called.
1864 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001865 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001866 private boolean checkFinishBootingLocked() {
1867 final boolean booting = mService.mBooting;
1868 boolean enableScreen = false;
1869 mService.mBooting = false;
1870 if (!mService.mBooted) {
1871 mService.mBooted = true;
1872 enableScreen = true;
1873 }
1874 if (booting || enableScreen) {
1875 mService.postFinishBooting(booting, enableScreen);
1876 }
1877 return booting;
1878 }
1879
Craig Mautnerf3333272013-04-22 10:55:53 -07001880 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001881 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001883 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001884 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001885
Craig Mautnerf3333272013-04-22 10:55:53 -07001886 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001887 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001888 int NS = 0;
1889 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001891 boolean activityRemoved = false;
1892
Wale Ogunwale7d701172015-03-11 15:36:30 -07001893 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001894 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001895 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1896 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001897 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1898 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001899 if (fromTimeout) {
1900 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001901 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001902
1903 // This is a hack to semi-deal with a race condition
1904 // in the client where it can be constructed with a
1905 // newer configuration from when we asked it to launch.
1906 // We'll update with whatever configuration it now says
1907 // it used to launch.
1908 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001909 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001910 }
1911
1912 // We are now idle. If someone is waiting for a thumbnail from
1913 // us, we can now deliver.
1914 r.idle = true;
1915
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001916 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001917 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001918 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001919 }
1920 }
1921
1922 if (allResumedActivitiesIdle()) {
1923 if (r != null) {
1924 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001925 }
1926
1927 if (mLaunchingActivity.isHeld()) {
1928 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1929 if (VALIDATE_WAKE_LOCK_CALLER &&
1930 Binder.getCallingUid() != Process.myUid()) {
1931 throw new IllegalStateException("Calling must be system uid");
1932 }
1933 mLaunchingActivity.release();
1934 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001935 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001936 }
1937
1938 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001939 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1940 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001941 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001942 if ((NF = mFinishingActivities.size()) > 0) {
1943 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001944 mFinishingActivities.clear();
1945 }
1946
Craig Mautnerf3333272013-04-22 10:55:53 -07001947 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001948 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001949 mStartingUsers.clear();
1950 }
1951
Craig Mautnerf3333272013-04-22 10:55:53 -07001952 // Stop any activities that are scheduled to do so but have been
1953 // waiting for the next one to start.
1954 for (int i = 0; i < NS; i++) {
1955 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001956 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001957 if (stack != null) {
1958 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08001959 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
1960 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07001961 } else {
1962 stack.stopActivityLocked(r);
1963 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001964 }
1965 }
1966
1967 // Finish any activities that are scheduled to do so but have been
1968 // waiting for the next one to start.
1969 for (int i = 0; i < NF; i++) {
1970 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001971 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001972 if (stack != null) {
1973 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1974 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001975 }
1976
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001977 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001978 // Complete user switch
1979 if (startingUsers != null) {
1980 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001981 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001982 }
1983 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001984 }
1985
1986 mService.trimApplications();
1987 //dump();
1988 //mWindowManager.dump();
1989
Craig Mautnerf3333272013-04-22 10:55:53 -07001990 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001991 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001992 }
1993
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001994 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001995 }
1996
Craig Mautner8e569572013-10-11 17:36:59 -07001997 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001998 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001999 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002000 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2001 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2002 final ActivityStack stack = display.getChildAt(stackNdx);
2003 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002004 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002005 }
Craig Mautner19091252013-10-05 00:03:53 -07002006 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002007 }
2008
2009 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002011 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2012 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2013 final ActivityStack stack = display.getChildAt(stackNdx);
2014 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002015 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002016 }
2017 }
2018
Craig Mautner93529a42013-10-04 15:03:13 -07002019 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002020 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002021 }
2022
Craig Mautner8d341ef2013-03-26 09:03:27 -07002023 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002024 * Update the last used stack id for non-current user (current user's last
2025 * used stack is the focused stack)
2026 */
2027 void updateUserStackLocked(int userId, ActivityStack stack) {
2028 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002029 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002030 }
2031 }
2032
2033 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002034 * @return true if some activity was finished (or would have finished if doit were true).
2035 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002036 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2037 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002038 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002039 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002040 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2041 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2042 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002043 if (stack.finishDisabledPackageActivitiesLocked(
2044 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002045 didSomething = true;
2046 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002047 }
2048 }
2049 return didSomething;
2050 }
2051
Dianne Hackborna413dc02013-07-12 12:02:55 -07002052 void updatePreviousProcessLocked(ActivityRecord r) {
2053 // Now that this process has stopped, we may want to consider
2054 // it to be the previous app to try to keep around in case
2055 // the user wants to return to it.
2056
2057 // First, found out what is currently the foreground app, so that
2058 // we don't blow away the previous app if this activity is being
2059 // hosted by the process that is actually still the foreground.
2060 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002061 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002062 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2063 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2064 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002065 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002066 if (stack.mResumedActivity != null) {
2067 fgApp = stack.mResumedActivity.app;
2068 } else if (stack.mPausingActivity != null) {
2069 fgApp = stack.mPausingActivity.app;
2070 }
2071 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002072 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002073 }
2074 }
2075
2076 // Now set this one as the previous process, only if that really
2077 // makes sense to.
2078 if (r.app != null && fgApp != null && r.app != fgApp
2079 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002080 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002081 mService.mPreviousProcess = r.app;
2082 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2083 }
2084 }
2085
Wale Ogunwaled046a012015-12-24 13:05:59 -08002086 boolean resumeFocusedStackTopActivityLocked() {
2087 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002088 }
2089
Wale Ogunwaled046a012015-12-24 13:05:59 -08002090 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002091 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002092
2093 if (!readyToResume()) {
2094 return false;
2095 }
2096
Wale Ogunwaled046a012015-12-24 13:05:59 -08002097 if (targetStack != null && isFocusedStack(targetStack)) {
2098 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002099 }
chaviw59b98852017-06-13 12:05:44 -07002100
Wale Ogunwale06579d62016-04-30 15:29:06 -07002101 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2102 if (r == null || r.state != RESUMED) {
2103 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002104 } else if (r.state == RESUMED) {
2105 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2106 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002107 }
chaviw59b98852017-06-13 12:05:44 -07002108
Wale Ogunwaled046a012015-12-24 13:05:59 -08002109 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002110 }
2111
Todd Kennedy39bfee52016-02-24 10:28:21 -08002112 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2113 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002114 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2115 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2116 final ActivityStack stack = display.getChildAt(stackNdx);
2117 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002118 }
2119 }
2120 }
2121
Adrian Roos20d7df32016-01-12 18:59:43 +01002122 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2123 TaskRecord finishedTask = null;
2124 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002125 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002126 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2127 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002128 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002129 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002130 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2131 if (stack == focusedStack || finishedTask == null) {
2132 finishedTask = t;
2133 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002134 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002135 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002136 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 }
2138
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002139 void finishVoiceTask(IVoiceInteractionSession session) {
2140 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002141 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2142 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002143 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002144 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002145 stack.finishVoiceTask(session);
2146 }
2147 }
2148 }
2149
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002150 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2151 boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002152 final ActivityStack currentStack = task.getStack();
2153 if (currentStack == null) {
2154 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2155 + task + " to front. Stack is null");
2156 return;
2157 }
2158
Craig Mautneraea74a52014-03-08 14:23:10 -08002159 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2160 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002161 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002162
2163 final ActivityRecord prev = topRunningActivityLocked();
2164
2165 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2166 || (prev != null && prev.isActivityTypeRecents())) {
2167 // Caller wants the home activity moved with it or the previous task is recents in which
2168 // case we always return home from the task we are moving to the front.
2169 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002170 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002171
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002172 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002173 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002174 task.updateOverrideConfiguration(bounds);
2175
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002176 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002177
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002178 if (stack != currentStack) {
2179 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002180 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002181 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002182 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2183 // still need moveTaskToFrontLocked() below for any transition settings.
2184 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002185 if (stack.resizeStackWithLaunchBounds()) {
2186 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002187 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2188 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002189 } else {
2190 // WM resizeTask must be done after the task is moved to the correct stack,
2191 // because Task's setBounds() also updates dim layer's bounds, but that has
2192 // dependency on the stack.
2193 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002194 }
2195 }
2196
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002197 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002198 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002199 r == null ? null : r.appTimeTracker, reason);
2200
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002201 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002202 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002203
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002204 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002205 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002206 }
2207
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002208 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002209 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002210 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002211 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002212 return false;
2213 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002214 return (mService.mSupportsPictureInPicture
2215 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002216 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002217 }
2218
Bryce Leeec55eb02017-12-05 20:51:27 -08002219 LaunchParamsController getLaunchParamsController() {
2220 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002221 }
2222
Winson Chung55893332017-02-17 17:13:10 -08002223 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002224 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2225 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2226 if (stack != null) {
2227 return stack;
2228 }
2229 }
2230 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002231 }
2232
Wale Ogunwale68278562017-09-23 17:13:55 -07002233 /** @see ActivityDisplay#getStack(int, int) */
2234 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2235 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2236 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2237 if (stack != null) {
2238 return stack;
2239 }
2240 }
2241 return null;
2242 }
2243
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002244 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002245 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002246 // Preference is given to the activity type for the activity then the task since the type
2247 // once set shouldn't change.
2248 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2249 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2250 activityType = task.getActivityType();
2251 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002252 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2253 return activityType;
2254 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002255 if (options != null) {
2256 activityType = options.getLaunchActivityType();
2257 }
2258 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002259 }
2260
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002261 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2262 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2263 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002264 }
2265
2266 /**
2267 * Returns the right stack to use for launching factoring in all the input parameters.
2268 *
2269 * @param r The activity we are trying to launch. Can be null.
2270 * @param options The activity options used to the launch. Can be null.
2271 * @param candidateTask The possible task the activity might be launched in. Can be null.
2272 *
2273 * @return The stack to use for the launch or INVALID_STACK_ID.
2274 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002275 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2276 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2277 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002278 int taskId = INVALID_TASK_ID;
2279 int displayId = INVALID_DISPLAY;
2280 //Rect bounds = null;
2281
2282 // We give preference to the launch preference in activity options.
2283 if (options != null) {
2284 taskId = options.getLaunchTaskId();
2285 displayId = options.getLaunchDisplayId();
2286 // TODO: Need to work this into the equation...
2287 //bounds = options.getLaunchBounds();
2288 }
2289
2290 // First preference for stack goes to the task Id set in the activity options. Use the stack
2291 // associated with that if possible.
2292 if (taskId != INVALID_TASK_ID) {
2293 // Temporarily set the task id to invalid in case in re-entry.
2294 options.setLaunchTaskId(INVALID_TASK_ID);
2295 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002296 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002297 options.setLaunchTaskId(taskId);
2298 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002299 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002300 }
2301 }
2302
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002303 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002304 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002305
2306 // Next preference for stack goes to the display Id set in the activity options or the
2307 // candidate display.
2308 if (displayId == INVALID_DISPLAY) {
2309 displayId = candidateDisplayId;
2310 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002311 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002312 if (r != null) {
2313 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002314 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002315 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002316 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002317 }
2318 }
2319 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2320 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002321 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002322 if (stack != null) {
2323 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002324 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002325 }
2326 }
2327
2328 // Give preference to the stack and display of the input task and activity if they match the
2329 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002330 stack = null;
2331 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002332 if (candidateTask != null) {
2333 stack = candidateTask.getStack();
2334 }
2335 if (stack == null && r != null) {
2336 stack = r.getStack();
2337 }
2338 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002339 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002340 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002341 final int windowingMode =
2342 display.resolveWindowingMode(r, options, candidateTask, activityType);
2343 if (stack.isCompatible(windowingMode, activityType)) {
2344 return stack;
2345 }
2346 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002347 }
2348
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002349 if (display == null
Andrii Kulianb850ea52017-12-12 23:49:10 -08002350 || !canLaunchOnDisplay(r, display.mDisplayId)
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002351 // TODO: Can be removed once we figure-out how non-standard types should launch
2352 // outside the default display.
2353 || (activityType != ACTIVITY_TYPE_STANDARD
2354 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2355 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002356 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002357
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002358 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002359 }
2360
Andrii Kulianb850ea52017-12-12 23:49:10 -08002361 /** @return true if activity record is null or can be launched on provided display. */
2362 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2363 if (r == null) {
2364 return true;
2365 }
2366 return r.canBeLaunchedOnDisplay(displayId);
2367 }
2368
Andrii Kulian16802aa2016-11-02 12:21:33 -07002369 /**
2370 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2371 * If there is no such stack, new dynamic stack can be created.
2372 * @param displayId Target display.
2373 * @param r Activity that should be launched there.
2374 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2375 */
2376 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002377 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002378 if (activityDisplay == null) {
2379 throw new IllegalArgumentException(
2380 "Display with displayId=" + displayId + " not found.");
2381 }
2382
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002383 if (!r.canBeLaunchedOnDisplay(displayId)) {
2384 return null;
2385 }
2386
Andrii Kulian16802aa2016-11-02 12:21:33 -07002387 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002388 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2389 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002390 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002391 return stack;
2392 }
2393 }
2394
2395 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002396 if (displayId != DEFAULT_DISPLAY) {
2397 return activityDisplay.createStack(
2398 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002399 }
2400
2401 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2402 return null;
2403 }
2404
Wale Ogunwale68278562017-09-23 17:13:55 -07002405 // TODO: Can probably be consolidated into getLaunchStack()...
2406 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2407 switch (stack.getActivityType()) {
2408 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2409 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2410 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002411 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002412 switch (stack.getWindowingMode()) {
2413 case WINDOWING_MODE_FULLSCREEN: return true;
2414 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2415 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2416 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2417 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2418 }
2419
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002420 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002421 return r.canBeLaunchedOnDisplay(displayId);
2422 }
2423 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2424 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002425 }
2426
Andrii Kulian7fc22812016-12-28 13:04:11 -08002427 /**
2428 * Get next focusable stack in the system. This will search across displays and stacks
2429 * in last-focused order for a focusable and visible stack, different from the target stack.
2430 *
2431 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2432 * searching for next candidate.
2433 * @return Next focusable {@link ActivityStack}, null if not found.
2434 */
2435 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2436 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2437
2438 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2439 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002440 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002441 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2442 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2443 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002444 if (stack != currentFocus && stack.isFocusable()
2445 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002446 return stack;
2447 }
2448 }
2449 }
2450
2451 return null;
2452 }
2453
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002454 /**
2455 * Get next valid stack for launching provided activity in the system. This will search across
2456 * displays and stacks in last-focused order for a focusable and visible stack, except those
2457 * that are on a currently focused display.
2458 *
2459 * @param r The activity that is being launched.
2460 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2461 * searching for the next candidate.
2462 * @return Next valid {@link ActivityStack}, null if not found.
2463 */
2464 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2465 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2466 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2467 final int displayId = mTmpOrderedDisplayIds.get(i);
2468 if (displayId == currentFocus) {
2469 continue;
2470 }
2471 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2472 if (stack != null) {
2473 return stack;
2474 }
2475 }
2476 return null;
2477 }
2478
Craig Mautneree2e45a2014-06-27 12:10:03 -07002479 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002480 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002481 }
2482
2483 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002484 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2485 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2486 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002487 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002488 final ArrayList<ActivityRecord> activities = task.mActivities;
2489 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2490 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002491 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002492 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002493 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002494 }
2495 }
2496 }
2497 }
2498 return null;
2499 }
2500
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002501 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2502 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2503 boolean deferResume) {
2504
2505 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002506 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002507 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002508 return;
2509 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002510
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002511 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002512 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002513 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002514 // If the docked stack exists, don't resize non-floating stacks independently of the
2515 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002516 return;
2517 }
2518
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002519 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002520 mWindowManager.deferSurfaceLayout();
2521 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002522 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002523 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2524 // null bounds = fullscreen windowing mode...at least for now.
2525 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2526 } else if (splitScreenActive) {
2527 // If we are in split-screen mode and this stack support split-screen, then
2528 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2529 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2530 }
2531 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002532 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002533 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002534 stack.ensureVisibleActivitiesConfigurationLocked(
2535 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002536 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002537 } finally {
2538 mWindowManager.continueSurfaceLayout();
2539 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002540 }
2541 }
2542
Bryce Leed3624e12017-11-30 08:51:45 -08002543 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002544 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002545 if (stack != null) {
2546 stack.deferUpdateBounds();
2547 }
2548 }
2549
Bryce Leed3624e12017-11-30 08:51:45 -08002550 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002551 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002552 if (stack != null) {
2553 stack.continueUpdateBounds();
2554 }
2555 }
2556
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002557 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002558 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002559 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2560 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002561 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002562 if (task != null) {
2563 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002564 }
2565 }
2566 mResizingTasksDuringAnimation.clear();
2567 }
2568
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002569 /**
2570 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002571 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002572 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002573 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002574 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002575
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002576 mWindowManager.deferSurfaceLayout();
2577 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002578 final int windowingMode = fromStack.getWindowingMode();
2579 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2580 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2581
2582 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002583 // Tell the display we are exiting split-screen mode.
2584 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002585 // We are moving all tasks from the docked stack to the fullscreen stack,
2586 // which is dismissing the docked stack, so resize all other stacks to
2587 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002588 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2589 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002590 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2591 continue;
2592 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002593 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002594 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002595 }
2596
2597 // Also disable docked stack resizing since we have manually adjusted the
2598 // size of other stacks above and we don't want to trigger a docked stack
2599 // resize when we remove task from it below and it is detached from the
2600 // display because it no longer contains any tasks.
2601 mAllowDockedStackResize = false;
2602 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002603
Winson Chung5af42fc2017-03-24 17:11:33 -07002604 // If we are moving from the pinned stack, then the animation takes care of updating
2605 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002606 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002607 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002608
Wale Ogunwale388945c2017-10-04 12:13:46 -07002609 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002610 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002611 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002612 for (int i = 0; i < size; ++i) {
2613 final TaskRecord task = tasks.get(i);
2614 final ActivityStack toStack = toDisplay.getOrCreateStack(
2615 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002616
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002617 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002618 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002619 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002620 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002621 isTopTask /* animate */, DEFER_RESUME,
2622 schedulePictureInPictureModeChange,
2623 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08002624 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
2625 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002626 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002627 // Position the tasks in the fullscreen stack in order at the bottom of the
2628 // stack. Also defer resume until all the tasks have been moved to the
2629 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002630 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002631 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2632 schedulePictureInPictureModeChange,
2633 "moveTasksToFullscreenStack - NOT_onTop");
2634 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002635 }
2636 }
Winson Chung74666102017-02-22 17:49:24 -08002637
2638 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2639 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002640 } finally {
2641 mAllowDockedStackResize = true;
2642 mWindowManager.continueSurfaceLayout();
2643 }
2644 }
2645
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002646 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002647 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2648 }
2649
2650 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2651 mWindowManager.inSurfaceTransaction(() ->
2652 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002653 }
2654
Jorim Jaggidc249c42015-12-15 14:57:31 -08002655 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002656 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2657 boolean preserveWindows) {
2658 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2659 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2660 false /* deferResume */);
2661 }
2662
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002663 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002664 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2665 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002666
2667 if (!mAllowDockedStackResize) {
2668 // Docked stack resize currently disabled.
2669 return;
2670 }
2671
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002672 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002673 if (stack == null) {
2674 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2675 return;
2676 }
2677
2678 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2679 mWindowManager.deferSurfaceLayout();
2680 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002681 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2682 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002683 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002684 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002685
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002686 // TODO: Checking for isAttached might not be needed as if the user passes in null
2687 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002688 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2689 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002690 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002691 // In this case we make all other static stacks fullscreen and move all
2692 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002693 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002694
2695 // stack shouldn't contain anymore activities, so nothing to resume.
2696 r = null;
2697 } else {
2698 // Docked stacks occupy a dedicated region on screen so the size of all other
2699 // static stacks need to be adjusted so they don't overlap with the docked stack.
2700 // We get the bounds to use from window manager which has been adjusted for any
2701 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002702 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002703 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002704 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2705 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002706 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002707 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002708 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002709 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002710 continue;
2711 }
2712 // Need to set windowing mode here before we try to get the dock bounds.
2713 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2714 current.getStackDockedModeBounds(
2715 tempOtherTaskBounds /* currentTempTaskBounds */,
2716 tempRect /* outStackBounds */,
2717 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2718
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002719 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002720 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2721 tempOtherTaskInsetBounds, preserveWindows,
2722 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002723 }
2724 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002725 if (!deferResume) {
2726 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2727 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002728 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002729 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002730 mWindowManager.continueSurfaceLayout();
2731 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2732 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002733 }
2734
Robert Carr0d00c2e2016-02-29 17:45:02 -08002735 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002736 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002737 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002738 if (stack == null) {
2739 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2740 return;
2741 }
Winson Chung19953ca2017-04-11 11:19:23 -07002742
2743 // It is possible for the bounds animation from the WM to call this but be delayed by
2744 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2745 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2746 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2747 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002748 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002749 return;
2750 }
2751
Robert Carr0d00c2e2016-02-29 17:45:02 -08002752 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2753 mWindowManager.deferSurfaceLayout();
2754 try {
2755 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002756 Rect insetBounds = null;
2757 if (tempPinnedTaskBounds != null) {
2758 // We always use 0,0 as the position for the inset rect because
2759 // if we are getting insets at all in the pinned stack it must mean
2760 // we are headed for fullscreen.
2761 insetBounds = tempRect;
2762 insetBounds.top = 0;
2763 insetBounds.left = 0;
2764 insetBounds.right = tempPinnedTaskBounds.width();
2765 insetBounds.bottom = tempPinnedTaskBounds.height();
2766 }
2767 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002768 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002769 } finally {
2770 mWindowManager.continueSurfaceLayout();
2771 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2772 }
2773 }
2774
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002775 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002776 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002777 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002778 /**
2779 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2780 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2781 * that the client receives onStop() before it is reparented. We do this by detaching
2782 * the stack from the display so that it will be considered invisible when
2783 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2784 * invisible as well and added to the stopping list. After which we process the
2785 * stopping list by handling the idle.
2786 */
2787 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2788 pinnedStack.mForceHidden = true;
2789 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2790 pinnedStack.mForceHidden = false;
2791 activityIdleInternalLocked(null, false /* fromTimeout */,
2792 true /* processPausingActivites */, null /* configuration */);
2793
2794 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002795 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002796 } else {
2797 for (int i = tasks.size() - 1; i >= 0; i--) {
2798 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07002799 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08002800 }
2801 }
2802 }
2803
2804 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002805 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002806 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2807 * instead moved back onto the fullscreen stack.
2808 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002809 void removeStack(ActivityStack stack) {
2810 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002811 }
2812
Wale Ogunwale68278562017-09-23 17:13:55 -07002813 /**
2814 * Removes stacks in the input windowing modes from the system if they are of activity type
2815 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2816 */
2817 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002818 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002819 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2820 }
2821 }
2822
2823 void removeStacksWithActivityTypes(int... activityTypes) {
2824 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2825 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002826 }
Robert Carr6914f082017-03-20 19:04:30 -07002827 }
2828
2829 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002830 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2831 */
Winson Chung0ec2a352017-10-26 11:38:30 -07002832 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2833 String reason) {
2834 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
2835 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07002836 }
2837
2838 /**
Winson Chung010927a2016-12-15 16:12:35 -08002839 * Removes the task with the specified task id.
2840 *
2841 * @param taskId Identifier of the task to be removed.
2842 * @param killProcess Kill any process associated with the task if possible.
2843 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002844 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2845 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002846 * @return Returns true if the given task was found and removed.
2847 */
Winson Chung6954fc92017-03-24 16:22:12 -07002848 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07002849 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002850 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002851 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07002852 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08002853 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Charles Heff9b4dff2017-09-22 10:18:37 +01002854 mService.mLockTaskController.clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002855 if (tr.isPersistable) {
2856 mService.notifyTaskPersisterLocked(null, true);
2857 }
2858 return true;
2859 }
2860 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2861 return false;
2862 }
2863
2864 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2865 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002866 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002867 }
2868 ComponentName component = tr.getBaseIntent().getComponent();
2869 if (component == null) {
2870 Slog.w(TAG, "No component for base intent of task: " + tr);
2871 return;
2872 }
2873
2874 // Find any running services associated with this app and stop if needed.
2875 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2876
2877 if (!killProcess) {
2878 return;
2879 }
2880
2881 // Determine if the process(es) for this task should be killed.
2882 final String pkg = component.getPackageName();
2883 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2884 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2885 for (int i = 0; i < pmap.size(); i++) {
2886
2887 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2888 for (int j = 0; j < uids.size(); j++) {
2889 ProcessRecord proc = uids.valueAt(j);
2890 if (proc.userId != tr.userId) {
2891 // Don't kill process for a different user.
2892 continue;
2893 }
2894 if (proc == mService.mHomeProcess) {
2895 // Don't kill the home process along with tasks from the same package.
2896 continue;
2897 }
2898 if (!proc.pkgList.containsKey(pkg)) {
2899 // Don't kill process that is not associated with this task.
2900 continue;
2901 }
2902
2903 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002904 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002905 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2906 // Don't kill process(es) that has an activity in a different task that is
2907 // also in recents.
2908 return;
2909 }
2910 }
2911
2912 if (proc.foregroundServices) {
2913 // Don't kill process(es) with foreground service.
2914 return;
2915 }
2916
2917 // Add process to kill list.
2918 procsToKill.add(proc);
2919 }
2920 }
2921
2922 // Kill the running processes.
2923 for (int i = 0; i < procsToKill.size(); i++) {
2924 ProcessRecord pr = procsToKill.get(i);
2925 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2926 && pr.curReceivers.isEmpty()) {
2927 pr.kill("remove task", true);
2928 } else {
2929 // We delay killing processes that are not in the background or running a receiver.
2930 pr.waitingToKill = "remove task";
2931 }
2932 }
2933 }
2934
Chong Zhang5dcb2752015-08-18 13:50:26 -07002935 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002936 * Called to restore the state of the task into the stack that it's supposed to go into.
2937 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002938 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002939 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002940 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002941 * @return true if the task has been restored successfully.
2942 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002943 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2944 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002945 final ActivityStack currentStack = task.getStack();
2946 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002947 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002948 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002949 // Nothing else to do since it is already restored in the right stack.
2950 return true;
2951 }
2952 // Remove current stack association, so we can re-associate the task with the
2953 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002954 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002955 }
2956
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002957 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002958 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002959 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002960 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2961 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002962 final ArrayList<ActivityRecord> activities = task.mActivities;
2963 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002964 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002965 }
2966 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002967 }
2968
Winson Chung1dbc8112017-09-28 18:05:31 -07002969 @Override
2970 public void onRecentTaskAdded(TaskRecord task) {
2971 task.touchActiveTime();
2972 }
2973
2974 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07002975 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung0ec2a352017-10-26 11:38:30 -07002976 if (wasTrimmed) {
2977 // Task was trimmed from the recent tasks list -- remove the active task record as well
2978 // since the user won't really be able to go back to it
2979 removeTaskByIdLocked(task.taskId, false /* killProcess */,
2980 false /* removeFromRecents */, !PAUSE_IMMEDIATELY, "recent-task-trimmed");
2981 }
Winson Chung1dbc8112017-09-28 18:05:31 -07002982 task.removedFromRecents();
2983 }
2984
Wale Ogunwale040b4702015-08-06 18:10:50 -07002985 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002986 * Move stack with all its existing content to specified display.
2987 * @param stackId Id of stack to move.
2988 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002989 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002990 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002991 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002992 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002993 if (activityDisplay == null) {
2994 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2995 + displayId);
2996 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002997 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002998 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002999 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3000 + stackId);
3001 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003002
3003 final ActivityDisplay currentDisplay = stack.getDisplay();
3004 if (currentDisplay == null) {
3005 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3006 + " is not attached to any display.");
3007 }
3008
3009 if (currentDisplay.mDisplayId == displayId) {
3010 throw new IllegalArgumentException("Trying to move stack=" + stack
3011 + " to its current displayId=" + displayId);
3012 }
3013
3014 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003015 // TODO(multi-display): resize stacks properly if moved from split-screen.
3016 }
3017
3018 /**
Winson Chung74666102017-02-22 17:49:24 -08003019 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3020 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003021 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003022 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003023 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003024 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003025 final int stackId = stack.mStackId;
3026 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003027
Winson Chung74666102017-02-22 17:49:24 -08003028 // Check that we aren't reparenting to the same stack that the task is already in
3029 if (prevStack != null && prevStack.mStackId == stackId) {
3030 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3031 + " already in stackId=" + stackId);
3032 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003033 }
3034
Winson Chung74666102017-02-22 17:49:24 -08003035 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3036 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003037 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003038 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003039 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003040 }
3041
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003042 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3043 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003044 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003045 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3046 + " reparent task=" + task + " to stackId=" + stackId);
3047 }
3048
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003049 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003050 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3051 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003052 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3053 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003054 }
3055
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003056 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3057 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003058 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003059 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3060 + " Moving to a fullscreen stack instead.");
3061 if (prevStack != null) {
3062 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003063 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003064 stack = stack.getDisplay().createStack(
3065 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003066 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003067 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003068 }
3069
Winson Chung08f81892017-03-02 15:40:51 -08003070 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003071 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003072 if (stack == null) {
3073 throw new IllegalArgumentException(
3074 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3075 }
3076
3077 final ActivityRecord r = stack.topRunningActivityLocked();
3078 if (r == null) {
3079 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3080 + " in stack=" + stack);
3081 return false;
3082 }
3083
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003084 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003085 Slog.w(TAG,
3086 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003087 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003088 return false;
3089 }
3090
Winson Chung3a682872017-04-10 14:38:05 -07003091 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003092 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003093 return true;
3094 }
3095
Winson Chung8bca9e42017-04-16 15:59:43 -07003096 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003097 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003098
Wale Ogunwale480dca02016-02-06 13:58:29 -08003099 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003100
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003101 final ActivityDisplay display = r.getStack().getDisplay();
3102 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003103
Bryce Lee04ab3462017-04-10 15:06:33 -07003104 // This will clear the pinned stack by moving an existing task to the full screen stack,
3105 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003106 if (stack != null) {
3107 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3108 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003109
Wale Ogunwale1666e312016-12-16 11:27:18 -08003110 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003111 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003112
Winson Chunge55c0192017-08-24 14:50:48 -07003113 // Calculate the target bounds here before the task is reparented back into pinned windowing
3114 // mode (which will reset the saved bounds)
3115 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3116
Wale Ogunwale480dca02016-02-06 13:58:29 -08003117 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003118 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003119 // Resize the pinned stack to match the current size of the task the activity we are
3120 // going to be moving is currently contained in. We do this to have the right starting
3121 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003122 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003123 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003124 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003125
3126 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003127 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003128 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3129 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003130 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003131 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003132 // reveal/leave the other activities in their original task.
3133
3134 // Currently, we don't support reparenting activities across tasks in two different
3135 // stacks, so instead, just create a new task in the same stack, reparent the
3136 // activity into that task, and then reparent the whole task to the new stack. This
3137 // ensures that all the necessary work to migrate states in the old and new stacks
3138 // is also done.
3139 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003140 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003141 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3142
Winson Chung5af42fc2017-03-24 17:11:33 -07003143 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003144 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003145 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003146 }
Winson Chungc2baac02017-01-11 13:34:47 -08003147
3148 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3149 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003150 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003151 } finally {
3152 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003153 }
3154
Winson Chunge7ba6862017-05-24 12:13:33 -07003155 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3156 true /* fromFullscreen */);
3157
3158 // Update the visibility of all activities after the they have been reparented to the new
3159 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3160 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3161 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003162 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003163 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003164
Wale Ogunwale89be5762017-10-04 13:27:49 -07003165 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003166 }
3167
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003168 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003169 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3170 if (r == null || !r.isFocusable()) {
3171 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3172 "moveActivityStackToFront: unfocusable r=" + r);
3173 return false;
3174 }
3175
Bryce Leeaf691c02017-03-20 14:20:22 -07003176 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003177 final ActivityStack stack = r.getStack();
3178 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003179 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3180 + r + " task=" + task);
3181 return false;
3182 }
3183
Chong Zhang6cda19c2016-06-14 19:07:56 -07003184 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3185 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3186 "moveActivityStackToFront: already on top, r=" + r);
3187 return false;
3188 }
3189
3190 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3191 "moveActivityStackToFront: r=" + r);
3192
3193 stack.moveToFront(reason, task);
3194 return true;
3195 }
3196
David Stevensc6b91c62017-02-08 14:23:58 -08003197 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003198 mTmpFindTaskResult.r = null;
3199 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003200 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003201 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003203 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3204 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3205 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003206 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003207 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3208 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003209 continue;
3210 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003211 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003212 // It is possible to have tasks in multiple stacks with the same root affinity, so
3213 // we should keep looking after finding an affinity match to see if there is a
3214 // better match in another stack. Also, task affinity isn't a good enough reason
3215 // to target a display which isn't the source of the intent, so skip any affinity
3216 // matches not on the specified display.
3217 if (mTmpFindTaskResult.r != null) {
3218 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3219 return mTmpFindTaskResult.r;
3220 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003221 // Note: since the traversing through the stacks is top down, the floating
3222 // tasks should always have lower priority than any affinity-matching tasks
3223 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003224 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003225 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3226 Slog.d(TAG_TASKS, "Skipping match on different display "
3227 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003228 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003229 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003230 }
3231 }
Winson Chung5b895b72017-05-01 13:46:25 -07003232
David Stevensc6b91c62017-02-08 14:23:58 -08003233 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3234 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003235 }
3236
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003237 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003238 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003239 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003240 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3241 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3242 final ActivityStack stack = display.getChildAt(stackNdx);
3243 final ActivityRecord ar = stack.findActivityLocked(
3244 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003245 if (ar != null) {
3246 return ar;
3247 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003248 }
3249 }
3250 return null;
3251 }
3252
David Stevens9440dc82017-03-16 19:00:20 -07003253 boolean hasAwakeDisplay() {
3254 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3255 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3256 if (!display.shouldSleep()) {
3257 return true;
3258 }
3259 }
3260 return false;
3261 }
3262
Craig Mautner8d341ef2013-03-26 09:03:27 -07003263 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003264 scheduleSleepTimeout();
3265 if (!mGoingToSleep.isHeld()) {
3266 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003267 if (mLaunchingActivity.isHeld()) {
3268 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3269 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003270 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003271 mLaunchingActivity.release();
3272 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003273 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003274 }
David Stevens9440dc82017-03-16 19:00:20 -07003275
3276 applySleepTokensLocked(false /* applyToStacks */);
3277
3278 checkReadyForSleepLocked(true /* allowDelay */);
3279 }
3280
3281 void prepareForShutdownLocked() {
3282 for (int i = 0; i < mActivityDisplays.size(); i++) {
3283 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3284 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003285 }
3286
3287 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003288 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003289
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003290 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003291 final long endTime = System.currentTimeMillis() + timeout;
3292 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003293 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003294 long timeRemaining = endTime - System.currentTimeMillis();
3295 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003296 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003297 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003298 } catch (InterruptedException e) {
3299 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003300 } else {
3301 Slog.w(TAG, "Activity manager shutdown timed out");
3302 timedout = true;
3303 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003304 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003305 } else {
3306 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003307 }
3308 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003309
3310 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003311 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003312
Craig Mautner8d341ef2013-03-26 09:03:27 -07003313 return timedout;
3314 }
3315
3316 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003317 removeSleepTimeouts();
3318 if (mGoingToSleep.isHeld()) {
3319 mGoingToSleep.release();
3320 }
David Stevens9440dc82017-03-16 19:00:20 -07003321 }
3322
3323 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003324 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003325 // Set the sleeping state of the display.
3326 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3327 final boolean displayShouldSleep = display.shouldSleep();
3328 if (displayShouldSleep == display.isSleeping()) {
3329 continue;
3330 }
3331 display.setIsSleeping(displayShouldSleep);
3332
3333 if (!applyToStacks) {
3334 continue;
3335 }
3336
3337 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003338 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3339 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003340 if (displayShouldSleep) {
3341 stack.goToSleepIfPossible(false /* shuttingDown */);
3342 } else {
3343 stack.awakeFromSleepingLocked();
3344 if (isFocusedStack(stack)) {
3345 resumeFocusedStackTopActivityLocked();
3346 }
3347 }
3348 }
3349
3350 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3351 continue;
3352 }
3353 // The display is awake now, so clean up the going to sleep list.
3354 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3355 final ActivityRecord r = it.next();
3356 if (r.getDisplayId() == display.mDisplayId) {
3357 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003358 }
Craig Mautner5314a402013-09-26 12:40:16 -07003359 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003361 }
3362
3363 void activitySleptLocked(ActivityRecord r) {
3364 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003365 final ActivityStack s = r.getStack();
3366 if (s != null) {
3367 s.checkReadyForSleep();
3368 } else {
3369 checkReadyForSleepLocked(true);
3370 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003371 }
3372
David Stevens9440dc82017-03-16 19:00:20 -07003373 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003374 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003375 // Do not care.
3376 return;
3377 }
3378
David Stevens18abd0e2017-08-17 14:55:47 -07003379 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3380 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003381 }
3382
Wei Wang65c7a152016-06-02 18:51:22 -07003383 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003384 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003385
Craig Mautner0eea92c2013-05-16 13:35:39 -07003386 removeSleepTimeouts();
3387
3388 if (mGoingToSleep.isHeld()) {
3389 mGoingToSleep.release();
3390 }
3391 if (mService.mShuttingDown) {
3392 mService.notifyAll();
3393 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003394 }
3395
David Stevens18abd0e2017-08-17 14:55:47 -07003396 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3397 // successfully put to sleep.
3398 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3399 boolean allSleep = true;
3400 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003401 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3402 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3403 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003404 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003405 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003406 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003407 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003408 }
3409 }
3410 }
3411 return allSleep;
3412 }
3413
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003414 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003415 // A resumed activity cannot be stopping. remove from list
3416 mStoppingActivities.remove(r);
3417
Andrii Kulian02b7a832016-10-06 23:11:56 -07003418 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003419 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003420 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003421 }
3422 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003423 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003424 mWindowManager.executeAppTransition();
3425 return true;
3426 }
3427 return false;
3428 }
3429
Craig Mautner8d341ef2013-03-26 09:03:27 -07003430 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003431 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003432 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3433 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3434 final ActivityStack stack = display.getChildAt(stackNdx);
3435 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003436 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003437 }
3438 }
3439
Craig Mautnerbb742462014-07-07 15:28:55 -07003440 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003441 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003442 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003443 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003444
3445 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003446 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003447 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003448 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003449
3450 // When launching tasks behind, update the last active time of the top task after the new
3451 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003452 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003453 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003454 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003455 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003456 }
3457
3458 void scheduleLaunchTaskBehindComplete(IBinder token) {
3459 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3460 }
3461
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003462 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3463 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003464 mKeyguardController.beginActivityVisibilityUpdate();
3465 try {
3466 // First the front stacks. In case any are not fullscreen and are in front of home.
3467 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003468 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3469 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3470 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003471 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3472 }
Craig Mautner580ea812013-04-25 12:58:38 -07003473 }
Jorim Jaggife762342016-10-13 14:33:27 +02003474 } finally {
3475 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003476 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003477 }
3478
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003479 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3480 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003481 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3482 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3483 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003484 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3485 }
3486 }
3487 }
3488
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003489 void invalidateTaskLayers() {
3490 mTaskLayersChanged = true;
3491 }
3492
3493 void rankTaskLayersIfNeeded() {
3494 if (!mTaskLayersChanged) {
3495 return;
3496 }
3497 mTaskLayersChanged = false;
3498 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003499 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003500 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003501 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3502 final ActivityStack stack = display.getChildAt(stackNdx);
3503 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003504 }
3505 }
3506 }
3507
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003508 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003510 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3511 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3512 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003513 stack.clearOtherAppTimeTrackers(except);
3514 }
3515 }
3516 }
3517
Craig Mautner8d341ef2013-03-26 09:03:27 -07003518 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003519 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003520 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3521 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3522 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003523 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003525 }
3526 }
3527
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003528 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3529 // Examine all activities currently running in the process.
3530 TaskRecord firstTask = null;
3531 // Tasks is non-null only if two or more tasks are found.
3532 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003533 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3534 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003535 ActivityRecord r = app.activities.get(i);
3536 // First, if we find an activity that is in the process of being destroyed,
3537 // then we just aren't going to do anything for now; we want things to settle
3538 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003539 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003540 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003541 return;
3542 }
3543 // Don't consider any activies that are currently not in a state where they
3544 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003545 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3546 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003547 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003548 continue;
3549 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003550
3551 final TaskRecord task = r.getTask();
3552 if (task != null) {
3553 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003554 + " from " + r);
3555 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003556 firstTask = task;
3557 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003558 if (tasks == null) {
3559 tasks = new ArraySet<>();
3560 tasks.add(firstTask);
3561 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003562 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003563 }
3564 }
3565 }
3566 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003567 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003568 return;
3569 }
3570 // If we have activities in multiple tasks that are in a position to be destroyed,
3571 // let's iterate through the tasks and release the oldest one.
3572 final int numDisplays = mActivityDisplays.size();
3573 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003574 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3575 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003576 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003577 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3578 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003579 // Try to release activities in this stack; if we manage to, we are done.
3580 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3581 return;
3582 }
3583 }
3584 }
3585 }
3586
Amith Yamasani37a40c22015-06-17 13:25:42 -07003587 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003588 final int focusStackId = mFocusedStack.getStackId();
3589 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003590 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003591 if (dockedStack != null) {
3592 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3593 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003594 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3595 // also cause all tasks to be moved to the fullscreen stack at a position that is
3596 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003597 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003598
3599 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003600 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003601 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003602
Craig Mautner858d8a62013-04-23 17:08:34 -07003603 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003604 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003605 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3606 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3607 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003608 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003609 TaskRecord task = stack.topTask();
3610 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003611 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003612 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003613 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003614 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003615
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003616 ActivityStack stack = getStack(restoreStackId);
3617 if (stack == null) {
3618 stack = mHomeStack;
3619 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003620 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003621 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003622 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003623 } else {
3624 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003625 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 }
Craig Mautner93529a42013-10-04 15:03:13 -07003627 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003628 }
3629
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003630 /** Checks whether the userid is a profile of the current user. */
3631 boolean isCurrentProfileLocked(int userId) {
3632 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003633 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003634 }
3635
Bryce Leeb7c9b802017-05-02 14:20:24 -07003636 /**
3637 * Returns whether a stopping activity is present that should be stopped after visible, rather
3638 * than idle.
3639 * @return {@code true} if such activity is present. {@code false} otherwise.
3640 */
3641 boolean isStoppingNoHistoryActivity() {
3642 // Activities that are marked as nohistory should be stopped immediately after the resumed
3643 // activity has become visible.
3644 for (ActivityRecord record : mStoppingActivities) {
3645 if (record.isNoHistory()) {
3646 return true;
3647 }
3648 }
3649
3650 return false;
3651 }
3652
Winson Chung4dabf232017-01-25 13:25:22 -08003653 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3654 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003655 ArrayList<ActivityRecord> stops = null;
3656
3657 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003658 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3659 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003660 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003661 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003662 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3663 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003664 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003665 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003666 if (s.finishing) {
3667 // If this activity is finishing, it is sitting on top of
3668 // everyone else but we now know it is no longer needed...
3669 // so get rid of it. Otherwise, we need to go through the
3670 // normal flow and hide it once we determine that it is
3671 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003672 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003673 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003674 }
3675 }
David Stevens9440dc82017-03-16 19:00:20 -07003676 if (remove) {
3677 final ActivityStack stack = s.getStack();
3678 final boolean shouldSleepOrShutDown = stack != null
3679 ? stack.shouldSleepOrShutDownActivities()
3680 : mService.isSleepingOrShuttingDownLocked();
3681 if (!waitingVisible || shouldSleepOrShutDown) {
3682 if (!processPausingActivities && s.state == PAUSING) {
3683 // Defer processing pausing activities in this iteration and reschedule
3684 // a delayed idle to reprocess it again
3685 removeTimeoutsForActivityLocked(idleActivity);
3686 scheduleIdleTimeoutLocked(idleActivity);
3687 continue;
3688 }
Winson Chung4dabf232017-01-25 13:25:22 -08003689
David Stevens9440dc82017-03-16 19:00:20 -07003690 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3691 if (stops == null) {
3692 stops = new ArrayList<>();
3693 }
3694 stops.add(s);
3695 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003696 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003697 }
3698 }
3699
3700 return stops;
3701 }
3702
Craig Mautnercf910b02013-04-23 11:23:27 -07003703 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003705 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3706 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3707 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003708 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003709 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003710 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003711 if (r == null) Slog.e(TAG,
3712 "validateTop...: null top activity, stack=" + stack);
3713 else {
3714 final ActivityRecord pausing = stack.mPausingActivity;
3715 if (pausing != null && pausing == r) Slog.e(TAG,
3716 "validateTop...: top stack has pausing activity r=" + r
3717 + " state=" + state);
3718 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3719 "validateTop...: activity in front not resumed r=" + r
3720 + " state=" + state);
3721 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003722 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003723 final ActivityRecord resumed = stack.mResumedActivity;
3724 if (resumed != null && resumed == r) Slog.e(TAG,
3725 "validateTop...: back stack has resumed activity r=" + r
3726 + " state=" + state);
3727 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3728 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003729 }
3730 }
3731 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003732 }
3733
Bryce Lee77a7dd62018-01-22 15:47:09 -08003734 public void dumpDisplays(PrintWriter pw) {
3735 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3736 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3737 pw.print("[id:" + display.mDisplayId + " stacks:");
3738 display.dumpStacks(pw);
3739 pw.print("]");
3740 }
3741 }
3742
Craig Mautner27084302013-03-25 08:05:25 -07003743 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003744 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003746 pw.print(prefix);
3747 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003748 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003749 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3750 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003751 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003752 }
Bryce Lee4a194382017-04-04 14:32:48 -07003753 if (!mWaitingForActivityVisible.isEmpty()) {
3754 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3755 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3756 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3757 }
3758 }
3759
Jorim Jaggi8d786932016-10-26 19:08:36 -07003760 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003761 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003762 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003763
Yi Jin129fc6c2017-09-28 15:48:38 -07003764 public void writeToProto(ProtoOutputStream proto) {
Adrian Roos4921ccf2017-09-28 16:54:06 +02003765 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003766 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3767 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3768 activityDisplay.writeToProto(proto, DISPLAYS);
3769 }
3770 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3771 if (mFocusedStack != null) {
3772 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3773 ActivityRecord focusedActivity = getResumedActivityLocked();
3774 if (focusedActivity != null) {
3775 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3776 }
3777 } else {
3778 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3779 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003780 }
3781
Winson43d1f262016-06-14 16:05:55 -07003782 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003783 * Dump all connected displays' configurations.
3784 * @param prefix Prefix to apply to each line of the dump.
3785 */
3786 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3787 pw.print(prefix); pw.println("Display override configurations:");
3788 final int displayCount = mActivityDisplays.size();
3789 for (int i = 0; i < displayCount; i++) {
3790 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3791 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3792 pw.println(activityDisplay.getOverrideConfiguration());
3793 }
3794 }
3795
3796 /**
Winson43d1f262016-06-14 16:05:55 -07003797 * Dumps the activities matching the given {@param name} in the either the focused stack
3798 * or all visible stacks if {@param dumpVisibleStacks} is true.
3799 */
Winson Chung6998bc42017-02-28 17:07:05 -08003800 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3801 boolean dumpFocusedStackOnly) {
3802 if (dumpFocusedStackOnly) {
3803 return mFocusedStack.getDumpActivitiesLocked(name);
3804 } else {
Winson43d1f262016-06-14 16:05:55 -07003805 ArrayList<ActivityRecord> activities = new ArrayList<>();
3806 int numDisplays = mActivityDisplays.size();
3807 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003808 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3809 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3810 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003811 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003812 activities.addAll(stack.getDumpActivitiesLocked(name));
3813 }
3814 }
3815 }
3816 return activities;
Winson43d1f262016-06-14 16:05:55 -07003817 }
Craig Mautner20e72272013-04-01 13:45:53 -07003818 }
3819
Dianne Hackborn390517b2013-05-30 15:03:32 -07003820 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3821 boolean needSep, String prefix) {
3822 if (activity != null) {
3823 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3824 if (needSep) {
3825 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003826 }
3827 pw.print(prefix);
3828 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003829 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003830 }
3831 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003832 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003833 }
3834
Craig Mautner8d341ef2013-03-26 09:03:27 -07003835 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3836 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003837 boolean printed = false;
3838 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003839 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3840 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003841 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003842 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003843 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3844 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3845 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003846 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003847 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003848 + ": type=" + activityTypeToString(stack.getActivityType())
3849 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003850 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08003851 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07003852
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003853 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3854 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003855
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3857 !dumpAll, false, dumpPackage, true,
3858 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003859
Craig Mautner4a1cb222013-12-04 16:14:06 -08003860 needSep = printed;
3861 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3862 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003863 if (pr) {
3864 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003866 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003867 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3868 " mResumedActivity: ");
3869 if (pr) {
3870 printed = true;
3871 needSep = false;
3872 }
3873 if (dumpAll) {
3874 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3875 " mLastPausedActivity: ");
3876 if (pr) {
3877 printed = true;
3878 needSep = true;
3879 }
3880 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3881 needSep, " mLastNoHistoryActivity: ");
3882 }
3883 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003884 }
3885 }
3886
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003887 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3888 false, dumpPackage, true, " Activities waiting to finish:", null);
3889 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3890 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003891 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3892 false, !dumpAll, false, dumpPackage, true,
3893 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003894 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3895 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003896
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003897 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003898 }
3899
Dianne Hackborn390517b2013-05-30 15:03:32 -07003900 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003901 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003902 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003903 String innerPrefix = null;
3904 String[] args = null;
3905 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003906 for (int i=list.size()-1; i>=0; i--) {
3907 final ActivityRecord r = list.get(i);
3908 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3909 continue;
3910 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003911 if (innerPrefix == null) {
3912 innerPrefix = prefix + " ";
3913 args = new String[0];
3914 }
3915 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003916 final boolean full = !brief && (complete || !r.isInHistory());
3917 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003918 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003919 needNL = false;
3920 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003921 if (header != null) {
3922 pw.println(header);
3923 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003924 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003925 if (lastTask != r.getTask()) {
3926 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003927 pw.print(prefix);
3928 pw.print(full ? "* " : " ");
3929 pw.println(lastTask);
3930 if (full) {
3931 lastTask.dump(pw, prefix + " ");
3932 } else if (complete) {
3933 // Complete + brief == give a summary. Isn't that obvious?!?
3934 if (lastTask.intent != null) {
3935 pw.print(prefix); pw.print(" ");
3936 pw.println(lastTask.intent.toInsecureStringWithClip());
3937 }
3938 }
3939 }
3940 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3941 pw.print(" #"); pw.print(i); pw.print(": ");
3942 pw.println(r);
3943 if (full) {
3944 r.dump(pw, innerPrefix);
3945 } else if (complete) {
3946 // Complete + brief == give a summary. Isn't that obvious?!?
3947 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3948 if (r.app != null) {
3949 pw.print(innerPrefix); pw.println(r.app);
3950 }
3951 }
3952 if (client && r.app != null && r.app.thread != null) {
3953 // flush anything that is already in the PrintWriter since the thread is going
3954 // to write to the file descriptor directly
3955 pw.flush();
3956 try {
3957 TransferPipe tp = new TransferPipe();
3958 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003959 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003960 // Short timeout, since blocking here can
3961 // deadlock with the application.
3962 tp.go(fd, 2000);
3963 } finally {
3964 tp.kill();
3965 }
3966 } catch (IOException e) {
3967 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3968 } catch (RemoteException e) {
3969 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3970 }
3971 needNL = true;
3972 }
3973 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003974 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003975 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003976
Craig Mautnerf3333272013-04-22 10:55:53 -07003977 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003978 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3979 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003980 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3981 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003982 }
3983
3984 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003985 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003986 }
3987
3988 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003989 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3990 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003991 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3992 }
3993
Craig Mautner05d29032013-05-03 13:40:13 -07003994 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003995 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3996 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3997 }
Craig Mautner05d29032013-05-03 13:40:13 -07003998 }
3999
Craig Mautner0eea92c2013-05-16 13:35:39 -07004000 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004001 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4002 }
4003
4004 final void scheduleSleepTimeout() {
4005 removeSleepTimeouts();
4006 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4007 }
4008
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 @Override
4010 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004011 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4013 }
4014
4015 @Override
4016 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004017 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004018 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4019 }
4020
4021 @Override
4022 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004023 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004024 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4025 }
4026
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004027 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004029 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004031 }
4032
Andrii Kulianca007a62016-11-22 16:33:28 -08004033 /** Check if display with specified id is added to the list. */
4034 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004035 return getActivityDisplayOrCreateLocked(displayId) != null;
4036 }
4037
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004038 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004039 ActivityDisplay getActivityDisplay(int displayId) {
4040 return mActivityDisplays.get(displayId);
4041 }
4042
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004043 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4044 ActivityDisplay getDefaultDisplay() {
4045 return mActivityDisplays.get(DEFAULT_DISPLAY);
4046 }
4047
Andrii Kulian62e6f252017-05-30 22:46:53 -07004048 /**
4049 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4050 * corresponding record in display manager.
4051 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004052 // TODO: Look into consolidating with getActivityDisplay()
4053 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004054 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4055 if (activityDisplay != null) {
4056 return activityDisplay;
4057 }
4058 if (mDisplayManager == null) {
4059 // The system isn't fully initialized yet.
4060 return null;
4061 }
4062 final Display display = mDisplayManager.getDisplay(displayId);
4063 if (display == null) {
4064 // The display is not registered in DisplayManager.
4065 return null;
4066 }
4067 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004068 activityDisplay = new ActivityDisplay(this, displayId);
4069 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004070 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4071 mWindowManager.onDisplayAdded(displayId);
4072 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004073 }
4074
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004075 @VisibleForTesting
4076 void attachDisplay(ActivityDisplay display) {
4077 mActivityDisplays.put(display.mDisplayId, display);
4078 }
4079
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004080 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004081 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004082 mService.mContext.getResources().getDimensionPixelSize(
4083 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004084 }
4085
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004086 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004087 if (displayId == DEFAULT_DISPLAY) {
4088 throw new IllegalArgumentException("Can't remove the primary display.");
4089 }
4090
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004092 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4093 if (activityDisplay == null) {
4094 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004095 }
Bryce Leef19cbe22018-02-02 15:09:21 -08004096
4097 activityDisplay.remove();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004098
4099 releaseSleepTokens(activityDisplay);
4100
4101 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004102 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103 }
4104
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004105 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004106 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004107 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08004108 // TODO: The following code block should be moved into {@link ActivityDisplay}.
Craig Mautnere0a38842013-12-16 16:14:02 -08004109 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004110 // The window policy is responsible for stopping activities on the default display
4111 if (displayId != Display.DEFAULT_DISPLAY) {
4112 int displayState = activityDisplay.mDisplay.getState();
4113 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4114 activityDisplay.mOffToken =
4115 mService.acquireSleepToken("Display-off", displayId);
4116 } else if (displayState == Display.STATE_ON
4117 && activityDisplay.mOffToken != null) {
4118 activityDisplay.mOffToken.release();
4119 activityDisplay.mOffToken = null;
4120 }
4121 }
Bryce Leef3c6a472017-11-14 14:53:06 -08004122
4123 activityDisplay.updateBounds();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004124 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004125 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004126 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 }
4128
David Stevens9440dc82017-03-16 19:00:20 -07004129 SleepToken createSleepTokenLocked(String tag, int displayId) {
4130 ActivityDisplay display = mActivityDisplays.get(displayId);
4131 if (display == null) {
4132 throw new IllegalArgumentException("Invalid display: " + displayId);
4133 }
4134
4135 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4136 mSleepTokens.add(token);
4137 display.mAllSleepTokens.add(token);
4138 return token;
4139 }
4140
4141 private void removeSleepTokenLocked(SleepTokenImpl token) {
4142 mSleepTokens.remove(token);
4143
4144 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4145 if (display != null) {
4146 display.mAllSleepTokens.remove(token);
4147 if (display.mAllSleepTokens.isEmpty()) {
4148 mService.updateSleepIfNeededLocked();
4149 }
4150 }
4151 }
4152
4153 private void releaseSleepTokens(ActivityDisplay display) {
4154 if (display.mAllSleepTokens.isEmpty()) {
4155 return;
4156 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004157 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004158 mSleepTokens.remove(token);
4159 }
4160 display.mAllSleepTokens.clear();
4161
4162 mService.updateSleepIfNeededLocked();
4163 }
4164
Wale Ogunwale68278562017-09-23 17:13:55 -07004165 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004166 final int displayId = stack.mDisplayId;
4167 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004168 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004169 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004170 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004172 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004173 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004174 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004175 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004176 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004177
4178 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4179 final int numTasks = tasks.size();
4180 int[] taskIds = new int[numTasks];
4181 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004182 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004183 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 for (int i = 0; i < numTasks; ++i) {
4185 final TaskRecord task = tasks.get(i);
4186 taskIds[i] = task.taskId;
4187 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4188 : task.realActivity != null ? task.realActivity.flattenToString()
4189 : task.getTopActivity() != null ? task.getTopActivity().packageName
4190 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004191 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004192 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004193 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004194 }
4195 info.taskIds = taskIds;
4196 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004197 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004198 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004199
4200 final ActivityRecord top = stack.topRunningActivityLocked();
4201 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004202 return info;
4203 }
4204
Wale Ogunwale68278562017-09-23 17:13:55 -07004205 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 ActivityStack stack = getStack(stackId);
4207 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004208 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 }
4210 return null;
4211 }
4212
Wale Ogunwale68278562017-09-23 17:13:55 -07004213 StackInfo getStackInfo(int windowingMode, int activityType) {
4214 final ActivityStack stack = getStack(windowingMode, activityType);
4215 return (stack != null) ? getStackInfo(stack) : null;
4216 }
4217
Craig Mautner4a1cb222013-12-04 16:14:06 -08004218 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004219 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004220 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004221 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4222 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4223 final ActivityStack stack = display.getChildAt(stackNdx);
4224 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004225 }
4226 }
4227 return list;
4228 }
4229
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004230 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004231 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004232 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004233 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004234 }
4235
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004236 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004237 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004238 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004239 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004240 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004241 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004242 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004243 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004244 return;
4245 }
4246
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004247 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004248 if (isSecondaryDisplayPreferred) {
4249 final int actualDisplayId = task.getStack().mDisplayId;
4250 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004251 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004252 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004253 // The task might have landed on a display different from requested.
4254 // TODO(multi-display): Find proper stack for the task on the default display.
4255 mService.setTaskWindowingMode(task.taskId,
4256 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
4257 if (preferredDisplayId != actualDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004258 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4259 // display with config different from global config.
4260 mService.mTaskChangeNotificationController
4261 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004262 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004263 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004264 }
4265
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004266 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004267 // Display a warning toast that we tried to put an app that doesn't support split-screen
4268 // in split-screen.
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004269 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004270
Andrii Kulianc27916642016-04-12 17:59:27 -07004271 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4272 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004273
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004274 final ActivityStack dockedStack =
4275 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004276 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004277 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004278 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004279 return;
4280 }
4281
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004282 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004283 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
4284 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004285 final String packageName = topActivity.appInfo.packageName;
4286 final int reason = isSecondaryDisplayPreferred
4287 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4288 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004289 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004290 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004291 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004292 }
4293
Jorim Jaggife89d122015-12-22 16:28:44 +01004294 void activityRelaunchedLocked(IBinder token) {
4295 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004296 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4297 if (r != null) {
4298 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004299 r.setSleeping(true, true);
4300 }
4301 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004302 }
4303
4304 void activityRelaunchingLocked(ActivityRecord r) {
4305 mWindowManager.notifyAppRelaunching(r.appToken);
4306 }
4307
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004308 void logStackState() {
4309 mActivityMetricsLogger.logWindowState();
4310 }
4311
Winson Chung5af42fc2017-03-24 17:11:33 -07004312 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004313 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4314 // end, then ensure that we defer all in between multi-window mode changes
4315 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4316 return;
4317 }
4318
Wale Ogunwale22e25262016-02-01 10:32:02 -08004319 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4320 final ActivityRecord r = task.mActivities.get(i);
4321 if (r.app != null && r.app.thread != null) {
4322 mMultiWindowModeChangedActivities.add(r);
4323 }
4324 }
4325
4326 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4327 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4328 }
4329 }
4330
Winson Chung5af42fc2017-03-24 17:11:33 -07004331 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004332 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004333 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004334 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004335 return;
4336 }
4337
Bryce Leef3c6a472017-11-14 14:53:06 -08004338 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004339 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004340
Winson Chungab76bbc2017-08-14 13:33:51 -07004341 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4342 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4343 final ActivityRecord r = task.mActivities.get(i);
4344 if (r.app != null && r.app.thread != null) {
4345 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004346 // If we are scheduling pip change, then remove this activity from multi-window
4347 // change list as the processing of pip change will make sure multi-window changed
4348 // message is processed in the right order relative to pip changed.
4349 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004350 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004351 }
4352 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004353
Winson Chungab76bbc2017-08-14 13:33:51 -07004354 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4355 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4356 }
4357 }
4358
4359 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4360 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4361 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4362 final ActivityRecord r = task.mActivities.get(i);
4363 if (r.app != null && r.app.thread != null) {
4364 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004365 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004366 }
4367 }
4368
Tony Mak853304c2016-04-18 15:17:41 +01004369 void setDockedStackMinimized(boolean minimized) {
4370 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004371 }
4372
chaviw59b98852017-06-13 12:05:44 -07004373 void wakeUp(String reason) {
4374 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4375 }
4376
4377 /**
4378 * Begin deferring resume to avoid duplicate resumes in one pass.
4379 */
4380 private void beginDeferResume() {
4381 mDeferResumeCount++;
4382 }
4383
4384 /**
4385 * End deferring resume and determine if resume can be called.
4386 */
4387 private void endDeferResume() {
4388 mDeferResumeCount--;
4389 }
4390
4391 /**
4392 * @return True if resume can be called.
4393 */
4394 private boolean readyToResume() {
4395 return mDeferResumeCount == 0;
4396 }
4397
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004398 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004399
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004400 public ActivityStackSupervisorHandler(Looper looper) {
4401 super(looper);
4402 }
4403
Winson Chung4dabf232017-01-25 13:25:22 -08004404 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004405 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004406 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4407 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004408 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004409 }
4410
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004411 @Override
4412 public void handleMessage(Message msg) {
4413 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004414 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4415 synchronized (mService) {
4416 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4417 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004418 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004419 }
4420 }
4421 } break;
4422 case REPORT_PIP_MODE_CHANGED_MSG: {
4423 synchronized (mService) {
4424 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4425 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004426 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4427 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004428 }
4429 }
4430 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004431 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004432 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4433 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004434 // We don't at this point know if the activity is fullscreen,
4435 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004436 activityIdleInternal((ActivityRecord) msg.obj,
4437 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004438 } break;
4439 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004440 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004441 activityIdleInternal((ActivityRecord) msg.obj,
4442 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004443 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004444 case RESUME_TOP_ACTIVITY_MSG: {
4445 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004446 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004447 }
4448 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004449 case SLEEP_TIMEOUT_MSG: {
4450 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004451 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004452 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004453 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004454 }
4455 }
4456 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004457 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004458 synchronized (mService) {
4459 if (mLaunchingActivity.isHeld()) {
4460 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4461 if (VALIDATE_WAKE_LOCK_CALLER
4462 && Binder.getCallingUid() != Process.myUid()) {
4463 throw new IllegalStateException("Calling must be system uid");
4464 }
4465 mLaunchingActivity.release();
4466 }
4467 }
4468 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004469 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004470 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004471 } break;
4472 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004473 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004474 } break;
4475 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004476 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004478 case LAUNCH_TASK_BEHIND_COMPLETE: {
4479 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004480 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004481 if (r != null) {
4482 handleLaunchTaskBehindCompleteLocked(r);
4483 }
4484 }
4485 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004486
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004487 }
4488 }
4489 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004490
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004491 ActivityStack findStackBehind(ActivityStack stack) {
4492 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004493 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004494 if (display == null) {
4495 return null;
4496 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004497 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4498 if (display.getChildAt(i) == stack && i > 0) {
4499 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004500 }
4501 }
4502 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004503 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004504 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004505
Jorim Jaggic69bd222016-03-15 14:38:37 +01004506 /**
4507 * Puts a task into resizing mode during the next app transition.
4508 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004509 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004510 */
Bryce Leed3624e12017-11-30 08:51:45 -08004511 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004512 mResizingTasksDuringAnimation.add(task.taskId);
4513 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004514 }
4515
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004516 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4517 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004518 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004519 final String callingPackage;
4520 final Intent intent;
4521 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004522 int activityType = ACTIVITY_TYPE_UNDEFINED;
4523 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004524 final ActivityOptions activityOptions = options != null
4525 ? options.getOptions(this)
4526 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004527 if (activityOptions != null) {
4528 activityType = activityOptions.getLaunchActivityType();
4529 windowingMode = activityOptions.getLaunchWindowingMode();
4530 }
4531 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004532 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004533 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004534 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004535
Matthew Ng606dd802017-06-05 14:06:32 -07004536 mWindowManager.deferSurfaceLayout();
4537 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004538 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004539 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004540 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004541
Matthew Ng606dd802017-06-05 14:06:32 -07004542 // Defer updating the stack in which recents is until the app transition is done, to
4543 // not run into issues where we still need to draw the task in recents but the
4544 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004545 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004546 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004547 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004548
Matthew Ng606dd802017-06-05 14:06:32 -07004549 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004550 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004551 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004552 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004553 mWindowManager.executeAppTransition();
4554 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004555 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004556 }
4557
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004558 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4559 // We always want to return to the home activity instead of the recents activity
4560 // from whatever is started from the recents activity, so move the home stack
4561 // forward.
4562 moveHomeStackToFront("startActivityFromRecents");
4563 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004564
Matthew Ng606dd802017-06-05 14:06:32 -07004565 // If the user must confirm credentials (e.g. when first launching a work app and the
4566 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4567 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4568 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004569 final ActivityRecord targetActivity = task.getTopActivity();
4570
Bryce Leed3624e12017-11-30 08:51:45 -08004571 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004572 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004573 try {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004574 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004575 true /* fromRecents */);
4576 } finally {
4577 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4578 targetActivity);
4579 }
Matthew Ng606dd802017-06-05 14:06:32 -07004580
Bryce Leed3624e12017-11-30 08:51:45 -08004581 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
4582 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4583 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004584 return ActivityManager.START_TASK_TO_FRONT;
4585 }
Matthew Ng606dd802017-06-05 14:06:32 -07004586 callingPackage = task.mCallingPackage;
4587 intent = task.intent;
4588 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4589 userId = task.userId;
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004590 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004591 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004592 null, 0, 0, options, userId, task, "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004593 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004594 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
4595 // If we are launching the task in the docked stack, put it into resizing mode so
4596 // the window renders full-screen with the background filling the void. Also only
4597 // call this at the end to make sure that tasks exists on the window manager side.
4598 setResizingDuringAnimation(task);
4599
4600 final ActivityDisplay display = task.getStack().getDisplay();
4601 final ActivityStack topSecondaryStack =
4602 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
4603 if (topSecondaryStack.isActivityTypeHome()) {
4604 // If the home activity if the top split-screen secondary stack, then the
4605 // primary split-screen stack is in the minimized mode which means it can't
4606 // receive input keys, so we should move the focused app to the home app so that
4607 // window manager can correctly calculate the focus window that can receive
4608 // input keys.
4609 moveHomeStackToFront("startActivityFromRecents: homeVisibleInSplitScreen");
4610 }
4611 }
Matthew Ng606dd802017-06-05 14:06:32 -07004612 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004613 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004614 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004615
4616 /**
4617 * @return a list of activities which are the top ones in each visible stack. The first
4618 * entry will be the focused activity.
4619 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004620 List<IBinder> getTopVisibleActivities() {
4621 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4622 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004623 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004624 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4625 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004626 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4627 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004628 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004629 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004630 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004631 if (top != null) {
4632 if (stack == mFocusedStack) {
4633 topActivityTokens.add(0, top.appToken);
4634 } else {
4635 topActivityTokens.add(top.appToken);
4636 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004637 }
4638 }
4639 }
4640 }
4641 return topActivityTokens;
4642 }
Bryce Lee4a194382017-04-04 14:32:48 -07004643
4644 /**
4645 * Internal container to store a match qualifier alongside a WaitResult.
4646 */
4647 static class WaitInfo {
4648 private final ComponentName mTargetComponent;
4649 private final WaitResult mResult;
4650
4651 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4652 this.mTargetComponent = targetComponent;
4653 this.mResult = result;
4654 }
4655
Wale Ogunwale3270f172017-04-26 07:29:42 -07004656 public boolean matches(ComponentName targetComponent) {
4657 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004658 }
4659
4660 public WaitResult getResult() {
4661 return mResult;
4662 }
4663
4664 public ComponentName getComponent() {
4665 return mTargetComponent;
4666 }
4667
4668 public void dump(PrintWriter pw, String prefix) {
4669 pw.println(prefix + "WaitInfo:");
4670 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4671 pw.println(prefix + " mResult=");
4672 mResult.dump(pw, prefix);
4673 }
4674 }
David Stevens9440dc82017-03-16 19:00:20 -07004675
4676 private final class SleepTokenImpl extends SleepToken {
4677 private final String mTag;
4678 private final long mAcquireTime;
4679 private final int mDisplayId;
4680
4681 public SleepTokenImpl(String tag, int displayId) {
4682 mTag = tag;
4683 mDisplayId = displayId;
4684 mAcquireTime = SystemClock.uptimeMillis();
4685 }
4686
4687 @Override
4688 public void release() {
4689 synchronized (mService) {
4690 removeSleepTokenLocked(this);
4691 }
4692 }
4693
4694 @Override
4695 public String toString() {
4696 return "{\"" + mTag + "\", display " + mDisplayId
4697 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4698 }
4699 }
4700
Craig Mautner27084302013-03-25 08:05:25 -07004701}