blob: 1116fc3900d7721427b5959f74b22a1b3e7ca90e [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 Kuliane6ac20e2017-03-17 10:30:50 -070019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020020import static android.Manifest.permission.START_ANY_ACTIVITY;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
23import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
24import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.START_TASK_TO_FRONT;
26import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.HOME_STACK_ID;
32import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
33import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
34import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080035import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020036import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070041import static android.view.Display.FLAG_PRIVATE;
42import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080043import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020044
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
74import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
77import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
78import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
81import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
84import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
85import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
86import static com.android.server.am.ActivityStack.STACK_VISIBLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080092import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
93import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
94import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020095import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080096import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020097
Svetoslav7008b512015-06-24 18:47:07 -070098import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080099import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700100import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100101import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800109import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700110import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700113import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400114import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Intent;
119import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700121import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.pm.PackageManager;
123import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100124import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800126import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800127import android.hardware.display.DisplayManager;
128import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800129import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800130import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800131import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800132import android.hardware.input.InputManager;
133import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100135import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700136import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700137import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700140import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700142import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700144import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400145import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700147import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700148import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700149import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100150import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700151import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700152import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400153import android.provider.Settings;
154import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700155import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700157import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700158import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800159import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700160import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800163import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800164import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800165import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800166
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800167import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800168import com.android.internal.logging.MetricsLogger;
169import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700170import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400171import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700172import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400173import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800174import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700175import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700176import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700177
Craig Mautner8d341ef2013-03-26 09:03:27 -0700178import java.io.FileDescriptor;
179import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700180import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800181import java.lang.annotation.Retention;
182import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700183import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700184import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700186import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700187
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800188public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800189 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700191 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700193 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800200 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800202
Craig Mautnerf3333272013-04-22 10:55:53 -0700203 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700205
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700210 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211
Craig Mautner05d29032013-05-03 13:40:13 -0700212 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
213 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
214 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800217 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
218 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
219 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700220 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400221 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
222 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700223 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700224 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700225 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800226 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
227 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800228
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700230
Jason Monk62515be2014-05-21 16:06:19 -0400231 private static final String LOCK_TASK_TAG = "Lock-to-App";
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. stack) that we are trying to get
234 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800235 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700237 // Used to indicate that windows of activities should be preserved during the resize.
238 static final boolean PRESERVE_WINDOWS = true;
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. task) should be moved/created
241 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700242 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
244 // Used to indicate that an objects (e.g. task) removal from its container
245 // (e.g. stack) is due to it moving to another container.
246 static final boolean MOVING = true;
247
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700248 // Force the focus to change to the stack we are moving a task to..
249 static final boolean FORCE_FOCUS = true;
250
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700251 // Don't execute any calls to resume.
252 static final boolean DEFER_RESUME = true;
253
Winson Chung010927a2016-12-15 16:12:35 -0800254 // Used to indicate that a task is removed it should also be removed from recents.
255 static final boolean REMOVE_FROM_RECENTS = true;
256
Winson Chung7900bee2017-03-10 08:59:25 -0800257 /**
258 * The modes which affect which tasks are returned when calling
259 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
260 */
261 @Retention(RetentionPolicy.SOURCE)
262 @IntDef({
263 MATCH_TASK_IN_STACKS_ONLY,
264 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
265 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
266 })
267 public @interface AnyTaskForIdMatchTaskMode {}
268 // Match only tasks in the current stacks
269 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
270 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
271 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
272 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
273 // provided stack id
274 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
275
Svetoslav7008b512015-06-24 18:47:07 -0700276 // Activity actions an app cannot start if it uses a permission which is not granted.
277 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
278 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700279
Svetoslav7008b512015-06-24 18:47:07 -0700280 static {
281 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
282 Manifest.permission.CAMERA);
283 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
284 Manifest.permission.CAMERA);
285 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
286 Manifest.permission.CALL_PHONE);
287 }
288
Svet Ganov99b60432015-06-27 13:15:22 -0700289 /** Action restriction: launching the activity is not restricted. */
290 private static final int ACTIVITY_RESTRICTION_NONE = 0;
291 /** Action restriction: launching the activity is restricted by a permission. */
292 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
293 /** Action restriction: launching the activity is restricted by an app op. */
294 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700295
justinzhang5286d3f2014-05-12 17:06:01 -0400296 /** Status Bar Service **/
297 private IBinder mToken = new Binder();
298 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400299 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400300
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700301 // For debugging to make sure the caller when acquiring/releasing our
302 // wake lock is the system process.
303 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800304 /** The number of distinct task ids that can be assigned to the tasks of a single user */
305 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700306
Craig Mautner27084302013-03-25 08:05:25 -0700307 final ActivityManagerService mService;
308
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800309 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800310
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700311 final ActivityStackSupervisorHandler mHandler;
312
313 /** Short cut */
314 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700317 /** Counter for next free stack ID to use for dynamic activity stacks. */
318 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700319
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800320 /**
321 * Maps the task identifier that activities are currently being started in to the userId of the
322 * task. Each time a new task is created, the entry for the userId of the task is incremented
323 */
324 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700325
Craig Mautner2420ead2013-04-01 17:13:20 -0700326 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800327 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700328
Craig Mautnere0a38842013-12-16 16:14:02 -0800329 /** The stack containing the launcher app. Assumed to always be attached to
330 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800331 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700332
Craig Mautnere0a38842013-12-16 16:14:02 -0800333 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800334 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700335
Craig Mautner4a1cb222013-12-04 16:14:06 -0800336 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
337 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800339 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700340
341 /** List of activities that are waiting for a new activity to become visible before completing
342 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800343 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700344
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700345 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700346 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700347
348 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700349 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700350
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351 /** List of activities that are ready to be stopped, but waiting for the next activity to
352 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800353 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700354
Craig Mautnerf3333272013-04-22 10:55:53 -0700355 /** List of activities that are ready to be finished, but waiting for the previous activity to
356 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800357 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700358
Craig Mautner0eea92c2013-05-16 13:35:39 -0700359 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800360 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700361
Wale Ogunwale22e25262016-02-01 10:32:02 -0800362 /** List of activities whose multi-window mode changed that we need to report to the
363 * application */
364 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
365
366 /** List of activities whose picture-in-picture mode changed that we need to report to the
367 * application */
368 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
369
Craig Mautnerf3333272013-04-22 10:55:53 -0700370 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700371 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700372
Craig Mautnerde4ef022013-04-07 19:01:33 -0700373 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
374 * is being brought in front of us. */
375 boolean mUserLeaving = false;
376
Craig Mautner0eea92c2013-05-16 13:35:39 -0700377 /** Set when we have taken too long waiting to go to sleep. */
378 boolean mSleepTimeout = false;
379
380 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700381 * We don't want to allow the device to go to sleep while in the process
382 * of launching an activity. This is primarily to allow alarm intent
383 * receivers to launch an activity and get that to run before the device
384 * goes back to sleep.
385 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700386 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700387
388 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700389 * Set when the system is going to sleep, until we have
390 * successfully paused the current activity and released our wake lock.
391 * At that point the system is allowed to actually sleep.
392 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700393 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700394
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700395 /** Stack id of the front stack when user switched, indexed by userId. */
396 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700397
Craig Mautner4504de52013-12-20 09:06:56 -0800398 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800399 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800400 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800401
Bryce Leeaf3a4002017-03-20 10:37:12 -0700402 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700404 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800405
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800406 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
407
408 private DisplayManagerInternal mDisplayManagerInternal;
409 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800410
Craig Mautner15df08a2015-04-01 12:17:18 -0700411 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
412 * may be finished until there is only one entry left. If this is empty the system is not
413 * in lockTask mode. */
414 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000415 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700416 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
417 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000418 */
419 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400420 /**
421 * Notifies the user when entering/exiting lock-task.
422 */
423 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800424
Wale Ogunwaled046a012015-12-24 13:05:59 -0800425 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800426 boolean inResumeTopActivity;
427
Andrii Kulian1e32e022016-09-16 15:29:34 -0700428 /**
429 * Temporary rect used during docked stack resize calculation so we don't need to create a new
430 * object each time.
431 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700432 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700433
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700434 // The default minimal size that will be used if the activity doesn't specify its minimal size.
435 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700436 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700437
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700438 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
439 private boolean mTaskLayersChanged = true;
440
Jorim Jaggi275561a2016-02-23 10:11:02 -0500441 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800442
Andrii Kulian1779e612016-10-12 21:58:25 -0700443 @Override
444 protected int getChildCount() {
445 return mActivityDisplays.size();
446 }
447
448 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700449 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700450 return mActivityDisplays.valueAt(index);
451 }
452
453 @Override
454 protected ConfigurationContainer getParent() {
455 return null;
456 }
457
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700458 Configuration getDisplayOverrideConfiguration(int displayId) {
459 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
460 if (activityDisplay == null) {
461 throw new IllegalArgumentException("No display found with id: " + displayId);
462 }
463
464 return activityDisplay.getOverrideConfiguration();
465 }
466
467 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
468 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
469 if (activityDisplay == null) {
470 throw new IllegalArgumentException("No display found with id: " + displayId);
471 }
472
473 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
474 }
475
Wale Ogunwale39381972015-12-17 17:15:29 -0800476 static class FindTaskResult {
477 ActivityRecord r;
478 boolean matchedByRootAffinity;
479 }
480 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
481
Craig Mautneree36c772014-07-16 14:56:05 -0700482 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800483 * Temp storage for display ids sorted in focus order.
484 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
485 * it's more efficient, as the number of displays is usually small.
486 */
487 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
488
489 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100490 * Used to keep track whether app visibilities got changed since the last pause. Useful to
491 * determine whether to invoke the task stack change listener after pausing.
492 */
493 boolean mAppVisibilitiesChangedSinceLastPause;
494
495 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100496 * Set of tasks that are in resizing mode during an app transition to fill the "void".
497 */
498 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
499
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700500
501 /**
502 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
503 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
504 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
505 * like the docked stack going empty.
506 */
507 private boolean mAllowDockedStackResize = true;
508
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100509 /**
Tony Mak853304c2016-04-18 15:17:41 +0100510 * Is dock currently minimized.
511 */
512 boolean mIsDockMinimized;
513
Jorim Jaggife762342016-10-13 14:33:27 +0200514 final KeyguardController mKeyguardController;
515
Tony Mak853304c2016-04-18 15:17:41 +0100516 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700517 * Description of a request to start a new activity, which has been held
518 * due to app switches being disabled.
519 */
520 static class PendingActivityLaunch {
521 final ActivityRecord r;
522 final ActivityRecord sourceRecord;
523 final int startFlags;
524 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700525 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700526
527 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700528 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700529 r = _r;
530 sourceRecord = _sourceRecord;
531 startFlags = _startFlags;
532 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700533 callerApp = _callerApp;
534 }
535
536 void sendErrorResult(String message) {
537 try {
538 if (callerApp.thread != null) {
539 callerApp.thread.scheduleCrash(message);
540 }
541 } catch (RemoteException e) {
542 Slog.e(TAG, "Exception scheduling crash of failed "
543 + "activity launcher sourceRecord=" + sourceRecord, e);
544 }
Craig Mautneree36c772014-07-16 14:56:05 -0700545 }
546 }
547
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800548 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700549 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700550 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800551 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200552 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700553 }
554
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800555 void setRecentTasks(RecentTasks recentTasks) {
556 mRecentTasks = recentTasks;
557 }
558
Jeff Brown2c43c332014-06-12 22:38:59 -0700559 /**
560 * At the time when the constructor runs, the power manager has not yet been
561 * initialized. So we initialize our wakelocks afterwards.
562 */
563 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700565 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700566 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700567 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700568 }
569
justinzhang5286d3f2014-05-12 17:06:01 -0400570 // This function returns a IStatusBarService. The value is from ServiceManager.
571 // getService and is cached.
572 private IStatusBarService getStatusBarService() {
573 synchronized (mService) {
574 if (mStatusBarService == null) {
575 mStatusBarService = IStatusBarService.Stub.asInterface(
576 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
577 if (mStatusBarService == null) {
578 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
579 }
580 }
581 return mStatusBarService;
582 }
583 }
584
Jason Monk35c62a42014-06-17 10:24:47 -0400585 private IDevicePolicyManager getDevicePolicyManager() {
586 synchronized (mService) {
587 if (mDevicePolicyManager == null) {
588 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
589 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
590 if (mDevicePolicyManager == null) {
591 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
592 }
593 }
594 return mDevicePolicyManager;
595 }
596 }
597
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700598 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 synchronized (mService) {
600 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200601 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602
603 mDisplayManager =
604 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
605 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800606 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607
608 Display[] displays = mDisplayManager.getDisplays();
609 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
610 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800611 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700612 if (activityDisplay.mDisplay == null) {
613 throw new IllegalStateException("Default Display does not exist");
614 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800615 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700616 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617 }
618
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800619 mHomeStack = mFocusedStack = mLastFocusedStack =
620 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800621
622 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 }
Craig Mautner27084302013-03-25 08:05:25 -0700624 }
625
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700626 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700628 }
629
Craig Mautnerde4ef022013-04-07 19:01:33 -0700630 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700632 }
633
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700634 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700635 if (stack == null) {
636 return false;
637 }
638
Craig Mautnerdf88d732014-01-27 09:21:32 -0800639 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
640 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700641 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800642 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800643 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700644 }
645
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800646 /** The top most stack on its display. */
647 boolean isFrontStackOnDisplay(ActivityStack stack) {
648 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
649 }
650
651 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700652 if (stack == null) {
653 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700655
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700656 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
657 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700658 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800659 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800660 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700661 }
662
Wale Ogunwaled046a012015-12-24 13:05:59 -0800663 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800664 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
665 if (!focusCandidate.isFocusable()) {
666 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800667 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800668 }
669
670 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800671 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800672 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800673
Wale Ogunwaled046a012015-12-24 13:05:59 -0800674 EventLogTags.writeAmFocusedStack(
675 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
676 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
677 }
678
679 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800680 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800681 if (r != null && r.idle) {
682 checkFinishBootingLocked();
683 }
684 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700685 }
686
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700687 void moveHomeStackToFront(String reason) {
688 mHomeStack.moveToFront(reason);
689 }
690
Matthew Ng330757d2017-02-28 14:19:17 -0800691 void moveRecentsStackToFront(String reason) {
692 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
693 if (recentsStack != null) {
694 recentsStack.moveToFront(reason);
695 }
696 }
697
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700698 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800699 boolean moveHomeStackTaskToTop(String reason) {
700 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700701
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700702 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700703 if (top == null) {
704 return false;
705 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700706 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700707 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700708 }
709
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800710 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700711 if (!mService.mBooting && !mService.mBooted) {
712 // Not ready yet!
713 return false;
714 }
715
Craig Mautner84984fa2014-06-19 11:19:20 -0700716 if (prev != null) {
717 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
718 }
719
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800720 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700721 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800722 final String myReason = reason + " resumeHomeStackTask";
723
Mark Lua56ea122015-10-08 13:31:01 +0800724 // Only resume home activity if isn't finishing.
725 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700726 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800727 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700728 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800729 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700730 }
731
Craig Mautner8d341ef2013-03-26 09:03:27 -0700732 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800733 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
734 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700735 }
736
737 /**
738 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
739 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800740 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700741 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800742 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
743 * valid if the matchMode is
744 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700745 */
Winson Chung7900bee2017-03-10 08:59:25 -0800746 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
747 // If there is a stack id set, ensure that we are attempting to actually restore a task
748 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
749 stackId != INVALID_STACK_ID) {
750 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
751 }
752
Craig Mautnere0a38842013-12-16 16:14:02 -0800753 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800755 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
757 ActivityStack stack = stacks.get(stackNdx);
758 TaskRecord task = stack.taskForIdLocked(id);
759 if (task != null) {
760 return task;
761 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700762 }
763 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800764
Winson Chung7900bee2017-03-10 08:59:25 -0800765 // If we are matching stack tasks only, return now
766 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800767 return null;
768 }
769
Winson Chung7900bee2017-03-10 08:59:25 -0800770 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
771 // the task from recents
772 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
773 TaskRecord task = mRecentTasks.taskForIdLocked(id);
774 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
775 if (DEBUG_RECENTS && task == null) {
776 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
777 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700778 return task;
779 }
780
Winson Chung7900bee2017-03-10 08:59:25 -0800781 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700782 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700783 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
784 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800785 return null;
786 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700787 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800788 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700789 }
790
Craig Mautner6170f732013-04-02 13:05:23 -0700791 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800792 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800794 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
796 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
797 if (r != null) {
798 return r;
799 }
Craig Mautner6170f732013-04-02 13:05:23 -0700800 }
801 }
802 return null;
803 }
804
Tony Mak853304c2016-04-18 15:17:41 +0100805 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000806 * Detects whether we should show a lock screen in front of this task for a locked user.
807 * <p>
808 * We'll do this if either of the following holds:
809 * <ul>
810 * <li>The top activity explicitly belongs to {@param userId}.</li>
811 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
812 * </ul>
813 *
814 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100815 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000816 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
817 // To handle the case that work app is in the task but just is not the top one.
818 final ActivityRecord activityRecord = task.getTopActivity();
819 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
820
821 return (activityRecord != null && activityRecord.userId == userId)
822 || (resultTo != null && resultTo.userId == userId);
823 }
824
825 /**
826 * Find all visible task stacks containing {@param userId} and intercept them with an activity
827 * to block out the contents and possibly start a credential-confirming intent.
828 *
829 * @param userId user handle for the locked managed profile.
830 */
831 void lockAllProfileTasks(@UserIdInt int userId) {
832 mWindowManager.deferSurfaceLayout();
833 try {
834 final List<ActivityStack> stacks = getStacks();
835 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
836 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
837 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
838 final TaskRecord task = tasks.get(taskNdx);
839
840 // Check the task for a top activity belonging to userId, or returning a result
841 // to an activity belonging to userId. Example case: a document picker for
842 // personal files, opened by a work app, should still get locked.
843 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000844 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
845 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000846 }
Tony Mak853304c2016-04-18 15:17:41 +0100847 }
848 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000849 } finally {
850 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100851 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000852 }
853
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800854 void setNextTaskIdForUserLocked(int taskId, int userId) {
855 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
856 if (taskId > currentTaskId) {
857 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700858 }
859 }
860
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700861 static int nextTaskIdForUser(int taskId, int userId) {
862 int nextTaskId = taskId + 1;
863 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
864 // Wrap around as there will be smaller task ids that are available now.
865 nextTaskId -= MAX_TASK_IDS_PER_USER;
866 }
867 return nextTaskId;
868 }
869
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800870 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800871 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
872 // for a userId u, a taskId can only be in the range
873 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
874 // 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 -0700875 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800876 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800877 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800878 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700879 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800880 if (candidateTaskId == currentTaskId) {
881 // Something wrong!
882 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
883 throw new IllegalStateException("Cannot get an available task id."
884 + " Reached limit of " + MAX_TASK_IDS_PER_USER
885 + " running tasks per user.");
886 }
887 }
888 mCurTaskIdForUser.put(userId, candidateTaskId);
889 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700890 }
891
Chong Zhang6cda19c2016-06-14 19:07:56 -0700892 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800893 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700894 if (stack == null) {
895 return null;
896 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 ActivityRecord resumedActivity = stack.mResumedActivity;
898 if (resumedActivity == null || resumedActivity.app == null) {
899 resumedActivity = stack.mPausingActivity;
900 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700901 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 }
903 }
904 return resumedActivity;
905 }
906
Dianne Hackbornff072722014-09-24 10:56:28 -0700907 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700908 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
911 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
913 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700914 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 continue;
916 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700917 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 if (hr != null) {
919 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
920 && processName.equals(hr.processName)) {
921 try {
George Mount2c92c972014-03-20 09:38:23 -0700922 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 didSomething = true;
924 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700925 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 Slog.w(TAG, "Exception in new application when starting activity "
927 + hr.intent.getComponent().flattenToShortString(), e);
928 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700929 }
Craig Mautner20e72272013-04-01 13:45:53 -0700930 }
Craig Mautner20e72272013-04-01 13:45:53 -0700931 }
932 }
933 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700934 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700935 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700936 }
Craig Mautner20e72272013-04-01 13:45:53 -0700937 return didSomething;
938 }
939
940 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
942 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
944 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700945 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 continue;
947 }
948 final ActivityRecord resumedActivity = stack.mResumedActivity;
949 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700950 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800951 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 return false;
953 }
Craig Mautner20e72272013-04-01 13:45:53 -0700954 }
955 }
Wei Wang65c7a152016-06-02 18:51:22 -0700956 // Send launch end powerhint when idle
957 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700958 return true;
959 }
960
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
963 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800964 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
965 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700966 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700968 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 return false;
970 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700971 }
972 }
973 }
974 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700975 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
977 mLastFocusedStack + " to=" + mFocusedStack);
978 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700979 return true;
980 }
981
982 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800983 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800984 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
985 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
987 final ActivityStack stack = stacks.get(stackNdx);
988 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800989 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700990 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800991 return false;
992 }
993 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700995 }
996 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800997 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700998 }
999
Craig Mautner2acc3892013-09-23 10:28:14 -07001000 /**
1001 * Pause all activities in either all of the stacks or just the back stacks.
1002 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001003 * @param resuming The resuming activity.
1004 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1005 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001006 * @return true if any activity was paused as a result of this call.
1007 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001008 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001009 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1011 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001012 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1013 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001014 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001015 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001017 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1018 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001019 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001020 }
1021 }
1022 return someActivityPaused;
1023 }
1024
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001026 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001027 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1028 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001029 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1030 final ActivityStack stack = stacks.get(stackNdx);
1031 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001032 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001034 Slog.d(TAG_STATES,
1035 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 pausing = false;
1037 } else {
1038 return false;
1039 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001040 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001041 }
1042 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001043 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001044 }
1045
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001046 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001047 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001048 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001049 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1050 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1051 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1052 final ActivityStack stack = stacks.get(stackNdx);
1053 if (stack.mResumedActivity != null &&
1054 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001055 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001056 }
1057 }
1058 }
1059 }
1060
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001061 void cancelInitializingActivities() {
1062 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1063 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1064 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1065 stacks.get(stackNdx).cancelInitializingActivities();
1066 }
1067 }
1068 }
1069
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001070 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001071 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001072 }
1073
1074 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1075 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001076 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001077 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001078 if (w.who == null) {
1079 changed = true;
1080 w.timeout = false;
1081 if (r != null) {
1082 w.who = new ComponentName(r.info.packageName, r.info.name);
1083 }
1084 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1085 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001087 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001088 if (changed) {
1089 mService.notifyAll();
1090 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001091 }
1092
Chong Zhang5022da32016-06-21 16:31:37 -07001093 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1094 boolean changed = false;
1095 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1096 WaitResult w = mWaitingActivityLaunched.remove(i);
1097 if (w.who == null) {
1098 changed = true;
1099 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1100 // the starting activity ends up moving another activity to front, and it should
1101 // wait for this new activity to become visible instead.
1102 // Do not modify other fields.
1103 w.result = START_TASK_TO_FRONT;
1104 }
1105 }
1106 if (changed) {
1107 mService.notifyAll();
1108 }
1109 }
1110
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001111 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1112 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001113 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001114 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001115 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 if (w.who == null) {
1117 changed = true;
1118 w.timeout = timeout;
1119 if (r != null) {
1120 w.who = new ComponentName(r.info.packageName, r.info.name);
1121 }
1122 w.thisTime = thisTime;
1123 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001124 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001126 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001127 if (changed) {
1128 mService.notifyAll();
1129 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001130 }
1131
Craig Mautner29219d92013-04-16 20:19:12 -07001132 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001133 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001134 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001135 if (r != null) {
1136 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001137 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001138
Andrii Kulian7318d632016-07-20 18:59:28 -07001139 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001140 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1141
1142 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1143 final int displayId = mTmpOrderedDisplayIds.get(i);
1144 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1145 if (stacks == null) {
1146 continue;
1147 }
1148 for (int j = stacks.size() - 1; j >= 0; --j) {
1149 final ActivityStack stack = stacks.get(j);
1150 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1151 r = stack.topRunningActivityLocked();
1152 if (r != null) {
1153 return r;
1154 }
Craig Mautner29219d92013-04-16 20:19:12 -07001155 }
1156 }
1157 }
1158 return null;
1159 }
1160
Dianne Hackborn09233282014-04-30 11:33:59 -07001161 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001162 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001163 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1164 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001165 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001166 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001167 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001168 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1169 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001170 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001171 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001172 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001173 }
1174 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001175
1176 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1177 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1178 while (maxNum > 0) {
1179 long mostRecentActiveTime = Long.MIN_VALUE;
1180 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001181 final int numTaskLists = runningTaskLists.size();
1182 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1183 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001184 if (!stackTaskList.isEmpty()) {
1185 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1186 if (lastActiveTime > mostRecentActiveTime) {
1187 mostRecentActiveTime = lastActiveTime;
1188 selectedStackList = stackTaskList;
1189 }
1190 }
1191 }
1192 if (selectedStackList != null) {
1193 list.add(selectedStackList.remove(0));
1194 --maxNum;
1195 } else {
1196 break;
1197 }
1198 }
Craig Mautner20e72272013-04-01 13:45:53 -07001199 }
1200
Todd Kennedy7440f172015-12-09 14:31:22 -08001201 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1202 ProfilerInfo profilerInfo) {
1203 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001204 if (aInfo != null) {
1205 // Store the found target back into the intent, because now that
1206 // we have it we never want to do this again. For example, if the
1207 // user navigates back to this point in the history, we should
1208 // always restart the exact same activity.
1209 intent.setComponent(new ComponentName(
1210 aInfo.applicationInfo.packageName, aInfo.name));
1211
1212 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001213 if (!aInfo.processName.equals("system")) {
1214 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001215 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001216 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001217
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001218 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1219 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1220 }
1221
Man Caocfa78b22015-06-11 20:14:34 -07001222 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1223 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1224 }
1225
1226 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001227 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001228 }
1229 }
1230 }
1231 return aInfo;
1232 }
1233
Todd Kennedy7440f172015-12-09 14:31:22 -08001234 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001235 return resolveIntent(intent, resolvedType, userId, 0);
1236 }
1237
1238 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001239 synchronized (mService) {
1240 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001241 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001242 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001243 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001244 }
1245
1246 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1247 ProfilerInfo profilerInfo, int userId) {
1248 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1249 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1250 }
1251
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001252 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1253 boolean andResume, boolean checkConfig) throws RemoteException {
1254
1255 if (!allPausedActivitiesComplete()) {
1256 // While there are activities pausing we skipping starting any new activities until
1257 // pauses are complete. NOTE: that we also do this for activities that are starting in
1258 // the paused state because they will first be resumed then paused on the client side.
1259 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1260 "realStartActivityLocked: Skipping start of r=" + r
1261 + " some activities pausing...");
1262 return false;
1263 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001264
Andrii Kulianada10292017-02-09 23:19:29 -08001265 r.startFreezingScreenLocked(app, 0);
1266 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267
Andrii Kulianada10292017-02-09 23:19:29 -08001268 // schedule launch ticks to collect information about slow apps.
1269 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001270
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001271 // Have the window manager re-evaluate the orientation of the screen based on the new
1272 // activity order. Note that as a result of this, it can call back into the activity
1273 // manager with a new orientation. We don't care about that, because the activity is not
1274 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001276 final int displayId = r.getDisplayId();
1277 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1278 getDisplayOverrideConfiguration(displayId),
1279 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001280 // Deferring resume here because we're going to launch new activity shortly.
1281 // We don't want to perform a redundant launch of the same record while ensuring
1282 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001283 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1284 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 }
1286
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001287 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001288 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001289 }
1290
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 r.app = app;
1292 app.waitingToKill = null;
1293 r.launchCount++;
1294 r.lastLaunchTime = SystemClock.uptimeMillis();
1295
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001296 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001297
1298 int idx = app.activities.indexOf(r);
1299 if (idx < 0) {
1300 app.activities.add(r);
1301 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001302 mService.updateLruProcessLocked(app, true, null);
1303 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001304
Craig Mautner15df08a2015-04-01 12:17:18 -07001305 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001306 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1307 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001308 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001309 }
1310
Andrii Kulian02b7a832016-10-06 23:11:56 -07001311 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001312 try {
1313 if (app.thread == null) {
1314 throw new RemoteException();
1315 }
1316 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001317 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001318 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001319 // We don't need to deliver new intents and/or set results if activity is going
1320 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001321 results = r.results;
1322 newIntents = r.newIntents;
1323 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001324 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1325 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1326 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001327 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1328 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001329 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001330 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001331 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001332 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001333 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1334 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001335 r.sleeping = false;
1336 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001337 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001338 mService.showAskCompatModeDialogLocked(r);
1339 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001340 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001341 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1342 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1343 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001344 final String profileFile = mService.mProfileFile;
1345 if (profileFile != null) {
1346 ParcelFileDescriptor profileFd = mService.mProfileFd;
1347 if (profileFd != null) {
1348 try {
1349 profileFd = profileFd.dup();
1350 } catch (IOException e) {
1351 if (profileFd != null) {
1352 try {
1353 profileFd.close();
1354 } catch (IOException o) {
1355 }
1356 profileFd = null;
1357 }
1358 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001359 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001360
1361 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001362 mService.mSamplingInterval, mService.mAutoStopProfiler,
1363 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001364 }
1365 }
1366 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001367
Andrii Kulianada10292017-02-09 23:19:29 -08001368 app.hasShownUi = true;
1369 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001370 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001371 // Because we could be starting an Activity in the system process this may not go across
1372 // a Binder interface which would create a new Configuration. Consequently we have to
1373 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001374
1375 final Configuration globalConfiguration =
1376 new Configuration(mService.getGlobalConfiguration());
1377 r.setLastReportedGlobalConfiguration(globalConfiguration);
1378 final Configuration mergedOverrideConfiguration =
1379 new Configuration(task.getMergedOverrideConfiguration());
1380 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1381
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001383 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001384 globalConfiguration,
1385 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001386 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1387 r.persistentState, results, newIntents, !andResume,
1388 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001389
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001390 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001391 // This may be a heavy-weight process! Note that the package
1392 // manager will ensure that only activity can run in the main
1393 // process of the .apk, which is the only thing that will be
1394 // considered heavy-weight.
1395 if (app.processName.equals(app.info.packageName)) {
1396 if (mService.mHeavyWeightProcess != null
1397 && mService.mHeavyWeightProcess != app) {
1398 Slog.w(TAG, "Starting new heavy weight process " + app
1399 + " when already running "
1400 + mService.mHeavyWeightProcess);
1401 }
1402 mService.mHeavyWeightProcess = app;
1403 Message msg = mService.mHandler.obtainMessage(
1404 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1405 msg.obj = r;
1406 mService.mHandler.sendMessage(msg);
1407 }
1408 }
1409
1410 } catch (RemoteException e) {
1411 if (r.launchFailed) {
1412 // This is the second time we failed -- finish activity
1413 // and give up.
1414 Slog.e(TAG, "Second failure launching "
1415 + r.intent.getComponent().flattenToShortString()
1416 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001417 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001418 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1419 "2nd-crash", false);
1420 return false;
1421 }
1422
1423 // This is the first time we failed -- restart process and
1424 // retry.
1425 app.activities.remove(r);
1426 throw e;
1427 }
1428
1429 r.launchFailed = false;
1430 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001431 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001432 }
1433
1434 if (andResume) {
1435 // As part of the process of launching, ActivityThread also performs
1436 // a resume.
1437 stack.minimalResumeActivityLocked(r);
1438 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001439 // This activity is not starting in the resumed state... which should look like we asked
1440 // it to pause+stop (but remain visible), and it has done so and reported back the
1441 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001442 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001443 "Moving to PAUSED: " + r + " (starting in paused state)");
1444 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001445 }
1446
1447 // Launch the new version setup screen if needed. We do this -after-
1448 // launching the initial activity (that is, home), so that it can have
1449 // a chance to initialize itself while in the background, making the
1450 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001451 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001452 mService.startSetupActivityLocked();
1453 }
1454
Dianne Hackborn465fa392014-09-14 14:21:18 -07001455 // Update any services we are bound to that might care about whether
1456 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001457 if (r.app != null) {
1458 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1459 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001460
Craig Mautner2420ead2013-04-01 17:13:20 -07001461 return true;
1462 }
1463
Craig Mautnere79d42682013-04-01 19:01:53 -07001464 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001465 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001466 // Is this activity's application already running?
1467 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001468 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001469
Andrii Kulian02b7a832016-10-06 23:11:56 -07001470 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001471
1472 if (app != null && app.thread != null) {
1473 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001474 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1475 || !"android".equals(r.info.packageName)) {
1476 // Don't add this if it is a platform component that is marked
1477 // to run in multiple processes, because this is actually
1478 // part of the framework so doesn't make sense to track as a
1479 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001480 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1481 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001482 }
George Mount2c92c972014-03-20 09:38:23 -07001483 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001484 return;
1485 } catch (RemoteException e) {
1486 Slog.w(TAG, "Exception when starting activity "
1487 + r.intent.getComponent().flattenToShortString(), e);
1488 }
1489
1490 // If a dead object exception was thrown -- fall through to
1491 // restart the application.
1492 }
1493
1494 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001495 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001496 }
1497
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001498 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001499 String resultWho, int requestCode, int callingPid, int callingUid,
1500 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001501 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001502 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1503 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001504 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001505 return true;
1506 }
1507 final int componentRestriction = getComponentRestrictionForCallingPackage(
1508 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1509 final int actionRestriction = getActionRestrictionForCallingPackage(
1510 intent.getAction(), callingPackage, callingPid, callingUid);
1511 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1512 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1513 if (resultRecord != null) {
1514 resultStack.sendActivityResultLocked(-1,
1515 resultRecord, resultWho, requestCode,
1516 Activity.RESULT_CANCELED, null);
1517 }
1518 final String msg;
1519 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1520 msg = "Permission Denial: starting " + intent.toString()
1521 + " from " + callerApp + " (pid=" + callingPid
1522 + ", uid=" + callingUid + ")" + " with revoked permission "
1523 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1524 } else if (!aInfo.exported) {
1525 msg = "Permission Denial: starting " + intent.toString()
1526 + " from " + callerApp + " (pid=" + callingPid
1527 + ", uid=" + callingUid + ")"
1528 + " not exported from uid " + aInfo.applicationInfo.uid;
1529 } else {
1530 msg = "Permission Denial: starting " + intent.toString()
1531 + " from " + callerApp + " (pid=" + callingPid
1532 + ", uid=" + callingUid + ")"
1533 + " requires " + aInfo.permission;
1534 }
1535 Slog.w(TAG, msg);
1536 throw new SecurityException(msg);
1537 }
1538
1539 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1540 final String message = "Appop Denial: starting " + intent.toString()
1541 + " from " + callerApp + " (pid=" + callingPid
1542 + ", uid=" + callingUid + ")"
1543 + " requires " + AppOpsManager.permissionToOp(
1544 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1545 Slog.w(TAG, message);
1546 return false;
1547 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1548 final String message = "Appop Denial: starting " + intent.toString()
1549 + " from " + callerApp + " (pid=" + callingPid
1550 + ", uid=" + callingUid + ")"
1551 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1552 Slog.w(TAG, message);
1553 return false;
1554 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001555 if (options != null) {
1556 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1557 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1558 callingPid, callingUid);
1559 if (startInTaskPerm != PERMISSION_GRANTED) {
1560 final String msg = "Permission Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ") with launchTaskId="
1563 + options.getLaunchTaskId();
1564 Slog.w(TAG, msg);
1565 throw new SecurityException(msg);
1566 }
1567 }
1568 // Check if someone tries to launch an activity on a private display with a different
1569 // owner.
1570 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001571 if (launchDisplayId != INVALID_DISPLAY
1572 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1573 final String msg = "Permission Denial: starting " + intent.toString()
1574 + " from " + callerApp + " (pid=" + callingPid
1575 + ", uid=" + callingUid + ") with launchDisplayId="
1576 + launchDisplayId;
1577 Slog.w(TAG, msg);
1578 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001579 }
1580 }
1581
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001582 return true;
1583 }
1584
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001585 /** Check if caller is allowed to launch activities on specified display. */
1586 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1587 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1588 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1589
1590 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1591 if (activityDisplay == null) {
1592 Slog.w(TAG, "Launch on display check: display not found");
1593 return false;
1594 }
1595
1596 if (!activityDisplay.isPrivate()) {
1597 // Anyone can launch on a public display.
1598 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1599 + " allow launch on public display");
1600 return true;
1601 }
1602
1603 // Check if the caller is the owner of the display.
1604 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1605 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1606 + " allow launch for owner of the display");
1607 return true;
1608 }
1609
1610 // Check if caller is present on display
1611 if (activityDisplay.isUidPresent(callingUid)) {
1612 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1613 + " allow launch for caller present on the display");
1614 return true;
1615 }
1616
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001617 // Check if the caller can manage activity stacks.
1618 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001619 callingUid);
1620 if (startAnyPerm == PERMISSION_GRANTED) {
1621 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1622 + " allow launch any on display");
1623 return true;
1624 }
1625
1626 Slog.w(TAG, "Launch on display check: denied");
1627 return false;
1628 }
1629
1630 /** Update lists of UIDs that are present on displays and have access to them. */
1631 void updateUIDsPresentOnDisplay() {
1632 mDisplayAccessUIDs.clear();
1633 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1634 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1635 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1636 }
1637 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1638 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1639 }
1640
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001641 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001642 final long identity = Binder.clearCallingIdentity();
1643 try {
1644 return UserManager.get(mService.mContext).getUserInfo(userId);
1645 } finally {
1646 Binder.restoreCallingIdentity(identity);
1647 }
1648 }
1649
Svet Ganov99b60432015-06-27 13:15:22 -07001650 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001651 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001652 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1653 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001654 == PackageManager.PERMISSION_DENIED) {
1655 return ACTIVITY_RESTRICTION_PERMISSION;
1656 }
1657
Christopher Tateff7add02015-08-17 10:23:22 -07001658 if (activityInfo.permission == null) {
1659 return ACTIVITY_RESTRICTION_NONE;
1660 }
1661
Svet Ganov99b60432015-06-27 13:15:22 -07001662 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1663 if (opCode == AppOpsManager.OP_NONE) {
1664 return ACTIVITY_RESTRICTION_NONE;
1665 }
1666
1667 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1668 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001669 if (!ignoreTargetSecurity) {
1670 return ACTIVITY_RESTRICTION_APPOP;
1671 }
Svet Ganov99b60432015-06-27 13:15:22 -07001672 }
1673
1674 return ACTIVITY_RESTRICTION_NONE;
1675 }
1676
Svetoslav7008b512015-06-24 18:47:07 -07001677 private int getActionRestrictionForCallingPackage(String action,
1678 String callingPackage, int callingPid, int callingUid) {
1679 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001680 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001681 }
1682
1683 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1684 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001685 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001686 }
1687
1688 final PackageInfo packageInfo;
1689 try {
1690 packageInfo = mService.mContext.getPackageManager()
1691 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1692 } catch (PackageManager.NameNotFoundException e) {
1693 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001694 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001695 }
1696
1697 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001698 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001699 }
1700
1701 if (mService.checkPermission(permission, callingPid, callingUid) ==
1702 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001703 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001704 }
1705
1706 final int opCode = AppOpsManager.permissionToOpCode(permission);
1707 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001708 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001709 }
1710
1711 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1712 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001713 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001714 }
1715
Svet Ganov99b60432015-06-27 13:15:22 -07001716 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001717 }
1718
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001719 void setLaunchSource(int uid) {
1720 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1721 }
1722
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001723 void acquireLaunchWakelock() {
1724 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1725 throw new IllegalStateException("Calling must be system uid");
1726 }
1727 mLaunchingActivity.acquire();
1728 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1729 // To be safe, don't allow the wake lock to be held for too long.
1730 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1731 }
1732 }
1733
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001734 /**
1735 * Called when the frontmost task is idle.
1736 * @return the state of mService.mBooting before this was called.
1737 */
1738 private boolean checkFinishBootingLocked() {
1739 final boolean booting = mService.mBooting;
1740 boolean enableScreen = false;
1741 mService.mBooting = false;
1742 if (!mService.mBooted) {
1743 mService.mBooted = true;
1744 enableScreen = true;
1745 }
1746 if (booting || enableScreen) {
1747 mService.postFinishBooting(booting, enableScreen);
1748 }
1749 return booting;
1750 }
1751
Craig Mautnerf3333272013-04-22 10:55:53 -07001752 // Checked.
1753 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001754 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001755 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001756
Craig Mautnerf3333272013-04-22 10:55:53 -07001757 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001758 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001759 int NS = 0;
1760 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001761 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001762 boolean activityRemoved = false;
1763
Wale Ogunwale7d701172015-03-11 15:36:30 -07001764 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001765 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001766 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1767 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001768 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1769 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001770 if (fromTimeout) {
1771 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001772 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001773
1774 // This is a hack to semi-deal with a race condition
1775 // in the client where it can be constructed with a
1776 // newer configuration from when we asked it to launch.
1777 // We'll update with whatever configuration it now says
1778 // it used to launch.
1779 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001780 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001781 }
1782
1783 // We are now idle. If someone is waiting for a thumbnail from
1784 // us, we can now deliver.
1785 r.idle = true;
1786
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001787 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001788 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001789 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001790 }
1791 }
1792
1793 if (allResumedActivitiesIdle()) {
1794 if (r != null) {
1795 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001796 }
1797
1798 if (mLaunchingActivity.isHeld()) {
1799 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1800 if (VALIDATE_WAKE_LOCK_CALLER &&
1801 Binder.getCallingUid() != Process.myUid()) {
1802 throw new IllegalStateException("Calling must be system uid");
1803 }
1804 mLaunchingActivity.release();
1805 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001806 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001807 }
1808
1809 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001810 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1811 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001812 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001813 if ((NF = mFinishingActivities.size()) > 0) {
1814 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001815 mFinishingActivities.clear();
1816 }
1817
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001819 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001820 mStartingUsers.clear();
1821 }
1822
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 // Stop any activities that are scheduled to do so but have been
1824 // waiting for the next one to start.
1825 for (int i = 0; i < NS; i++) {
1826 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001827 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001828 if (stack != null) {
1829 if (r.finishing) {
1830 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1831 } else {
1832 stack.stopActivityLocked(r);
1833 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 }
1835 }
1836
1837 // Finish any activities that are scheduled to do so but have been
1838 // waiting for the next one to start.
1839 for (int i = 0; i < NF; i++) {
1840 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001841 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001842 if (stack != null) {
1843 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1844 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001845 }
1846
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001847 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001848 // Complete user switch
1849 if (startingUsers != null) {
1850 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001851 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001852 }
1853 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001854 }
1855
1856 mService.trimApplications();
1857 //dump();
1858 //mWindowManager.dump();
1859
Craig Mautnerf3333272013-04-22 10:55:53 -07001860 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001861 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001862 }
1863
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001864 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001865 }
1866
Craig Mautner8e569572013-10-11 17:36:59 -07001867 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001868 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001869 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1870 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001871 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1872 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1873 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001874 }
Craig Mautner19091252013-10-05 00:03:53 -07001875 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001876 }
1877
1878 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001879 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1880 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1882 stacks.get(stackNdx).closeSystemDialogsLocked();
1883 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001884 }
1885 }
1886
Craig Mautner93529a42013-10-04 15:03:13 -07001887 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001888 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001889 }
1890
Craig Mautner8d341ef2013-03-26 09:03:27 -07001891 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001892 * Update the last used stack id for non-current user (current user's last
1893 * used stack is the focused stack)
1894 */
1895 void updateUserStackLocked(int userId, ActivityStack stack) {
1896 if (userId != mCurrentUser) {
1897 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1898 }
1899 }
1900
1901 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001902 * @return true if some activity was finished (or would have finished if doit were true).
1903 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001904 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1905 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001906 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001907 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1908 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001909 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001910 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001911 if (stack.finishDisabledPackageActivitiesLocked(
1912 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001913 didSomething = true;
1914 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001915 }
1916 }
1917 return didSomething;
1918 }
1919
Dianne Hackborna413dc02013-07-12 12:02:55 -07001920 void updatePreviousProcessLocked(ActivityRecord r) {
1921 // Now that this process has stopped, we may want to consider
1922 // it to be the previous app to try to keep around in case
1923 // the user wants to return to it.
1924
1925 // First, found out what is currently the foreground app, so that
1926 // we don't blow away the previous app if this activity is being
1927 // hosted by the process that is actually still the foreground.
1928 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1930 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001931 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1932 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001933 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001934 if (stack.mResumedActivity != null) {
1935 fgApp = stack.mResumedActivity.app;
1936 } else if (stack.mPausingActivity != null) {
1937 fgApp = stack.mPausingActivity.app;
1938 }
1939 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001940 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001941 }
1942 }
1943
1944 // Now set this one as the previous process, only if that really
1945 // makes sense to.
1946 if (r.app != null && fgApp != null && r.app != fgApp
1947 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001948 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001949 mService.mPreviousProcess = r.app;
1950 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1951 }
1952 }
1953
Wale Ogunwaled046a012015-12-24 13:05:59 -08001954 boolean resumeFocusedStackTopActivityLocked() {
1955 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001956 }
1957
Wale Ogunwaled046a012015-12-24 13:05:59 -08001958 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001959 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001960 if (targetStack != null && isFocusedStack(targetStack)) {
1961 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001962 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001963 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1964 if (r == null || r.state != RESUMED) {
1965 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001966 } else if (r.state == RESUMED) {
1967 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1968 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001969 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001970 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001971 }
1972
Todd Kennedy39bfee52016-02-24 10:28:21 -08001973 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1975 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1977 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1978 }
1979 }
1980 }
1981
Adrian Roos20d7df32016-01-12 18:59:43 +01001982 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1983 TaskRecord finishedTask = null;
1984 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001985 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1986 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001987 final int numStacks = stacks.size();
1988 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1989 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001990 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1991 if (stack == focusedStack || finishedTask == null) {
1992 finishedTask = t;
1993 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001994 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001995 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001996 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001997 }
1998
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001999 void finishVoiceTask(IVoiceInteractionSession session) {
2000 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2001 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2002 final int numStacks = stacks.size();
2003 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2004 final ActivityStack stack = stacks.get(stackNdx);
2005 stack.finishVoiceTask(session);
2006 }
2007 }
2008 }
2009
Andrii Kulianc27916642016-04-12 17:59:27 -07002010 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2011 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002012 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2013 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002014 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002015 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2016 // Caller wants the home activity moved with it. To accomplish this,
2017 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002018 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002019 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002020 ActivityStack currentStack = task.getStack();
2021 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002022 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2023 + task + " to front. Stack is null");
2024 return;
2025 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002026
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002027 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002028 int stackId = options.getLaunchStackId();
2029 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002030 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002031 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002032 if (stackId == INVALID_STACK_ID) {
2033 stackId = task.getLaunchStackId();
2034 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002035 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002036 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2037 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002038 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002039 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2040 // still need moveTaskToFrontLocked() below for any transition settings.
2041 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002042 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2043 resizeStackLocked(stackId, bounds,
2044 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002045 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002046 } else {
2047 // WM resizeTask must be done after the task is moved to the correct stack,
2048 // because Task's setBounds() also updates dim layer's bounds, but that has
2049 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002050 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002051 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002052 }
2053 }
2054
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002055 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002056 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002057 r == null ? null : r.appTimeTracker, reason);
2058
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002059 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002060 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002061
Andrii Kulian02b7a832016-10-06 23:11:56 -07002062 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002063 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002064 }
2065
Wale Ogunwale854809c2015-12-27 16:18:19 -08002066 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002067 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002068 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002069 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002070 return false;
2071 }
2072 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2073 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002074 }
2075
Winson Chung55893332017-02-17 17:13:10 -08002076 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002077 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002078 }
2079
Winson Chung55893332017-02-17 17:13:10 -08002080 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2081 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002082 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002083 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002084 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002085 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002086 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002087 return null;
2088 }
Matthew Ng330757d2017-02-28 14:19:17 -08002089 if (stackId == DOCKED_STACK_ID) {
2090 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2091 // the other side of the docked stack and we make visibility decisions based on that.
2092 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2093 }
Winson Chung55893332017-02-17 17:13:10 -08002094 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002095 }
2096
Andrii Kulian16802aa2016-11-02 12:21:33 -07002097 /**
2098 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2099 * If there is no such stack, new dynamic stack can be created.
2100 * @param displayId Target display.
2101 * @param r Activity that should be launched there.
2102 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2103 */
2104 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2105 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2106 if (activityDisplay == null) {
2107 throw new IllegalArgumentException(
2108 "Display with displayId=" + displayId + " not found.");
2109 }
2110
2111 // Return the topmost valid stack on the display.
2112 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2113 final ActivityStack stack = activityDisplay.mStacks.get(i);
2114 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2115 return stack;
2116 }
2117 }
2118
2119 // If there is no valid stack on the external display - check if new dynamic stack will do.
2120 if (displayId != Display.DEFAULT_DISPLAY) {
2121 final int newDynamicStackId = getNextStackId();
2122 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2123 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2124 }
2125 }
2126
2127 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2128 return null;
2129 }
2130
Craig Mautner967212c2013-04-13 21:10:58 -07002131 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002132 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002133 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2134 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002135 }
2136 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002137 }
2138
Jorim Jaggife762342016-10-13 14:33:27 +02002139 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2140 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2141 }
2142
Andrii Kulian7fc22812016-12-28 13:04:11 -08002143 /**
2144 * Get next focusable stack in the system. This will search across displays and stacks
2145 * in last-focused order for a focusable and visible stack, different from the target stack.
2146 *
2147 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2148 * searching for next candidate.
2149 * @return Next focusable {@link ActivityStack}, null if not found.
2150 */
2151 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2152 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2153
2154 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2155 final int displayId = mTmpOrderedDisplayIds.get(i);
2156 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2157 if (stacks == null) {
2158 continue;
2159 }
2160 for (int j = stacks.size() - 1; j >= 0; --j) {
2161 final ActivityStack stack = stacks.get(j);
2162 if (stack != currentFocus && stack.isFocusable()
2163 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2164 return stack;
2165 }
2166 }
2167 }
2168
2169 return null;
2170 }
2171
Craig Mautneree2e45a2014-06-27 12:10:03 -07002172 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002173 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002174 }
2175
2176 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002177 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2178 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2179 final TaskRecord task = tasks.get(taskNdx);
2180 if (task.isHomeTask()) {
2181 final ArrayList<ActivityRecord> activities = task.mActivities;
2182 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2183 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002184 if (r.isHomeActivity()
2185 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002186 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002187 }
2188 }
2189 }
2190 }
2191 return null;
2192 }
2193
Chong Zhangb15758a2015-11-17 12:12:03 -08002194 /**
2195 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2196 * the docked stack itself, or if it's side-by-side to the docked stack.
2197 */
2198 boolean isStackDockedInEffect(int stackId) {
2199 return stackId == DOCKED_STACK_ID ||
2200 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2201 }
2202
Todd Kennedyca4d8422015-01-15 15:19:22 -08002203 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002204 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002205 ActivityContainer activityContainer =
2206 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002207 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002208 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2209 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002210 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002211 return activityContainer;
2212 }
2213
Craig Mautner34b73df2014-01-12 21:11:08 -08002214 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002215 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2216 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2217 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002218 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2219 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002220 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002221 }
2222 }
2223
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002224 void deleteActivityContainerRecord(int stackId) {
2225 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2226 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2227 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002228 }
2229
Jorim Jaggidc249c42015-12-15 14:57:31 -08002230 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002231 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002232 if (stackId == DOCKED_STACK_ID) {
2233 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002234 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002235 return;
2236 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002237 final ActivityStack stack = getStack(stackId);
2238 if (stack == null) {
2239 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2240 return;
2241 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002242
Jorim Jaggidc249c42015-12-15 14:57:31 -08002243 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002244 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2245 // stack size changing so things don't get out of sync.
2246 return;
2247 }
2248
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002249 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002250 mWindowManager.deferSurfaceLayout();
2251 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002252 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002253 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002254 stack.ensureVisibleActivitiesConfigurationLocked(
2255 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002256 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002257 } finally {
2258 mWindowManager.continueSurfaceLayout();
2259 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002260 }
2261 }
2262
Jorim Jaggi192086e2016-03-11 17:17:03 +01002263 void deferUpdateBounds(int stackId) {
2264 final ActivityStack stack = getStack(stackId);
2265 if (stack != null) {
2266 stack.deferUpdateBounds();
2267 }
2268 }
2269
2270 void continueUpdateBounds(int stackId) {
2271 final ActivityStack stack = getStack(stackId);
2272 if (stack != null) {
2273 stack.continueUpdateBounds();
2274 }
2275 }
2276
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002277 void notifyAppTransitionDone() {
2278 continueUpdateBounds(HOME_STACK_ID);
2279 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2280 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002281 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002282 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002283 if (task != null) {
2284 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002285 }
2286 }
2287 mResizingTasksDuringAnimation.clear();
2288 }
2289
Robert Carr6914f082017-03-20 19:04:30 -07002290 private class MoveTaskToFullscreenArgs {
2291 public int fromStackId;
2292 public boolean onTop;
2293 };
2294 // Used only to closure over the arguments to moveTasksToFullscreenStack without
2295 // allocation
2296 private MoveTaskToFullscreenArgs mMoveToFullscreenArgs = new MoveTaskToFullscreenArgs();
2297
2298 private void moveTasksToFullscreenStackInnerLocked() {
2299 int fromStackId = mMoveToFullscreenArgs.fromStackId;
2300 boolean onTop = mMoveToFullscreenArgs.onTop;
2301
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002302 final ActivityStack stack = getStack(fromStackId);
2303 if (stack == null) {
2304 return;
2305 }
2306
2307 mWindowManager.deferSurfaceLayout();
2308 try {
2309 if (fromStackId == DOCKED_STACK_ID) {
2310
2311 // We are moving all tasks from the docked stack to the fullscreen stack,
2312 // which is dismissing the docked stack, so resize all other stacks to
2313 // fullscreen here already so we don't end up with resize trashing.
2314 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2315 if (StackId.isResizeableByDockedStack(i)) {
2316 ActivityStack otherStack = getStack(i);
2317 if (otherStack != null) {
2318 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2319 true /* allowResizeInDockedMode */, DEFER_RESUME);
2320 }
2321 }
2322 }
2323
2324 // Also disable docked stack resizing since we have manually adjusted the
2325 // size of other stacks above and we don't want to trigger a docked stack
2326 // resize when we remove task from it below and it is detached from the
2327 // display because it no longer contains any tasks.
2328 mAllowDockedStackResize = false;
2329 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002330 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002331 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2332 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002333 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2334 final int size = tasks.size();
2335 if (onTop) {
2336 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002337 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002338 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002339 if (fromStackId == PINNED_STACK_ID) {
2340 // Update the return-to to reflect where the pinned stack task was moved
2341 // from so that we retain the stack that was previously visible if the
2342 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002343 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2344 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002345 MetricsLogger.action(mService.mContext,
2346 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002347 }
Winson Chung74666102017-02-22 17:49:24 -08002348 // Defer resume until all the tasks have been moved to the fullscreen stack
2349 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2350 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
2351 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002352 }
2353 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002354 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002355 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002356 final int position = fullscreenStack != null
2357 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2358 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002359 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung74666102017-02-22 17:49:24 -08002360 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
2361 DEFER_RESUME, "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002362 }
2363 }
Winson Chung74666102017-02-22 17:49:24 -08002364
2365 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2366 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002367 } finally {
2368 mAllowDockedStackResize = true;
2369 mWindowManager.continueSurfaceLayout();
2370 }
2371 }
2372
Robert Carr6914f082017-03-20 19:04:30 -07002373 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2374 mMoveToFullscreenArgs.fromStackId = fromStackId;
2375 mMoveToFullscreenArgs.onTop = onTop;
2376
2377 mWindowManager.inSurfaceTransaction(this::moveTasksToFullscreenStackInnerLocked);
2378 }
2379
Jorim Jaggidc249c42015-12-15 14:57:31 -08002380 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002381 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2382 boolean preserveWindows) {
2383 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2384 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2385 false /* deferResume */);
2386 }
2387
2388 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2389 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2390 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002391
2392 if (!mAllowDockedStackResize) {
2393 // Docked stack resize currently disabled.
2394 return;
2395 }
2396
Jorim Jaggidc249c42015-12-15 14:57:31 -08002397 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2398 if (stack == null) {
2399 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2400 return;
2401 }
2402
2403 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2404 mWindowManager.deferSurfaceLayout();
2405 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002406 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2407 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002408 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002409 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002410
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002411 // TODO: Checking for isAttached might not be needed as if the user passes in null
2412 // dockedBounds then they want the docked stack to be dismissed.
2413 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2414 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002415 // In this case we make all other static stacks fullscreen and move all
2416 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002417 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002418
2419 // stack shouldn't contain anymore activities, so nothing to resume.
2420 r = null;
2421 } else {
2422 // Docked stacks occupy a dedicated region on screen so the size of all other
2423 // static stacks need to be adjusted so they don't overlap with the docked stack.
2424 // We get the bounds to use from window manager which has been adjusted for any
2425 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002426 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002427 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002428 final ActivityStack current = getStack(i);
2429 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002430 current.getStackDockedModeBounds(
2431 tempOtherTaskBounds /* currentTempTaskBounds */,
2432 tempRect /* outStackBounds */,
2433 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2434
Matthew Nge15352e2016-12-20 15:36:29 -08002435 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002436 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2437 tempOtherTaskInsetBounds, preserveWindows,
2438 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002439 }
2440 }
2441 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002442 if (!deferResume) {
2443 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2444 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002445 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002446 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002447 mWindowManager.continueSurfaceLayout();
2448 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2449 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002450 }
2451
Robert Carr0d00c2e2016-02-29 17:45:02 -08002452 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2453 final ActivityStack stack = getStack(PINNED_STACK_ID);
2454 if (stack == null) {
2455 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2456 return;
2457 }
2458 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2459 mWindowManager.deferSurfaceLayout();
2460 try {
2461 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002462 Rect insetBounds = null;
2463 if (tempPinnedTaskBounds != null) {
2464 // We always use 0,0 as the position for the inset rect because
2465 // if we are getting insets at all in the pinned stack it must mean
2466 // we are headed for fullscreen.
2467 insetBounds = tempRect;
2468 insetBounds.top = 0;
2469 insetBounds.left = 0;
2470 insetBounds.right = tempPinnedTaskBounds.width();
2471 insetBounds.bottom = tempPinnedTaskBounds.height();
2472 }
2473 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002474 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002475 } finally {
2476 mWindowManager.continueSurfaceLayout();
2477 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2478 }
2479 }
2480
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002481 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002482 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002483 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002484 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002485 }
2486
Wale Ogunwale1666e312016-12-16 11:27:18 -08002487 final ActivityContainer activityContainer =
2488 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002489 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002490 }
2491
Robert Carr6914f082017-03-20 19:04:30 -07002492
2493 // Used only to closure over the argument to removeStack without allocation.
2494 private int mRemoveStackStackId;
2495 void removeStackInnerLocked() {
2496 int stackId = mRemoveStackStackId;
2497
Winson Chung010927a2016-12-15 16:12:35 -08002498 final ActivityStack stack = getStack(stackId);
2499 if (stack == null) {
2500 return;
2501 }
2502
2503 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2504 if (stack.getStackId() == PINNED_STACK_ID) {
2505 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2506 if (fullscreenStack != null) {
2507 final boolean isFullscreenStackVisible =
2508 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2509 for (int i = 0; i < tasks.size(); i++) {
2510 // Insert the task either at the top of the fullscreen stack if it is hidden,
2511 // or just under the top task if it is currently visible
2512 final int insertPosition = isFullscreenStackVisible
2513 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2514 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002515 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002516 // Defer resume until we remove all the tasks
2517 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2518 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002519 }
2520 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2521 resumeFocusedStackTopActivityLocked();
2522 } else {
2523 // If there is no fullscreen stack, then create the stack and move all the tasks
2524 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002525 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002526 }
2527 } else {
2528 for (int i = tasks.size() - 1; i >= 0; i--) {
2529 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2530 REMOVE_FROM_RECENTS);
2531 }
2532 }
2533 }
2534
2535 /**
Robert Carr6914f082017-03-20 19:04:30 -07002536 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2537 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2538 * instead moved back onto the fullscreen stack.
2539 */
2540 void removeStackLocked(int stackId) {
2541 mRemoveStackStackId = stackId;
2542 mWindowManager.inSurfaceTransaction(this::removeStackInnerLocked);
2543 }
2544
2545 /**
Winson Chung010927a2016-12-15 16:12:35 -08002546 * Removes the task with the specified task id.
2547 *
2548 * @param taskId Identifier of the task to be removed.
2549 * @param killProcess Kill any process associated with the task if possible.
2550 * @param removeFromRecents Whether to also remove the task from recents.
2551 * @return Returns true if the given task was found and removed.
2552 */
2553 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
Winson Chung7900bee2017-03-10 08:59:25 -08002554 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2555 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002556 if (tr != null) {
2557 tr.removeTaskActivitiesLocked();
2558 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2559 if (tr.isPersistable) {
2560 mService.notifyTaskPersisterLocked(null, true);
2561 }
2562 return true;
2563 }
2564 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2565 return false;
2566 }
2567
2568 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2569 if (removeFromRecents) {
2570 mRecentTasks.remove(tr);
2571 tr.removedFromRecents();
2572 }
2573 ComponentName component = tr.getBaseIntent().getComponent();
2574 if (component == null) {
2575 Slog.w(TAG, "No component for base intent of task: " + tr);
2576 return;
2577 }
2578
2579 // Find any running services associated with this app and stop if needed.
2580 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2581
2582 if (!killProcess) {
2583 return;
2584 }
2585
2586 // Determine if the process(es) for this task should be killed.
2587 final String pkg = component.getPackageName();
2588 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2589 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2590 for (int i = 0; i < pmap.size(); i++) {
2591
2592 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2593 for (int j = 0; j < uids.size(); j++) {
2594 ProcessRecord proc = uids.valueAt(j);
2595 if (proc.userId != tr.userId) {
2596 // Don't kill process for a different user.
2597 continue;
2598 }
2599 if (proc == mService.mHomeProcess) {
2600 // Don't kill the home process along with tasks from the same package.
2601 continue;
2602 }
2603 if (!proc.pkgList.containsKey(pkg)) {
2604 // Don't kill process that is not associated with this task.
2605 continue;
2606 }
2607
2608 for (int k = 0; k < proc.activities.size(); k++) {
2609 TaskRecord otherTask = proc.activities.get(k).task;
2610 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2611 // Don't kill process(es) that has an activity in a different task that is
2612 // also in recents.
2613 return;
2614 }
2615 }
2616
2617 if (proc.foregroundServices) {
2618 // Don't kill process(es) with foreground service.
2619 return;
2620 }
2621
2622 // Add process to kill list.
2623 procsToKill.add(proc);
2624 }
2625 }
2626
2627 // Kill the running processes.
2628 for (int i = 0; i < procsToKill.size(); i++) {
2629 ProcessRecord pr = procsToKill.get(i);
2630 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2631 && pr.curReceivers.isEmpty()) {
2632 pr.kill("remove task", true);
2633 } else {
2634 // We delay killing processes that are not in the background or running a receiver.
2635 pr.waitingToKill = "remove task";
2636 }
2637 }
2638 }
2639
Craig Mautner4a1cb222013-12-04 16:14:06 -08002640 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002641 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002642 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2643 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002644 break;
2645 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002646 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002647 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002648 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002649 }
2650
Chong Zhang5dcb2752015-08-18 13:50:26 -07002651 /**
2652 * Restores a recent task to a stack
2653 * @param task The recent task to be restored.
2654 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002655 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2656 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002657 * @return true if the task has been restored successfully.
2658 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002659 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002660 if (!StackId.isStaticStack(stackId)) {
2661 // If stack is not static (or stack id is invalid) - use the default one.
2662 // This means that tasks that were on external displays will be restored on the
2663 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002664 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002665 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002666 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2667 // Put it in the fullscreen stack.
2668 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002669 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002670
Andrii Kulian02b7a832016-10-06 23:11:56 -07002671 final ActivityStack currentStack = task.getStack();
2672 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002673 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002674 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002675 // Nothing else to do since it is already restored in the right stack.
2676 return true;
2677 }
2678 // Remove current stack association, so we can re-associate the task with the
2679 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002680 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002681 }
2682
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002683 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002684 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002685
2686 if (stack == null) {
2687 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002688 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2689 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002690 return false;
2691 }
2692
Wale Ogunwale72919d22016-12-08 18:58:50 -08002693 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002694 // TODO: move call for creation here and other place into Stack.addTask()
2695 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002696 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2697 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002698 final ArrayList<ActivityRecord> activities = task.mActivities;
2699 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002700 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002701 }
2702 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002703 }
2704
Wale Ogunwale040b4702015-08-06 18:10:50 -07002705 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002706 * Move stack with all its existing content to specified display.
2707 * @param stackId Id of stack to move.
2708 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002709 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002710 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002711 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002712 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2713 if (activityDisplay == null) {
2714 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2715 + displayId);
2716 }
2717 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2718 if (activityContainer != null) {
2719 if (activityContainer.isAttachedLocked()) {
2720 if (activityContainer.getDisplayId() == displayId) {
2721 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2722 + " to its current displayId=" + displayId);
2723 }
2724
Andrii Kulian250d6532017-02-08 23:30:45 -08002725 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002726 } else {
2727 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2728 + stackId + " is not attached to any display.");
2729 }
2730 } else {
2731 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2732 + stackId);
2733 }
2734
2735 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2736 !PRESERVE_WINDOWS);
2737 // TODO(multi-display): resize stacks properly if moved from split-screen.
2738 }
2739
2740 /**
Winson Chung74666102017-02-22 17:49:24 -08002741 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2742 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002743 */
Winson Chung74666102017-02-22 17:49:24 -08002744 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002745 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002746
Winson Chung74666102017-02-22 17:49:24 -08002747 // Check that we aren't reparenting to the same stack that the task is already in
2748 if (prevStack != null && prevStack.mStackId == stackId) {
2749 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2750 + " already in stackId=" + stackId);
2751 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002752 }
2753
Winson Chung74666102017-02-22 17:49:24 -08002754 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2755 // support
2756 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2757 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2758 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002759 }
2760
Winson Chung74666102017-02-22 17:49:24 -08002761 // Ensure that we aren't trying to move into a freeform stack without freeform
2762 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002763 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002764 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2765 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002766 }
2767
Winson Chung74666102017-02-22 17:49:24 -08002768 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2769 // used for split-screen mode and will cause things like the docked divider to show up. We
2770 // instead leave the task in its current stack or move it to the fullscreen stack if it
2771 // isn't currently in a stack.
2772 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2773 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2774 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2775 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002776 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002777
Winson Chung74666102017-02-22 17:49:24 -08002778 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2779 // example, a docked stack is created which will lead to the stack we are moving from being
2780 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002781 try {
Winson Chung74666102017-02-22 17:49:24 -08002782 task.mTemporarilyUnresizable = true;
2783 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002784 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002785 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002786 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002787 }
2788
Wale Ogunwale079a0042015-10-24 11:44:07 -07002789 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2790 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2791 if (stack == null) {
2792 throw new IllegalArgumentException(
2793 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2794 }
2795
2796 final ActivityRecord r = stack.topRunningActivityLocked();
2797 if (r == null) {
2798 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2799 + " in stack=" + stack);
2800 return false;
2801 }
2802
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002803 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002804 Slog.w(TAG,
2805 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002806 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002807 return false;
2808 }
2809
Winson Chungb5c41b72016-12-07 15:00:47 -08002810 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2811 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002812 return true;
2813 }
2814
Winson Chungb5c41b72016-12-07 15:00:47 -08002815 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2816 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002817 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002818
Wale Ogunwale1666e312016-12-16 11:27:18 -08002819 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002820 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002821
Wale Ogunwale480dca02016-02-06 13:58:29 -08002822 try {
2823 final TaskRecord task = r.task;
2824
Andrii Kulian02b7a832016-10-06 23:11:56 -07002825 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002826 // An activity can't be pinned and visible behind at the same time. Go ahead and
2827 // release it from been visible behind before pinning.
2828 requestVisibleBehindLocked(r, false);
2829 }
2830
Wale Ogunwale480dca02016-02-06 13:58:29 -08002831 // Resize the pinned stack to match the current size of the task the activity we are
2832 // going to be moving is currently contained in. We do this to have the right starting
2833 // animation bounds for the pinned stack to the desired bounds the caller wants.
2834 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2835 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002836 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002837
2838 if (task.mActivities.size() == 1) {
2839 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002840 // the stack without re-parenting the activity in a different task. We don't
2841 // move the home stack forward if we are currently entering picture-in-picture
2842 // while pausing because that changes the focused stack and may prevent the new
2843 // starting activity from resuming.
2844 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002845 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002846 // Move the home stack forward if the task we just moved to the pinned stack
2847 // was launched from home so home should be visible behind it.
2848 moveHomeStackToFront(reason);
2849 }
Winson Chung74666102017-02-22 17:49:24 -08002850 // Defer resume until below
2851 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2852 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002853 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002854 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002855 // reveal/leave the other activities in their original task.
2856
2857 // Currently, we don't support reparenting activities across tasks in two different
2858 // stacks, so instead, just create a new task in the same stack, reparent the
2859 // activity into that task, and then reparent the whole task to the new stack. This
2860 // ensures that all the necessary work to migrate states in the old and new stacks
2861 // is also done.
2862 final TaskRecord newTask = task.getStack().createTaskRecord(
2863 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2864 r.mActivityType);
2865 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2866
2867 // Defer resume until below
2868 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2869 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002870 }
Winson Chungc2baac02017-01-11 13:34:47 -08002871
2872 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2873 // to the pinned stack
2874 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002875 } finally {
2876 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002877 }
2878
Wale Ogunwale480dca02016-02-06 13:58:29 -08002879 // The task might have already been running and its visibility needs to be synchronized
2880 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002881 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002882 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002883
Winson Chung55893332017-02-17 17:13:10 -08002884 stack.animateResizePinnedStack(bounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002885 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002886 }
2887
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002888 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002889 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2890 if (r == null || !r.isFocusable()) {
2891 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2892 "moveActivityStackToFront: unfocusable r=" + r);
2893 return false;
2894 }
2895
2896 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002897 final ActivityStack stack = r.getStack();
2898 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002899 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2900 + r + " task=" + task);
2901 return false;
2902 }
2903
Chong Zhang6cda19c2016-06-14 19:07:56 -07002904 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2905 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2906 "moveActivityStackToFront: already on top, r=" + r);
2907 return false;
2908 }
2909
2910 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2911 "moveActivityStackToFront: r=" + r);
2912
2913 stack.moveToFront(reason, task);
2914 return true;
2915 }
2916
David Stevensc6b91c62017-02-08 14:23:58 -08002917 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002918 mTmpFindTaskResult.r = null;
2919 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002920 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002921 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2923 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2925 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002926 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002927 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002928 continue;
2929 }
2930 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002931 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2932 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 continue;
2934 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002935 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002936 // It is possible to have tasks in multiple stacks with the same root affinity, so
2937 // we should keep looking after finding an affinity match to see if there is a
2938 // better match in another stack. Also, task affinity isn't a good enough reason
2939 // to target a display which isn't the source of the intent, so skip any affinity
2940 // matches not on the specified display.
2941 if (mTmpFindTaskResult.r != null) {
2942 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2943 return mTmpFindTaskResult.r;
2944 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2945 affinityMatch = mTmpFindTaskResult.r;
2946 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002947 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002948 }
2949 }
David Stevensc6b91c62017-02-08 14:23:58 -08002950 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2951 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002952 }
2953
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002954 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2955 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002956 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2957 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002959 final ActivityRecord ar = stacks.get(stackNdx)
2960 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002961 if (ar != null) {
2962 return ar;
2963 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002964 }
2965 }
2966 return null;
2967 }
2968
Craig Mautner8d341ef2013-03-26 09:03:27 -07002969 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002970 scheduleSleepTimeout();
2971 if (!mGoingToSleep.isHeld()) {
2972 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002973 if (mLaunchingActivity.isHeld()) {
2974 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2975 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002976 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002977 mLaunchingActivity.release();
2978 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002979 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002980 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002981 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 }
2983
2984 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002985 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002986
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002987 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002988 final long endTime = System.currentTimeMillis() + timeout;
2989 while (true) {
2990 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002991 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2992 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002993 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2994 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2995 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 }
2997 if (cantShutdown) {
2998 long timeRemaining = endTime - System.currentTimeMillis();
2999 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003000 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003001 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003002 } catch (InterruptedException e) {
3003 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003004 } else {
3005 Slog.w(TAG, "Activity manager shutdown timed out");
3006 timedout = true;
3007 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003008 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003009 } else {
3010 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003011 }
3012 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013
3014 // Force checkReadyForSleep to complete.
3015 mSleepTimeout = true;
3016 checkReadyForSleepLocked();
3017
Craig Mautner8d341ef2013-03-26 09:03:27 -07003018 return timedout;
3019 }
3020
3021 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003022 removeSleepTimeouts();
3023 if (mGoingToSleep.isHeld()) {
3024 mGoingToSleep.release();
3025 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003026 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3027 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003028 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3029 final ActivityStack stack = stacks.get(stackNdx);
3030 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003031 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003032 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003033 }
Craig Mautner5314a402013-09-26 12:40:16 -07003034 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003036 mGoingToSleepActivities.clear();
3037 }
3038
3039 void activitySleptLocked(ActivityRecord r) {
3040 mGoingToSleepActivities.remove(r);
3041 checkReadyForSleepLocked();
3042 }
3043
3044 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003045 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003046 // Do not care.
3047 return;
3048 }
3049
3050 if (!mSleepTimeout) {
3051 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003052 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3053 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3055 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3056 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003057 }
3058
3059 if (mStoppingActivities.size() > 0) {
3060 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003061 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003062 + mStoppingActivities.size() + " activities");
3063 scheduleIdleLocked();
3064 dontSleep = true;
3065 }
3066
3067 if (mGoingToSleepActivities.size() > 0) {
3068 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003069 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003070 + mGoingToSleepActivities.size() + " activities");
3071 dontSleep = true;
3072 }
3073
3074 if (dontSleep) {
3075 return;
3076 }
3077 }
3078
Wei Wang65c7a152016-06-02 18:51:22 -07003079 // Send launch end powerhint before going sleep
3080 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3081
Craig Mautnere0a38842013-12-16 16:14:02 -08003082 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3083 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003084 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3085 stacks.get(stackNdx).goToSleep();
3086 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003087 }
3088
3089 removeSleepTimeouts();
3090
3091 if (mGoingToSleep.isHeld()) {
3092 mGoingToSleep.release();
3093 }
3094 if (mService.mShuttingDown) {
3095 mService.notifyAll();
3096 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003097 }
3098
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003099 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003100 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003101 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003102 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003103 }
3104 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003105 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003106 mWindowManager.executeAppTransition();
3107 return true;
3108 }
3109 return false;
3110 }
3111
Craig Mautner8d341ef2013-03-26 09:03:27 -07003112 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003113 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3114 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003115 int stackNdx = stacks.size() - 1;
3116 while (stackNdx >= 0) {
3117 stacks.get(stackNdx).handleAppCrashLocked(app);
3118 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 }
3121 }
3122
Jose Lima4b6c6692014-08-12 17:41:12 -07003123 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003124 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003125 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003126 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3127 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003128 return false;
3129 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003130
3131 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3132 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3133 + " visible=" + visible + " stackId=" + stack.mStackId
3134 + " can't contain visible behind activities");
3135 return false;
3136 }
3137
Jose Lima4b6c6692014-08-12 17:41:12 -07003138 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003139 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3140 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003141
3142 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003143 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003144 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003145 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003146 return true;
3147 }
3148
3149 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003150 if (visible && top.fullscreen) {
3151 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003152 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3153 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3154 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3155 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003156 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003157 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3158 // Only the activity set as currently visible behind should actively reset its
3159 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003160 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3161 "requestVisibleBehind: returning visible=" + visible
3162 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3163 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003164 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003165 }
3166
Jose Lima4b6c6692014-08-12 17:41:12 -07003167 stack.setVisibleBehindActivity(visible ? r : null);
3168 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003169 // If there is a translucent home activity, we need to force it stop being translucent,
3170 // because we can't depend on the application to necessarily perform that operation.
3171 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003172 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003173 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003174 mService.convertFromTranslucent(next.appToken);
3175 }
3176 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003177 if (top.app != null && top.app.thread != null) {
3178 // Notify the top app of the change.
3179 try {
3180 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3181 } catch (RemoteException e) {
3182 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003183 }
3184 return true;
3185 }
3186
Craig Mautnerbb742462014-07-07 15:28:55 -07003187 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003188 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003189 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003190 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003191
3192 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003193 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003194 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003195 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003196 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003197
3198 // When launching tasks behind, update the last active time of the top task after the new
3199 // task has been shown briefly
3200 final ActivityRecord top = stack.topActivity();
3201 if (top != null) {
3202 top.task.touchActiveTime();
3203 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003204 }
3205
3206 void scheduleLaunchTaskBehindComplete(IBinder token) {
3207 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3208 }
3209
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003210 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3211 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003212 mKeyguardController.beginActivityVisibilityUpdate();
3213 try {
3214 // First the front stacks. In case any are not fullscreen and are in front of home.
3215 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3216 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3217 final int topStackNdx = stacks.size() - 1;
3218 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3219 final ActivityStack stack = stacks.get(stackNdx);
3220 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3221 }
Craig Mautner580ea812013-04-25 12:58:38 -07003222 }
Jorim Jaggife762342016-10-13 14:33:27 +02003223 } finally {
3224 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003225 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003226 }
3227
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003228 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3229 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3230 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3231 final int topStackNdx = stacks.size() - 1;
3232 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3233 final ActivityStack stack = stacks.get(stackNdx);
3234 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3235 }
3236 }
3237 }
3238
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003239 void invalidateTaskLayers() {
3240 mTaskLayersChanged = true;
3241 }
3242
3243 void rankTaskLayersIfNeeded() {
3244 if (!mTaskLayersChanged) {
3245 return;
3246 }
3247 mTaskLayersChanged = false;
3248 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3249 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3250 int baseLayer = 0;
3251 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3252 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3253 }
3254 }
3255 }
3256
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003257 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3258 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3259 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3260 final int topStackNdx = stacks.size() - 1;
3261 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3262 final ActivityStack stack = stacks.get(stackNdx);
3263 stack.clearOtherAppTimeTrackers(except);
3264 }
3265 }
3266 }
3267
Craig Mautner8d341ef2013-03-26 09:03:27 -07003268 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003269 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3270 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003271 final int numStacks = stacks.size();
3272 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3273 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003274 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003275 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003276 }
3277 }
3278
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003279 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3280 // Examine all activities currently running in the process.
3281 TaskRecord firstTask = null;
3282 // Tasks is non-null only if two or more tasks are found.
3283 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003284 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3285 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003286 ActivityRecord r = app.activities.get(i);
3287 // First, if we find an activity that is in the process of being destroyed,
3288 // then we just aren't going to do anything for now; we want things to settle
3289 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003290 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003291 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003292 return;
3293 }
3294 // Don't consider any activies that are currently not in a state where they
3295 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003296 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3297 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003298 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003299 continue;
3300 }
3301 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003302 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003303 + " from " + r);
3304 if (firstTask == null) {
3305 firstTask = r.task;
3306 } else if (firstTask != r.task) {
3307 if (tasks == null) {
3308 tasks = new ArraySet<>();
3309 tasks.add(firstTask);
3310 }
3311 tasks.add(r.task);
3312 }
3313 }
3314 }
3315 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003316 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003317 return;
3318 }
3319 // If we have activities in multiple tasks that are in a position to be destroyed,
3320 // let's iterate through the tasks and release the oldest one.
3321 final int numDisplays = mActivityDisplays.size();
3322 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3323 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3324 // Step through all stacks starting from behind, to hit the oldest things first.
3325 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3326 final ActivityStack stack = stacks.get(stackNdx);
3327 // Try to release activities in this stack; if we manage to, we are done.
3328 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3329 return;
3330 }
3331 }
3332 }
3333 }
3334
Amith Yamasani37a40c22015-06-17 13:25:42 -07003335 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003336 final int focusStackId = mFocusedStack.getStackId();
3337 // We dismiss the docked stack whenever we switch users.
3338 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003339 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3340 // also cause all tasks to be moved to the fullscreen stack at a position that is
3341 // appropriate.
3342 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003343
3344 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003345 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003346 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003347
Craig Mautner858d8a62013-04-23 17:08:34 -07003348 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003349 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3350 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003351 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003352 final ActivityStack stack = stacks.get(stackNdx);
3353 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003354 TaskRecord task = stack.topTask();
3355 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003356 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003357 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003358 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003359 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003360
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003361 ActivityStack stack = getStack(restoreStackId);
3362 if (stack == null) {
3363 stack = mHomeStack;
3364 }
3365 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003366 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003367 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003368 } else {
3369 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003370 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003371 }
Craig Mautner93529a42013-10-04 15:03:13 -07003372 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003373 }
3374
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003375 /** Checks whether the userid is a profile of the current user. */
3376 boolean isCurrentProfileLocked(int userId) {
3377 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003378 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003379 }
3380
Winson Chung4dabf232017-01-25 13:25:22 -08003381 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3382 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003383 ArrayList<ActivityRecord> stops = null;
3384
3385 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003386 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3387 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003388 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3389 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003390 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003391 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003392 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3393 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003394 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003395 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003396 if (s.finishing) {
3397 // If this activity is finishing, it is sitting on top of
3398 // everyone else but we now know it is no longer needed...
3399 // so get rid of it. Otherwise, we need to go through the
3400 // normal flow and hide it once we determine that it is
3401 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003402 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003403 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003404 }
3405 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003406 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003407 if (!processPausingActivities && s.state == PAUSING) {
3408 // Defer processing pausing activities in this iteration and reschedule
3409 // a delayed idle to reprocess it again
3410 removeTimeoutsForActivityLocked(idleActivity);
3411 scheduleIdleTimeoutLocked(idleActivity);
3412 continue;
3413 }
3414
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003415 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003416 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003417 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003418 }
3419 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003420 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003421 }
3422 }
3423
3424 return stops;
3425 }
3426
Craig Mautnercf910b02013-04-23 11:23:27 -07003427 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003428 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3429 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3430 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3431 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003432 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003433 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003434 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003435 if (r == null) Slog.e(TAG,
3436 "validateTop...: null top activity, stack=" + stack);
3437 else {
3438 final ActivityRecord pausing = stack.mPausingActivity;
3439 if (pausing != null && pausing == r) Slog.e(TAG,
3440 "validateTop...: top stack has pausing activity r=" + r
3441 + " state=" + state);
3442 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3443 "validateTop...: activity in front not resumed r=" + r
3444 + " state=" + state);
3445 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003446 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003447 final ActivityRecord resumed = stack.mResumedActivity;
3448 if (resumed != null && resumed == r) Slog.e(TAG,
3449 "validateTop...: back stack has resumed activity r=" + r
3450 + " state=" + state);
3451 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3452 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003453 }
3454 }
3455 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003456 }
3457
Craig Mautnere0570202015-05-13 13:06:11 -07003458 private String lockTaskModeToString() {
3459 switch (mLockTaskModeState) {
3460 case LOCK_TASK_MODE_LOCKED:
3461 return "LOCKED";
3462 case LOCK_TASK_MODE_PINNED:
3463 return "PINNED";
3464 case LOCK_TASK_MODE_NONE:
3465 return "NONE";
3466 default: return "unknown=" + mLockTaskModeState;
3467 }
3468 }
3469
Craig Mautner27084302013-03-25 08:05:25 -07003470 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003471 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003472 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003473 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003474 pw.print(prefix);
3475 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003476 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003477 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003478 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003479 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3480 if (packages.size() > 0) {
3481 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3482 for (int i = 0; i < packages.size(); ++i) {
3483 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3484 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3485 }
3486 }
3487 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3488 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003489 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003490
Winson43d1f262016-06-14 16:05:55 -07003491 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003492 * Dump all connected displays' configurations.
3493 * @param prefix Prefix to apply to each line of the dump.
3494 */
3495 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3496 pw.print(prefix); pw.println("Display override configurations:");
3497 final int displayCount = mActivityDisplays.size();
3498 for (int i = 0; i < displayCount; i++) {
3499 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3500 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3501 pw.println(activityDisplay.getOverrideConfiguration());
3502 }
3503 }
3504
3505 /**
Winson43d1f262016-06-14 16:05:55 -07003506 * Dumps the activities matching the given {@param name} in the either the focused stack
3507 * or all visible stacks if {@param dumpVisibleStacks} is true.
3508 */
Winson Chung6998bc42017-02-28 17:07:05 -08003509 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3510 boolean dumpFocusedStackOnly) {
3511 if (dumpFocusedStackOnly) {
3512 return mFocusedStack.getDumpActivitiesLocked(name);
3513 } else {
Winson43d1f262016-06-14 16:05:55 -07003514 ArrayList<ActivityRecord> activities = new ArrayList<>();
3515 int numDisplays = mActivityDisplays.size();
3516 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3517 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3518 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3519 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003520 if (!dumpVisibleStacksOnly ||
3521 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003522 activities.addAll(stack.getDumpActivitiesLocked(name));
3523 }
3524 }
3525 }
3526 return activities;
Winson43d1f262016-06-14 16:05:55 -07003527 }
Craig Mautner20e72272013-04-01 13:45:53 -07003528 }
3529
Dianne Hackborn390517b2013-05-30 15:03:32 -07003530 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3531 boolean needSep, String prefix) {
3532 if (activity != null) {
3533 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3534 if (needSep) {
3535 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003536 }
3537 pw.print(prefix);
3538 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003539 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003540 }
3541 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003542 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003543 }
3544
Craig Mautner8d341ef2013-03-26 09:03:27 -07003545 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3546 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003547 boolean printed = false;
3548 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003549 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3550 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003551 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003552 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003553 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003554 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003555 final ActivityStack stack = stacks.get(stackNdx);
3556 StringBuilder stackHeader = new StringBuilder(128);
3557 stackHeader.append(" Stack #");
3558 stackHeader.append(stack.mStackId);
3559 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003560 stackHeader.append("\n");
3561 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3562 stackHeader.append("\n");
3563 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003564 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3565 needSep, stackHeader.toString());
3566 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3567 !dumpAll, false, dumpPackage, true,
3568 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003569
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 needSep = printed;
3571 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3572 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003573 if (pr) {
3574 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003576 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003577 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3578 " mResumedActivity: ");
3579 if (pr) {
3580 printed = true;
3581 needSep = false;
3582 }
3583 if (dumpAll) {
3584 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3585 " mLastPausedActivity: ");
3586 if (pr) {
3587 printed = true;
3588 needSep = true;
3589 }
3590 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3591 needSep, " mLastNoHistoryActivity: ");
3592 }
3593 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003594 }
3595 }
3596
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003597 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3598 false, dumpPackage, true, " Activities waiting to finish:", null);
3599 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3600 false, dumpPackage, true, " Activities waiting to stop:", null);
3601 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3602 false, dumpPackage, true, " Activities waiting for another to become visible:",
3603 null);
3604 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3605 false, dumpPackage, true, " Activities waiting to sleep:", null);
3606 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3607 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003608
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003609 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003610 }
3611
Dianne Hackborn390517b2013-05-30 15:03:32 -07003612 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003613 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003614 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003615 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003616 String innerPrefix = null;
3617 String[] args = null;
3618 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003619 for (int i=list.size()-1; i>=0; i--) {
3620 final ActivityRecord r = list.get(i);
3621 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3622 continue;
3623 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003624 if (innerPrefix == null) {
3625 innerPrefix = prefix + " ";
3626 args = new String[0];
3627 }
3628 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629 final boolean full = !brief && (complete || !r.isInHistory());
3630 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003631 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003632 needNL = false;
3633 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003634 if (header1 != null) {
3635 pw.println(header1);
3636 header1 = null;
3637 }
3638 if (header2 != null) {
3639 pw.println(header2);
3640 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003641 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003642 if (lastTask != r.task) {
3643 lastTask = r.task;
3644 pw.print(prefix);
3645 pw.print(full ? "* " : " ");
3646 pw.println(lastTask);
3647 if (full) {
3648 lastTask.dump(pw, prefix + " ");
3649 } else if (complete) {
3650 // Complete + brief == give a summary. Isn't that obvious?!?
3651 if (lastTask.intent != null) {
3652 pw.print(prefix); pw.print(" ");
3653 pw.println(lastTask.intent.toInsecureStringWithClip());
3654 }
3655 }
3656 }
3657 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3658 pw.print(" #"); pw.print(i); pw.print(": ");
3659 pw.println(r);
3660 if (full) {
3661 r.dump(pw, innerPrefix);
3662 } else if (complete) {
3663 // Complete + brief == give a summary. Isn't that obvious?!?
3664 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3665 if (r.app != null) {
3666 pw.print(innerPrefix); pw.println(r.app);
3667 }
3668 }
3669 if (client && r.app != null && r.app.thread != null) {
3670 // flush anything that is already in the PrintWriter since the thread is going
3671 // to write to the file descriptor directly
3672 pw.flush();
3673 try {
3674 TransferPipe tp = new TransferPipe();
3675 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003676 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003677 // Short timeout, since blocking here can
3678 // deadlock with the application.
3679 tp.go(fd, 2000);
3680 } finally {
3681 tp.kill();
3682 }
3683 } catch (IOException e) {
3684 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3685 } catch (RemoteException e) {
3686 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3687 }
3688 needNL = true;
3689 }
3690 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003691 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003692 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003693
Craig Mautnerf3333272013-04-22 10:55:53 -07003694 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003695 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3696 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003697 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3698 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003699 }
3700
3701 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003702 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003703 }
3704
3705 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003706 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3707 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003708 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3709 }
3710
Craig Mautner05d29032013-05-03 13:40:13 -07003711 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003712 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3713 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3714 }
Craig Mautner05d29032013-05-03 13:40:13 -07003715 }
3716
Craig Mautner0eea92c2013-05-16 13:35:39 -07003717 void removeSleepTimeouts() {
3718 mSleepTimeout = false;
3719 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3720 }
3721
3722 final void scheduleSleepTimeout() {
3723 removeSleepTimeouts();
3724 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3725 }
3726
Craig Mautner4a1cb222013-12-04 16:14:06 -08003727 @Override
3728 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003729 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003730 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3731 }
3732
3733 @Override
3734 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003735 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003736 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3737 }
3738
3739 @Override
3740 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003741 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3743 }
3744
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003745 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003746 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003747 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003748 newDisplay = mActivityDisplays.get(displayId) == null;
3749 if (newDisplay) {
3750 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003751 if (activityDisplay.mDisplay == null) {
3752 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3753 return;
3754 }
Craig Mautner4504de52013-12-20 09:06:56 -08003755 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003756 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003757 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003758 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003759 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003760 }
3761
Andrii Kulianca007a62016-11-22 16:33:28 -08003762 /** Check if display with specified id is added to the list. */
3763 boolean isDisplayAdded(int displayId) {
3764 return mActivityDisplays.get(displayId) != null;
3765 }
3766
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003767 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003768 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003769 mService.mContext.getResources().getDimensionPixelSize(
3770 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003771 }
3772
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003773 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003774 if (displayId == DEFAULT_DISPLAY) {
3775 throw new IllegalArgumentException("Can't remove the primary display.");
3776 }
3777
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003779 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3780 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003781 final boolean destroyContentOnRemoval
3782 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003783 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3784 while (!stacks.isEmpty()) {
3785 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003786 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003787 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3788 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003789 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003790 } else {
3791 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3792 // a valid launch stack for all activities. This way the task history from
3793 // external display will be preserved on primary after move.
3794 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003795 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003797 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003798 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 }
3800 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 }
3802
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003803 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003805 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3806 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 // TODO: Update the bounds.
3808 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003809 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003810 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003811 }
3812
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003813 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003814 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003816 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003817 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003818 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003819 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003820 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003821 info.position = display != null
3822 ? display.mStacks.indexOf(stack)
3823 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824
3825 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3826 final int numTasks = tasks.size();
3827 int[] taskIds = new int[numTasks];
3828 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003829 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003830 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 for (int i = 0; i < numTasks; ++i) {
3832 final TaskRecord task = tasks.get(i);
3833 taskIds[i] = task.taskId;
3834 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3835 : task.realActivity != null ? task.realActivity.flattenToString()
3836 : task.getTopActivity() != null ? task.getTopActivity().packageName
3837 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003838 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003839 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003840 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 }
3842 info.taskIds = taskIds;
3843 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003844 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003845 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003846
3847 final ActivityRecord top = stack.topRunningActivityLocked();
3848 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849 return info;
3850 }
3851
3852 StackInfo getStackInfoLocked(int stackId) {
3853 ActivityStack stack = getStack(stackId);
3854 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003855 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 }
3857 return null;
3858 }
3859
3860 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003861 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3863 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003865 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 }
3867 }
3868 return list;
3869 }
3870
Craig Mautner15df08a2015-04-01 12:17:18 -07003871 TaskRecord getLockedTaskLocked() {
3872 final int top = mLockTaskModeTasks.size() - 1;
3873 if (top >= 0) {
3874 return mLockTaskModeTasks.get(top);
3875 }
3876 return null;
3877 }
3878
3879 boolean isLockedTask(TaskRecord task) {
3880 return mLockTaskModeTasks.contains(task);
3881 }
3882
3883 boolean isLastLockedTask(TaskRecord task) {
3884 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3885 }
3886
3887 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003888 if (!mLockTaskModeTasks.remove(task)) {
3889 return;
3890 }
3891 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3892 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003893 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003894 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3895 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003896 final Message lockTaskMsg = Message.obtain();
3897 lockTaskMsg.arg1 = task.userId;
3898 lockTaskMsg.what = LOCK_TASK_END_MSG;
3899 mHandler.sendMessage(lockTaskMsg);
3900 }
3901 }
3902
Andrii Kulianc27916642016-04-12 17:59:27 -07003903 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3904 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3905 false /* forceNonResizable */);
3906 }
3907
Jorim Jaggid53f0922016-04-06 22:16:23 -07003908 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003909 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003910 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3911 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003912 return;
3913 }
3914
Jorim Jaggicd13d332016-04-27 15:40:20 -07003915 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003916 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003917 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003918 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003919
Andrii Kulianc27916642016-04-12 17:59:27 -07003920 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3921 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003922 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003923 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003924 && !topActivity.noDisplay) {
3925 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003926 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3927 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003928 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003929 }
3930
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003931 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003932 if (mLockTaskNotify != null) {
3933 mLockTaskNotify.showToast(mLockTaskModeState);
3934 }
Jason Monka8f569c2014-07-07 12:15:21 -04003935 }
3936
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003937 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3938 if (mLockTaskModeTasks.contains(task)) {
3939 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3940 }
3941 }
3942
Craig Mautner432f64e2015-05-20 14:59:57 -07003943 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3944 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003945 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003946 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003947 final TaskRecord lockedTask = getLockedTaskLocked();
3948 if (lockedTask != null) {
3949 removeLockedTaskLocked(lockedTask);
3950 if (!mLockTaskModeTasks.isEmpty()) {
3951 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003952 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3953 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003954 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003955 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003956 return;
3957 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003958 }
Craig Mautnere0570202015-05-13 13:06:11 -07003959 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3960 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003961 return;
3962 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003963
3964 // Should have already been checked, but do it again.
3965 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003966 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3967 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003968 return;
3969 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003971 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003972 return;
3973 }
3974
3975 if (mLockTaskModeTasks.isEmpty()) {
3976 // First locktask.
3977 final Message lockTaskMsg = Message.obtain();
3978 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3979 lockTaskMsg.arg1 = task.userId;
3980 lockTaskMsg.what = LOCK_TASK_START_MSG;
3981 lockTaskMsg.arg2 = lockTaskModeState;
3982 mHandler.sendMessage(lockTaskMsg);
3983 }
3984 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003985 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3986 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003987 mLockTaskModeTasks.remove(task);
3988 mLockTaskModeTasks.add(task);
3989
3990 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003991 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003992 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003993
3994 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003995 findTaskToMoveToFrontLocked(task, 0, null, reason,
3996 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003997 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003998 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003999 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004000 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004001 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004002 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004003 }
4004
4005 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004006 return isLockTaskModeViolation(task, false);
4007 }
4008
4009 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4010 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004011 return false;
4012 }
4013 final int lockTaskAuth = task.mLockTaskAuth;
4014 switch (lockTaskAuth) {
4015 case LOCK_TASK_AUTH_DONT_LOCK:
4016 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004017 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004018 case LOCK_TASK_AUTH_LAUNCHABLE:
4019 case LOCK_TASK_AUTH_WHITELISTED:
4020 return false;
4021 case LOCK_TASK_AUTH_PINNABLE:
4022 // Pinnable tasks can't be launched on top of locktask tasks.
4023 return !mLockTaskModeTasks.isEmpty();
4024 default:
4025 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4026 return true;
4027 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004028 }
4029
Craig Mautner15df08a2015-04-01 12:17:18 -07004030 void onLockTaskPackagesUpdatedLocked() {
4031 boolean didSomething = false;
4032 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4033 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004034 final boolean wasWhitelisted =
4035 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4036 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004037 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004038 final boolean isWhitelisted =
4039 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4040 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4041 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004042 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004043 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4044 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004045 removeLockedTaskLocked(lockedTask);
4046 lockedTask.performClearTaskLocked();
4047 didSomething = true;
4048 }
4049 }
4050 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4051 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4052 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4053 final ActivityStack stack = stacks.get(stackNdx);
4054 stack.onLockTaskPackagesUpdatedLocked();
4055 }
4056 }
Craig Mautnere0570202015-05-13 13:06:11 -07004057 final ActivityRecord r = topRunningActivityLocked();
4058 final TaskRecord task = r != null ? r.task : null;
4059 if (mLockTaskModeTasks.isEmpty() && task != null
4060 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4061 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004062 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4063 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4064 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4065 false);
4066 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004067 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004068 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004069 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004070 }
4071 }
4072
Benjamin Franz43261142015-02-11 15:59:44 +00004073 int getLockTaskModeState() {
4074 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004075 }
4076
Jorim Jaggife89d122015-12-22 16:28:44 +01004077 void activityRelaunchedLocked(IBinder token) {
4078 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004079 if (mService.isSleepingOrShuttingDownLocked()) {
4080 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4081 if (r != null) {
4082 r.setSleeping(true, true);
4083 }
4084 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004085 }
4086
4087 void activityRelaunchingLocked(ActivityRecord r) {
4088 mWindowManager.notifyAppRelaunching(r.appToken);
4089 }
4090
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004091 void logStackState() {
4092 mActivityMetricsLogger.logWindowState();
4093 }
4094
Andrii Kulian933076d2016-03-29 17:04:42 -07004095 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004096 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4097 final ActivityRecord r = task.mActivities.get(i);
4098 if (r.app != null && r.app.thread != null) {
4099 mMultiWindowModeChangedActivities.add(r);
4100 }
4101 }
4102
4103 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4104 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4105 }
4106 }
4107
Andrii Kulian933076d2016-03-29 17:04:42 -07004108 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004109 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004110 if (prevStack == null || prevStack == stack
4111 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4112 return;
4113 }
4114
4115 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4116 final ActivityRecord r = task.mActivities.get(i);
4117 if (r.app != null && r.app.thread != null) {
4118 mPipModeChangedActivities.add(r);
4119 }
4120 }
4121
4122 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4123 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4124 }
4125 }
4126
Tony Mak853304c2016-04-18 15:17:41 +01004127 void setDockedStackMinimized(boolean minimized) {
4128 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004129 }
4130
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004131 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004132
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004133 public ActivityStackSupervisorHandler(Looper looper) {
4134 super(looper);
4135 }
4136
Winson Chung4dabf232017-01-25 13:25:22 -08004137 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004138 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004139 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4140 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004141 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004142 }
4143
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004144 @Override
4145 public void handleMessage(Message msg) {
4146 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004147 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4148 synchronized (mService) {
4149 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4150 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004151 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004152 }
4153 }
4154 } break;
4155 case REPORT_PIP_MODE_CHANGED_MSG: {
4156 synchronized (mService) {
4157 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4158 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004159 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004160 }
4161 }
4162 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004163 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004164 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4165 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004166 if (mService.mDidDexOpt) {
4167 mService.mDidDexOpt = false;
4168 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4169 nmsg.obj = msg.obj;
4170 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4171 return;
4172 }
4173 // We don't at this point know if the activity is fullscreen,
4174 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004175 activityIdleInternal((ActivityRecord) msg.obj,
4176 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004177 } break;
4178 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004179 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004180 activityIdleInternal((ActivityRecord) msg.obj,
4181 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004182 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004183 case RESUME_TOP_ACTIVITY_MSG: {
4184 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004185 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004186 }
4187 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004188 case SLEEP_TIMEOUT_MSG: {
4189 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004190 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004191 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4192 mSleepTimeout = true;
4193 checkReadyForSleepLocked();
4194 }
4195 }
4196 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004197 case LAUNCH_TIMEOUT_MSG: {
4198 if (mService.mDidDexOpt) {
4199 mService.mDidDexOpt = false;
4200 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4201 return;
4202 }
4203 synchronized (mService) {
4204 if (mLaunchingActivity.isHeld()) {
4205 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4206 if (VALIDATE_WAKE_LOCK_CALLER
4207 && Binder.getCallingUid() != Process.myUid()) {
4208 throw new IllegalStateException("Calling must be system uid");
4209 }
4210 mLaunchingActivity.release();
4211 }
4212 }
4213 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004215 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 } break;
4217 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004218 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004219 } break;
4220 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004221 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004222 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004223 case CONTAINER_CALLBACK_VISIBILITY: {
4224 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004225 final IActivityContainerCallback callback = container.mCallback;
4226 if (callback != null) {
4227 try {
4228 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4229 } catch (RemoteException e) {
4230 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004231 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004232 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004233 case LOCK_TASK_START_MSG: {
4234 // When lock task starts, we disable the status bars.
4235 try {
Jason Monk62515be2014-05-21 16:06:19 -04004236 if (mLockTaskNotify == null) {
4237 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004238 }
Jason Monk62515be2014-05-21 16:06:19 -04004239 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004240 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004241 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004242 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004243 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004244 flags = StatusBarManager.DISABLE_MASK
4245 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004246 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004247 flags = StatusBarManager.DISABLE_MASK
4248 & (~StatusBarManager.DISABLE_BACK)
4249 & (~StatusBarManager.DISABLE_HOME)
4250 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004251 }
4252 getStatusBarService().disable(flags, mToken,
4253 mService.mContext.getPackageName());
4254 }
4255 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004256 if (getDevicePolicyManager() != null) {
4257 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004258 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004259 }
justinzhang5286d3f2014-05-12 17:06:01 -04004260 } catch (RemoteException ex) {
4261 throw new RuntimeException(ex);
4262 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004263 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004264 case LOCK_TASK_END_MSG: {
4265 // When lock task ends, we enable the status bars.
4266 try {
Jason Monk62515be2014-05-21 16:06:19 -04004267 if (getStatusBarService() != null) {
4268 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4269 mService.mContext.getPackageName());
4270 }
4271 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004272 if (getDevicePolicyManager() != null) {
4273 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4274 msg.arg1);
4275 }
Jason Monk62515be2014-05-21 16:06:19 -04004276 if (mLockTaskNotify == null) {
4277 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4278 }
4279 mLockTaskNotify.show(false);
4280 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004281 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004282 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004283 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004284 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004285 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004286 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004287 new LockPatternUtils(mService.mContext)
4288 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004289 }
4290 } catch (SettingNotFoundException e) {
4291 // No setting, don't lock.
4292 }
justinzhang5286d3f2014-05-12 17:06:01 -04004293 } catch (RemoteException ex) {
4294 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004295 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004296 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004297 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004298 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004299 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4300 if (mLockTaskNotify == null) {
4301 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4302 }
4303 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4304 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004305 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4306 final ActivityContainer container = (ActivityContainer) msg.obj;
4307 final IActivityContainerCallback callback = container.mCallback;
4308 if (callback != null) {
4309 try {
4310 callback.onAllActivitiesComplete(container.asBinder());
4311 } catch (RemoteException e) {
4312 }
4313 }
4314 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004315 case LAUNCH_TASK_BEHIND_COMPLETE: {
4316 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004317 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004318 if (r != null) {
4319 handleLaunchTaskBehindCompleteLocked(r);
4320 }
4321 }
4322 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004324 }
4325 }
4326 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004327
Ying Wangb081a592014-04-22 15:20:16 -07004328 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004329 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4330 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004331 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004332 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004333 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004334 ActivityRecord mParentActivity = null;
4335 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004336
Craig Mautnere3a00d72014-04-16 08:31:19 -07004337 boolean mVisible = true;
4338
Craig Mautner4a1cb222013-12-04 16:14:06 -08004339 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004340 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004341
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004342 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4343 final static int CONTAINER_STATE_NO_SURFACE = 1;
4344 final static int CONTAINER_STATE_FINISHING = 2;
4345 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4346
Wale Ogunwale1666e312016-12-16 11:27:18 -08004347 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 synchronized (mService) {
4349 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004350 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004351 switch (mStackId) {
4352 case PINNED_STACK_ID:
4353 new PinnedActivityStack(this, mRecentTasks, onTop);
4354 break;
4355 default:
4356 new ActivityStack(this, mRecentTasks, onTop);
4357 break;
4358 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004359 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004360 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004361 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004362 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004363
Andrii Kulian839def92016-11-02 10:58:58 -07004364 /**
4365 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004366 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004367 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004368 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004369 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004370 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004371 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004372 if (mActivityDisplay != null) {
4373 throw new IllegalStateException("ActivityContainer is already attached, " +
4374 "displayId=" + mActivityDisplay.mDisplayId);
4375 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004376 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004377 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004378 }
4379
4380 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004381 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004382 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004383 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4384 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004385 return;
4386 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004387 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004388 }
4389 }
4390
4391 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004392 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004393 synchronized (mService) {
4394 if (mActivityDisplay != null) {
4395 return mActivityDisplay.mDisplayId;
4396 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004397 }
4398 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004399 }
4400
Jeff Brownca9bc702014-02-11 14:32:56 -08004401 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004402 public int getStackId() {
4403 synchronized (mService) {
4404 return mStackId;
4405 }
4406 }
4407
4408 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004409 public boolean injectEvent(InputEvent event) {
4410 final long origId = Binder.clearCallingIdentity();
4411 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004412 synchronized (mService) {
4413 if (mActivityDisplay != null) {
4414 return mInputManagerInternal.injectInputEvent(event,
4415 mActivityDisplay.mDisplayId,
4416 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4417 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004418 }
4419 return false;
4420 } finally {
4421 Binder.restoreCallingIdentity(origId);
4422 }
4423 }
4424
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004425 @Override
4426 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004427 synchronized (mService) {
4428 if (mContainerState == CONTAINER_STATE_FINISHING) {
4429 return;
4430 }
4431 mContainerState = CONTAINER_STATE_FINISHING;
4432
Craig Mautnerd163e752014-06-13 17:18:47 -07004433 long origId = Binder.clearCallingIdentity();
4434 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004435 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004436 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004437 } finally {
4438 Binder.restoreCallingIdentity(origId);
4439 }
4440 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004441 }
4442
Andrii Kulian03c403d2016-11-11 11:14:12 -08004443 /**
4444 * Remove the stack completely. Must be called only when there are no tasks left in it,
4445 * as this method does not finish running activities.
4446 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004447 void removeLocked() {
4448 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004449 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004450 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004451 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004452 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004453 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004454 }
4455
Andrii Kulian839def92016-11-02 10:58:58 -07004456 /**
4457 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4458 * completely or re-parented.
4459 */
4460 private void removeFromDisplayLocked() {
4461 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4462 + " current displayId=" + mActivityDisplay.mDisplayId);
4463
Wale Ogunwale1666e312016-12-16 11:27:18 -08004464 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004465 mActivityDisplay = null;
4466 }
4467
4468 /**
4469 * Move the stack to specified display.
4470 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004471 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004472 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004473 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004474 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4475 + mActivityDisplay + " to display=" + activityDisplay
4476 + " Callers=" + Debug.getCallers(2));
4477
4478 removeFromDisplayLocked();
4479
4480 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004481 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004482 }
4483
Craig Mautner4a1cb222013-12-04 16:14:06 -08004484 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004485 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004486 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004487 }
4488
4489 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004490 public final int startActivityIntentSender(IIntentSender intentSender)
4491 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004492 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4493
4494 if (!(intentSender instanceof PendingIntentRecord)) {
4495 throw new IllegalArgumentException("Bad PendingIntent object");
4496 }
4497
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004498 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004499 Binder.getCallingUid(), mCurrentUser, false,
4500 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004501
4502 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4503 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4504 pendingIntent.key.requestResolvedType);
4505
4506 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4507 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4508 }
4509
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004510 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004511 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004512 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004513 throw new SecurityException(
4514 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4515 }
4516 }
4517
Craig Mautnerdf88d732014-01-27 09:21:32 -08004518 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004519 public IBinder asBinder() {
4520 return this;
4521 }
4522
Craig Mautner4504de52013-12-20 09:06:56 -08004523 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004524 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004525 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004526 }
4527
Craig Mautner4a1cb222013-12-04 16:14:06 -08004528 ActivityStackSupervisor getOuter() {
4529 return ActivityStackSupervisor.this;
4530 }
4531
Craig Mautnerd163e752014-06-13 17:18:47 -07004532 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004533 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534 }
4535
Craig Mautner6985bad2014-04-21 15:22:06 -07004536 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004537 void setVisible(boolean visible) {
4538 if (mVisible != visible) {
4539 mVisible = visible;
4540 if (mCallback != null) {
4541 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4542 0 /* unused */, this).sendToTarget();
4543 }
4544 }
4545 }
4546
Craig Mautner6985bad2014-04-21 15:22:06 -07004547 void setDrawn() {
4548 }
4549
Craig Mautner1b4bf852014-05-26 15:06:32 -07004550 // You can always start a new task on a regular ActivityStack.
4551 boolean isEligibleForNewTasks() {
4552 return true;
4553 }
4554
Craig Mautnerd163e752014-06-13 17:18:47 -07004555 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004556 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004557 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004558 }
4559
Craig Mautner34b73df2014-01-12 21:11:08 -08004560 @Override
4561 public String toString() {
4562 return mIdString + (mActivityDisplay == null ? "N" : "A");
4563 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004564 }
4565
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004566 private class VirtualActivityContainer extends ActivityContainer {
4567 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004568 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004569
4570 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004571 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4572 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004573 mParentActivity = parent;
4574 mCallback = callback;
4575 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004576 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004577 }
4578
4579 @Override
4580 public void setSurface(Surface surface, int width, int height, int density) {
4581 super.setSurface(surface, width, height, density);
4582
4583 synchronized (mService) {
4584 final long origId = Binder.clearCallingIdentity();
4585 try {
4586 setSurfaceLocked(surface, width, height, density);
4587 } finally {
4588 Binder.restoreCallingIdentity(origId);
4589 }
4590 }
4591 }
4592
4593 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4594 if (mContainerState == CONTAINER_STATE_FINISHING) {
4595 return;
4596 }
4597 VirtualActivityDisplay virtualActivityDisplay =
4598 (VirtualActivityDisplay) mActivityDisplay;
4599 if (virtualActivityDisplay == null) {
4600 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004601 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004602 mActivityDisplay = virtualActivityDisplay;
4603 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004604 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004605 }
4606
4607 if (mSurface != null) {
4608 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004609 }
4610
Craig Mautner6985bad2014-04-21 15:22:06 -07004611 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004612 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004613 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004614 } else {
4615 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004616 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004617 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004618 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004619 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004620 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004621
Craig Mautnerd163e752014-06-13 17:18:47 -07004622 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004623
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004624 if (DEBUG_STACK) Slog.d(TAG_STACK,
4625 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004626 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004627
Craig Mautner6985bad2014-04-21 15:22:06 -07004628 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004629 boolean isAttachedLocked() {
4630 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004631 }
4632
4633 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004634 void setDrawn() {
4635 synchronized (mService) {
4636 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004637 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004638 }
4639 }
4640
Craig Mautner1b4bf852014-05-26 15:06:32 -07004641 // Never start a new task on an ActivityView if it isn't explicitly specified.
4642 @Override
4643 boolean isEligibleForNewTasks() {
4644 return false;
4645 }
4646
Craig Mautnerd163e752014-06-13 17:18:47 -07004647 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004648 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004649 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4650 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4651 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4652 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4653 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004654 }
4655 }
4656
Craig Mautner4a1cb222013-12-04 16:14:06 -08004657 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4658 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004659 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004660 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004661 int mDisplayId;
4662 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004663
Craig Mautner4a1cb222013-12-04 16:14:06 -08004664 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4665 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004666 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004667
Jose Lima4b6c6692014-08-12 17:41:12 -07004668 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004669
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004670 /** Array of all UIDs that are present on the display. */
4671 private IntArray mDisplayAccessUIDs = new IntArray();
4672
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004673 ActivityDisplay() {
4674 }
Craig Mautner4504de52013-12-20 09:06:56 -08004675
Craig Mautner1a70a162014-09-13 12:09:31 -07004676 // After instantiation, check that mDisplay is not null before using this. The alternative
4677 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004678 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004679 final Display display = mDisplayManager.getDisplay(displayId);
4680 if (display == null) {
4681 return;
4682 }
4683 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004684 }
4685
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004686 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004687 mDisplay = display;
4688 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004689 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004690
Winson Chung7c3ede32017-03-14 14:18:34 -07004691 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004692 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004693 + " to displayId=" + mDisplayId + " position=" + position);
4694 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004695 }
4696
Wale Ogunwale1666e312016-12-16 11:27:18 -08004697 void detachStack(ActivityStack stack) {
4698 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004699 + " from displayId=" + mDisplayId);
4700 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004701 }
4702
Jose Lima4b6c6692014-08-12 17:41:12 -07004703 void setVisibleBehindActivity(ActivityRecord r) {
4704 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004705 }
4706
Jose Lima4b6c6692014-08-12 17:41:12 -07004707 boolean hasVisibleBehindActivity() {
4708 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004709 }
4710
Craig Mautner34b73df2014-01-12 21:11:08 -08004711 @Override
4712 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004713 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4714 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004715
4716 @Override
4717 protected int getChildCount() {
4718 return mStacks.size();
4719 }
4720
4721 @Override
4722 protected ConfigurationContainer getChildAt(int index) {
4723 return mStacks.get(index);
4724 }
4725
4726 @Override
4727 protected ConfigurationContainer getParent() {
4728 return ActivityStackSupervisor.this;
4729 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004730
4731 boolean isPrivate() {
4732 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4733 }
4734
4735 boolean isUidPresent(int uid) {
4736 for (ActivityStack stack : mStacks) {
4737 if (stack.isUidPresent(uid)) {
4738 return true;
4739 }
4740 }
4741 return false;
4742 }
4743
4744 /** Update and get all UIDs that are present on the display and have access to it. */
4745 private IntArray getPresentUIDs() {
4746 mDisplayAccessUIDs.clear();
4747 for (ActivityStack stack : mStacks) {
4748 stack.getPresentUIDs(mDisplayAccessUIDs);
4749 }
4750 return mDisplayAccessUIDs;
4751 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004752
4753 boolean shouldDestroyContentOnRemove() {
4754 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4755 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004756 }
4757
4758 class VirtualActivityDisplay extends ActivityDisplay {
4759 VirtualDisplay mVirtualDisplay;
4760
Craig Mautner6985bad2014-04-21 15:22:06 -07004761 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004762 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004763 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4764 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4765 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4766 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004767
4768 init(mVirtualDisplay.getDisplay());
4769
Bryce Leeaf3a4002017-03-20 10:37:12 -07004770 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004771 }
4772
4773 void setSurface(Surface surface) {
4774 if (mVirtualDisplay != null) {
4775 mVirtualDisplay.setSurface(surface);
4776 }
4777 }
4778
4779 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004780 void detachStack(ActivityStack stack) {
4781 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004782 if (mVirtualDisplay != null) {
4783 mVirtualDisplay.release();
4784 mVirtualDisplay = null;
4785 }
4786 }
4787
4788 @Override
4789 public String toString() {
4790 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004791 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004792 }
Jose Lima58e66d62014-05-27 20:00:27 -07004793
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004794 ActivityStack findStackBehind(ActivityStack stack) {
4795 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004796 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004797 if (display == null) {
4798 return null;
4799 }
4800 final ArrayList<ActivityStack> stacks = display.mStacks;
4801 for (int i = stacks.size() - 1; i >= 0; i--) {
4802 if (stacks.get(i) == stack && i > 0) {
4803 return stacks.get(i - 1);
4804 }
4805 }
4806 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4807 + " in=" + stacks);
4808 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004809
Jorim Jaggic69bd222016-03-15 14:38:37 +01004810 /**
4811 * Puts a task into resizing mode during the next app transition.
4812 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004813 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004814 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004815 private void setResizingDuringAnimation(TaskRecord task) {
4816 mResizingTasksDuringAnimation.add(task.taskId);
4817 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004818 }
4819
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004820 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4821 final TaskRecord task;
4822 final int callingUid;
4823 final String callingPackage;
4824 final Intent intent;
4825 final int userId;
4826 final ActivityOptions activityOptions = (bOptions != null)
4827 ? new ActivityOptions(bOptions) : null;
4828 final int launchStackId = (activityOptions != null)
4829 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004830 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004831 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004832 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004833 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004834
4835 if (launchStackId == DOCKED_STACK_ID) {
4836 mWindowManager.setDockedStackCreateState(
4837 activityOptions.getDockCreateMode(), null /* initialBounds */);
4838
4839 // Defer updating the stack in which recents is until the app transition is done, to
4840 // not run into issues where we still need to draw the task in recents but the
4841 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004842 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004843 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4844 }
4845
Winson Chung7900bee2017-03-10 08:59:25 -08004846 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4847 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004848 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004849 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004850 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004851 throw new IllegalArgumentException(
4852 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4853 }
4854
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004855 // Since we don't have an actual source record here, we assume that the currently focused
4856 // activity was the source.
4857 final ActivityStack focusedStack = getFocusedStack();
4858 final ActivityRecord sourceRecord =
4859 focusedStack != null ? focusedStack.topActivity() : null;
4860
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004861 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004862 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004863 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4864 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004865 }
4866 }
4867
4868 // If the user must confirm credentials (e.g. when first launching a work app and the
4869 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4870 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4871 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004872 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004873 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004874 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004875 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4876 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004877
4878 // If we are launching the task in the docked stack, put it into resizing mode so
4879 // the window renders full-screen with the background filling the void. Also only
4880 // call this at the end to make sure that tasks exists on the window manager side.
4881 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004882 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004883 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004884
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004885 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004886 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004887 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4888 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004889 return ActivityManager.START_TASK_TO_FRONT;
4890 }
4891 callingUid = task.mCallingUid;
4892 callingPackage = task.mCallingPackage;
4893 intent = task.intent;
4894 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4895 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004896 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4897 null, null, 0, 0, bOptions, userId, null, task);
4898 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004899 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004900 }
4901 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004902 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004903
4904 /**
4905 * @return a list of activities which are the top ones in each visible stack. The first
4906 * entry will be the focused activity.
4907 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004908 List<IBinder> getTopVisibleActivities() {
4909 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4910 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004911 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004912 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4913 // Traverse all stacks on a display.
4914 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4915 final ActivityStack stack = display.mStacks.get(j);
4916 // Get top activity from a visible stack and add it to the list.
4917 if (stack.getStackVisibilityLocked(null /* starting */)
4918 == ActivityStack.STACK_VISIBLE) {
4919 final ActivityRecord top = stack.topActivity();
4920 if (top != null) {
4921 if (stack == mFocusedStack) {
4922 topActivityTokens.add(0, top.appToken);
4923 } else {
4924 topActivityTokens.add(top.appToken);
4925 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004926 }
4927 }
4928 }
4929 }
4930 return topActivityTokens;
4931 }
Craig Mautner27084302013-03-25 08:05:25 -07004932}