blob: 0a52ad3f5fbf6046b557fe4e46ef87078a9f7548 [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;
Andrii Kulian3a95edc2017-06-28 16:21:07 -070020import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Jorim Jaggife762342016-10-13 14:33:27 +020021import static android.Manifest.permission.START_ANY_ACTIVITY;
22import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Charles Heff9b4dff2017-09-22 10:18:37 +010023import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
Jorim Jaggife762342016-10-13 14:33:27 +020024import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070026import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
27import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070028import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070029import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070030import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
32import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070033import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070034import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
37import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070039import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070040import static android.app.WindowConfiguration.activityTypeToString;
41import static android.app.WindowConfiguration.windowingModeToString;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070042import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020043import static android.content.pm.PackageManager.PERMISSION_GRANTED;
chaviw59b98852017-06-13 12:05:44 -070044import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010045import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
47import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070048import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070049import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070050
Winson Chung47900652017-04-06 18:44:25 -070051import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020052import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020053import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020062import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020071import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
72import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
73import static com.android.server.am.ActivityManagerService.ANIMATE;
74import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
76import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
77import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
78import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
80import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
81import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
83import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070084import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020085import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
86import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Charles Heff9b4dff2017-09-22 10:18:37 +010087import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080088import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
89import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
90import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Winson Chung1dbc8112017-09-28 18:05:31 -070091import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Steven Timotius4346f0a2017-09-12 11:07:21 -070092import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
93import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
94import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
95import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggife762342016-10-13 14:33:27 +020096import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -070097
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080098import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020099
Svetoslav7008b512015-06-24 18:47:07 -0700100import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800101import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700102import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700103import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100104import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700105import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800107import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800108import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700109import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700111import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700112import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700113import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700114import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700115import android.app.WindowConfiguration.ActivityType;
116import android.app.WindowConfiguration.WindowingMode;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Context;
119import android.content.Intent;
120import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700122import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.PackageManager;
124import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100125import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800127import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128import android.hardware.display.DisplayManager;
129import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800130import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800131import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100133import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700134import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700135import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700136import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700137import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700138import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700139import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700140import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700141import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700143import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700144import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100145import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700146import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700147import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700148import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700149import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700150import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700151import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800152import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700153import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700154import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800155import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800156import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700157import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700158import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800159import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800160
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700161import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800162import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800163import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700164import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700165import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800166import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700167import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700168import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700169import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700171
Craig Mautner8d341ef2013-03-26 09:03:27 -0700172import java.io.FileDescriptor;
173import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700174import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800175import java.lang.annotation.Retention;
176import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700177import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700178import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700180import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700181
Winson Chung1dbc8112017-09-28 18:05:31 -0700182public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
183 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700185 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800193 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194
Craig Mautnerf3333272013-04-22 10:55:53 -0700195 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700197
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203
Craig Mautner05d29032013-05-03 13:40:13 -0700204 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
205 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
206 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
210 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
211 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700212 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800213 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
214 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218 // Used to indicate if an object (e.g. stack) that we are trying to get
219 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800220 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700222 // Used to indicate that windows of activities should be preserved during the resize.
223 static final boolean PRESERVE_WINDOWS = true;
224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 // Used to indicate if an object (e.g. task) should be moved/created
226 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700227 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700229 // Don't execute any calls to resume.
230 static final boolean DEFER_RESUME = true;
231
Winson Chung010927a2016-12-15 16:12:35 -0800232 // Used to indicate that a task is removed it should also be removed from recents.
233 static final boolean REMOVE_FROM_RECENTS = true;
234
Winson Chung6954fc92017-03-24 16:22:12 -0700235 // Used to indicate that pausing an activity should occur immediately without waiting for
236 // the activity callback indicating that it has completed pausing
237 static final boolean PAUSE_IMMEDIATELY = true;
238
Winson Chung7900bee2017-03-10 08:59:25 -0800239 /**
240 * The modes which affect which tasks are returned when calling
241 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
242 */
243 @Retention(RetentionPolicy.SOURCE)
244 @IntDef({
245 MATCH_TASK_IN_STACKS_ONLY,
246 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
247 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
248 })
249 public @interface AnyTaskForIdMatchTaskMode {}
250 // Match only tasks in the current stacks
251 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
252 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
253 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
254 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
255 // provided stack id
256 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
257
Svetoslav7008b512015-06-24 18:47:07 -0700258 // Activity actions an app cannot start if it uses a permission which is not granted.
259 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
260 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700261
Svetoslav7008b512015-06-24 18:47:07 -0700262 static {
263 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
264 Manifest.permission.CAMERA);
265 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
266 Manifest.permission.CAMERA);
267 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
268 Manifest.permission.CALL_PHONE);
269 }
270
Svet Ganov99b60432015-06-27 13:15:22 -0700271 /** Action restriction: launching the activity is not restricted. */
272 private static final int ACTIVITY_RESTRICTION_NONE = 0;
273 /** Action restriction: launching the activity is restricted by a permission. */
274 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
275 /** Action restriction: launching the activity is restricted by an app op. */
276 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700277
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700278 // For debugging to make sure the caller when acquiring/releasing our
279 // wake lock is the system process.
280 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800281 /** The number of distinct task ids that can be assigned to the tasks of a single user */
282 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700283
Craig Mautner27084302013-03-25 08:05:25 -0700284 final ActivityManagerService mService;
285
Winson Chung61c9e5a2017-10-11 10:39:32 -0700286 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700287 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800288
Winson Chung61c9e5a2017-10-11 10:39:32 -0700289 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700290 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700291
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700292 final ActivityStackSupervisorHandler mHandler;
293
294 /** Short cut */
295 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700297
Bryce Leedacefc42017-10-10 12:56:02 -0700298 private final LaunchingBoundsController mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700299
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700300 /** Counter for next free stack ID to use for dynamic activity stacks. */
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700301 private int mNextFreeStackId = 0;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800303 /**
304 * Maps the task identifier that activities are currently being started in to the userId of the
305 * task. Each time a new task is created, the entry for the userId of the task is incremented
306 */
307 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700308
Craig Mautner2420ead2013-04-01 17:13:20 -0700309 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack containing the launcher app. Assumed to always be attached to
313 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700315
Craig Mautnere0a38842013-12-16 16:14:02 -0800316 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800317 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700318
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
320 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800321 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
324 /** List of activities that are waiting for a new activity to become visible before completing
325 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700326 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
327 // mStoppingActivities when something else comes up.
328 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329
Bryce Lee4a194382017-04-04 14:32:48 -0700330 /** List of processes waiting to find out when a specific activity becomes visible. */
331 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700332
333 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700334 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700335
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336 /** List of activities that are ready to be stopped, but waiting for the next activity to
337 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800338 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700339
Craig Mautnerf3333272013-04-22 10:55:53 -0700340 /** List of activities that are ready to be finished, but waiting for the previous activity to
341 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800342 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700343
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800345 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346
Wale Ogunwale22e25262016-02-01 10:32:02 -0800347 /** List of activities whose multi-window mode changed that we need to report to the
348 * application */
349 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
350
351 /** List of activities whose picture-in-picture mode changed that we need to report to the
352 * application */
353 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
354
Winson Chung5af42fc2017-03-24 17:11:33 -0700355 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
356 * the application */
357 Rect mPipModeChangedTargetStackBounds;
358
Craig Mautnerf3333272013-04-22 10:55:53 -0700359 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700360 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700361
Craig Mautnerde4ef022013-04-07 19:01:33 -0700362 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
363 * is being brought in front of us. */
364 boolean mUserLeaving = false;
365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700367 * We don't want to allow the device to go to sleep while in the process
368 * of launching an activity. This is primarily to allow alarm intent
369 * receivers to launch an activity and get that to run before the device
370 * goes back to sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700373
374 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700375 * Set when the system is going to sleep, until we have
376 * successfully paused the current activity and released our wake lock.
377 * At that point the system is allowed to actually sleep.
378 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700379 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700380
David Stevens9440dc82017-03-16 19:00:20 -0700381 /**
382 * A list of tokens that cause the top activity to be put to sleep.
383 * They are used by components that may hide and block interaction with underlying
384 * activities.
385 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700386 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700387
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700388 /** Stack id of the front stack when user switched, indexed by userId. */
389 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700390
Bryce Leeaf3a4002017-03-20 10:37:12 -0700391 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700393 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800394
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800395 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
396
397 private DisplayManagerInternal mDisplayManagerInternal;
398 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800399
Wale Ogunwaled046a012015-12-24 13:05:59 -0800400 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800401 boolean inResumeTopActivity;
402
Andrii Kulian1e32e022016-09-16 15:29:34 -0700403 /**
404 * Temporary rect used during docked stack resize calculation so we don't need to create a new
405 * object each time.
406 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700407 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700408 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700409
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700410 // The default minimal size that will be used if the activity doesn't specify its minimal size.
411 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700412 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700413
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700414 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
415 private boolean mTaskLayersChanged = true;
416
Jorim Jaggi275561a2016-02-23 10:11:02 -0500417 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800418
Jorim Jaggiea039a82017-08-02 14:37:49 +0200419 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
420
Andrii Kulian1779e612016-10-12 21:58:25 -0700421 @Override
422 protected int getChildCount() {
423 return mActivityDisplays.size();
424 }
425
426 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700427 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700428 return mActivityDisplays.valueAt(index);
429 }
430
431 @Override
432 protected ConfigurationContainer getParent() {
433 return null;
434 }
435
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700436 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700437 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700438 if (activityDisplay == null) {
439 throw new IllegalArgumentException("No display found with id: " + displayId);
440 }
441
442 return activityDisplay.getOverrideConfiguration();
443 }
444
445 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700446 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700447 if (activityDisplay == null) {
448 throw new IllegalArgumentException("No display found with id: " + displayId);
449 }
450
451 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
452 }
453
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700454 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700455 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
456 int callingUid, ActivityInfo activityInfo) {
457 if (displayId == DEFAULT_DISPLAY) {
458 // No restrictions for the default display.
459 return true;
460 }
461 if (!mService.mSupportsMultiDisplay) {
462 // Can't launch on secondary displays if feature is not supported.
463 return false;
464 }
465 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
466 // Can't apply wrong configuration to non-resizeable activities.
467 return false;
468 }
469 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
470 // Can't place activities to a display that has restricted launch rules.
471 // In this case the request should be made by explicitly adding target display id and
472 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
473 return false;
474 }
475 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700476 }
477
478 /**
479 * Check if configuration of specified display matches current global config.
480 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
481 * the same config as on the default display.
482 * @param displayId Id of the display to check.
483 * @return {@code true} if configuration matches.
484 */
485 private boolean displayConfigMatchesGlobal(int displayId) {
486 if (displayId == DEFAULT_DISPLAY) {
487 return true;
488 }
489 if (displayId == INVALID_DISPLAY) {
490 return false;
491 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700492 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700493 if (targetDisplay == null) {
494 throw new IllegalArgumentException("No display found with id: " + displayId);
495 }
496 return getConfiguration().equals(targetDisplay.getConfiguration());
497 }
498
Wale Ogunwale39381972015-12-17 17:15:29 -0800499 static class FindTaskResult {
500 ActivityRecord r;
501 boolean matchedByRootAffinity;
502 }
503 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
504
Craig Mautneree36c772014-07-16 14:56:05 -0700505 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800506 * Temp storage for display ids sorted in focus order.
507 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
508 * it's more efficient, as the number of displays is usually small.
509 */
510 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
511
512 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100513 * Used to keep track whether app visibilities got changed since the last pause. Useful to
514 * determine whether to invoke the task stack change listener after pausing.
515 */
516 boolean mAppVisibilitiesChangedSinceLastPause;
517
518 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100519 * Set of tasks that are in resizing mode during an app transition to fill the "void".
520 */
521 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
522
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700523
524 /**
525 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
526 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
527 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
528 * like the docked stack going empty.
529 */
530 private boolean mAllowDockedStackResize = true;
531
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100532 /**
Tony Mak853304c2016-04-18 15:17:41 +0100533 * Is dock currently minimized.
534 */
535 boolean mIsDockMinimized;
536
Jorim Jaggife762342016-10-13 14:33:27 +0200537 final KeyguardController mKeyguardController;
538
chaviw59b98852017-06-13 12:05:44 -0700539 private PowerManager mPowerManager;
540 private int mDeferResumeCount;
541
Tony Mak853304c2016-04-18 15:17:41 +0100542 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700543 * Description of a request to start a new activity, which has been held
544 * due to app switches being disabled.
545 */
546 static class PendingActivityLaunch {
547 final ActivityRecord r;
548 final ActivityRecord sourceRecord;
549 final int startFlags;
550 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700551 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700552
553 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700554 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700555 r = _r;
556 sourceRecord = _sourceRecord;
557 startFlags = _startFlags;
558 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700559 callerApp = _callerApp;
560 }
561
562 void sendErrorResult(String message) {
563 try {
564 if (callerApp.thread != null) {
565 callerApp.thread.scheduleCrash(message);
566 }
567 } catch (RemoteException e) {
568 Slog.e(TAG, "Exception scheduling crash of failed "
569 + "activity launcher sourceRecord=" + sourceRecord, e);
570 }
Craig Mautneree36c772014-07-16 14:56:05 -0700571 }
572 }
573
Bryce Leeaf691c02017-03-20 14:20:22 -0700574 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700575 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700576 mHandler = new ActivityStackSupervisorHandler(looper);
Winson Chung3f0e59a2017-10-25 10:19:05 -0700577 mRunningTasks = createRunningTasks();
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200578 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext, looper);
Jorim Jaggife762342016-10-13 14:33:27 +0200579 mKeyguardController = new KeyguardController(service, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700580
581 mLaunchingBoundsController = new LaunchingBoundsController();
582 mLaunchingBoundsController.registerDefaultPositioners(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700583 }
584
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800585 void setRecentTasks(RecentTasks recentTasks) {
586 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700587 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800588 }
589
Winson Chung3f0e59a2017-10-25 10:19:05 -0700590 @VisibleForTesting
591 RunningTasks createRunningTasks() {
592 return new RunningTasks();
593 }
594
Jeff Brown2c43c332014-06-12 22:38:59 -0700595 /**
596 * At the time when the constructor runs, the power manager has not yet been
597 * initialized. So we initialize our wakelocks afterwards.
598 */
599 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700600 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
601 mGoingToSleep = mPowerManager
602 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
603 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700604 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700605 }
606
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700607 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800608 synchronized (mService) {
609 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200610 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611
612 mDisplayManager =
613 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
614 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800615 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616
617 Display[] displays = mDisplayManager.getDisplays();
618 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
619 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700620 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800621 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700622 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 }
624
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700625 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
626 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800627
628 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 }
Craig Mautner27084302013-03-25 08:05:25 -0700630 }
631
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700632 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800633 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700634 }
635
Craig Mautnerde4ef022013-04-07 19:01:33 -0700636 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700638 }
639
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700640 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700641 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700642 }
643
Wale Ogunwaled046a012015-12-24 13:05:59 -0800644 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800645 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
646 if (!focusCandidate.isFocusable()) {
647 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800648 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800649 }
650
651 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800652 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800653 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800654
Wale Ogunwaled046a012015-12-24 13:05:59 -0800655 EventLogTags.writeAmFocusedStack(
656 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
657 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
658 }
659
660 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800661 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800662 if (r != null && r.idle) {
663 checkFinishBootingLocked();
664 }
665 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700666 }
667
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700668 void moveHomeStackToFront(String reason) {
669 mHomeStack.moveToFront(reason);
670 }
671
Matthew Ng330757d2017-02-28 14:19:17 -0800672 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700673 final ActivityStack recentsStack = getDefaultDisplay().getStack(
674 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800675 if (recentsStack != null) {
676 recentsStack.moveToFront(reason);
677 }
678 }
679
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700680 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800681 boolean moveHomeStackTaskToTop(String reason) {
682 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700683
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700684 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700685 if (top == null) {
686 return false;
687 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700688 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700689 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700690 }
691
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800692 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700693 if (!mService.mBooting && !mService.mBooted) {
694 // Not ready yet!
695 return false;
696 }
697
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800698 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700699 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800700 final String myReason = reason + " resumeHomeStackTask";
701
Mark Lua56ea122015-10-08 13:31:01 +0800702 // Only resume home activity if isn't finishing.
703 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700704 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700706 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800707 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700708 }
709
Craig Mautner8d341ef2013-03-26 09:03:27 -0700710 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700711 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
712 }
713
714 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700715 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700716 }
717
718 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700719 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700720 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800721 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700722 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700723 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700724 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700725 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700726 @Nullable ActivityOptions aOptions, boolean onTop) {
727 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700728 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
729 throw new IllegalArgumentException("Should not specify activity options for non-restore"
730 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800731 }
732
Craig Mautnere0a38842013-12-16 16:14:02 -0800733 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700735 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
736 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
737 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700738 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700739 if (task == null) {
740 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800741 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700742 if (aOptions != null) {
743 // Resolve the stack the task should be placed in now based on options
744 // and reparent if needed.
745 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
746 if (launchStack != null && stack != launchStack) {
747 final int reparentMode = onTop
748 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
749 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
750 "anyTaskForIdLocked");
751 }
752 }
753 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700754 }
755 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800756
Winson Chung7900bee2017-03-10 08:59:25 -0800757 // If we are matching stack tasks only, return now
758 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800759 return null;
760 }
761
Winson Chung7900bee2017-03-10 08:59:25 -0800762 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
763 // the task from recents
764 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700765 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700766
767 if (task == null) {
768 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800769 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
770 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700771
772 return null;
773 }
774
775 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700776 return task;
777 }
778
Winson Chung7900bee2017-03-10 08:59:25 -0800779 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700780 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700781 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
782 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800783 return null;
784 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700785 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800786 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700787 }
788
Craig Mautner6170f732013-04-02 13:05:23 -0700789 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700792 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
793 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
794 final ActivityStack stack = display.getChildAt(stackNdx);
795 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 if (r != null) {
797 return r;
798 }
Craig Mautner6170f732013-04-02 13:05:23 -0700799 }
800 }
801 return null;
802 }
803
Tony Mak853304c2016-04-18 15:17:41 +0100804 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000805 * Detects whether we should show a lock screen in front of this task for a locked user.
806 * <p>
807 * We'll do this if either of the following holds:
808 * <ul>
809 * <li>The top activity explicitly belongs to {@param userId}.</li>
810 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
811 * </ul>
812 *
813 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100814 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000815 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
816 // To handle the case that work app is in the task but just is not the top one.
817 final ActivityRecord activityRecord = task.getTopActivity();
818 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
819
820 return (activityRecord != null && activityRecord.userId == userId)
821 || (resultTo != null && resultTo.userId == userId);
822 }
823
824 /**
825 * Find all visible task stacks containing {@param userId} and intercept them with an activity
826 * to block out the contents and possibly start a credential-confirming intent.
827 *
828 * @param userId user handle for the locked managed profile.
829 */
830 void lockAllProfileTasks(@UserIdInt int userId) {
831 mWindowManager.deferSurfaceLayout();
832 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700833 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
834 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
835 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
836 final ActivityStack stack = display.getChildAt(stackNdx);
837 final List<TaskRecord> tasks = stack.getAllTasks();
838 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
839 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000840
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700841 // Check the task for a top activity belonging to userId, or returning a
842 // result to an activity belonging to userId. Example case: a document
843 // picker for personal files, opened by a work app, should still get locked.
844 if (taskTopActivityIsUser(task, userId)) {
845 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
846 task.taskId, userId);
847 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000848 }
Tony Mak853304c2016-04-18 15:17:41 +0100849 }
850 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000851 } finally {
852 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100853 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000854 }
855
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800856 void setNextTaskIdForUserLocked(int taskId, int userId) {
857 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
858 if (taskId > currentTaskId) {
859 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700860 }
861 }
862
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700863 static int nextTaskIdForUser(int taskId, int userId) {
864 int nextTaskId = taskId + 1;
865 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
866 // Wrap around as there will be smaller task ids that are available now.
867 nextTaskId -= MAX_TASK_IDS_PER_USER;
868 }
869 return nextTaskId;
870 }
871
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800872 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800873 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
874 // for a userId u, a taskId can only be in the range
875 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
876 // 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 -0700877 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700878 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700879 || anyTaskForIdLocked(
880 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700881 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800882 if (candidateTaskId == currentTaskId) {
883 // Something wrong!
884 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
885 throw new IllegalStateException("Cannot get an available task id."
886 + " Reached limit of " + MAX_TASK_IDS_PER_USER
887 + " running tasks per user.");
888 }
889 }
890 mCurTaskIdForUser.put(userId, candidateTaskId);
891 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700892 }
893
Chong Zhang6cda19c2016-06-14 19:07:56 -0700894 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800895 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700896 if (stack == null) {
897 return null;
898 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700899 ActivityRecord resumedActivity = stack.mResumedActivity;
900 if (resumedActivity == null || resumedActivity.app == null) {
901 resumedActivity = stack.mPausingActivity;
902 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700903 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700904 }
905 }
906 return resumedActivity;
907 }
908
Dianne Hackbornff072722014-09-24 10:56:28 -0700909 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700910 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800912 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700913 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
914 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
915 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700916 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 continue;
918 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200919 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
920 final ActivityRecord top = stack.topRunningActivityLocked();
921 final int size = mTmpActivityList.size();
922 for (int i = 0; i < size; i++) {
923 final ActivityRecord activity = mTmpActivityList.get(i);
924 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
925 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200927 if (realStartActivityLocked(activity, app,
928 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 didSomething = true;
930 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700931 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200933 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700935 }
Craig Mautner20e72272013-04-01 13:45:53 -0700936 }
Craig Mautner20e72272013-04-01 13:45:53 -0700937 }
938 }
939 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700940 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700941 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700942 }
Craig Mautner20e72272013-04-01 13:45:53 -0700943 return didSomething;
944 }
945
946 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700948 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
949 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
950 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700951 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 continue;
953 }
954 final ActivityRecord resumedActivity = stack.mResumedActivity;
955 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700956 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800957 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800958 return false;
959 }
Craig Mautner20e72272013-04-01 13:45:53 -0700960 }
961 }
Wei Wang65c7a152016-06-02 18:51:22 -0700962 // Send launch end powerhint when idle
963 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700964 return true;
965 }
966
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700969 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
970 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
971 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700972 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700974 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 return false;
976 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700977 }
978 }
979 }
980 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
983 mLastFocusedStack + " to=" + mFocusedStack);
984 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 return true;
986 }
987
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700988 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800989 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700991 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
992 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800995 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700996 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800997 return false;
998 }
999 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 }
1002 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001003 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 }
1005
Craig Mautner2acc3892013-09-23 10:28:14 -07001006 /**
1007 * Pause all activities in either all of the stacks or just the back stacks.
1008 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001009 * @param resuming The resuming activity.
1010 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1011 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001012 * @return true if any activity was paused as a result of this call.
1013 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001014 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001015 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001017 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1018 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1019 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001020 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001021 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001022 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001023 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1024 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001025 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001026 }
1027 }
1028 return someActivityPaused;
1029 }
1030
Craig Mautnerde4ef022013-04-07 19:01:33 -07001031 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001032 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001033 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001034 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1035 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1036 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001038 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001039 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001040 Slog.d(TAG_STATES,
1041 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001042 pausing = false;
1043 } else {
1044 return false;
1045 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001046 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001047 }
1048 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001049 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001050 }
1051
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001052 void cancelInitializingActivities() {
1053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001054 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1055 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1056 final ActivityStack stack = display.getChildAt(stackNdx);
1057 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001058 }
1059 }
1060 }
1061
Bryce Lee4a194382017-04-04 14:32:48 -07001062 void waitActivityVisible(ComponentName name, WaitResult result) {
1063 final WaitInfo waitInfo = new WaitInfo(name, result);
1064 mWaitingForActivityVisible.add(waitInfo);
1065 }
1066
1067 void cleanupActivity(ActivityRecord r) {
1068 // Make sure this record is no longer in the pending finishes list.
1069 // This could happen, for example, if we are trimming activities
1070 // down to the max limit while they are still waiting to finish.
1071 mFinishingActivities.remove(r);
1072 mActivitiesWaitingForVisibleActivity.remove(r);
1073
1074 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001075 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001076 mWaitingForActivityVisible.remove(i);
1077 }
1078 }
1079 }
1080
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001081 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001082 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001083 }
1084
1085 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1086 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001087 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1088 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001089 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001090 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001091 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001092 result.timeout = false;
1093 result.who = w.getComponent();
1094 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1095 result.thisTime = result.totalTime;
1096 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001097 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001098 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001099 if (changed) {
1100 mService.notifyAll();
1101 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001102 }
1103
Chong Zhang5022da32016-06-21 16:31:37 -07001104 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1105 boolean changed = false;
1106 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1107 WaitResult w = mWaitingActivityLaunched.remove(i);
1108 if (w.who == null) {
1109 changed = true;
1110 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1111 // the starting activity ends up moving another activity to front, and it should
1112 // wait for this new activity to become visible instead.
1113 // Do not modify other fields.
1114 w.result = START_TASK_TO_FRONT;
1115 }
1116 }
1117 if (changed) {
1118 mService.notifyAll();
1119 }
1120 }
1121
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1123 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001126 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001127 if (w.who == null) {
1128 changed = true;
1129 w.timeout = timeout;
1130 if (r != null) {
1131 w.who = new ComponentName(r.info.packageName, r.info.name);
1132 }
1133 w.thisTime = thisTime;
1134 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001135 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001136 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001137 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001138 if (changed) {
1139 mService.notifyAll();
1140 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001141 }
1142
Craig Mautner29219d92013-04-16 20:19:12 -07001143 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001144 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001145 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001146 if (r != null) {
1147 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001148 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001149
Andrii Kulian7318d632016-07-20 18:59:28 -07001150 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001151 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1152
1153 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1154 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001155 final ActivityDisplay display = mActivityDisplays.get(displayId);
1156 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1157 final ActivityStack stack = display.getChildAt(j);
1158 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001159 r = stack.topRunningActivityLocked();
1160 if (r != null) {
1161 return r;
1162 }
Craig Mautner29219d92013-04-16 20:19:12 -07001163 }
1164 }
1165 }
1166 return null;
1167 }
1168
Winson Chung61c9e5a2017-10-11 10:39:32 -07001169 @VisibleForTesting
1170 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1171 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1172 int callingUid, boolean allowed) {
1173 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1174 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001175 }
1176
Todd Kennedy7440f172015-12-09 14:31:22 -08001177 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1178 ProfilerInfo profilerInfo) {
1179 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 if (aInfo != null) {
1181 // Store the found target back into the intent, because now that
1182 // we have it we never want to do this again. For example, if the
1183 // user navigates back to this point in the history, we should
1184 // always restart the exact same activity.
1185 intent.setComponent(new ComponentName(
1186 aInfo.applicationInfo.packageName, aInfo.name));
1187
1188 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001189 if (!aInfo.processName.equals("system")) {
1190 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001191 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001192 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001193
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001194 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1195 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1196 }
1197
Man Caocfa78b22015-06-11 20:14:34 -07001198 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1199 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1200 }
1201
1202 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001203 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001204 }
1205 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001206 final String intentLaunchToken = intent.getLaunchToken();
1207 if (aInfo.launchToken == null && intentLaunchToken != null) {
1208 aInfo.launchToken = intentLaunchToken;
1209 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001210 }
1211 return aInfo;
1212 }
1213
Todd Kennedy7440f172015-12-09 14:31:22 -08001214 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001215 return resolveIntent(intent, resolvedType, userId, 0);
1216 }
1217
1218 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001219 synchronized (mService) {
1220 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001221 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001222 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001223 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001224 }
1225
1226 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1227 ProfilerInfo profilerInfo, int userId) {
1228 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1229 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1230 }
1231
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001232 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1233 boolean andResume, boolean checkConfig) throws RemoteException {
1234
1235 if (!allPausedActivitiesComplete()) {
1236 // While there are activities pausing we skipping starting any new activities until
1237 // pauses are complete. NOTE: that we also do this for activities that are starting in
1238 // the paused state because they will first be resumed then paused on the client side.
1239 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1240 "realStartActivityLocked: Skipping start of r=" + r
1241 + " some activities pausing...");
1242 return false;
1243 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001244
Bryce Leeaf691c02017-03-20 14:20:22 -07001245 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001246 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001247
1248 beginDeferResume();
1249
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 try {
chaviw59b98852017-06-13 12:05:44 -07001251 r.startFreezingScreenLocked(app, 0);
1252
1253 // schedule launch ticks to collect information about slow apps.
1254 r.startLaunchTickingLocked();
1255
1256 r.app = app;
1257
Jorim Jaggi838c2452017-08-28 15:44:43 +02001258 if (mKeyguardController.isKeyguardLocked()) {
1259 r.notifyUnknownVisibilityLaunched();
1260 }
1261
chaviw59b98852017-06-13 12:05:44 -07001262 // Have the window manager re-evaluate the orientation of the screen based on the new
1263 // activity order. Note that as a result of this, it can call back into the activity
1264 // manager with a new orientation. We don't care about that, because the activity is
1265 // not currently running so we are just restarting it anyway.
1266 if (checkConfig) {
1267 final int displayId = r.getDisplayId();
1268 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1269 getDisplayOverrideConfiguration(displayId),
1270 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1271 // Deferring resume here because we're going to launch new activity shortly.
1272 // We don't want to perform a redundant launch of the same record while ensuring
1273 // configurations and trying to resume top activity of focused stack.
1274 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1275 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 }
chaviw59b98852017-06-13 12:05:44 -07001277
1278 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1279 true /* isTop */)) {
1280 // We only set the visibility to true if the activity is allowed to be visible
1281 // based on
1282 // keyguard state. This avoids setting this into motion in window manager that is
1283 // later cancelled due to later calls to ensure visible activities that set
1284 // visibility back to false.
1285 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001286 }
chaviw59b98852017-06-13 12:05:44 -07001287
chaviw59b98852017-06-13 12:05:44 -07001288 final int applicationInfoUid =
1289 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1290 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1291 Slog.wtf(TAG,
1292 "User ID for activity changing for " + r
1293 + " appInfo.uid=" + r.appInfo.uid
1294 + " info.ai.uid=" + applicationInfoUid
1295 + " old=" + r.app + " new=" + app);
1296 }
1297
1298 app.waitingToKill = null;
1299 r.launchCount++;
1300 r.lastLaunchTime = SystemClock.uptimeMillis();
1301
1302 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1303
1304 int idx = app.activities.indexOf(r);
1305 if (idx < 0) {
1306 app.activities.add(r);
1307 }
1308 mService.updateLruProcessLocked(app, true, null);
1309 mService.updateOomAdjLocked();
1310
Charles Heff9b4dff2017-09-22 10:18:37 +01001311 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1312 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1313 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
1314 && mService.mLockTaskController.getLockTaskModeState()
1315 == LOCK_TASK_MODE_LOCKED)) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001316 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001317 }
1318
1319 try {
1320 if (app.thread == null) {
1321 throw new RemoteException();
1322 }
1323 List<ResultInfo> results = null;
1324 List<ReferrerIntent> newIntents = null;
1325 if (andResume) {
1326 // We don't need to deliver new intents and/or set results if activity is going
1327 // to pause immediately after launch.
1328 results = r.results;
1329 newIntents = r.newIntents;
1330 }
1331 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1332 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1333 + " newIntents=" + newIntents + " andResume=" + andResume);
1334 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1335 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001336 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001337 // Home process is the root process of the task.
1338 mService.mHomeProcess = task.mActivities.get(0).app;
1339 }
1340 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1341 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1342 r.sleeping = false;
1343 r.forceNewConfig = false;
1344 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1345 mService.showAskCompatModeDialogLocked(r);
1346 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1347 ProfilerInfo profilerInfo = null;
1348 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1349 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1350 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001351 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1352 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1353 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001354 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001355 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001356 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001357 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001358 }
1359 }
chaviw59b98852017-06-13 12:05:44 -07001360
Andreas Gampe2b073a02017-06-22 17:28:57 -07001361 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 }
1364 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001365
chaviw59b98852017-06-13 12:05:44 -07001366 app.hasShownUi = true;
1367 app.pendingUiClean = true;
1368 app.forceProcessStateUpTo(mService.mTopProcessState);
1369 // Because we could be starting an Activity in the system process this may not go
1370 // across a Binder interface which would create a new Configuration. Consequently
1371 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001372
chaviw59b98852017-06-13 12:05:44 -07001373 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1374 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1375 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001376
chaviw59b98852017-06-13 12:05:44 -07001377 logIfTransactionTooLarge(r.intent, r.icicle);
1378 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1379 System.identityHashCode(r), r.info,
1380 // TODO: Have this take the merged configuration instead of separate global
1381 // and override configs.
1382 mergedConfiguration.getGlobalConfiguration(),
1383 mergedConfiguration.getOverrideConfiguration(), r.compat,
1384 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1385 r.persistentState, results, newIntents, !andResume,
1386 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001387
chaviw59b98852017-06-13 12:05:44 -07001388 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1389 // This may be a heavy-weight process! Note that the package
1390 // manager will ensure that only activity can run in the main
1391 // process of the .apk, which is the only thing that will be
1392 // considered heavy-weight.
1393 if (app.processName.equals(app.info.packageName)) {
1394 if (mService.mHeavyWeightProcess != null
1395 && mService.mHeavyWeightProcess != app) {
1396 Slog.w(TAG, "Starting new heavy weight process " + app
1397 + " when already running "
1398 + mService.mHeavyWeightProcess);
1399 }
1400 mService.mHeavyWeightProcess = app;
1401 Message msg = mService.mHandler.obtainMessage(
1402 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1403 msg.obj = r;
1404 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001405 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001407
chaviw59b98852017-06-13 12:05:44 -07001408 } catch (RemoteException e) {
1409 if (r.launchFailed) {
1410 // This is the second time we failed -- finish activity
1411 // and give up.
1412 Slog.e(TAG, "Second failure launching "
1413 + r.intent.getComponent().flattenToShortString()
1414 + ", giving up", e);
1415 mService.appDiedLocked(app);
1416 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1417 "2nd-crash", false);
1418 return false;
1419 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001420
chaviw59b98852017-06-13 12:05:44 -07001421 // This is the first time we failed -- restart process and
1422 // retry.
1423 r.launchFailed = true;
1424 app.activities.remove(r);
1425 throw e;
1426 }
1427 } finally {
1428 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001429 }
1430
1431 r.launchFailed = false;
1432 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001433 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001434 }
1435
chaviw59b98852017-06-13 12:05:44 -07001436 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001437 // As part of the process of launching, ActivityThread also performs
1438 // a resume.
1439 stack.minimalResumeActivityLocked(r);
1440 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001441 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001442 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001443 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001444 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001445 "Moving to PAUSED: " + r + " (starting in paused state)");
1446 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001447 }
1448
1449 // Launch the new version setup screen if needed. We do this -after-
1450 // launching the initial activity (that is, home), so that it can have
1451 // a chance to initialize itself while in the background, making the
1452 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001453 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001454 mService.startSetupActivityLocked();
1455 }
1456
Dianne Hackborn465fa392014-09-14 14:21:18 -07001457 // Update any services we are bound to that might care about whether
1458 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001459 if (r.app != null) {
1460 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1461 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001462
Craig Mautner2420ead2013-04-01 17:13:20 -07001463 return true;
1464 }
1465
Sudheer Shankafab200f2017-05-17 20:41:53 -07001466 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1467 int extrasSize = 0;
1468 if (intent != null) {
1469 final Bundle extras = intent.getExtras();
1470 if (extras != null) {
1471 extrasSize = extras.getSize();
1472 }
1473 }
1474 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1475 if (extrasSize + icicleSize > 200000) {
1476 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1477 + ", icicle size: " + icicleSize);
1478 }
1479 }
1480
Craig Mautnere79d42682013-04-01 19:01:53 -07001481 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001482 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001483 // Is this activity's application already running?
1484 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001485 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001486
Andrii Kulian02b7a832016-10-06 23:11:56 -07001487 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001488
1489 if (app != null && app.thread != null) {
1490 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001491 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1492 || !"android".equals(r.info.packageName)) {
1493 // Don't add this if it is a platform component that is marked
1494 // to run in multiple processes, because this is actually
1495 // part of the framework so doesn't make sense to track as a
1496 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001497 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1498 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001499 }
George Mount2c92c972014-03-20 09:38:23 -07001500 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001501 return;
1502 } catch (RemoteException e) {
1503 Slog.w(TAG, "Exception when starting activity "
1504 + r.intent.getComponent().flattenToShortString(), e);
1505 }
1506
1507 // If a dead object exception was thrown -- fall through to
1508 // restart the application.
1509 }
1510
1511 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001512 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001513 }
1514
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001515 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001516 String resultWho, int requestCode, int callingPid, int callingUid,
1517 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001518 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001519 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1520 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001521 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001522 return true;
1523 }
1524 final int componentRestriction = getComponentRestrictionForCallingPackage(
1525 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1526 final int actionRestriction = getActionRestrictionForCallingPackage(
1527 intent.getAction(), callingPackage, callingPid, callingUid);
1528 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1529 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1530 if (resultRecord != null) {
1531 resultStack.sendActivityResultLocked(-1,
1532 resultRecord, resultWho, requestCode,
1533 Activity.RESULT_CANCELED, null);
1534 }
1535 final String msg;
1536 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1537 msg = "Permission Denial: starting " + intent.toString()
1538 + " from " + callerApp + " (pid=" + callingPid
1539 + ", uid=" + callingUid + ")" + " with revoked permission "
1540 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1541 } else if (!aInfo.exported) {
1542 msg = "Permission Denial: starting " + intent.toString()
1543 + " from " + callerApp + " (pid=" + callingPid
1544 + ", uid=" + callingUid + ")"
1545 + " not exported from uid " + aInfo.applicationInfo.uid;
1546 } else {
1547 msg = "Permission Denial: starting " + intent.toString()
1548 + " from " + callerApp + " (pid=" + callingPid
1549 + ", uid=" + callingUid + ")"
1550 + " requires " + aInfo.permission;
1551 }
1552 Slog.w(TAG, msg);
1553 throw new SecurityException(msg);
1554 }
1555
1556 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1557 final String message = "Appop Denial: starting " + intent.toString()
1558 + " from " + callerApp + " (pid=" + callingPid
1559 + ", uid=" + callingUid + ")"
1560 + " requires " + AppOpsManager.permissionToOp(
1561 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1562 Slog.w(TAG, message);
1563 return false;
1564 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1565 final String message = "Appop Denial: starting " + intent.toString()
1566 + " from " + callerApp + " (pid=" + callingPid
1567 + ", uid=" + callingUid + ")"
1568 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1569 Slog.w(TAG, message);
1570 return false;
1571 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001572 if (options != null) {
Winson Chung3f0e59a2017-10-25 10:19:05 -07001573 // If a launch task id is specified, then ensure that the caller is the recents
1574 // component or has the START_TASKS_FROM_RECENTS permission
1575 if (options.getLaunchTaskId() != INVALID_TASK_ID
1576 && !mRecentTasks.isCallerRecents(callingUid)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001577 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1578 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001579 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001580 final String msg = "Permission Denial: starting " + intent.toString()
1581 + " from " + callerApp + " (pid=" + callingPid
1582 + ", uid=" + callingUid + ") with launchTaskId="
1583 + options.getLaunchTaskId();
1584 Slog.w(TAG, msg);
1585 throw new SecurityException(msg);
1586 }
1587 }
1588 // Check if someone tries to launch an activity on a private display with a different
1589 // owner.
1590 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001591 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1592 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001593 final String msg = "Permission Denial: starting " + intent.toString()
1594 + " from " + callerApp + " (pid=" + callingPid
1595 + ", uid=" + callingUid + ") with launchDisplayId="
1596 + launchDisplayId;
1597 Slog.w(TAG, msg);
1598 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001599 }
Charles He2bf28322017-10-12 22:24:49 +01001600 // Check if someone tries to launch an unwhitelisted activity into LockTask mode.
1601 final boolean lockTaskMode = options.getLockTaskMode();
1602 if (lockTaskMode && !mService.mLockTaskController.isPackageWhitelisted(
1603 UserHandle.getUserId(callingUid), aInfo.packageName)) {
1604 final String msg = "Permission Denial: starting " + intent.toString()
1605 + " from " + callerApp + " (pid=" + callingPid
1606 + ", uid=" + callingUid + ") with lockTaskMode=true";
1607 Slog.w(TAG, msg);
1608 throw new SecurityException(msg);
1609 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001610 }
1611
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001612 return true;
1613 }
1614
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001615 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001616 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1617 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001618 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1619 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1620
Andrii Kulian02689a72017-07-06 14:28:59 -07001621 if (callingPid == -1 && callingUid == -1) {
1622 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1623 return true;
1624 }
1625
Andrii Kulian62e6f252017-05-30 22:46:53 -07001626 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001627 if (activityDisplay == null) {
1628 Slog.w(TAG, "Launch on display check: display not found");
1629 return false;
1630 }
1631
Andrii Kulian02689a72017-07-06 14:28:59 -07001632 // Check if the caller has enough privileges to embed activities and launch to private
1633 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001634 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001635 callingUid);
1636 if (startAnyPerm == PERMISSION_GRANTED) {
1637 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1638 + " allow launch any on display");
1639 return true;
1640 }
1641
Andrii Kulian8f070292017-09-12 22:56:49 -07001642 // Check if caller is already present on display
1643 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1644
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001645 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1646 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1647 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001648 // Limit launching on virtual displays, because their contents can be read from Surface
1649 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001650 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1651 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1652 + " disallow launch on virtual display for not-embedded activity.");
1653 return false;
1654 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001655 // Check if the caller is allowed to embed activities from other apps.
1656 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001657 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001658 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1659 + " disallow activity embedding without permission.");
1660 return false;
1661 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001662 }
1663
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001664 if (!activityDisplay.isPrivate()) {
1665 // Anyone can launch on a public display.
1666 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1667 + " allow launch on public display");
1668 return true;
1669 }
1670
1671 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001672 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001673 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1674 + " allow launch for owner of the display");
1675 return true;
1676 }
1677
Andrii Kulian8f070292017-09-12 22:56:49 -07001678 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001679 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1680 + " allow launch for caller present on the display");
1681 return true;
1682 }
1683
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001684 Slog.w(TAG, "Launch on display check: denied");
1685 return false;
1686 }
1687
1688 /** Update lists of UIDs that are present on displays and have access to them. */
1689 void updateUIDsPresentOnDisplay() {
1690 mDisplayAccessUIDs.clear();
1691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1692 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001693 // Only bother calculating the whitelist for private displays
1694 if (activityDisplay.isPrivate()) {
1695 mDisplayAccessUIDs.append(
1696 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1697 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001698 }
1699 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1700 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1701 }
1702
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001703 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001704 final long identity = Binder.clearCallingIdentity();
1705 try {
1706 return UserManager.get(mService.mContext).getUserInfo(userId);
1707 } finally {
1708 Binder.restoreCallingIdentity(identity);
1709 }
1710 }
1711
Svet Ganov99b60432015-06-27 13:15:22 -07001712 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001713 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001714 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1715 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001716 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001717 return ACTIVITY_RESTRICTION_PERMISSION;
1718 }
1719
Christopher Tateff7add02015-08-17 10:23:22 -07001720 if (activityInfo.permission == null) {
1721 return ACTIVITY_RESTRICTION_NONE;
1722 }
1723
Svet Ganov99b60432015-06-27 13:15:22 -07001724 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1725 if (opCode == AppOpsManager.OP_NONE) {
1726 return ACTIVITY_RESTRICTION_NONE;
1727 }
1728
1729 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1730 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001731 if (!ignoreTargetSecurity) {
1732 return ACTIVITY_RESTRICTION_APPOP;
1733 }
Svet Ganov99b60432015-06-27 13:15:22 -07001734 }
1735
1736 return ACTIVITY_RESTRICTION_NONE;
1737 }
1738
Svetoslav7008b512015-06-24 18:47:07 -07001739 private int getActionRestrictionForCallingPackage(String action,
1740 String callingPackage, int callingPid, int callingUid) {
1741 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1746 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001747 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001748 }
1749
1750 final PackageInfo packageInfo;
1751 try {
1752 packageInfo = mService.mContext.getPackageManager()
1753 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1754 } catch (PackageManager.NameNotFoundException e) {
1755 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001756 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001757 }
1758
1759 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001763 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 final int opCode = AppOpsManager.permissionToOpCode(permission);
1768 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
1772 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1773 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001774 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001775 }
1776
Svet Ganov99b60432015-06-27 13:15:22 -07001777 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001778 }
1779
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001780 void setLaunchSource(int uid) {
1781 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1782 }
1783
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001784 void acquireLaunchWakelock() {
1785 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1786 throw new IllegalStateException("Calling must be system uid");
1787 }
1788 mLaunchingActivity.acquire();
1789 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1790 // To be safe, don't allow the wake lock to be held for too long.
1791 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1792 }
1793 }
1794
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001795 /**
1796 * Called when the frontmost task is idle.
1797 * @return the state of mService.mBooting before this was called.
1798 */
1799 private boolean checkFinishBootingLocked() {
1800 final boolean booting = mService.mBooting;
1801 boolean enableScreen = false;
1802 mService.mBooting = false;
1803 if (!mService.mBooted) {
1804 mService.mBooted = true;
1805 enableScreen = true;
1806 }
1807 if (booting || enableScreen) {
1808 mService.postFinishBooting(booting, enableScreen);
1809 }
1810 return booting;
1811 }
1812
Craig Mautnerf3333272013-04-22 10:55:53 -07001813 // Checked.
1814 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001815 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001816 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001817
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001819 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001820 int NS = 0;
1821 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001822 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 boolean activityRemoved = false;
1824
Wale Ogunwale7d701172015-03-11 15:36:30 -07001825 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001826 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001827 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1828 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001829 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1830 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001831 if (fromTimeout) {
1832 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001833 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834
1835 // This is a hack to semi-deal with a race condition
1836 // in the client where it can be constructed with a
1837 // newer configuration from when we asked it to launch.
1838 // We'll update with whatever configuration it now says
1839 // it used to launch.
1840 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001841 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001842 }
1843
1844 // We are now idle. If someone is waiting for a thumbnail from
1845 // us, we can now deliver.
1846 r.idle = true;
1847
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001848 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001849 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001850 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001851 }
1852 }
1853
1854 if (allResumedActivitiesIdle()) {
1855 if (r != null) {
1856 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001857 }
1858
1859 if (mLaunchingActivity.isHeld()) {
1860 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1861 if (VALIDATE_WAKE_LOCK_CALLER &&
1862 Binder.getCallingUid() != Process.myUid()) {
1863 throw new IllegalStateException("Calling must be system uid");
1864 }
1865 mLaunchingActivity.release();
1866 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001867 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001868 }
1869
1870 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001871 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1872 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001873 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001874 if ((NF = mFinishingActivities.size()) > 0) {
1875 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 mFinishingActivities.clear();
1877 }
1878
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001880 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001881 mStartingUsers.clear();
1882 }
1883
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 // Stop any activities that are scheduled to do so but have been
1885 // waiting for the next one to start.
1886 for (int i = 0; i < NS; i++) {
1887 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001888 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001889 if (stack != null) {
1890 if (r.finishing) {
1891 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1892 } else {
1893 stack.stopActivityLocked(r);
1894 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 }
1896 }
1897
1898 // Finish any activities that are scheduled to do so but have been
1899 // waiting for the next one to start.
1900 for (int i = 0; i < NF; i++) {
1901 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001902 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001903 if (stack != null) {
1904 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1905 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 }
1907
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001908 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001909 // Complete user switch
1910 if (startingUsers != null) {
1911 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001912 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001913 }
1914 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001915 }
1916
1917 mService.trimApplications();
1918 //dump();
1919 //mWindowManager.dump();
1920
Craig Mautnerf3333272013-04-22 10:55:53 -07001921 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001922 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001923 }
1924
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001925 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001926 }
1927
Craig Mautner8e569572013-10-11 17:36:59 -07001928 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001929 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001931 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1932 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1933 final ActivityStack stack = display.getChildAt(stackNdx);
1934 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001935 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001936 }
Craig Mautner19091252013-10-05 00:03:53 -07001937 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001938 }
1939
1940 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001942 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1943 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1944 final ActivityStack stack = display.getChildAt(stackNdx);
1945 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001946 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001947 }
1948 }
1949
Craig Mautner93529a42013-10-04 15:03:13 -07001950 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001951 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001952 }
1953
Craig Mautner8d341ef2013-03-26 09:03:27 -07001954 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001955 * Update the last used stack id for non-current user (current user's last
1956 * used stack is the focused stack)
1957 */
1958 void updateUserStackLocked(int userId, ActivityStack stack) {
1959 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001960 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001961 }
1962 }
1963
1964 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001965 * @return true if some activity was finished (or would have finished if doit were true).
1966 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001967 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1968 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001969 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001971 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1972 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1973 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001974 if (stack.finishDisabledPackageActivitiesLocked(
1975 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001976 didSomething = true;
1977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001978 }
1979 }
1980 return didSomething;
1981 }
1982
Dianne Hackborna413dc02013-07-12 12:02:55 -07001983 void updatePreviousProcessLocked(ActivityRecord r) {
1984 // Now that this process has stopped, we may want to consider
1985 // it to be the previous app to try to keep around in case
1986 // the user wants to return to it.
1987
1988 // First, found out what is currently the foreground app, so that
1989 // we don't blow away the previous app if this activity is being
1990 // hosted by the process that is actually still the foreground.
1991 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001993 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1994 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1995 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001996 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001997 if (stack.mResumedActivity != null) {
1998 fgApp = stack.mResumedActivity.app;
1999 } else if (stack.mPausingActivity != null) {
2000 fgApp = stack.mPausingActivity.app;
2001 }
2002 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002003 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002004 }
2005 }
2006
2007 // Now set this one as the previous process, only if that really
2008 // makes sense to.
2009 if (r.app != null && fgApp != null && r.app != fgApp
2010 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002011 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002012 mService.mPreviousProcess = r.app;
2013 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2014 }
2015 }
2016
Wale Ogunwaled046a012015-12-24 13:05:59 -08002017 boolean resumeFocusedStackTopActivityLocked() {
2018 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002019 }
2020
Wale Ogunwaled046a012015-12-24 13:05:59 -08002021 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002022 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002023
2024 if (!readyToResume()) {
2025 return false;
2026 }
2027
Wale Ogunwaled046a012015-12-24 13:05:59 -08002028 if (targetStack != null && isFocusedStack(targetStack)) {
2029 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002030 }
chaviw59b98852017-06-13 12:05:44 -07002031
Wale Ogunwale06579d62016-04-30 15:29:06 -07002032 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2033 if (r == null || r.state != RESUMED) {
2034 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002035 } else if (r.state == RESUMED) {
2036 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2037 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002038 }
chaviw59b98852017-06-13 12:05:44 -07002039
Wale Ogunwaled046a012015-12-24 13:05:59 -08002040 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002041 }
2042
Todd Kennedy39bfee52016-02-24 10:28:21 -08002043 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2044 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002045 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2046 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2047 final ActivityStack stack = display.getChildAt(stackNdx);
2048 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002049 }
2050 }
2051 }
2052
Adrian Roos20d7df32016-01-12 18:59:43 +01002053 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2054 TaskRecord finishedTask = null;
2055 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002056 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002057 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2058 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002059 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002060 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002061 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2062 if (stack == focusedStack || finishedTask == null) {
2063 finishedTask = t;
2064 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002065 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002066 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002067 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002068 }
2069
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002070 void finishVoiceTask(IVoiceInteractionSession session) {
2071 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002072 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2073 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002074 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002075 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002076 stack.finishVoiceTask(session);
2077 }
2078 }
2079 }
2080
Wale Ogunwale66e16852017-10-19 13:35:52 -07002081 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options,
Andrii Kulianc27916642016-04-12 17:59:27 -07002082 String reason, boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002083 final ActivityStack currentStack = task.getStack();
2084 if (currentStack == null) {
2085 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2086 + task + " to front. Stack is null");
2087 return;
2088 }
2089
Craig Mautneraea74a52014-03-08 14:23:10 -08002090 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2091 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002092 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002093
2094 final ActivityRecord prev = topRunningActivityLocked();
2095
2096 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2097 || (prev != null && prev.isActivityTypeRecents())) {
2098 // Caller wants the home activity moved with it or the previous task is recents in which
2099 // case we always return home from the task we are moving to the front.
2100 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002101 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002102
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002103 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2104 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2105 task.updateOverrideConfiguration(bounds);
2106
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002107 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002108
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002109 if (stack != currentStack) {
2110 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002111 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002112 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002113 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2114 // still need moveTaskToFrontLocked() below for any transition settings.
2115 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002116 if (stack.resizeStackWithLaunchBounds()) {
2117 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002118 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2119 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002120 } else {
2121 // WM resizeTask must be done after the task is moved to the correct stack,
2122 // because Task's setBounds() also updates dim layer's bounds, but that has
2123 // dependency on the stack.
2124 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002125 }
2126 }
2127
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002128 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002129 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002130 r == null ? null : r.appTimeTracker, reason);
2131
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002132 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002133 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002134
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002135 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002136 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 }
2138
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002139 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002140 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002141 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002142 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002143 return false;
2144 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002145 return (mService.mSupportsPictureInPicture
2146 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002147 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002148 }
2149
Bryce Leedacefc42017-10-10 12:56:02 -07002150 LaunchingBoundsController getLaunchingBoundsController() {
2151 return mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002152 }
2153
Winson Chung55893332017-02-17 17:13:10 -08002154 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002155 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2156 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2157 if (stack != null) {
2158 return stack;
2159 }
2160 }
2161 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002162 }
2163
Wale Ogunwale68278562017-09-23 17:13:55 -07002164 /** @see ActivityDisplay#getStack(int, int) */
2165 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2166 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2167 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2168 if (stack != null) {
2169 return stack;
2170 }
2171 }
2172 return null;
2173 }
2174
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002175 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002176 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002177 // Preference is given to the activity type for the activity then the task since the type
2178 // once set shouldn't change.
2179 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2180 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2181 activityType = task.getActivityType();
2182 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002183 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2184 return activityType;
2185 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002186 if (options != null) {
2187 activityType = options.getLaunchActivityType();
2188 }
2189 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002190 }
2191
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002192 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2193 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2194 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002195 }
2196
2197 /**
2198 * Returns the right stack to use for launching factoring in all the input parameters.
2199 *
2200 * @param r The activity we are trying to launch. Can be null.
2201 * @param options The activity options used to the launch. Can be null.
2202 * @param candidateTask The possible task the activity might be launched in. Can be null.
2203 *
2204 * @return The stack to use for the launch or INVALID_STACK_ID.
2205 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002206 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2207 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2208 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002209 int taskId = INVALID_TASK_ID;
2210 int displayId = INVALID_DISPLAY;
2211 //Rect bounds = null;
2212
2213 // We give preference to the launch preference in activity options.
2214 if (options != null) {
2215 taskId = options.getLaunchTaskId();
2216 displayId = options.getLaunchDisplayId();
2217 // TODO: Need to work this into the equation...
2218 //bounds = options.getLaunchBounds();
2219 }
2220
2221 // First preference for stack goes to the task Id set in the activity options. Use the stack
2222 // associated with that if possible.
2223 if (taskId != INVALID_TASK_ID) {
2224 // Temporarily set the task id to invalid in case in re-entry.
2225 options.setLaunchTaskId(INVALID_TASK_ID);
2226 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002227 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002228 options.setLaunchTaskId(taskId);
2229 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002230 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002231 }
2232 }
2233
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002234 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002235 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002236
2237 // Next preference for stack goes to the display Id set in the activity options or the
2238 // candidate display.
2239 if (displayId == INVALID_DISPLAY) {
2240 displayId = candidateDisplayId;
2241 }
2242 if (displayId != INVALID_DISPLAY) {
2243 if (r != null) {
2244 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002245 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002246 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002247 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002248 }
2249 }
2250 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2251 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002252 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002253 if (stack != null) {
2254 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002255 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002256 }
2257 }
2258
2259 // Give preference to the stack and display of the input task and activity if they match the
2260 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002261 stack = null;
2262 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002263 if (candidateTask != null) {
2264 stack = candidateTask.getStack();
2265 }
2266 if (stack == null && r != null) {
2267 stack = r.getStack();
2268 }
2269 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002270 display = stack.getDisplay();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002271 if (display != null) {
2272 final int windowingMode =
2273 display.resolveWindowingMode(r, options, candidateTask, activityType);
2274 if (stack.isCompatible(windowingMode, activityType)) {
2275 return stack;
2276 }
2277 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002278 }
2279
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002280 if (display == null
2281 // TODO: Can be removed once we figure-out how non-standard types should launch
2282 // outside the default display.
2283 || (activityType != ACTIVITY_TYPE_STANDARD
2284 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2285 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002286 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002287
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002288 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002289 }
2290
Andrii Kulian16802aa2016-11-02 12:21:33 -07002291 /**
2292 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2293 * If there is no such stack, new dynamic stack can be created.
2294 * @param displayId Target display.
2295 * @param r Activity that should be launched there.
2296 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2297 */
2298 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002299 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002300 if (activityDisplay == null) {
2301 throw new IllegalArgumentException(
2302 "Display with displayId=" + displayId + " not found.");
2303 }
2304
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002305 if (!r.canBeLaunchedOnDisplay(displayId)) {
2306 return null;
2307 }
2308
Andrii Kulian16802aa2016-11-02 12:21:33 -07002309 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002310 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2311 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002312 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002313 return stack;
2314 }
2315 }
2316
2317 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002318 if (displayId != DEFAULT_DISPLAY) {
2319 return activityDisplay.createStack(
2320 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002321 }
2322
2323 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2324 return null;
2325 }
2326
Wale Ogunwale68278562017-09-23 17:13:55 -07002327 // TODO: Can probably be consolidated into getLaunchStack()...
2328 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2329 switch (stack.getActivityType()) {
2330 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2331 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2332 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002333 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002334 switch (stack.getWindowingMode()) {
2335 case WINDOWING_MODE_FULLSCREEN: return true;
2336 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2337 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2338 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2339 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2340 }
2341
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002342 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002343 return r.canBeLaunchedOnDisplay(displayId);
2344 }
2345 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2346 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002347 }
2348
Andrii Kulian7fc22812016-12-28 13:04:11 -08002349 /**
2350 * Get next focusable stack in the system. This will search across displays and stacks
2351 * in last-focused order for a focusable and visible stack, different from the target stack.
2352 *
2353 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2354 * searching for next candidate.
2355 * @return Next focusable {@link ActivityStack}, null if not found.
2356 */
2357 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2358 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2359
2360 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2361 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002362 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002363 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2364 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2365 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002366 if (stack != currentFocus && stack.isFocusable()
2367 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002368 return stack;
2369 }
2370 }
2371 }
2372
2373 return null;
2374 }
2375
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002376 /**
2377 * Get next valid stack for launching provided activity in the system. This will search across
2378 * displays and stacks in last-focused order for a focusable and visible stack, except those
2379 * that are on a currently focused display.
2380 *
2381 * @param r The activity that is being launched.
2382 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2383 * searching for the next candidate.
2384 * @return Next valid {@link ActivityStack}, null if not found.
2385 */
2386 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2387 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2388 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2389 final int displayId = mTmpOrderedDisplayIds.get(i);
2390 if (displayId == currentFocus) {
2391 continue;
2392 }
2393 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2394 if (stack != null) {
2395 return stack;
2396 }
2397 }
2398 return null;
2399 }
2400
Craig Mautneree2e45a2014-06-27 12:10:03 -07002401 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002402 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002403 }
2404
2405 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002406 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2407 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2408 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002409 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002410 final ArrayList<ActivityRecord> activities = task.mActivities;
2411 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2412 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002413 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002414 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002415 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002416 }
2417 }
2418 }
2419 }
2420 return null;
2421 }
2422
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002423 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2424 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2425 boolean deferResume) {
2426
2427 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002428 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002429 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002430 return;
2431 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002432
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002433 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002434 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002435 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002436 // If the docked stack exists, don't resize non-floating stacks independently of the
2437 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002438 return;
2439 }
2440
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002441 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002442 mWindowManager.deferSurfaceLayout();
2443 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002444 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002445 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2446 // null bounds = fullscreen windowing mode...at least for now.
2447 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2448 } else if (splitScreenActive) {
2449 // If we are in split-screen mode and this stack support split-screen, then
2450 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2451 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2452 }
2453 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002454 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002455 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002456 stack.ensureVisibleActivitiesConfigurationLocked(
2457 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002458 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002459 } finally {
2460 mWindowManager.continueSurfaceLayout();
2461 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002462 }
2463 }
2464
Wale Ogunwale68278562017-09-23 17:13:55 -07002465 private void deferUpdateBounds(int activityType) {
2466 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002467 if (stack != null) {
2468 stack.deferUpdateBounds();
2469 }
2470 }
2471
Wale Ogunwale68278562017-09-23 17:13:55 -07002472 private void continueUpdateBounds(int activityType) {
2473 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002474 if (stack != null) {
2475 stack.continueUpdateBounds();
2476 }
2477 }
2478
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002479 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002480 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002481 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2482 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002483 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002484 if (task != null) {
2485 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002486 }
2487 }
2488 mResizingTasksDuringAnimation.clear();
2489 }
2490
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002491 /**
2492 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002493 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002494 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002495 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002496 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002497
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002498 mWindowManager.deferSurfaceLayout();
2499 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002500 final int windowingMode = fromStack.getWindowingMode();
2501 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2502 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2503
2504 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002505 // Tell the display we are exiting split-screen mode.
2506 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002507 // We are moving all tasks from the docked stack to the fullscreen stack,
2508 // which is dismissing the docked stack, so resize all other stacks to
2509 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002510 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2511 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002512 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2513 continue;
2514 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002515 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002516 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002517 }
2518
2519 // Also disable docked stack resizing since we have manually adjusted the
2520 // size of other stacks above and we don't want to trigger a docked stack
2521 // resize when we remove task from it below and it is detached from the
2522 // display because it no longer contains any tasks.
2523 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002524 } else if (inPinnedWindowingMode && onTop) {
2525 // Log if we are expanding the PiP to fullscreen
2526 MetricsLogger.action(mService.mContext,
2527 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002528 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002529
Winson Chung5af42fc2017-03-24 17:11:33 -07002530 // If we are moving from the pinned stack, then the animation takes care of updating
2531 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002532 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002533 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002534
Wale Ogunwale388945c2017-10-04 12:13:46 -07002535 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002536 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002537 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002538 for (int i = 0; i < size; ++i) {
2539 final TaskRecord task = tasks.get(i);
2540 final ActivityStack toStack = toDisplay.getOrCreateStack(
2541 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002542
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002543 if (onTop) {
2544 final int returnToType =
2545 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002546 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002547 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002548 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002549 isTopTask /* animate */, DEFER_RESUME,
2550 schedulePictureInPictureModeChange,
2551 "moveTasksToFullscreenStack - onTop");
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002552 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002553 // Position the tasks in the fullscreen stack in order at the bottom of the
2554 // stack. Also defer resume until all the tasks have been moved to the
2555 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002556 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002557 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2558 schedulePictureInPictureModeChange,
2559 "moveTasksToFullscreenStack - NOT_onTop");
2560 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002561 }
2562 }
Winson Chung74666102017-02-22 17:49:24 -08002563
2564 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2565 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002566 } finally {
2567 mAllowDockedStackResize = true;
2568 mWindowManager.continueSurfaceLayout();
2569 }
2570 }
2571
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002572 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002573 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2574 }
2575
2576 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2577 mWindowManager.inSurfaceTransaction(() ->
2578 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002579 }
2580
Jorim Jaggidc249c42015-12-15 14:57:31 -08002581 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002582 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2583 boolean preserveWindows) {
2584 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2585 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2586 false /* deferResume */);
2587 }
2588
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002589 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002590 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2591 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002592
2593 if (!mAllowDockedStackResize) {
2594 // Docked stack resize currently disabled.
2595 return;
2596 }
2597
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002598 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002599 if (stack == null) {
2600 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2601 return;
2602 }
2603
2604 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2605 mWindowManager.deferSurfaceLayout();
2606 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002607 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2608 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002609 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002610 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002611
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002612 // TODO: Checking for isAttached might not be needed as if the user passes in null
2613 // dockedBounds then they want the docked stack to be dismissed.
2614 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2615 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002616 // In this case we make all other static stacks fullscreen and move all
2617 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002618 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002619
2620 // stack shouldn't contain anymore activities, so nothing to resume.
2621 r = null;
2622 } else {
2623 // Docked stacks occupy a dedicated region on screen so the size of all other
2624 // static stacks need to be adjusted so they don't overlap with the docked stack.
2625 // We get the bounds to use from window manager which has been adjusted for any
2626 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002627 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002628 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002629 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2630 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002631 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002632 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002633 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002634 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002635 continue;
2636 }
2637 // Need to set windowing mode here before we try to get the dock bounds.
2638 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2639 current.getStackDockedModeBounds(
2640 tempOtherTaskBounds /* currentTempTaskBounds */,
2641 tempRect /* outStackBounds */,
2642 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2643
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002644 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002645 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2646 tempOtherTaskInsetBounds, preserveWindows,
2647 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002648 }
2649 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002650 if (!deferResume) {
2651 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2652 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002653 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002654 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002655 mWindowManager.continueSurfaceLayout();
2656 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2657 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002658 }
2659
Robert Carr0d00c2e2016-02-29 17:45:02 -08002660 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002661 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002662 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002663 if (stack == null) {
2664 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2665 return;
2666 }
Winson Chung19953ca2017-04-11 11:19:23 -07002667
2668 // It is possible for the bounds animation from the WM to call this but be delayed by
2669 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2670 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2671 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2672 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002673 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002674 return;
2675 }
2676
Robert Carr0d00c2e2016-02-29 17:45:02 -08002677 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2678 mWindowManager.deferSurfaceLayout();
2679 try {
2680 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002681 Rect insetBounds = null;
2682 if (tempPinnedTaskBounds != null) {
2683 // We always use 0,0 as the position for the inset rect because
2684 // if we are getting insets at all in the pinned stack it must mean
2685 // we are headed for fullscreen.
2686 insetBounds = tempRect;
2687 insetBounds.top = 0;
2688 insetBounds.left = 0;
2689 insetBounds.right = tempPinnedTaskBounds.width();
2690 insetBounds.bottom = tempPinnedTaskBounds.height();
2691 }
2692 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002693 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002694 } finally {
2695 mWindowManager.continueSurfaceLayout();
2696 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2697 }
2698 }
2699
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002700 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002701 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002702 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002703 /**
2704 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2705 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2706 * that the client receives onStop() before it is reparented. We do this by detaching
2707 * the stack from the display so that it will be considered invisible when
2708 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2709 * invisible as well and added to the stopping list. After which we process the
2710 * stopping list by handling the idle.
2711 */
2712 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2713 pinnedStack.mForceHidden = true;
2714 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2715 pinnedStack.mForceHidden = false;
2716 activityIdleInternalLocked(null, false /* fromTimeout */,
2717 true /* processPausingActivites */, null /* configuration */);
2718
2719 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002720 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002721 } else {
2722 for (int i = tasks.size() - 1; i >= 0; i--) {
2723 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2724 REMOVE_FROM_RECENTS);
2725 }
2726 }
2727 }
2728
2729 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002730 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002731 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2732 * instead moved back onto the fullscreen stack.
2733 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002734 void removeStack(ActivityStack stack) {
2735 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002736 }
2737
Wale Ogunwale68278562017-09-23 17:13:55 -07002738 /**
2739 * Removes stacks in the input windowing modes from the system if they are of activity type
2740 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2741 */
2742 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002743 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002744 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2745 }
2746 }
2747
2748 void removeStacksWithActivityTypes(int... activityTypes) {
2749 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2750 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002751 }
Robert Carr6914f082017-03-20 19:04:30 -07002752 }
2753
2754 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002755 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2756 */
2757 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2758 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2759 }
2760
2761 /**
Winson Chung010927a2016-12-15 16:12:35 -08002762 * Removes the task with the specified task id.
2763 *
2764 * @param taskId Identifier of the task to be removed.
2765 * @param killProcess Kill any process associated with the task if possible.
2766 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002767 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2768 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002769 * @return Returns true if the given task was found and removed.
2770 */
Winson Chung6954fc92017-03-24 16:22:12 -07002771 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2772 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002773 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002774 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002775 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002776 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Charles Heff9b4dff2017-09-22 10:18:37 +01002777 mService.mLockTaskController.clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002778 if (tr.isPersistable) {
2779 mService.notifyTaskPersisterLocked(null, true);
2780 }
2781 return true;
2782 }
2783 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2784 return false;
2785 }
2786
2787 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2788 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002789 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002790 }
2791 ComponentName component = tr.getBaseIntent().getComponent();
2792 if (component == null) {
2793 Slog.w(TAG, "No component for base intent of task: " + tr);
2794 return;
2795 }
2796
2797 // Find any running services associated with this app and stop if needed.
2798 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2799
2800 if (!killProcess) {
2801 return;
2802 }
2803
2804 // Determine if the process(es) for this task should be killed.
2805 final String pkg = component.getPackageName();
2806 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2807 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2808 for (int i = 0; i < pmap.size(); i++) {
2809
2810 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2811 for (int j = 0; j < uids.size(); j++) {
2812 ProcessRecord proc = uids.valueAt(j);
2813 if (proc.userId != tr.userId) {
2814 // Don't kill process for a different user.
2815 continue;
2816 }
2817 if (proc == mService.mHomeProcess) {
2818 // Don't kill the home process along with tasks from the same package.
2819 continue;
2820 }
2821 if (!proc.pkgList.containsKey(pkg)) {
2822 // Don't kill process that is not associated with this task.
2823 continue;
2824 }
2825
2826 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002827 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002828 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2829 // Don't kill process(es) that has an activity in a different task that is
2830 // also in recents.
2831 return;
2832 }
2833 }
2834
2835 if (proc.foregroundServices) {
2836 // Don't kill process(es) with foreground service.
2837 return;
2838 }
2839
2840 // Add process to kill list.
2841 procsToKill.add(proc);
2842 }
2843 }
2844
2845 // Kill the running processes.
2846 for (int i = 0; i < procsToKill.size(); i++) {
2847 ProcessRecord pr = procsToKill.get(i);
2848 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2849 && pr.curReceivers.isEmpty()) {
2850 pr.kill("remove task", true);
2851 } else {
2852 // We delay killing processes that are not in the background or running a receiver.
2853 pr.waitingToKill = "remove task";
2854 }
2855 }
2856 }
2857
Craig Mautner4a1cb222013-12-04 16:14:06 -08002858 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002859 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002860 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002861 break;
2862 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002863 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002865 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002866 }
2867
Chong Zhang5dcb2752015-08-18 13:50:26 -07002868 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002869 * Called to restore the state of the task into the stack that it's supposed to go into.
2870 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002871 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002872 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002873 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002874 * @return true if the task has been restored successfully.
2875 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002876 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2877 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002878 final ActivityStack currentStack = task.getStack();
2879 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002880 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002881 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002882 // Nothing else to do since it is already restored in the right stack.
2883 return true;
2884 }
2885 // Remove current stack association, so we can re-associate the task with the
2886 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002887 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002888 }
2889
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002890 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002891 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002892 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002893 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2894 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002895 final ArrayList<ActivityRecord> activities = task.mActivities;
2896 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002897 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002898 }
2899 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002900 }
2901
Winson Chung1dbc8112017-09-28 18:05:31 -07002902 @Override
2903 public void onRecentTaskAdded(TaskRecord task) {
2904 task.touchActiveTime();
2905 }
2906
2907 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07002908 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung14cfbb42017-10-20 13:54:39 -07002909 // TODO: Trim active task once b/68045330 is fixed
Winson Chung1dbc8112017-09-28 18:05:31 -07002910 task.removedFromRecents();
2911 }
2912
Wale Ogunwale040b4702015-08-06 18:10:50 -07002913 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002914 * Move stack with all its existing content to specified display.
2915 * @param stackId Id of stack to move.
2916 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002917 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002918 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002919 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002920 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002921 if (activityDisplay == null) {
2922 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2923 + displayId);
2924 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002925 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002926 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002927 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2928 + stackId);
2929 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002930
2931 final ActivityDisplay currentDisplay = stack.getDisplay();
2932 if (currentDisplay == null) {
2933 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
2934 + " is not attached to any display.");
2935 }
2936
2937 if (currentDisplay.mDisplayId == displayId) {
2938 throw new IllegalArgumentException("Trying to move stack=" + stack
2939 + " to its current displayId=" + displayId);
2940 }
2941
2942 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002943 // TODO(multi-display): resize stacks properly if moved from split-screen.
2944 }
2945
2946 /**
Winson Chung74666102017-02-22 17:49:24 -08002947 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2948 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002949 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002950 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002951 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002952 final int stackId = stack.mStackId;
2953 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07002954
Winson Chung74666102017-02-22 17:49:24 -08002955 // Check that we aren't reparenting to the same stack that the task is already in
2956 if (prevStack != null && prevStack.mStackId == stackId) {
2957 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2958 + " already in stackId=" + stackId);
2959 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002960 }
2961
Winson Chung74666102017-02-22 17:49:24 -08002962 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2963 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002964 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08002965 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002966 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08002967 }
2968
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002969 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2970 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002971 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002972 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2973 + " reparent task=" + task + " to stackId=" + stackId);
2974 }
2975
Winson Chung74666102017-02-22 17:49:24 -08002976 // Ensure that we aren't trying to move into a freeform stack without freeform
2977 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002978 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
2979 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002980 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2981 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002982 }
2983
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002984 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
2985 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002986 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002987 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
2988 + " Moving to a fullscreen stack instead.");
2989 if (prevStack != null) {
2990 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002991 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002992 stack = stack.getDisplay().createStack(
2993 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002994 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002995 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002996 }
2997
Winson Chung08f81892017-03-02 15:40:51 -08002998 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002999 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003000 if (stack == null) {
3001 throw new IllegalArgumentException(
3002 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3003 }
3004
3005 final ActivityRecord r = stack.topRunningActivityLocked();
3006 if (r == null) {
3007 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3008 + " in stack=" + stack);
3009 return false;
3010 }
3011
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003012 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003013 Slog.w(TAG,
3014 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003015 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003016 return false;
3017 }
3018
Winson Chung3a682872017-04-10 14:38:05 -07003019 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003020 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003021 return true;
3022 }
3023
Winson Chung8bca9e42017-04-16 15:59:43 -07003024 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003025 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003026
Wale Ogunwale480dca02016-02-06 13:58:29 -08003027 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003028
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003029 final ActivityDisplay display = r.getStack().getDisplay();
3030 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003031
Bryce Lee04ab3462017-04-10 15:06:33 -07003032 // This will clear the pinned stack by moving an existing task to the full screen stack,
3033 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003034 if (stack != null) {
3035 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3036 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003037
Wale Ogunwale1666e312016-12-16 11:27:18 -08003038 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003039 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003040
Wale Ogunwale480dca02016-02-06 13:58:29 -08003041 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003042 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003043 // Resize the pinned stack to match the current size of the task the activity we are
3044 // going to be moving is currently contained in. We do this to have the right starting
3045 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003046 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003047 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003048 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003049
3050 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003051 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003052 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3053 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003054 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003055 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003056 // reveal/leave the other activities in their original task.
3057
3058 // Currently, we don't support reparenting activities across tasks in two different
3059 // stacks, so instead, just create a new task in the same stack, reparent the
3060 // activity into that task, and then reparent the whole task to the new stack. This
3061 // ensures that all the necessary work to migrate states in the old and new stacks
3062 // is also done.
3063 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003064 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003065 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3066
Winson Chung5af42fc2017-03-24 17:11:33 -07003067 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003068 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003069 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003070 }
Winson Chungc2baac02017-01-11 13:34:47 -08003071
3072 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3073 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003074 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003075 } finally {
3076 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003077 }
3078
Winson Chunge7ba6862017-05-24 12:13:33 -07003079 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3080 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3081 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003082 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3083
Winson Chunge7ba6862017-05-24 12:13:33 -07003084 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3085 true /* fromFullscreen */);
3086
3087 // Update the visibility of all activities after the they have been reparented to the new
3088 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3089 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3090 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003091 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003092 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003093
Wale Ogunwale89be5762017-10-04 13:27:49 -07003094 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003095 }
3096
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003097 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003098 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3099 if (r == null || !r.isFocusable()) {
3100 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3101 "moveActivityStackToFront: unfocusable r=" + r);
3102 return false;
3103 }
3104
Bryce Leeaf691c02017-03-20 14:20:22 -07003105 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003106 final ActivityStack stack = r.getStack();
3107 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003108 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3109 + r + " task=" + task);
3110 return false;
3111 }
3112
Chong Zhang6cda19c2016-06-14 19:07:56 -07003113 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3114 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3115 "moveActivityStackToFront: already on top, r=" + r);
3116 return false;
3117 }
3118
3119 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3120 "moveActivityStackToFront: r=" + r);
3121
3122 stack.moveToFront(reason, task);
3123 return true;
3124 }
3125
David Stevensc6b91c62017-02-08 14:23:58 -08003126 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003127 mTmpFindTaskResult.r = null;
3128 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003129 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003130 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003132 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3133 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3134 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003135 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003136 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3137 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003138 continue;
3139 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003140 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003141 // It is possible to have tasks in multiple stacks with the same root affinity, so
3142 // we should keep looking after finding an affinity match to see if there is a
3143 // better match in another stack. Also, task affinity isn't a good enough reason
3144 // to target a display which isn't the source of the intent, so skip any affinity
3145 // matches not on the specified display.
3146 if (mTmpFindTaskResult.r != null) {
3147 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3148 return mTmpFindTaskResult.r;
3149 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003150 // Note: since the traversing through the stacks is top down, the floating
3151 // tasks should always have lower priority than any affinity-matching tasks
3152 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003153 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003154 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3155 Slog.d(TAG_TASKS, "Skipping match on different display "
3156 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003157 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003158 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003159 }
3160 }
Winson Chung5b895b72017-05-01 13:46:25 -07003161
David Stevensc6b91c62017-02-08 14:23:58 -08003162 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3163 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003164 }
3165
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003166 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003167 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003168 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003169 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3170 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3171 final ActivityStack stack = display.getChildAt(stackNdx);
3172 final ActivityRecord ar = stack.findActivityLocked(
3173 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174 if (ar != null) {
3175 return ar;
3176 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003177 }
3178 }
3179 return null;
3180 }
3181
David Stevens9440dc82017-03-16 19:00:20 -07003182 boolean hasAwakeDisplay() {
3183 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3184 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3185 if (!display.shouldSleep()) {
3186 return true;
3187 }
3188 }
3189 return false;
3190 }
3191
Craig Mautner8d341ef2013-03-26 09:03:27 -07003192 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003193 scheduleSleepTimeout();
3194 if (!mGoingToSleep.isHeld()) {
3195 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003196 if (mLaunchingActivity.isHeld()) {
3197 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3198 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003199 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003200 mLaunchingActivity.release();
3201 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003202 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003203 }
David Stevens9440dc82017-03-16 19:00:20 -07003204
3205 applySleepTokensLocked(false /* applyToStacks */);
3206
3207 checkReadyForSleepLocked(true /* allowDelay */);
3208 }
3209
3210 void prepareForShutdownLocked() {
3211 for (int i = 0; i < mActivityDisplays.size(); i++) {
3212 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3213 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003214 }
3215
3216 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003217 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003218
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003219 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003220 final long endTime = System.currentTimeMillis() + timeout;
3221 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003222 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003223 long timeRemaining = endTime - System.currentTimeMillis();
3224 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003225 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003226 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003227 } catch (InterruptedException e) {
3228 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003229 } else {
3230 Slog.w(TAG, "Activity manager shutdown timed out");
3231 timedout = true;
3232 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003233 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003234 } else {
3235 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 }
3237 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003238
3239 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003240 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003241
Craig Mautner8d341ef2013-03-26 09:03:27 -07003242 return timedout;
3243 }
3244
3245 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003246 removeSleepTimeouts();
3247 if (mGoingToSleep.isHeld()) {
3248 mGoingToSleep.release();
3249 }
David Stevens9440dc82017-03-16 19:00:20 -07003250 }
3251
3252 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003253 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003254 // Set the sleeping state of the display.
3255 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3256 final boolean displayShouldSleep = display.shouldSleep();
3257 if (displayShouldSleep == display.isSleeping()) {
3258 continue;
3259 }
3260 display.setIsSleeping(displayShouldSleep);
3261
3262 if (!applyToStacks) {
3263 continue;
3264 }
3265
3266 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003267 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3268 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003269 if (displayShouldSleep) {
3270 stack.goToSleepIfPossible(false /* shuttingDown */);
3271 } else {
3272 stack.awakeFromSleepingLocked();
3273 if (isFocusedStack(stack)) {
3274 resumeFocusedStackTopActivityLocked();
3275 }
3276 }
3277 }
3278
3279 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3280 continue;
3281 }
3282 // The display is awake now, so clean up the going to sleep list.
3283 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3284 final ActivityRecord r = it.next();
3285 if (r.getDisplayId() == display.mDisplayId) {
3286 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003287 }
Craig Mautner5314a402013-09-26 12:40:16 -07003288 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003289 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003290 }
3291
3292 void activitySleptLocked(ActivityRecord r) {
3293 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003294 final ActivityStack s = r.getStack();
3295 if (s != null) {
3296 s.checkReadyForSleep();
3297 } else {
3298 checkReadyForSleepLocked(true);
3299 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003300 }
3301
David Stevens9440dc82017-03-16 19:00:20 -07003302 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003303 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003304 // Do not care.
3305 return;
3306 }
3307
David Stevens18abd0e2017-08-17 14:55:47 -07003308 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3309 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003310 }
3311
Wei Wang65c7a152016-06-02 18:51:22 -07003312 // Send launch end powerhint before going sleep
3313 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3314
Craig Mautner0eea92c2013-05-16 13:35:39 -07003315 removeSleepTimeouts();
3316
3317 if (mGoingToSleep.isHeld()) {
3318 mGoingToSleep.release();
3319 }
3320 if (mService.mShuttingDown) {
3321 mService.notifyAll();
3322 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003323 }
3324
David Stevens18abd0e2017-08-17 14:55:47 -07003325 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3326 // successfully put to sleep.
3327 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3328 boolean allSleep = true;
3329 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003330 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3331 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3332 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003333 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003334 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003335 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003336 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003337 }
3338 }
3339 }
3340 return allSleep;
3341 }
3342
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003343 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003344 // A resumed activity cannot be stopping. remove from list
3345 mStoppingActivities.remove(r);
3346
Andrii Kulian02b7a832016-10-06 23:11:56 -07003347 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003348 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003349 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003350 }
3351 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003352 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003353 mWindowManager.executeAppTransition();
3354 return true;
3355 }
3356 return false;
3357 }
3358
Craig Mautner8d341ef2013-03-26 09:03:27 -07003359 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003360 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003361 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3362 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3363 final ActivityStack stack = display.getChildAt(stackNdx);
3364 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003365 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003366 }
3367 }
3368
Craig Mautnerbb742462014-07-07 15:28:55 -07003369 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003370 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003371 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003372 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003373
3374 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003375 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003376 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003377 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003378
3379 // When launching tasks behind, update the last active time of the top task after the new
3380 // task has been shown briefly
3381 final ActivityRecord top = stack.topActivity();
3382 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003383 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003384 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003385 }
3386
3387 void scheduleLaunchTaskBehindComplete(IBinder token) {
3388 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3389 }
3390
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003391 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3392 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003393 mKeyguardController.beginActivityVisibilityUpdate();
3394 try {
3395 // First the front stacks. In case any are not fullscreen and are in front of home.
3396 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003397 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3398 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3399 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003400 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3401 }
Craig Mautner580ea812013-04-25 12:58:38 -07003402 }
Jorim Jaggife762342016-10-13 14:33:27 +02003403 } finally {
3404 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003405 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003406 }
3407
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003408 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3409 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003410 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3411 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3412 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003413 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3414 }
3415 }
3416 }
3417
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003418 void invalidateTaskLayers() {
3419 mTaskLayersChanged = true;
3420 }
3421
3422 void rankTaskLayersIfNeeded() {
3423 if (!mTaskLayersChanged) {
3424 return;
3425 }
3426 mTaskLayersChanged = false;
3427 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003428 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003429 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003430 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3431 final ActivityStack stack = display.getChildAt(stackNdx);
3432 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003433 }
3434 }
3435 }
3436
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003437 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3438 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003439 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3440 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3441 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003442 stack.clearOtherAppTimeTrackers(except);
3443 }
3444 }
3445 }
3446
Craig Mautner8d341ef2013-03-26 09:03:27 -07003447 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003448 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003449 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3450 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3451 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003452 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003453 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003454 }
3455 }
3456
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003457 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3458 // Examine all activities currently running in the process.
3459 TaskRecord firstTask = null;
3460 // Tasks is non-null only if two or more tasks are found.
3461 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003462 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3463 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003464 ActivityRecord r = app.activities.get(i);
3465 // First, if we find an activity that is in the process of being destroyed,
3466 // then we just aren't going to do anything for now; we want things to settle
3467 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003468 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003469 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003470 return;
3471 }
3472 // Don't consider any activies that are currently not in a state where they
3473 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003474 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3475 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003476 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003477 continue;
3478 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003479
3480 final TaskRecord task = r.getTask();
3481 if (task != null) {
3482 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003483 + " from " + r);
3484 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003485 firstTask = task;
3486 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003487 if (tasks == null) {
3488 tasks = new ArraySet<>();
3489 tasks.add(firstTask);
3490 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003491 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003492 }
3493 }
3494 }
3495 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003496 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003497 return;
3498 }
3499 // If we have activities in multiple tasks that are in a position to be destroyed,
3500 // let's iterate through the tasks and release the oldest one.
3501 final int numDisplays = mActivityDisplays.size();
3502 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003503 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3504 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003505 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003506 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3507 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003508 // Try to release activities in this stack; if we manage to, we are done.
3509 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3510 return;
3511 }
3512 }
3513 }
3514 }
3515
Amith Yamasani37a40c22015-06-17 13:25:42 -07003516 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003517 final int focusStackId = mFocusedStack.getStackId();
3518 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003519 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003520 if (dockedStack != null) {
3521 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3522 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003523 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3524 // also cause all tasks to be moved to the fullscreen stack at a position that is
3525 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003526 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003527
3528 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003529 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003530 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003531
Craig Mautner858d8a62013-04-23 17:08:34 -07003532 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003533 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003534 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3535 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3536 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003537 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003538 TaskRecord task = stack.topTask();
3539 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003540 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003541 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003542 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003543 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003544
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003545 ActivityStack stack = getStack(restoreStackId);
3546 if (stack == null) {
3547 stack = mHomeStack;
3548 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003549 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003550 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003551 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003552 } else {
3553 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003554 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003555 }
Craig Mautner93529a42013-10-04 15:03:13 -07003556 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003557 }
3558
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003559 /** Checks whether the userid is a profile of the current user. */
3560 boolean isCurrentProfileLocked(int userId) {
3561 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003562 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003563 }
3564
Bryce Leeb7c9b802017-05-02 14:20:24 -07003565 /**
3566 * Returns whether a stopping activity is present that should be stopped after visible, rather
3567 * than idle.
3568 * @return {@code true} if such activity is present. {@code false} otherwise.
3569 */
3570 boolean isStoppingNoHistoryActivity() {
3571 // Activities that are marked as nohistory should be stopped immediately after the resumed
3572 // activity has become visible.
3573 for (ActivityRecord record : mStoppingActivities) {
3574 if (record.isNoHistory()) {
3575 return true;
3576 }
3577 }
3578
3579 return false;
3580 }
3581
Winson Chung4dabf232017-01-25 13:25:22 -08003582 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3583 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003584 ArrayList<ActivityRecord> stops = null;
3585
3586 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003587 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3588 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003589 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003590 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003591 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3592 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003593 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003594 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003595 if (s.finishing) {
3596 // If this activity is finishing, it is sitting on top of
3597 // everyone else but we now know it is no longer needed...
3598 // so get rid of it. Otherwise, we need to go through the
3599 // normal flow and hide it once we determine that it is
3600 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003601 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003602 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003603 }
3604 }
David Stevens9440dc82017-03-16 19:00:20 -07003605 if (remove) {
3606 final ActivityStack stack = s.getStack();
3607 final boolean shouldSleepOrShutDown = stack != null
3608 ? stack.shouldSleepOrShutDownActivities()
3609 : mService.isSleepingOrShuttingDownLocked();
3610 if (!waitingVisible || shouldSleepOrShutDown) {
3611 if (!processPausingActivities && s.state == PAUSING) {
3612 // Defer processing pausing activities in this iteration and reschedule
3613 // a delayed idle to reprocess it again
3614 removeTimeoutsForActivityLocked(idleActivity);
3615 scheduleIdleTimeoutLocked(idleActivity);
3616 continue;
3617 }
Winson Chung4dabf232017-01-25 13:25:22 -08003618
David Stevens9440dc82017-03-16 19:00:20 -07003619 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3620 if (stops == null) {
3621 stops = new ArrayList<>();
3622 }
3623 stops.add(s);
3624 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003625 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003626 }
3627 }
3628
3629 return stops;
3630 }
3631
Craig Mautnercf910b02013-04-23 11:23:27 -07003632 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003633 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003634 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3635 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3636 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003637 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003638 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003639 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003640 if (r == null) Slog.e(TAG,
3641 "validateTop...: null top activity, stack=" + stack);
3642 else {
3643 final ActivityRecord pausing = stack.mPausingActivity;
3644 if (pausing != null && pausing == r) Slog.e(TAG,
3645 "validateTop...: top stack has pausing activity r=" + r
3646 + " state=" + state);
3647 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3648 "validateTop...: activity in front not resumed r=" + r
3649 + " state=" + state);
3650 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003651 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003652 final ActivityRecord resumed = stack.mResumedActivity;
3653 if (resumed != null && resumed == r) Slog.e(TAG,
3654 "validateTop...: back stack has resumed activity r=" + r
3655 + " state=" + state);
3656 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3657 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003658 }
3659 }
3660 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003661 }
3662
Craig Mautner27084302013-03-25 08:05:25 -07003663 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003664 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003665 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003666 pw.print(prefix);
3667 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003668 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003669 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3670 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003671 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003672 }
Bryce Lee4a194382017-04-04 14:32:48 -07003673 if (!mWaitingForActivityVisible.isEmpty()) {
3674 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3675 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3676 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3677 }
3678 }
3679
Jorim Jaggi8d786932016-10-26 19:08:36 -07003680 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003681 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003682 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003683
Yi Jin129fc6c2017-09-28 15:48:38 -07003684 public void writeToProto(ProtoOutputStream proto) {
Adrian Roos4921ccf2017-09-28 16:54:06 +02003685 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003686 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3687 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3688 activityDisplay.writeToProto(proto, DISPLAYS);
3689 }
3690 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3691 if (mFocusedStack != null) {
3692 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3693 ActivityRecord focusedActivity = getResumedActivityLocked();
3694 if (focusedActivity != null) {
3695 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3696 }
3697 } else {
3698 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3699 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003700 }
3701
Winson43d1f262016-06-14 16:05:55 -07003702 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003703 * Dump all connected displays' configurations.
3704 * @param prefix Prefix to apply to each line of the dump.
3705 */
3706 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3707 pw.print(prefix); pw.println("Display override configurations:");
3708 final int displayCount = mActivityDisplays.size();
3709 for (int i = 0; i < displayCount; i++) {
3710 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3711 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3712 pw.println(activityDisplay.getOverrideConfiguration());
3713 }
3714 }
3715
3716 /**
Winson43d1f262016-06-14 16:05:55 -07003717 * Dumps the activities matching the given {@param name} in the either the focused stack
3718 * or all visible stacks if {@param dumpVisibleStacks} is true.
3719 */
Winson Chung6998bc42017-02-28 17:07:05 -08003720 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3721 boolean dumpFocusedStackOnly) {
3722 if (dumpFocusedStackOnly) {
3723 return mFocusedStack.getDumpActivitiesLocked(name);
3724 } else {
Winson43d1f262016-06-14 16:05:55 -07003725 ArrayList<ActivityRecord> activities = new ArrayList<>();
3726 int numDisplays = mActivityDisplays.size();
3727 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003728 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3729 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3730 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003731 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003732 activities.addAll(stack.getDumpActivitiesLocked(name));
3733 }
3734 }
3735 }
3736 return activities;
Winson43d1f262016-06-14 16:05:55 -07003737 }
Craig Mautner20e72272013-04-01 13:45:53 -07003738 }
3739
Dianne Hackborn390517b2013-05-30 15:03:32 -07003740 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3741 boolean needSep, String prefix) {
3742 if (activity != null) {
3743 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3744 if (needSep) {
3745 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003746 }
3747 pw.print(prefix);
3748 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003749 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003750 }
3751 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003752 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003753 }
3754
Craig Mautner8d341ef2013-03-26 09:03:27 -07003755 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3756 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003757 boolean printed = false;
3758 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003759 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3760 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003761 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003762 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003763 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3764 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3765 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003766 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003767 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003768 + ": type=" + activityTypeToString(stack.getActivityType())
3769 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003770 pw.println(" mFullscreen=" + stack.mFullscreen);
3771 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3772 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003773
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003774 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3775 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003776
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3778 !dumpAll, false, dumpPackage, true,
3779 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003780
Craig Mautner4a1cb222013-12-04 16:14:06 -08003781 needSep = printed;
3782 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3783 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003784 if (pr) {
3785 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003787 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003788 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3789 " mResumedActivity: ");
3790 if (pr) {
3791 printed = true;
3792 needSep = false;
3793 }
3794 if (dumpAll) {
3795 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3796 " mLastPausedActivity: ");
3797 if (pr) {
3798 printed = true;
3799 needSep = true;
3800 }
3801 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3802 needSep, " mLastNoHistoryActivity: ");
3803 }
3804 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003805 }
3806 }
3807
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003808 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3809 false, dumpPackage, true, " Activities waiting to finish:", null);
3810 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3811 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003812 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3813 false, !dumpAll, false, dumpPackage, true,
3814 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003815 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3816 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003817
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003818 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003819 }
3820
Dianne Hackborn390517b2013-05-30 15:03:32 -07003821 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003822 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003823 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003824 String innerPrefix = null;
3825 String[] args = null;
3826 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003827 for (int i=list.size()-1; i>=0; i--) {
3828 final ActivityRecord r = list.get(i);
3829 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3830 continue;
3831 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003832 if (innerPrefix == null) {
3833 innerPrefix = prefix + " ";
3834 args = new String[0];
3835 }
3836 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003837 final boolean full = !brief && (complete || !r.isInHistory());
3838 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003839 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003840 needNL = false;
3841 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003842 if (header != null) {
3843 pw.println(header);
3844 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003845 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003846 if (lastTask != r.getTask()) {
3847 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003848 pw.print(prefix);
3849 pw.print(full ? "* " : " ");
3850 pw.println(lastTask);
3851 if (full) {
3852 lastTask.dump(pw, prefix + " ");
3853 } else if (complete) {
3854 // Complete + brief == give a summary. Isn't that obvious?!?
3855 if (lastTask.intent != null) {
3856 pw.print(prefix); pw.print(" ");
3857 pw.println(lastTask.intent.toInsecureStringWithClip());
3858 }
3859 }
3860 }
3861 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3862 pw.print(" #"); pw.print(i); pw.print(": ");
3863 pw.println(r);
3864 if (full) {
3865 r.dump(pw, innerPrefix);
3866 } else if (complete) {
3867 // Complete + brief == give a summary. Isn't that obvious?!?
3868 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3869 if (r.app != null) {
3870 pw.print(innerPrefix); pw.println(r.app);
3871 }
3872 }
3873 if (client && r.app != null && r.app.thread != null) {
3874 // flush anything that is already in the PrintWriter since the thread is going
3875 // to write to the file descriptor directly
3876 pw.flush();
3877 try {
3878 TransferPipe tp = new TransferPipe();
3879 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003880 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003881 // Short timeout, since blocking here can
3882 // deadlock with the application.
3883 tp.go(fd, 2000);
3884 } finally {
3885 tp.kill();
3886 }
3887 } catch (IOException e) {
3888 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3889 } catch (RemoteException e) {
3890 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3891 }
3892 needNL = true;
3893 }
3894 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003895 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003896 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003897
Craig Mautnerf3333272013-04-22 10:55:53 -07003898 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003899 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3900 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003901 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3902 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003903 }
3904
3905 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003906 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003907 }
3908
3909 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003910 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3911 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003912 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3913 }
3914
Craig Mautner05d29032013-05-03 13:40:13 -07003915 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003916 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3917 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3918 }
Craig Mautner05d29032013-05-03 13:40:13 -07003919 }
3920
Craig Mautner0eea92c2013-05-16 13:35:39 -07003921 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003922 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3923 }
3924
3925 final void scheduleSleepTimeout() {
3926 removeSleepTimeouts();
3927 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3928 }
3929
Craig Mautner4a1cb222013-12-04 16:14:06 -08003930 @Override
3931 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003932 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3934 }
3935
3936 @Override
3937 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003938 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3940 }
3941
3942 @Override
3943 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003944 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3946 }
3947
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003948 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003949 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003950 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952 }
3953
Andrii Kulianca007a62016-11-22 16:33:28 -08003954 /** Check if display with specified id is added to the list. */
3955 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003956 return getActivityDisplayOrCreateLocked(displayId) != null;
3957 }
3958
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003959 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003960 ActivityDisplay getActivityDisplay(int displayId) {
3961 return mActivityDisplays.get(displayId);
3962 }
3963
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003964 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
3965 ActivityDisplay getDefaultDisplay() {
3966 return mActivityDisplays.get(DEFAULT_DISPLAY);
3967 }
3968
Andrii Kulian62e6f252017-05-30 22:46:53 -07003969 /**
3970 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3971 * corresponding record in display manager.
3972 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003973 // TODO: Look into consolidating with getActivityDisplay()
3974 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003975 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3976 if (activityDisplay != null) {
3977 return activityDisplay;
3978 }
3979 if (mDisplayManager == null) {
3980 // The system isn't fully initialized yet.
3981 return null;
3982 }
3983 final Display display = mDisplayManager.getDisplay(displayId);
3984 if (display == null) {
3985 // The display is not registered in DisplayManager.
3986 return null;
3987 }
3988 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003989 activityDisplay = new ActivityDisplay(this, displayId);
3990 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07003991 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3992 mWindowManager.onDisplayAdded(displayId);
3993 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003994 }
3995
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003996 @VisibleForTesting
3997 void attachDisplay(ActivityDisplay display) {
3998 mActivityDisplays.put(display.mDisplayId, display);
3999 }
4000
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004001 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004002 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004003 mService.mContext.getResources().getDimensionPixelSize(
4004 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004005 }
4006
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004007 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004008 if (displayId == DEFAULT_DISPLAY) {
4009 throw new IllegalArgumentException("Can't remove the primary display.");
4010 }
4011
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004013 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4014 if (activityDisplay == null) {
4015 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004016 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004017 final boolean destroyContentOnRemoval
4018 = activityDisplay.shouldDestroyContentOnRemove();
4019 while (activityDisplay.getChildCount() > 0) {
4020 final ActivityStack stack = activityDisplay.getChildAt(0);
4021 if (destroyContentOnRemoval) {
4022 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, false /* onTop */);
4023 stack.finishAllActivitiesLocked(true /* immediately */);
4024 } else {
4025 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4026 // a valid launch stack for all activities. This way the task history from
4027 // external display will be preserved on primary after move.
4028 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
4029 }
4030 }
4031
4032 releaseSleepTokens(activityDisplay);
4033
4034 mActivityDisplays.remove(displayId);
4035 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004037 }
4038
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004039 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004041 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4042 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004043 // The window policy is responsible for stopping activities on the default display
4044 if (displayId != Display.DEFAULT_DISPLAY) {
4045 int displayState = activityDisplay.mDisplay.getState();
4046 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4047 activityDisplay.mOffToken =
4048 mService.acquireSleepToken("Display-off", displayId);
4049 } else if (displayState == Display.STATE_ON
4050 && activityDisplay.mOffToken != null) {
4051 activityDisplay.mOffToken.release();
4052 activityDisplay.mOffToken = null;
4053 }
4054 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055 // TODO: Update the bounds.
4056 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004057 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004058 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 }
4060
David Stevens9440dc82017-03-16 19:00:20 -07004061 SleepToken createSleepTokenLocked(String tag, int displayId) {
4062 ActivityDisplay display = mActivityDisplays.get(displayId);
4063 if (display == null) {
4064 throw new IllegalArgumentException("Invalid display: " + displayId);
4065 }
4066
4067 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4068 mSleepTokens.add(token);
4069 display.mAllSleepTokens.add(token);
4070 return token;
4071 }
4072
4073 private void removeSleepTokenLocked(SleepTokenImpl token) {
4074 mSleepTokens.remove(token);
4075
4076 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4077 if (display != null) {
4078 display.mAllSleepTokens.remove(token);
4079 if (display.mAllSleepTokens.isEmpty()) {
4080 mService.updateSleepIfNeededLocked();
4081 }
4082 }
4083 }
4084
4085 private void releaseSleepTokens(ActivityDisplay display) {
4086 if (display.mAllSleepTokens.isEmpty()) {
4087 return;
4088 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004089 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004090 mSleepTokens.remove(token);
4091 }
4092 display.mAllSleepTokens.clear();
4093
4094 mService.updateSleepIfNeededLocked();
4095 }
4096
Wale Ogunwale68278562017-09-23 17:13:55 -07004097 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004098 final int displayId = stack.mDisplayId;
4099 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004101 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004102 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004104 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004105 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004106 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004107 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004108 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004109
4110 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4111 final int numTasks = tasks.size();
4112 int[] taskIds = new int[numTasks];
4113 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004114 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004115 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004116 for (int i = 0; i < numTasks; ++i) {
4117 final TaskRecord task = tasks.get(i);
4118 taskIds[i] = task.taskId;
4119 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4120 : task.realActivity != null ? task.realActivity.flattenToString()
4121 : task.getTopActivity() != null ? task.getTopActivity().packageName
4122 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004123 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004124 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004125 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004126 }
4127 info.taskIds = taskIds;
4128 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004129 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004130 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004131
4132 final ActivityRecord top = stack.topRunningActivityLocked();
4133 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004134 return info;
4135 }
4136
Wale Ogunwale68278562017-09-23 17:13:55 -07004137 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 ActivityStack stack = getStack(stackId);
4139 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004140 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004141 }
4142 return null;
4143 }
4144
Wale Ogunwale68278562017-09-23 17:13:55 -07004145 StackInfo getStackInfo(int windowingMode, int activityType) {
4146 final ActivityStack stack = getStack(windowingMode, activityType);
4147 return (stack != null) ? getStackInfo(stack) : null;
4148 }
4149
Craig Mautner4a1cb222013-12-04 16:14:06 -08004150 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004151 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004152 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004153 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4154 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4155 final ActivityStack stack = display.getChildAt(stackNdx);
4156 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 }
4158 }
4159 return list;
4160 }
4161
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004162 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004163 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004164 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004165 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004166 }
4167
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004168 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004169 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004170 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004171 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004172 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004173 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004174 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004175 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004176 return;
4177 }
4178
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004179 // Handle incorrect launch/move to secondary display if needed.
4180 final boolean launchOnSecondaryDisplayFailed;
4181 if (isSecondaryDisplayPreferred) {
4182 final int actualDisplayId = task.getStack().mDisplayId;
4183 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4184 // The task landed on an inappropriate display somehow, move it to the default
4185 // display.
4186 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004187 mService.setTaskWindowingMode(task.taskId,
4188 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004189 launchOnSecondaryDisplayFailed = true;
4190 } else {
4191 // The task might have landed on a display different from requested.
4192 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4193 || (preferredDisplayId != INVALID_DISPLAY
4194 && preferredDisplayId != actualDisplayId);
4195 }
4196 } else {
4197 // The task wasn't requested to be on a secondary display.
4198 launchOnSecondaryDisplayFailed = false;
4199 }
4200
Jorim Jaggicd13d332016-04-27 15:40:20 -07004201 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004202 if (launchOnSecondaryDisplayFailed
4203 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004204 if (launchOnSecondaryDisplayFailed) {
4205 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4206 // display with config different from global config.
4207 mService.mTaskChangeNotificationController
4208 .notifyActivityLaunchOnSecondaryDisplayFailed();
4209 } else {
4210 // Display a warning toast that we tried to put a non-dockable task in the docked
4211 // stack.
4212 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4213 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004214
Andrii Kulianc27916642016-04-12 17:59:27 -07004215 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4216 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004217
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004218 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004219 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004220 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004221 }
Winson Chungd3395382016-12-13 11:49:09 -08004222 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004223 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004224 final String packageName = topActivity.appInfo.packageName;
4225 final int reason = isSecondaryDisplayPreferred
4226 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4227 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004228 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004229 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004230 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004231 }
4232
Jorim Jaggife89d122015-12-22 16:28:44 +01004233 void activityRelaunchedLocked(IBinder token) {
4234 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004235 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4236 if (r != null) {
4237 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004238 r.setSleeping(true, true);
4239 }
4240 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004241 }
4242
4243 void activityRelaunchingLocked(ActivityRecord r) {
4244 mWindowManager.notifyAppRelaunching(r.appToken);
4245 }
4246
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004247 void logStackState() {
4248 mActivityMetricsLogger.logWindowState();
4249 }
4250
Winson Chung5af42fc2017-03-24 17:11:33 -07004251 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004252 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4253 // end, then ensure that we defer all in between multi-window mode changes
4254 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4255 return;
4256 }
4257
Wale Ogunwale22e25262016-02-01 10:32:02 -08004258 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4259 final ActivityRecord r = task.mActivities.get(i);
4260 if (r.app != null && r.app.thread != null) {
4261 mMultiWindowModeChangedActivities.add(r);
4262 }
4263 }
4264
4265 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4266 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4267 }
4268 }
4269
Winson Chung5af42fc2017-03-24 17:11:33 -07004270 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004271 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004272 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004273 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004274 return;
4275 }
4276
Winson Chungab76bbc2017-08-14 13:33:51 -07004277 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004278 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004279
Winson Chungab76bbc2017-08-14 13:33:51 -07004280 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4281 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4282 final ActivityRecord r = task.mActivities.get(i);
4283 if (r.app != null && r.app.thread != null) {
4284 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004285 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004286 }
4287 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004288
Winson Chungab76bbc2017-08-14 13:33:51 -07004289 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4290 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4291 }
4292 }
4293
4294 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4295 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4296 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4297 final ActivityRecord r = task.mActivities.get(i);
4298 if (r.app != null && r.app.thread != null) {
4299 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004300 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004301 }
4302 }
4303
Tony Mak853304c2016-04-18 15:17:41 +01004304 void setDockedStackMinimized(boolean minimized) {
4305 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004306 }
4307
chaviw59b98852017-06-13 12:05:44 -07004308 void wakeUp(String reason) {
4309 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4310 }
4311
4312 /**
4313 * Begin deferring resume to avoid duplicate resumes in one pass.
4314 */
4315 private void beginDeferResume() {
4316 mDeferResumeCount++;
4317 }
4318
4319 /**
4320 * End deferring resume and determine if resume can be called.
4321 */
4322 private void endDeferResume() {
4323 mDeferResumeCount--;
4324 }
4325
4326 /**
4327 * @return True if resume can be called.
4328 */
4329 private boolean readyToResume() {
4330 return mDeferResumeCount == 0;
4331 }
4332
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004333 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004334
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004335 public ActivityStackSupervisorHandler(Looper looper) {
4336 super(looper);
4337 }
4338
Winson Chung4dabf232017-01-25 13:25:22 -08004339 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004340 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004341 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4342 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004343 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004344 }
4345
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004346 @Override
4347 public void handleMessage(Message msg) {
4348 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004349 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4350 synchronized (mService) {
4351 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4352 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004353 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004354 }
4355 }
4356 } break;
4357 case REPORT_PIP_MODE_CHANGED_MSG: {
4358 synchronized (mService) {
4359 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4360 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004361 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4362 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004363 }
4364 }
4365 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004366 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004367 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4368 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004369 // We don't at this point know if the activity is fullscreen,
4370 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004371 activityIdleInternal((ActivityRecord) msg.obj,
4372 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004373 } break;
4374 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004375 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004376 activityIdleInternal((ActivityRecord) msg.obj,
4377 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004378 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004379 case RESUME_TOP_ACTIVITY_MSG: {
4380 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004381 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004382 }
4383 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004384 case SLEEP_TIMEOUT_MSG: {
4385 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004386 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004387 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004388 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004389 }
4390 }
4391 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004392 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004393 synchronized (mService) {
4394 if (mLaunchingActivity.isHeld()) {
4395 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4396 if (VALIDATE_WAKE_LOCK_CALLER
4397 && Binder.getCallingUid() != Process.myUid()) {
4398 throw new IllegalStateException("Calling must be system uid");
4399 }
4400 mLaunchingActivity.release();
4401 }
4402 }
4403 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004404 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004405 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004406 } break;
4407 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004408 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409 } break;
4410 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004411 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004412 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004413 case LAUNCH_TASK_BEHIND_COMPLETE: {
4414 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004415 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004416 if (r != null) {
4417 handleLaunchTaskBehindCompleteLocked(r);
4418 }
4419 }
4420 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004422 }
4423 }
4424 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004425
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004426 ActivityStack findStackBehind(ActivityStack stack) {
4427 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004428 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004429 if (display == null) {
4430 return null;
4431 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004432 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4433 if (display.getChildAt(i) == stack && i > 0) {
4434 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004435 }
4436 }
4437 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004438 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004439 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004440
Jorim Jaggic69bd222016-03-15 14:38:37 +01004441 /**
4442 * Puts a task into resizing mode during the next app transition.
4443 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004444 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004445 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004446 private void setResizingDuringAnimation(TaskRecord task) {
4447 mResizingTasksDuringAnimation.add(task.taskId);
4448 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004449 }
4450
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004451 int startActivityFromRecents(int taskId, Bundle bOptions) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004452 final TaskRecord task;
4453 final int callingUid;
4454 final String callingPackage;
4455 final Intent intent;
4456 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004457 int activityType = ACTIVITY_TYPE_UNDEFINED;
4458 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004459 final ActivityOptions activityOptions = (bOptions != null)
4460 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004461 if (activityOptions != null) {
4462 activityType = activityOptions.getLaunchActivityType();
4463 windowingMode = activityOptions.getLaunchWindowingMode();
4464 }
4465 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004466 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004467 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004468 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004469
Matthew Ng606dd802017-06-05 14:06:32 -07004470 mWindowManager.deferSurfaceLayout();
4471 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004472 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004473 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004474 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004475
Matthew Ng606dd802017-06-05 14:06:32 -07004476 // Defer updating the stack in which recents is until the app transition is done, to
4477 // not run into issues where we still need to draw the task in recents but the
4478 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004479 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004480 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004481 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004482
Matthew Ng606dd802017-06-05 14:06:32 -07004483 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004484 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004485 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004486 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004487 mWindowManager.executeAppTransition();
4488 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004489 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004490 }
4491
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004492 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4493 // We always want to return to the home activity instead of the recents activity
4494 // from whatever is started from the recents activity, so move the home stack
4495 // forward.
4496 moveHomeStackToFront("startActivityFromRecents");
4497 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004498
Matthew Ng606dd802017-06-05 14:06:32 -07004499 // If the user must confirm credentials (e.g. when first launching a work app and the
4500 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4501 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4502 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004503 final ActivityRecord targetActivity = task.getTopActivity();
4504
4505 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4506 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004507 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004508 try {
4509 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions,
4510 true /* fromRecents */);
4511 } finally {
4512 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4513 targetActivity);
4514 }
Matthew Ng606dd802017-06-05 14:06:32 -07004515
4516 // If we are launching the task in the docked stack, put it into resizing mode so
4517 // the window renders full-screen with the background filling the void. Also only
4518 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004519 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004520 setResizingDuringAnimation(task);
4521 }
4522
4523 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004524 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004525 return ActivityManager.START_TASK_TO_FRONT;
4526 }
4527 callingUid = task.mCallingUid;
4528 callingPackage = task.mCallingPackage;
4529 intent = task.intent;
4530 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4531 userId = task.userId;
4532 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004533 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004534 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004535 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004536 }
Matthew Ng606dd802017-06-05 14:06:32 -07004537 return result;
4538 } finally {
4539 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004540 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004541 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004542
4543 /**
4544 * @return a list of activities which are the top ones in each visible stack. The first
4545 * entry will be the focused activity.
4546 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004547 List<IBinder> getTopVisibleActivities() {
4548 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4549 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004550 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004551 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4552 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004553 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4554 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004555 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004556 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004557 final ActivityRecord top = stack.topActivity();
4558 if (top != null) {
4559 if (stack == mFocusedStack) {
4560 topActivityTokens.add(0, top.appToken);
4561 } else {
4562 topActivityTokens.add(top.appToken);
4563 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004564 }
4565 }
4566 }
4567 }
4568 return topActivityTokens;
4569 }
Bryce Lee4a194382017-04-04 14:32:48 -07004570
4571 /**
4572 * Internal container to store a match qualifier alongside a WaitResult.
4573 */
4574 static class WaitInfo {
4575 private final ComponentName mTargetComponent;
4576 private final WaitResult mResult;
4577
4578 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4579 this.mTargetComponent = targetComponent;
4580 this.mResult = result;
4581 }
4582
Wale Ogunwale3270f172017-04-26 07:29:42 -07004583 public boolean matches(ComponentName targetComponent) {
4584 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004585 }
4586
4587 public WaitResult getResult() {
4588 return mResult;
4589 }
4590
4591 public ComponentName getComponent() {
4592 return mTargetComponent;
4593 }
4594
4595 public void dump(PrintWriter pw, String prefix) {
4596 pw.println(prefix + "WaitInfo:");
4597 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4598 pw.println(prefix + " mResult=");
4599 mResult.dump(pw, prefix);
4600 }
4601 }
David Stevens9440dc82017-03-16 19:00:20 -07004602
4603 private final class SleepTokenImpl extends SleepToken {
4604 private final String mTag;
4605 private final long mAcquireTime;
4606 private final int mDisplayId;
4607
4608 public SleepTokenImpl(String tag, int displayId) {
4609 mTag = tag;
4610 mDisplayId = displayId;
4611 mAcquireTime = SystemClock.uptimeMillis();
4612 }
4613
4614 @Override
4615 public void release() {
4616 synchronized (mService) {
4617 removeSleepTokenLocked(this);
4618 }
4619 }
4620
4621 @Override
4622 public String toString() {
4623 return "{\"" + mTag + "\", display " + mDisplayId
4624 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4625 }
4626 }
4627
Craig Mautner27084302013-03-25 08:05:25 -07004628}