blob: d6a8c07d976ac3951b185c1e37978b7a64bd88df [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);
David Stevens82ea6cb2017-03-03 16:18:50 -08001635 // Only bother calculating the whitelist for private displays
1636 if (activityDisplay.isPrivate()) {
1637 mDisplayAccessUIDs.append(
1638 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1639 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001640 }
1641 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1642 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1643 }
1644
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001645 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001646 final long identity = Binder.clearCallingIdentity();
1647 try {
1648 return UserManager.get(mService.mContext).getUserInfo(userId);
1649 } finally {
1650 Binder.restoreCallingIdentity(identity);
1651 }
1652 }
1653
Svet Ganov99b60432015-06-27 13:15:22 -07001654 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001655 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001656 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1657 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001658 == PackageManager.PERMISSION_DENIED) {
1659 return ACTIVITY_RESTRICTION_PERMISSION;
1660 }
1661
Christopher Tateff7add02015-08-17 10:23:22 -07001662 if (activityInfo.permission == null) {
1663 return ACTIVITY_RESTRICTION_NONE;
1664 }
1665
Svet Ganov99b60432015-06-27 13:15:22 -07001666 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1667 if (opCode == AppOpsManager.OP_NONE) {
1668 return ACTIVITY_RESTRICTION_NONE;
1669 }
1670
1671 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1672 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001673 if (!ignoreTargetSecurity) {
1674 return ACTIVITY_RESTRICTION_APPOP;
1675 }
Svet Ganov99b60432015-06-27 13:15:22 -07001676 }
1677
1678 return ACTIVITY_RESTRICTION_NONE;
1679 }
1680
Svetoslav7008b512015-06-24 18:47:07 -07001681 private int getActionRestrictionForCallingPackage(String action,
1682 String callingPackage, int callingPid, int callingUid) {
1683 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001684 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001685 }
1686
1687 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1688 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001689 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001690 }
1691
1692 final PackageInfo packageInfo;
1693 try {
1694 packageInfo = mService.mContext.getPackageManager()
1695 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1696 } catch (PackageManager.NameNotFoundException e) {
1697 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001698 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001699 }
1700
1701 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001702 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001703 }
1704
1705 if (mService.checkPermission(permission, callingPid, callingUid) ==
1706 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001707 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001708 }
1709
1710 final int opCode = AppOpsManager.permissionToOpCode(permission);
1711 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001712 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001713 }
1714
1715 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1716 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001717 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001718 }
1719
Svet Ganov99b60432015-06-27 13:15:22 -07001720 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001721 }
1722
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001723 void setLaunchSource(int uid) {
1724 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1725 }
1726
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001727 void acquireLaunchWakelock() {
1728 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1729 throw new IllegalStateException("Calling must be system uid");
1730 }
1731 mLaunchingActivity.acquire();
1732 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1733 // To be safe, don't allow the wake lock to be held for too long.
1734 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1735 }
1736 }
1737
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001738 /**
1739 * Called when the frontmost task is idle.
1740 * @return the state of mService.mBooting before this was called.
1741 */
1742 private boolean checkFinishBootingLocked() {
1743 final boolean booting = mService.mBooting;
1744 boolean enableScreen = false;
1745 mService.mBooting = false;
1746 if (!mService.mBooted) {
1747 mService.mBooted = true;
1748 enableScreen = true;
1749 }
1750 if (booting || enableScreen) {
1751 mService.postFinishBooting(booting, enableScreen);
1752 }
1753 return booting;
1754 }
1755
Craig Mautnerf3333272013-04-22 10:55:53 -07001756 // Checked.
1757 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001758 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001759 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001760
Craig Mautnerf3333272013-04-22 10:55:53 -07001761 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001762 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001763 int NS = 0;
1764 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001765 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001766 boolean activityRemoved = false;
1767
Wale Ogunwale7d701172015-03-11 15:36:30 -07001768 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001769 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001770 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1771 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001772 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1773 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001774 if (fromTimeout) {
1775 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001776 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001777
1778 // This is a hack to semi-deal with a race condition
1779 // in the client where it can be constructed with a
1780 // newer configuration from when we asked it to launch.
1781 // We'll update with whatever configuration it now says
1782 // it used to launch.
1783 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001784 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001785 }
1786
1787 // We are now idle. If someone is waiting for a thumbnail from
1788 // us, we can now deliver.
1789 r.idle = true;
1790
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001791 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001792 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001793 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001794 }
1795 }
1796
1797 if (allResumedActivitiesIdle()) {
1798 if (r != null) {
1799 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001800 }
1801
1802 if (mLaunchingActivity.isHeld()) {
1803 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1804 if (VALIDATE_WAKE_LOCK_CALLER &&
1805 Binder.getCallingUid() != Process.myUid()) {
1806 throw new IllegalStateException("Calling must be system uid");
1807 }
1808 mLaunchingActivity.release();
1809 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001810 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001811 }
1812
1813 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001814 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1815 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001817 if ((NF = mFinishingActivities.size()) > 0) {
1818 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001819 mFinishingActivities.clear();
1820 }
1821
Craig Mautnerf3333272013-04-22 10:55:53 -07001822 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001823 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 mStartingUsers.clear();
1825 }
1826
Craig Mautnerf3333272013-04-22 10:55:53 -07001827 // Stop any activities that are scheduled to do so but have been
1828 // waiting for the next one to start.
1829 for (int i = 0; i < NS; i++) {
1830 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001831 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001832 if (stack != null) {
1833 if (r.finishing) {
1834 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1835 } else {
1836 stack.stopActivityLocked(r);
1837 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001838 }
1839 }
1840
1841 // Finish any activities that are scheduled to do so but have been
1842 // waiting for the next one to start.
1843 for (int i = 0; i < NF; i++) {
1844 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001845 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001846 if (stack != null) {
1847 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1848 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001849 }
1850
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001851 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001852 // Complete user switch
1853 if (startingUsers != null) {
1854 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001855 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001856 }
1857 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001858 }
1859
1860 mService.trimApplications();
1861 //dump();
1862 //mWindowManager.dump();
1863
Craig Mautnerf3333272013-04-22 10:55:53 -07001864 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001865 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001866 }
1867
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001868 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001869 }
1870
Craig Mautner8e569572013-10-11 17:36:59 -07001871 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001872 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001873 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1874 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001875 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1876 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1877 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001878 }
Craig Mautner19091252013-10-05 00:03:53 -07001879 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001880 }
1881
1882 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001883 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1884 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001885 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1886 stacks.get(stackNdx).closeSystemDialogsLocked();
1887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001888 }
1889 }
1890
Craig Mautner93529a42013-10-04 15:03:13 -07001891 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001892 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001893 }
1894
Craig Mautner8d341ef2013-03-26 09:03:27 -07001895 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001896 * Update the last used stack id for non-current user (current user's last
1897 * used stack is the focused stack)
1898 */
1899 void updateUserStackLocked(int userId, ActivityStack stack) {
1900 if (userId != mCurrentUser) {
1901 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1902 }
1903 }
1904
1905 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001906 * @return true if some activity was finished (or would have finished if doit were true).
1907 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001908 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1909 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001910 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001911 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1912 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001913 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001914 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001915 if (stack.finishDisabledPackageActivitiesLocked(
1916 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001917 didSomething = true;
1918 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001919 }
1920 }
1921 return didSomething;
1922 }
1923
Dianne Hackborna413dc02013-07-12 12:02:55 -07001924 void updatePreviousProcessLocked(ActivityRecord r) {
1925 // Now that this process has stopped, we may want to consider
1926 // it to be the previous app to try to keep around in case
1927 // the user wants to return to it.
1928
1929 // First, found out what is currently the foreground app, so that
1930 // we don't blow away the previous app if this activity is being
1931 // hosted by the process that is actually still the foreground.
1932 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001933 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1934 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001935 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1936 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001937 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001938 if (stack.mResumedActivity != null) {
1939 fgApp = stack.mResumedActivity.app;
1940 } else if (stack.mPausingActivity != null) {
1941 fgApp = stack.mPausingActivity.app;
1942 }
1943 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001944 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001945 }
1946 }
1947
1948 // Now set this one as the previous process, only if that really
1949 // makes sense to.
1950 if (r.app != null && fgApp != null && r.app != fgApp
1951 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001952 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001953 mService.mPreviousProcess = r.app;
1954 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1955 }
1956 }
1957
Wale Ogunwaled046a012015-12-24 13:05:59 -08001958 boolean resumeFocusedStackTopActivityLocked() {
1959 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001960 }
1961
Wale Ogunwaled046a012015-12-24 13:05:59 -08001962 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001963 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001964 if (targetStack != null && isFocusedStack(targetStack)) {
1965 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001966 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001967 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1968 if (r == null || r.state != RESUMED) {
1969 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001970 } else if (r.state == RESUMED) {
1971 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1972 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001973 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001974 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001975 }
1976
Todd Kennedy39bfee52016-02-24 10:28:21 -08001977 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1978 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1979 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1980 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1981 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1982 }
1983 }
1984 }
1985
Adrian Roos20d7df32016-01-12 18:59:43 +01001986 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1987 TaskRecord finishedTask = null;
1988 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001989 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1990 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001991 final int numStacks = stacks.size();
1992 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1993 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001994 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1995 if (stack == focusedStack || finishedTask == null) {
1996 finishedTask = t;
1997 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001998 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001999 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002000 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002001 }
2002
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002003 void finishVoiceTask(IVoiceInteractionSession session) {
2004 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2005 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2006 final int numStacks = stacks.size();
2007 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2008 final ActivityStack stack = stacks.get(stackNdx);
2009 stack.finishVoiceTask(session);
2010 }
2011 }
2012 }
2013
Andrii Kulianc27916642016-04-12 17:59:27 -07002014 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2015 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002016 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2017 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002018 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002019 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2020 // Caller wants the home activity moved with it. To accomplish this,
2021 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002022 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002023 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002024 ActivityStack currentStack = task.getStack();
2025 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002026 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2027 + task + " to front. Stack is null");
2028 return;
2029 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002030
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002031 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002032 int stackId = options.getLaunchStackId();
2033 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002034 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002035 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002036 if (stackId == INVALID_STACK_ID) {
2037 stackId = task.getLaunchStackId();
2038 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002039 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002040 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2041 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002042 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002043 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2044 // still need moveTaskToFrontLocked() below for any transition settings.
2045 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002046 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2047 resizeStackLocked(stackId, bounds,
2048 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002049 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002050 } else {
2051 // WM resizeTask must be done after the task is moved to the correct stack,
2052 // because Task's setBounds() also updates dim layer's bounds, but that has
2053 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002054 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002055 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002056 }
2057 }
2058
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002059 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002060 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002061 r == null ? null : r.appTimeTracker, reason);
2062
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002063 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002064 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002065
Andrii Kulian02b7a832016-10-06 23:11:56 -07002066 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002067 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002068 }
2069
Wale Ogunwale854809c2015-12-27 16:18:19 -08002070 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002071 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002072 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002073 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002074 return false;
2075 }
2076 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2077 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002078 }
2079
Winson Chung55893332017-02-17 17:13:10 -08002080 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002081 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002082 }
2083
Winson Chung55893332017-02-17 17:13:10 -08002084 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2085 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002086 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002087 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002088 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002089 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002090 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002091 return null;
2092 }
Matthew Ng330757d2017-02-28 14:19:17 -08002093 if (stackId == DOCKED_STACK_ID) {
2094 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2095 // the other side of the docked stack and we make visibility decisions based on that.
2096 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2097 }
Winson Chung55893332017-02-17 17:13:10 -08002098 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002099 }
2100
Andrii Kulian16802aa2016-11-02 12:21:33 -07002101 /**
2102 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2103 * If there is no such stack, new dynamic stack can be created.
2104 * @param displayId Target display.
2105 * @param r Activity that should be launched there.
2106 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2107 */
2108 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2109 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2110 if (activityDisplay == null) {
2111 throw new IllegalArgumentException(
2112 "Display with displayId=" + displayId + " not found.");
2113 }
2114
2115 // Return the topmost valid stack on the display.
2116 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2117 final ActivityStack stack = activityDisplay.mStacks.get(i);
2118 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2119 return stack;
2120 }
2121 }
2122
2123 // If there is no valid stack on the external display - check if new dynamic stack will do.
2124 if (displayId != Display.DEFAULT_DISPLAY) {
2125 final int newDynamicStackId = getNextStackId();
2126 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2127 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2128 }
2129 }
2130
2131 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2132 return null;
2133 }
2134
Craig Mautner967212c2013-04-13 21:10:58 -07002135 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002136 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002137 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2138 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002139 }
2140 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002141 }
2142
Jorim Jaggife762342016-10-13 14:33:27 +02002143 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2144 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2145 }
2146
Andrii Kulian7fc22812016-12-28 13:04:11 -08002147 /**
2148 * Get next focusable stack in the system. This will search across displays and stacks
2149 * in last-focused order for a focusable and visible stack, different from the target stack.
2150 *
2151 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2152 * searching for next candidate.
2153 * @return Next focusable {@link ActivityStack}, null if not found.
2154 */
2155 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2156 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2157
2158 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2159 final int displayId = mTmpOrderedDisplayIds.get(i);
2160 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2161 if (stacks == null) {
2162 continue;
2163 }
2164 for (int j = stacks.size() - 1; j >= 0; --j) {
2165 final ActivityStack stack = stacks.get(j);
2166 if (stack != currentFocus && stack.isFocusable()
2167 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2168 return stack;
2169 }
2170 }
2171 }
2172
2173 return null;
2174 }
2175
Craig Mautneree2e45a2014-06-27 12:10:03 -07002176 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002177 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002178 }
2179
2180 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002181 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2182 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2183 final TaskRecord task = tasks.get(taskNdx);
2184 if (task.isHomeTask()) {
2185 final ArrayList<ActivityRecord> activities = task.mActivities;
2186 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2187 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002188 if (r.isHomeActivity()
2189 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002190 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002191 }
2192 }
2193 }
2194 }
2195 return null;
2196 }
2197
Chong Zhangb15758a2015-11-17 12:12:03 -08002198 /**
2199 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2200 * the docked stack itself, or if it's side-by-side to the docked stack.
2201 */
2202 boolean isStackDockedInEffect(int stackId) {
2203 return stackId == DOCKED_STACK_ID ||
2204 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2205 }
2206
Todd Kennedyca4d8422015-01-15 15:19:22 -08002207 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002208 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002209 ActivityContainer activityContainer =
2210 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002211 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002212 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2213 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002214 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002215 return activityContainer;
2216 }
2217
Craig Mautner34b73df2014-01-12 21:11:08 -08002218 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002219 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2220 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2221 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002222 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2223 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002224 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002225 }
2226 }
2227
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002228 void deleteActivityContainerRecord(int stackId) {
2229 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2230 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2231 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002232 }
2233
Jorim Jaggidc249c42015-12-15 14:57:31 -08002234 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002235 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002236 if (stackId == DOCKED_STACK_ID) {
2237 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002238 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002239 return;
2240 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002241 final ActivityStack stack = getStack(stackId);
2242 if (stack == null) {
2243 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2244 return;
2245 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002246
Jorim Jaggidc249c42015-12-15 14:57:31 -08002247 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002248 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2249 // stack size changing so things don't get out of sync.
2250 return;
2251 }
2252
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002253 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002254 mWindowManager.deferSurfaceLayout();
2255 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002256 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002257 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002258 stack.ensureVisibleActivitiesConfigurationLocked(
2259 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002260 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002261 } finally {
2262 mWindowManager.continueSurfaceLayout();
2263 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002264 }
2265 }
2266
Jorim Jaggi192086e2016-03-11 17:17:03 +01002267 void deferUpdateBounds(int stackId) {
2268 final ActivityStack stack = getStack(stackId);
2269 if (stack != null) {
2270 stack.deferUpdateBounds();
2271 }
2272 }
2273
2274 void continueUpdateBounds(int stackId) {
2275 final ActivityStack stack = getStack(stackId);
2276 if (stack != null) {
2277 stack.continueUpdateBounds();
2278 }
2279 }
2280
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002281 void notifyAppTransitionDone() {
2282 continueUpdateBounds(HOME_STACK_ID);
2283 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2284 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002285 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002286 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002287 if (task != null) {
2288 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002289 }
2290 }
2291 mResizingTasksDuringAnimation.clear();
2292 }
2293
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002294 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2295 final ActivityStack stack = getStack(fromStackId);
2296 if (stack == null) {
2297 return;
2298 }
2299
2300 mWindowManager.deferSurfaceLayout();
2301 try {
2302 if (fromStackId == DOCKED_STACK_ID) {
2303
2304 // We are moving all tasks from the docked stack to the fullscreen stack,
2305 // which is dismissing the docked stack, so resize all other stacks to
2306 // fullscreen here already so we don't end up with resize trashing.
2307 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2308 if (StackId.isResizeableByDockedStack(i)) {
2309 ActivityStack otherStack = getStack(i);
2310 if (otherStack != null) {
2311 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2312 true /* allowResizeInDockedMode */, DEFER_RESUME);
2313 }
2314 }
2315 }
2316
2317 // Also disable docked stack resizing since we have manually adjusted the
2318 // size of other stacks above and we don't want to trigger a docked stack
2319 // resize when we remove task from it below and it is detached from the
2320 // display because it no longer contains any tasks.
2321 mAllowDockedStackResize = false;
2322 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002323 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002324 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2325 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002326 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2327 final int size = tasks.size();
2328 if (onTop) {
2329 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002330 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002331 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002332 if (fromStackId == PINNED_STACK_ID) {
2333 // Update the return-to to reflect where the pinned stack task was moved
2334 // from so that we retain the stack that was previously visible if the
2335 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002336 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2337 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002338 MetricsLogger.action(mService.mContext,
2339 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002340 }
Winson Chung74666102017-02-22 17:49:24 -08002341 // Defer resume until all the tasks have been moved to the fullscreen stack
2342 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2343 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
2344 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002345 }
2346 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002347 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002348 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002349 final int position = fullscreenStack != null
2350 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2351 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002352 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung74666102017-02-22 17:49:24 -08002353 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
2354 DEFER_RESUME, "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002355 }
2356 }
Winson Chung74666102017-02-22 17:49:24 -08002357
2358 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2359 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002360 } finally {
2361 mAllowDockedStackResize = true;
2362 mWindowManager.continueSurfaceLayout();
2363 }
2364 }
2365
Jorim Jaggidc249c42015-12-15 14:57:31 -08002366 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002367 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2368 boolean preserveWindows) {
2369 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2370 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2371 false /* deferResume */);
2372 }
2373
2374 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2375 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2376 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002377
2378 if (!mAllowDockedStackResize) {
2379 // Docked stack resize currently disabled.
2380 return;
2381 }
2382
Jorim Jaggidc249c42015-12-15 14:57:31 -08002383 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2384 if (stack == null) {
2385 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2386 return;
2387 }
2388
2389 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2390 mWindowManager.deferSurfaceLayout();
2391 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002392 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2393 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002394 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002395 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002396
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002397 // TODO: Checking for isAttached might not be needed as if the user passes in null
2398 // dockedBounds then they want the docked stack to be dismissed.
2399 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2400 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002401 // In this case we make all other static stacks fullscreen and move all
2402 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002403 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002404
2405 // stack shouldn't contain anymore activities, so nothing to resume.
2406 r = null;
2407 } else {
2408 // Docked stacks occupy a dedicated region on screen so the size of all other
2409 // static stacks need to be adjusted so they don't overlap with the docked stack.
2410 // We get the bounds to use from window manager which has been adjusted for any
2411 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002412 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002413 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002414 final ActivityStack current = getStack(i);
2415 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002416 current.getStackDockedModeBounds(
2417 tempOtherTaskBounds /* currentTempTaskBounds */,
2418 tempRect /* outStackBounds */,
2419 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2420
Matthew Nge15352e2016-12-20 15:36:29 -08002421 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002422 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2423 tempOtherTaskInsetBounds, preserveWindows,
2424 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002425 }
2426 }
2427 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002428 if (!deferResume) {
2429 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2430 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002431 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002432 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002433 mWindowManager.continueSurfaceLayout();
2434 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2435 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002436 }
2437
Robert Carr0d00c2e2016-02-29 17:45:02 -08002438 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2439 final ActivityStack stack = getStack(PINNED_STACK_ID);
2440 if (stack == null) {
2441 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2442 return;
2443 }
2444 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2445 mWindowManager.deferSurfaceLayout();
2446 try {
2447 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002448 Rect insetBounds = null;
2449 if (tempPinnedTaskBounds != null) {
2450 // We always use 0,0 as the position for the inset rect because
2451 // if we are getting insets at all in the pinned stack it must mean
2452 // we are headed for fullscreen.
2453 insetBounds = tempRect;
2454 insetBounds.top = 0;
2455 insetBounds.left = 0;
2456 insetBounds.right = tempPinnedTaskBounds.width();
2457 insetBounds.bottom = tempPinnedTaskBounds.height();
2458 }
2459 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002460 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002461 } finally {
2462 mWindowManager.continueSurfaceLayout();
2463 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2464 }
2465 }
2466
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002467 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002468 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002469 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002470 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002471 }
2472
Wale Ogunwale1666e312016-12-16 11:27:18 -08002473 final ActivityContainer activityContainer =
2474 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002475 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002476 }
2477
Winson Chung010927a2016-12-15 16:12:35 -08002478 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002479 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002480 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2481 * instead moved back onto the fullscreen stack.
2482 */
2483 void removeStackLocked(int stackId) {
2484 final ActivityStack stack = getStack(stackId);
2485 if (stack == null) {
2486 return;
2487 }
2488
2489 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2490 if (stack.getStackId() == PINNED_STACK_ID) {
2491 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2492 if (fullscreenStack != null) {
2493 final boolean isFullscreenStackVisible =
2494 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2495 for (int i = 0; i < tasks.size(); i++) {
2496 // Insert the task either at the top of the fullscreen stack if it is hidden,
2497 // or just under the top task if it is currently visible
2498 final int insertPosition = isFullscreenStackVisible
2499 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2500 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002501 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002502 // Defer resume until we remove all the tasks
2503 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2504 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002505 }
2506 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2507 resumeFocusedStackTopActivityLocked();
2508 } else {
2509 // If there is no fullscreen stack, then create the stack and move all the tasks
2510 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002511 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002512 }
2513 } else {
2514 for (int i = tasks.size() - 1; i >= 0; i--) {
2515 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2516 REMOVE_FROM_RECENTS);
2517 }
2518 }
2519 }
2520
2521 /**
2522 * Removes the task with the specified task id.
2523 *
2524 * @param taskId Identifier of the task to be removed.
2525 * @param killProcess Kill any process associated with the task if possible.
2526 * @param removeFromRecents Whether to also remove the task from recents.
2527 * @return Returns true if the given task was found and removed.
2528 */
2529 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
Winson Chung7900bee2017-03-10 08:59:25 -08002530 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2531 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002532 if (tr != null) {
2533 tr.removeTaskActivitiesLocked();
2534 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2535 if (tr.isPersistable) {
2536 mService.notifyTaskPersisterLocked(null, true);
2537 }
2538 return true;
2539 }
2540 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2541 return false;
2542 }
2543
2544 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2545 if (removeFromRecents) {
2546 mRecentTasks.remove(tr);
2547 tr.removedFromRecents();
2548 }
2549 ComponentName component = tr.getBaseIntent().getComponent();
2550 if (component == null) {
2551 Slog.w(TAG, "No component for base intent of task: " + tr);
2552 return;
2553 }
2554
2555 // Find any running services associated with this app and stop if needed.
2556 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2557
2558 if (!killProcess) {
2559 return;
2560 }
2561
2562 // Determine if the process(es) for this task should be killed.
2563 final String pkg = component.getPackageName();
2564 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2565 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2566 for (int i = 0; i < pmap.size(); i++) {
2567
2568 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2569 for (int j = 0; j < uids.size(); j++) {
2570 ProcessRecord proc = uids.valueAt(j);
2571 if (proc.userId != tr.userId) {
2572 // Don't kill process for a different user.
2573 continue;
2574 }
2575 if (proc == mService.mHomeProcess) {
2576 // Don't kill the home process along with tasks from the same package.
2577 continue;
2578 }
2579 if (!proc.pkgList.containsKey(pkg)) {
2580 // Don't kill process that is not associated with this task.
2581 continue;
2582 }
2583
2584 for (int k = 0; k < proc.activities.size(); k++) {
2585 TaskRecord otherTask = proc.activities.get(k).task;
2586 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2587 // Don't kill process(es) that has an activity in a different task that is
2588 // also in recents.
2589 return;
2590 }
2591 }
2592
2593 if (proc.foregroundServices) {
2594 // Don't kill process(es) with foreground service.
2595 return;
2596 }
2597
2598 // Add process to kill list.
2599 procsToKill.add(proc);
2600 }
2601 }
2602
2603 // Kill the running processes.
2604 for (int i = 0; i < procsToKill.size(); i++) {
2605 ProcessRecord pr = procsToKill.get(i);
2606 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2607 && pr.curReceivers.isEmpty()) {
2608 pr.kill("remove task", true);
2609 } else {
2610 // We delay killing processes that are not in the background or running a receiver.
2611 pr.waitingToKill = "remove task";
2612 }
2613 }
2614 }
2615
Craig Mautner4a1cb222013-12-04 16:14:06 -08002616 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002617 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002618 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2619 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002620 break;
2621 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002622 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002623 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002624 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002625 }
2626
Chong Zhang5dcb2752015-08-18 13:50:26 -07002627 /**
2628 * Restores a recent task to a stack
2629 * @param task The recent task to be restored.
2630 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002631 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2632 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002633 * @return true if the task has been restored successfully.
2634 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002635 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002636 if (!StackId.isStaticStack(stackId)) {
2637 // If stack is not static (or stack id is invalid) - use the default one.
2638 // This means that tasks that were on external displays will be restored on the
2639 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002640 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002641 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002642 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2643 // Put it in the fullscreen stack.
2644 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002645 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002646
Andrii Kulian02b7a832016-10-06 23:11:56 -07002647 final ActivityStack currentStack = task.getStack();
2648 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002649 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002650 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002651 // Nothing else to do since it is already restored in the right stack.
2652 return true;
2653 }
2654 // Remove current stack association, so we can re-associate the task with the
2655 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002656 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002657 }
2658
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002659 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002660 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002661
2662 if (stack == null) {
2663 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002664 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2665 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002666 return false;
2667 }
2668
Wale Ogunwale72919d22016-12-08 18:58:50 -08002669 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002670 // TODO: move call for creation here and other place into Stack.addTask()
2671 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002672 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2673 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002674 final ArrayList<ActivityRecord> activities = task.mActivities;
2675 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002676 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002677 }
2678 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002679 }
2680
Wale Ogunwale040b4702015-08-06 18:10:50 -07002681 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002682 * Move stack with all its existing content to specified display.
2683 * @param stackId Id of stack to move.
2684 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002685 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002686 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002687 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002688 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2689 if (activityDisplay == null) {
2690 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2691 + displayId);
2692 }
2693 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2694 if (activityContainer != null) {
2695 if (activityContainer.isAttachedLocked()) {
2696 if (activityContainer.getDisplayId() == displayId) {
2697 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2698 + " to its current displayId=" + displayId);
2699 }
2700
Andrii Kulian250d6532017-02-08 23:30:45 -08002701 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002702 } else {
2703 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2704 + stackId + " is not attached to any display.");
2705 }
2706 } else {
2707 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2708 + stackId);
2709 }
2710
2711 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2712 !PRESERVE_WINDOWS);
2713 // TODO(multi-display): resize stacks properly if moved from split-screen.
2714 }
2715
2716 /**
Winson Chung74666102017-02-22 17:49:24 -08002717 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2718 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002719 */
Winson Chung74666102017-02-22 17:49:24 -08002720 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002721 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002722
Winson Chung74666102017-02-22 17:49:24 -08002723 // Check that we aren't reparenting to the same stack that the task is already in
2724 if (prevStack != null && prevStack.mStackId == stackId) {
2725 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2726 + " already in stackId=" + stackId);
2727 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002728 }
2729
Winson Chung74666102017-02-22 17:49:24 -08002730 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2731 // support
2732 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2733 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2734 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002735 }
2736
Winson Chung74666102017-02-22 17:49:24 -08002737 // Ensure that we aren't trying to move into a freeform stack without freeform
2738 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002739 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002740 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2741 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002742 }
2743
Winson Chung74666102017-02-22 17:49:24 -08002744 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2745 // used for split-screen mode and will cause things like the docked divider to show up. We
2746 // instead leave the task in its current stack or move it to the fullscreen stack if it
2747 // isn't currently in a stack.
2748 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2749 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2750 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2751 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002752 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002753
Winson Chung74666102017-02-22 17:49:24 -08002754 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2755 // example, a docked stack is created which will lead to the stack we are moving from being
2756 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002757 try {
Winson Chung74666102017-02-22 17:49:24 -08002758 task.mTemporarilyUnresizable = true;
2759 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002760 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002761 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002762 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002763 }
2764
Wale Ogunwale079a0042015-10-24 11:44:07 -07002765 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2766 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2767 if (stack == null) {
2768 throw new IllegalArgumentException(
2769 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2770 }
2771
2772 final ActivityRecord r = stack.topRunningActivityLocked();
2773 if (r == null) {
2774 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2775 + " in stack=" + stack);
2776 return false;
2777 }
2778
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002779 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002780 Slog.w(TAG,
2781 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002782 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002783 return false;
2784 }
2785
Winson Chungb5c41b72016-12-07 15:00:47 -08002786 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2787 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002788 return true;
2789 }
2790
Winson Chungb5c41b72016-12-07 15:00:47 -08002791 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2792 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002793 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002794
Wale Ogunwale1666e312016-12-16 11:27:18 -08002795 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002796 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002797
Wale Ogunwale480dca02016-02-06 13:58:29 -08002798 try {
2799 final TaskRecord task = r.task;
2800
Andrii Kulian02b7a832016-10-06 23:11:56 -07002801 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002802 // An activity can't be pinned and visible behind at the same time. Go ahead and
2803 // release it from been visible behind before pinning.
2804 requestVisibleBehindLocked(r, false);
2805 }
2806
Wale Ogunwale480dca02016-02-06 13:58:29 -08002807 // Resize the pinned stack to match the current size of the task the activity we are
2808 // going to be moving is currently contained in. We do this to have the right starting
2809 // animation bounds for the pinned stack to the desired bounds the caller wants.
2810 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2811 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002812 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002813
2814 if (task.mActivities.size() == 1) {
2815 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002816 // the stack without re-parenting the activity in a different task. We don't
2817 // move the home stack forward if we are currently entering picture-in-picture
2818 // while pausing because that changes the focused stack and may prevent the new
2819 // starting activity from resuming.
2820 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002821 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002822 // Move the home stack forward if the task we just moved to the pinned stack
2823 // was launched from home so home should be visible behind it.
2824 moveHomeStackToFront(reason);
2825 }
Winson Chung74666102017-02-22 17:49:24 -08002826 // Defer resume until below
2827 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2828 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002829 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002830 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002831 // reveal/leave the other activities in their original task.
2832
2833 // Currently, we don't support reparenting activities across tasks in two different
2834 // stacks, so instead, just create a new task in the same stack, reparent the
2835 // activity into that task, and then reparent the whole task to the new stack. This
2836 // ensures that all the necessary work to migrate states in the old and new stacks
2837 // is also done.
2838 final TaskRecord newTask = task.getStack().createTaskRecord(
2839 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2840 r.mActivityType);
2841 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2842
2843 // Defer resume until below
2844 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2845 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002846 }
Winson Chungc2baac02017-01-11 13:34:47 -08002847
2848 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2849 // to the pinned stack
2850 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002851 } finally {
2852 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002853 }
2854
Wale Ogunwale480dca02016-02-06 13:58:29 -08002855 // The task might have already been running and its visibility needs to be synchronized
2856 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002857 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002858 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002859
Winson Chung55893332017-02-17 17:13:10 -08002860 stack.animateResizePinnedStack(bounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002861 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002862 }
2863
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002864 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002865 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2866 if (r == null || !r.isFocusable()) {
2867 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2868 "moveActivityStackToFront: unfocusable r=" + r);
2869 return false;
2870 }
2871
2872 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002873 final ActivityStack stack = r.getStack();
2874 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002875 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2876 + r + " task=" + task);
2877 return false;
2878 }
2879
Chong Zhang6cda19c2016-06-14 19:07:56 -07002880 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2881 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2882 "moveActivityStackToFront: already on top, r=" + r);
2883 return false;
2884 }
2885
2886 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2887 "moveActivityStackToFront: r=" + r);
2888
2889 stack.moveToFront(reason, task);
2890 return true;
2891 }
2892
David Stevensc6b91c62017-02-08 14:23:58 -08002893 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002894 mTmpFindTaskResult.r = null;
2895 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002896 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002897 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2899 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2901 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002902 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002903 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002904 continue;
2905 }
2906 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002907 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2908 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002909 continue;
2910 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002911 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002912 // It is possible to have tasks in multiple stacks with the same root affinity, so
2913 // we should keep looking after finding an affinity match to see if there is a
2914 // better match in another stack. Also, task affinity isn't a good enough reason
2915 // to target a display which isn't the source of the intent, so skip any affinity
2916 // matches not on the specified display.
2917 if (mTmpFindTaskResult.r != null) {
2918 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2919 return mTmpFindTaskResult.r;
2920 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2921 affinityMatch = mTmpFindTaskResult.r;
2922 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002923 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002924 }
2925 }
David Stevensc6b91c62017-02-08 14:23:58 -08002926 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2927 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002928 }
2929
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002930 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2931 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002932 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2933 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002934 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002935 final ActivityRecord ar = stacks.get(stackNdx)
2936 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002937 if (ar != null) {
2938 return ar;
2939 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002940 }
2941 }
2942 return null;
2943 }
2944
Craig Mautner8d341ef2013-03-26 09:03:27 -07002945 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002946 scheduleSleepTimeout();
2947 if (!mGoingToSleep.isHeld()) {
2948 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002949 if (mLaunchingActivity.isHeld()) {
2950 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2951 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002952 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002953 mLaunchingActivity.release();
2954 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002955 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002957 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002958 }
2959
2960 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002962
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002963 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002964 final long endTime = System.currentTimeMillis() + timeout;
2965 while (true) {
2966 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002967 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2968 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002969 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2970 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2971 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002972 }
2973 if (cantShutdown) {
2974 long timeRemaining = endTime - System.currentTimeMillis();
2975 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002976 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002977 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 } catch (InterruptedException e) {
2979 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002980 } else {
2981 Slog.w(TAG, "Activity manager shutdown timed out");
2982 timedout = true;
2983 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002984 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002985 } else {
2986 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002987 }
2988 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002989
2990 // Force checkReadyForSleep to complete.
2991 mSleepTimeout = true;
2992 checkReadyForSleepLocked();
2993
Craig Mautner8d341ef2013-03-26 09:03:27 -07002994 return timedout;
2995 }
2996
2997 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002998 removeSleepTimeouts();
2999 if (mGoingToSleep.isHeld()) {
3000 mGoingToSleep.release();
3001 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3003 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003004 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3005 final ActivityStack stack = stacks.get(stackNdx);
3006 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003007 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003008 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003009 }
Craig Mautner5314a402013-09-26 12:40:16 -07003010 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003011 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003012 mGoingToSleepActivities.clear();
3013 }
3014
3015 void activitySleptLocked(ActivityRecord r) {
3016 mGoingToSleepActivities.remove(r);
3017 checkReadyForSleepLocked();
3018 }
3019
3020 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003021 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003022 // Do not care.
3023 return;
3024 }
3025
3026 if (!mSleepTimeout) {
3027 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3029 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3031 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3032 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003033 }
3034
3035 if (mStoppingActivities.size() > 0) {
3036 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003037 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003038 + mStoppingActivities.size() + " activities");
3039 scheduleIdleLocked();
3040 dontSleep = true;
3041 }
3042
3043 if (mGoingToSleepActivities.size() > 0) {
3044 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003045 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003046 + mGoingToSleepActivities.size() + " activities");
3047 dontSleep = true;
3048 }
3049
3050 if (dontSleep) {
3051 return;
3052 }
3053 }
3054
Wei Wang65c7a152016-06-02 18:51:22 -07003055 // Send launch end powerhint before going sleep
3056 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3057
Craig Mautnere0a38842013-12-16 16:14:02 -08003058 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3059 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003060 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3061 stacks.get(stackNdx).goToSleep();
3062 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003063 }
3064
3065 removeSleepTimeouts();
3066
3067 if (mGoingToSleep.isHeld()) {
3068 mGoingToSleep.release();
3069 }
3070 if (mService.mShuttingDown) {
3071 mService.notifyAll();
3072 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003073 }
3074
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003075 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003076 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003077 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003078 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003079 }
3080 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003081 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003082 mWindowManager.executeAppTransition();
3083 return true;
3084 }
3085 return false;
3086 }
3087
Craig Mautner8d341ef2013-03-26 09:03:27 -07003088 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003089 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3090 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003091 int stackNdx = stacks.size() - 1;
3092 while (stackNdx >= 0) {
3093 stacks.get(stackNdx).handleAppCrashLocked(app);
3094 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003095 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003096 }
3097 }
3098
Jose Lima4b6c6692014-08-12 17:41:12 -07003099 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003100 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003101 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003102 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3103 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003104 return false;
3105 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003106
3107 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3108 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3109 + " visible=" + visible + " stackId=" + stack.mStackId
3110 + " can't contain visible behind activities");
3111 return false;
3112 }
3113
Jose Lima4b6c6692014-08-12 17:41:12 -07003114 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003115 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3116 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003117
3118 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003119 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003120 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003121 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003122 return true;
3123 }
3124
3125 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003126 if (visible && top.fullscreen) {
3127 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003128 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3129 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3130 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3131 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003132 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003133 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3134 // Only the activity set as currently visible behind should actively reset its
3135 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003136 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3137 "requestVisibleBehind: returning visible=" + visible
3138 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3139 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003140 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003141 }
3142
Jose Lima4b6c6692014-08-12 17:41:12 -07003143 stack.setVisibleBehindActivity(visible ? r : null);
3144 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003145 // If there is a translucent home activity, we need to force it stop being translucent,
3146 // because we can't depend on the application to necessarily perform that operation.
3147 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003148 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003149 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003150 mService.convertFromTranslucent(next.appToken);
3151 }
3152 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003153 if (top.app != null && top.app.thread != null) {
3154 // Notify the top app of the change.
3155 try {
3156 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3157 } catch (RemoteException e) {
3158 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003159 }
3160 return true;
3161 }
3162
Craig Mautnerbb742462014-07-07 15:28:55 -07003163 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003164 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003165 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003166 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003167
3168 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003169 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003170 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003171 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003172 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003173
3174 // When launching tasks behind, update the last active time of the top task after the new
3175 // task has been shown briefly
3176 final ActivityRecord top = stack.topActivity();
3177 if (top != null) {
3178 top.task.touchActiveTime();
3179 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003180 }
3181
3182 void scheduleLaunchTaskBehindComplete(IBinder token) {
3183 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3184 }
3185
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003186 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3187 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003188 mKeyguardController.beginActivityVisibilityUpdate();
3189 try {
3190 // First the front stacks. In case any are not fullscreen and are in front of home.
3191 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3192 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3193 final int topStackNdx = stacks.size() - 1;
3194 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3195 final ActivityStack stack = stacks.get(stackNdx);
3196 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3197 }
Craig Mautner580ea812013-04-25 12:58:38 -07003198 }
Jorim Jaggife762342016-10-13 14:33:27 +02003199 } finally {
3200 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003201 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003202 }
3203
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003204 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3205 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3206 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3207 final int topStackNdx = stacks.size() - 1;
3208 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3209 final ActivityStack stack = stacks.get(stackNdx);
3210 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3211 }
3212 }
3213 }
3214
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003215 void invalidateTaskLayers() {
3216 mTaskLayersChanged = true;
3217 }
3218
3219 void rankTaskLayersIfNeeded() {
3220 if (!mTaskLayersChanged) {
3221 return;
3222 }
3223 mTaskLayersChanged = false;
3224 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3225 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3226 int baseLayer = 0;
3227 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3228 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3229 }
3230 }
3231 }
3232
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003233 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3234 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3235 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3236 final int topStackNdx = stacks.size() - 1;
3237 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3238 final ActivityStack stack = stacks.get(stackNdx);
3239 stack.clearOtherAppTimeTrackers(except);
3240 }
3241 }
3242 }
3243
Craig Mautner8d341ef2013-03-26 09:03:27 -07003244 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003245 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3246 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003247 final int numStacks = stacks.size();
3248 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3249 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003250 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003251 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003252 }
3253 }
3254
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003255 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3256 // Examine all activities currently running in the process.
3257 TaskRecord firstTask = null;
3258 // Tasks is non-null only if two or more tasks are found.
3259 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003260 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3261 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003262 ActivityRecord r = app.activities.get(i);
3263 // First, if we find an activity that is in the process of being destroyed,
3264 // then we just aren't going to do anything for now; we want things to settle
3265 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003266 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003267 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003268 return;
3269 }
3270 // Don't consider any activies that are currently not in a state where they
3271 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003272 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3273 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003274 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003275 continue;
3276 }
3277 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003278 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003279 + " from " + r);
3280 if (firstTask == null) {
3281 firstTask = r.task;
3282 } else if (firstTask != r.task) {
3283 if (tasks == null) {
3284 tasks = new ArraySet<>();
3285 tasks.add(firstTask);
3286 }
3287 tasks.add(r.task);
3288 }
3289 }
3290 }
3291 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003292 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003293 return;
3294 }
3295 // If we have activities in multiple tasks that are in a position to be destroyed,
3296 // let's iterate through the tasks and release the oldest one.
3297 final int numDisplays = mActivityDisplays.size();
3298 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3299 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3300 // Step through all stacks starting from behind, to hit the oldest things first.
3301 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3302 final ActivityStack stack = stacks.get(stackNdx);
3303 // Try to release activities in this stack; if we manage to, we are done.
3304 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3305 return;
3306 }
3307 }
3308 }
3309 }
3310
Amith Yamasani37a40c22015-06-17 13:25:42 -07003311 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003312 final int focusStackId = mFocusedStack.getStackId();
3313 // We dismiss the docked stack whenever we switch users.
3314 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003315 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3316 // also cause all tasks to be moved to the fullscreen stack at a position that is
3317 // appropriate.
3318 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003319
3320 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003321 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003322 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003323
Craig Mautner858d8a62013-04-23 17:08:34 -07003324 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003325 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3326 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003328 final ActivityStack stack = stacks.get(stackNdx);
3329 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003330 TaskRecord task = stack.topTask();
3331 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003332 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003333 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003334 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003335 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003336
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003337 ActivityStack stack = getStack(restoreStackId);
3338 if (stack == null) {
3339 stack = mHomeStack;
3340 }
3341 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003342 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003343 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003344 } else {
3345 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003346 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003347 }
Craig Mautner93529a42013-10-04 15:03:13 -07003348 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003349 }
3350
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003351 /** Checks whether the userid is a profile of the current user. */
3352 boolean isCurrentProfileLocked(int userId) {
3353 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003354 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003355 }
3356
Winson Chung4dabf232017-01-25 13:25:22 -08003357 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3358 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003359 ArrayList<ActivityRecord> stops = null;
3360
3361 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003362 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3363 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003364 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3365 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003366 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003367 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003368 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3369 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003370 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003371 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003372 if (s.finishing) {
3373 // If this activity is finishing, it is sitting on top of
3374 // everyone else but we now know it is no longer needed...
3375 // so get rid of it. Otherwise, we need to go through the
3376 // normal flow and hide it once we determine that it is
3377 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003378 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003379 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003380 }
3381 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003382 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003383 if (!processPausingActivities && s.state == PAUSING) {
3384 // Defer processing pausing activities in this iteration and reschedule
3385 // a delayed idle to reprocess it again
3386 removeTimeoutsForActivityLocked(idleActivity);
3387 scheduleIdleTimeoutLocked(idleActivity);
3388 continue;
3389 }
3390
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003391 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003392 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003393 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003394 }
3395 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003396 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003397 }
3398 }
3399
3400 return stops;
3401 }
3402
Craig Mautnercf910b02013-04-23 11:23:27 -07003403 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003404 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3405 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3406 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3407 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003408 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003409 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003410 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003411 if (r == null) Slog.e(TAG,
3412 "validateTop...: null top activity, stack=" + stack);
3413 else {
3414 final ActivityRecord pausing = stack.mPausingActivity;
3415 if (pausing != null && pausing == r) Slog.e(TAG,
3416 "validateTop...: top stack has pausing activity r=" + r
3417 + " state=" + state);
3418 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3419 "validateTop...: activity in front not resumed r=" + r
3420 + " state=" + state);
3421 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003422 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003423 final ActivityRecord resumed = stack.mResumedActivity;
3424 if (resumed != null && resumed == r) Slog.e(TAG,
3425 "validateTop...: back stack has resumed activity r=" + r
3426 + " state=" + state);
3427 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3428 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003429 }
3430 }
3431 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003432 }
3433
Craig Mautnere0570202015-05-13 13:06:11 -07003434 private String lockTaskModeToString() {
3435 switch (mLockTaskModeState) {
3436 case LOCK_TASK_MODE_LOCKED:
3437 return "LOCKED";
3438 case LOCK_TASK_MODE_PINNED:
3439 return "PINNED";
3440 case LOCK_TASK_MODE_NONE:
3441 return "NONE";
3442 default: return "unknown=" + mLockTaskModeState;
3443 }
3444 }
3445
Craig Mautner27084302013-03-25 08:05:25 -07003446 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003447 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003449 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003450 pw.print(prefix);
3451 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003452 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003453 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003454 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003455 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3456 if (packages.size() > 0) {
3457 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3458 for (int i = 0; i < packages.size(); ++i) {
3459 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3460 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3461 }
3462 }
3463 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3464 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003465 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003466
Winson43d1f262016-06-14 16:05:55 -07003467 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003468 * Dump all connected displays' configurations.
3469 * @param prefix Prefix to apply to each line of the dump.
3470 */
3471 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3472 pw.print(prefix); pw.println("Display override configurations:");
3473 final int displayCount = mActivityDisplays.size();
3474 for (int i = 0; i < displayCount; i++) {
3475 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3476 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3477 pw.println(activityDisplay.getOverrideConfiguration());
3478 }
3479 }
3480
3481 /**
Winson43d1f262016-06-14 16:05:55 -07003482 * Dumps the activities matching the given {@param name} in the either the focused stack
3483 * or all visible stacks if {@param dumpVisibleStacks} is true.
3484 */
Winson Chung6998bc42017-02-28 17:07:05 -08003485 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3486 boolean dumpFocusedStackOnly) {
3487 if (dumpFocusedStackOnly) {
3488 return mFocusedStack.getDumpActivitiesLocked(name);
3489 } else {
Winson43d1f262016-06-14 16:05:55 -07003490 ArrayList<ActivityRecord> activities = new ArrayList<>();
3491 int numDisplays = mActivityDisplays.size();
3492 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3493 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3494 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3495 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003496 if (!dumpVisibleStacksOnly ||
3497 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003498 activities.addAll(stack.getDumpActivitiesLocked(name));
3499 }
3500 }
3501 }
3502 return activities;
Winson43d1f262016-06-14 16:05:55 -07003503 }
Craig Mautner20e72272013-04-01 13:45:53 -07003504 }
3505
Dianne Hackborn390517b2013-05-30 15:03:32 -07003506 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3507 boolean needSep, String prefix) {
3508 if (activity != null) {
3509 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3510 if (needSep) {
3511 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003512 }
3513 pw.print(prefix);
3514 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003515 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003516 }
3517 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003518 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003519 }
3520
Craig Mautner8d341ef2013-03-26 09:03:27 -07003521 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3522 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003523 boolean printed = false;
3524 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003525 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3526 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003527 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003528 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003529 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003530 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003531 final ActivityStack stack = stacks.get(stackNdx);
3532 StringBuilder stackHeader = new StringBuilder(128);
3533 stackHeader.append(" Stack #");
3534 stackHeader.append(stack.mStackId);
3535 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003536 stackHeader.append("\n");
3537 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3538 stackHeader.append("\n");
3539 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3541 needSep, stackHeader.toString());
3542 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3543 !dumpAll, false, dumpPackage, true,
3544 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003545
Craig Mautner4a1cb222013-12-04 16:14:06 -08003546 needSep = printed;
3547 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3548 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003549 if (pr) {
3550 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003551 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003552 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003553 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3554 " mResumedActivity: ");
3555 if (pr) {
3556 printed = true;
3557 needSep = false;
3558 }
3559 if (dumpAll) {
3560 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3561 " mLastPausedActivity: ");
3562 if (pr) {
3563 printed = true;
3564 needSep = true;
3565 }
3566 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3567 needSep, " mLastNoHistoryActivity: ");
3568 }
3569 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003570 }
3571 }
3572
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003573 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3574 false, dumpPackage, true, " Activities waiting to finish:", null);
3575 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3576 false, dumpPackage, true, " Activities waiting to stop:", null);
3577 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3578 false, dumpPackage, true, " Activities waiting for another to become visible:",
3579 null);
3580 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3581 false, dumpPackage, true, " Activities waiting to sleep:", null);
3582 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3583 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003584
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003585 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003586 }
3587
Dianne Hackborn390517b2013-05-30 15:03:32 -07003588 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003589 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003590 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003591 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003592 String innerPrefix = null;
3593 String[] args = null;
3594 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003595 for (int i=list.size()-1; i>=0; i--) {
3596 final ActivityRecord r = list.get(i);
3597 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3598 continue;
3599 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003600 if (innerPrefix == null) {
3601 innerPrefix = prefix + " ";
3602 args = new String[0];
3603 }
3604 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003605 final boolean full = !brief && (complete || !r.isInHistory());
3606 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003607 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003608 needNL = false;
3609 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003610 if (header1 != null) {
3611 pw.println(header1);
3612 header1 = null;
3613 }
3614 if (header2 != null) {
3615 pw.println(header2);
3616 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003617 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003618 if (lastTask != r.task) {
3619 lastTask = r.task;
3620 pw.print(prefix);
3621 pw.print(full ? "* " : " ");
3622 pw.println(lastTask);
3623 if (full) {
3624 lastTask.dump(pw, prefix + " ");
3625 } else if (complete) {
3626 // Complete + brief == give a summary. Isn't that obvious?!?
3627 if (lastTask.intent != null) {
3628 pw.print(prefix); pw.print(" ");
3629 pw.println(lastTask.intent.toInsecureStringWithClip());
3630 }
3631 }
3632 }
3633 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3634 pw.print(" #"); pw.print(i); pw.print(": ");
3635 pw.println(r);
3636 if (full) {
3637 r.dump(pw, innerPrefix);
3638 } else if (complete) {
3639 // Complete + brief == give a summary. Isn't that obvious?!?
3640 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3641 if (r.app != null) {
3642 pw.print(innerPrefix); pw.println(r.app);
3643 }
3644 }
3645 if (client && r.app != null && r.app.thread != null) {
3646 // flush anything that is already in the PrintWriter since the thread is going
3647 // to write to the file descriptor directly
3648 pw.flush();
3649 try {
3650 TransferPipe tp = new TransferPipe();
3651 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003652 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003653 // Short timeout, since blocking here can
3654 // deadlock with the application.
3655 tp.go(fd, 2000);
3656 } finally {
3657 tp.kill();
3658 }
3659 } catch (IOException e) {
3660 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3661 } catch (RemoteException e) {
3662 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3663 }
3664 needNL = true;
3665 }
3666 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003667 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003668 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003669
Craig Mautnerf3333272013-04-22 10:55:53 -07003670 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003671 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3672 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003673 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3674 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003675 }
3676
3677 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003678 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003679 }
3680
3681 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003682 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3683 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003684 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3685 }
3686
Craig Mautner05d29032013-05-03 13:40:13 -07003687 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003688 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3689 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3690 }
Craig Mautner05d29032013-05-03 13:40:13 -07003691 }
3692
Craig Mautner0eea92c2013-05-16 13:35:39 -07003693 void removeSleepTimeouts() {
3694 mSleepTimeout = false;
3695 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3696 }
3697
3698 final void scheduleSleepTimeout() {
3699 removeSleepTimeouts();
3700 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3701 }
3702
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 @Override
3704 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003705 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003706 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3707 }
3708
3709 @Override
3710 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003711 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003712 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3713 }
3714
3715 @Override
3716 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003717 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003718 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3719 }
3720
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003721 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003722 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003724 newDisplay = mActivityDisplays.get(displayId) == null;
3725 if (newDisplay) {
3726 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003727 if (activityDisplay.mDisplay == null) {
3728 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3729 return;
3730 }
Craig Mautner4504de52013-12-20 09:06:56 -08003731 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003732 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003733 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003734 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003736 }
3737
Andrii Kulianca007a62016-11-22 16:33:28 -08003738 /** Check if display with specified id is added to the list. */
3739 boolean isDisplayAdded(int displayId) {
3740 return mActivityDisplays.get(displayId) != null;
3741 }
3742
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003743 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003744 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003745 mService.mContext.getResources().getDimensionPixelSize(
3746 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003747 }
3748
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003749 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003750 if (displayId == DEFAULT_DISPLAY) {
3751 throw new IllegalArgumentException("Can't remove the primary display.");
3752 }
3753
Craig Mautner4a1cb222013-12-04 16:14:06 -08003754 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003755 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3756 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003757 final boolean destroyContentOnRemoval
3758 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003759 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3760 while (!stacks.isEmpty()) {
3761 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003762 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003763 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3764 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003765 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003766 } else {
3767 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3768 // a valid launch stack for all activities. This way the task history from
3769 // external display will be preserved on primary after move.
3770 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003771 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003772 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003773 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003774 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 }
3776 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 }
3778
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003779 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003781 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3782 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003783 // TODO: Update the bounds.
3784 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003785 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 }
3788
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003789 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003790 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003792 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003793 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003795 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003796 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003797 info.position = display != null
3798 ? display.mStacks.indexOf(stack)
3799 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800
3801 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3802 final int numTasks = tasks.size();
3803 int[] taskIds = new int[numTasks];
3804 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003805 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003806 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 for (int i = 0; i < numTasks; ++i) {
3808 final TaskRecord task = tasks.get(i);
3809 taskIds[i] = task.taskId;
3810 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3811 : task.realActivity != null ? task.realActivity.flattenToString()
3812 : task.getTopActivity() != null ? task.getTopActivity().packageName
3813 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003814 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003815 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003816 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 }
3818 info.taskIds = taskIds;
3819 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003820 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003821 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003822
3823 final ActivityRecord top = stack.topRunningActivityLocked();
3824 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 return info;
3826 }
3827
3828 StackInfo getStackInfoLocked(int stackId) {
3829 ActivityStack stack = getStack(stackId);
3830 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003831 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 }
3833 return null;
3834 }
3835
3836 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003837 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003838 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3839 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003841 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 }
3843 }
3844 return list;
3845 }
3846
Craig Mautner15df08a2015-04-01 12:17:18 -07003847 TaskRecord getLockedTaskLocked() {
3848 final int top = mLockTaskModeTasks.size() - 1;
3849 if (top >= 0) {
3850 return mLockTaskModeTasks.get(top);
3851 }
3852 return null;
3853 }
3854
3855 boolean isLockedTask(TaskRecord task) {
3856 return mLockTaskModeTasks.contains(task);
3857 }
3858
3859 boolean isLastLockedTask(TaskRecord task) {
3860 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3861 }
3862
3863 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003864 if (!mLockTaskModeTasks.remove(task)) {
3865 return;
3866 }
3867 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3868 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003869 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003870 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3871 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003872 final Message lockTaskMsg = Message.obtain();
3873 lockTaskMsg.arg1 = task.userId;
3874 lockTaskMsg.what = LOCK_TASK_END_MSG;
3875 mHandler.sendMessage(lockTaskMsg);
3876 }
3877 }
3878
Andrii Kulianc27916642016-04-12 17:59:27 -07003879 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3880 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3881 false /* forceNonResizable */);
3882 }
3883
Jorim Jaggid53f0922016-04-06 22:16:23 -07003884 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003885 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003886 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3887 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003888 return;
3889 }
3890
Jorim Jaggicd13d332016-04-27 15:40:20 -07003891 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003892 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003893 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003894 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003895
Andrii Kulianc27916642016-04-12 17:59:27 -07003896 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3897 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003898 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003899 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003900 && !topActivity.noDisplay) {
3901 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003902 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3903 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003904 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003905 }
3906
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003907 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003908 if (mLockTaskNotify != null) {
3909 mLockTaskNotify.showToast(mLockTaskModeState);
3910 }
Jason Monka8f569c2014-07-07 12:15:21 -04003911 }
3912
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003913 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3914 if (mLockTaskModeTasks.contains(task)) {
3915 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3916 }
3917 }
3918
Craig Mautner432f64e2015-05-20 14:59:57 -07003919 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3920 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003921 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003922 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003923 final TaskRecord lockedTask = getLockedTaskLocked();
3924 if (lockedTask != null) {
3925 removeLockedTaskLocked(lockedTask);
3926 if (!mLockTaskModeTasks.isEmpty()) {
3927 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003928 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3929 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003930 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003931 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003932 return;
3933 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003934 }
Craig Mautnere0570202015-05-13 13:06:11 -07003935 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3936 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003937 return;
3938 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003939
3940 // Should have already been checked, but do it again.
3941 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003942 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3943 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003944 return;
3945 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003946 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003947 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003948 return;
3949 }
3950
3951 if (mLockTaskModeTasks.isEmpty()) {
3952 // First locktask.
3953 final Message lockTaskMsg = Message.obtain();
3954 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3955 lockTaskMsg.arg1 = task.userId;
3956 lockTaskMsg.what = LOCK_TASK_START_MSG;
3957 lockTaskMsg.arg2 = lockTaskModeState;
3958 mHandler.sendMessage(lockTaskMsg);
3959 }
3960 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003961 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3962 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003963 mLockTaskModeTasks.remove(task);
3964 mLockTaskModeTasks.add(task);
3965
3966 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003967 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003968 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003969
3970 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003971 findTaskToMoveToFrontLocked(task, 0, null, reason,
3972 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003973 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003974 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003975 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003976 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003977 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003978 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003979 }
3980
3981 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003982 return isLockTaskModeViolation(task, false);
3983 }
3984
3985 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3986 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003987 return false;
3988 }
3989 final int lockTaskAuth = task.mLockTaskAuth;
3990 switch (lockTaskAuth) {
3991 case LOCK_TASK_AUTH_DONT_LOCK:
3992 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003993 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003994 case LOCK_TASK_AUTH_LAUNCHABLE:
3995 case LOCK_TASK_AUTH_WHITELISTED:
3996 return false;
3997 case LOCK_TASK_AUTH_PINNABLE:
3998 // Pinnable tasks can't be launched on top of locktask tasks.
3999 return !mLockTaskModeTasks.isEmpty();
4000 default:
4001 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4002 return true;
4003 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004004 }
4005
Craig Mautner15df08a2015-04-01 12:17:18 -07004006 void onLockTaskPackagesUpdatedLocked() {
4007 boolean didSomething = false;
4008 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4009 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004010 final boolean wasWhitelisted =
4011 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4012 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004013 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004014 final boolean isWhitelisted =
4015 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4016 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4017 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004018 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004019 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4020 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004021 removeLockedTaskLocked(lockedTask);
4022 lockedTask.performClearTaskLocked();
4023 didSomething = true;
4024 }
4025 }
4026 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4027 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4028 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4029 final ActivityStack stack = stacks.get(stackNdx);
4030 stack.onLockTaskPackagesUpdatedLocked();
4031 }
4032 }
Craig Mautnere0570202015-05-13 13:06:11 -07004033 final ActivityRecord r = topRunningActivityLocked();
4034 final TaskRecord task = r != null ? r.task : null;
4035 if (mLockTaskModeTasks.isEmpty() && task != null
4036 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4037 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004038 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4039 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4040 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4041 false);
4042 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004043 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004044 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004045 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004046 }
4047 }
4048
Benjamin Franz43261142015-02-11 15:59:44 +00004049 int getLockTaskModeState() {
4050 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004051 }
4052
Jorim Jaggife89d122015-12-22 16:28:44 +01004053 void activityRelaunchedLocked(IBinder token) {
4054 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004055 if (mService.isSleepingOrShuttingDownLocked()) {
4056 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4057 if (r != null) {
4058 r.setSleeping(true, true);
4059 }
4060 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004061 }
4062
4063 void activityRelaunchingLocked(ActivityRecord r) {
4064 mWindowManager.notifyAppRelaunching(r.appToken);
4065 }
4066
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004067 void logStackState() {
4068 mActivityMetricsLogger.logWindowState();
4069 }
4070
Andrii Kulian933076d2016-03-29 17:04:42 -07004071 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004072 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4073 final ActivityRecord r = task.mActivities.get(i);
4074 if (r.app != null && r.app.thread != null) {
4075 mMultiWindowModeChangedActivities.add(r);
4076 }
4077 }
4078
4079 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4080 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4081 }
4082 }
4083
Andrii Kulian933076d2016-03-29 17:04:42 -07004084 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004085 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004086 if (prevStack == null || prevStack == stack
4087 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4088 return;
4089 }
4090
4091 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4092 final ActivityRecord r = task.mActivities.get(i);
4093 if (r.app != null && r.app.thread != null) {
4094 mPipModeChangedActivities.add(r);
4095 }
4096 }
4097
4098 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4099 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4100 }
4101 }
4102
Tony Mak853304c2016-04-18 15:17:41 +01004103 void setDockedStackMinimized(boolean minimized) {
4104 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004105 }
4106
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004107 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004108
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004109 public ActivityStackSupervisorHandler(Looper looper) {
4110 super(looper);
4111 }
4112
Winson Chung4dabf232017-01-25 13:25:22 -08004113 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004114 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004115 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4116 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004117 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004118 }
4119
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004120 @Override
4121 public void handleMessage(Message msg) {
4122 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004123 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4124 synchronized (mService) {
4125 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4126 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004127 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004128 }
4129 }
4130 } break;
4131 case REPORT_PIP_MODE_CHANGED_MSG: {
4132 synchronized (mService) {
4133 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4134 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004135 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004136 }
4137 }
4138 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004139 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004140 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4141 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004142 if (mService.mDidDexOpt) {
4143 mService.mDidDexOpt = false;
4144 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4145 nmsg.obj = msg.obj;
4146 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4147 return;
4148 }
4149 // We don't at this point know if the activity is fullscreen,
4150 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004151 activityIdleInternal((ActivityRecord) msg.obj,
4152 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004153 } break;
4154 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004155 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004156 activityIdleInternal((ActivityRecord) msg.obj,
4157 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004158 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004159 case RESUME_TOP_ACTIVITY_MSG: {
4160 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004161 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004162 }
4163 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004164 case SLEEP_TIMEOUT_MSG: {
4165 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004166 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004167 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4168 mSleepTimeout = true;
4169 checkReadyForSleepLocked();
4170 }
4171 }
4172 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004173 case LAUNCH_TIMEOUT_MSG: {
4174 if (mService.mDidDexOpt) {
4175 mService.mDidDexOpt = false;
4176 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4177 return;
4178 }
4179 synchronized (mService) {
4180 if (mLaunchingActivity.isHeld()) {
4181 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4182 if (VALIDATE_WAKE_LOCK_CALLER
4183 && Binder.getCallingUid() != Process.myUid()) {
4184 throw new IllegalStateException("Calling must be system uid");
4185 }
4186 mLaunchingActivity.release();
4187 }
4188 }
4189 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004190 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004191 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 } break;
4193 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004194 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195 } break;
4196 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004197 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004198 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004199 case CONTAINER_CALLBACK_VISIBILITY: {
4200 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004201 final IActivityContainerCallback callback = container.mCallback;
4202 if (callback != null) {
4203 try {
4204 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4205 } catch (RemoteException e) {
4206 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004207 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004208 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004209 case LOCK_TASK_START_MSG: {
4210 // When lock task starts, we disable the status bars.
4211 try {
Jason Monk62515be2014-05-21 16:06:19 -04004212 if (mLockTaskNotify == null) {
4213 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004214 }
Jason Monk62515be2014-05-21 16:06:19 -04004215 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004216 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004217 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004218 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004219 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004220 flags = StatusBarManager.DISABLE_MASK
4221 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004222 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004223 flags = StatusBarManager.DISABLE_MASK
4224 & (~StatusBarManager.DISABLE_BACK)
4225 & (~StatusBarManager.DISABLE_HOME)
4226 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004227 }
4228 getStatusBarService().disable(flags, mToken,
4229 mService.mContext.getPackageName());
4230 }
4231 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004232 if (getDevicePolicyManager() != null) {
4233 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004234 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004235 }
justinzhang5286d3f2014-05-12 17:06:01 -04004236 } catch (RemoteException ex) {
4237 throw new RuntimeException(ex);
4238 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004239 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004240 case LOCK_TASK_END_MSG: {
4241 // When lock task ends, we enable the status bars.
4242 try {
Jason Monk62515be2014-05-21 16:06:19 -04004243 if (getStatusBarService() != null) {
4244 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4245 mService.mContext.getPackageName());
4246 }
4247 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004248 if (getDevicePolicyManager() != null) {
4249 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4250 msg.arg1);
4251 }
Jason Monk62515be2014-05-21 16:06:19 -04004252 if (mLockTaskNotify == null) {
4253 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4254 }
4255 mLockTaskNotify.show(false);
4256 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004257 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004258 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004259 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004261 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004262 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004263 new LockPatternUtils(mService.mContext)
4264 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004265 }
4266 } catch (SettingNotFoundException e) {
4267 // No setting, don't lock.
4268 }
justinzhang5286d3f2014-05-12 17:06:01 -04004269 } catch (RemoteException ex) {
4270 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004271 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004272 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004273 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004274 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004275 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4276 if (mLockTaskNotify == null) {
4277 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4278 }
4279 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4280 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004281 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4282 final ActivityContainer container = (ActivityContainer) msg.obj;
4283 final IActivityContainerCallback callback = container.mCallback;
4284 if (callback != null) {
4285 try {
4286 callback.onAllActivitiesComplete(container.asBinder());
4287 } catch (RemoteException e) {
4288 }
4289 }
4290 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004291 case LAUNCH_TASK_BEHIND_COMPLETE: {
4292 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004293 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004294 if (r != null) {
4295 handleLaunchTaskBehindCompleteLocked(r);
4296 }
4297 }
4298 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004299
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004300 }
4301 }
4302 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004303
Ying Wangb081a592014-04-22 15:20:16 -07004304 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004305 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4306 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004307 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004308 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004309 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004310 ActivityRecord mParentActivity = null;
4311 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004312
Craig Mautnere3a00d72014-04-16 08:31:19 -07004313 boolean mVisible = true;
4314
Craig Mautner4a1cb222013-12-04 16:14:06 -08004315 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004316 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004317
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004318 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4319 final static int CONTAINER_STATE_NO_SURFACE = 1;
4320 final static int CONTAINER_STATE_FINISHING = 2;
4321 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4322
Wale Ogunwale1666e312016-12-16 11:27:18 -08004323 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004324 synchronized (mService) {
4325 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004326 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004327 switch (mStackId) {
4328 case PINNED_STACK_ID:
4329 new PinnedActivityStack(this, mRecentTasks, onTop);
4330 break;
4331 default:
4332 new ActivityStack(this, mRecentTasks, onTop);
4333 break;
4334 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004335 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004336 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004337 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004338 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004339
Andrii Kulian839def92016-11-02 10:58:58 -07004340 /**
4341 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004342 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004343 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004344 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004345 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004346 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004347 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004348 if (mActivityDisplay != null) {
4349 throw new IllegalStateException("ActivityContainer is already attached, " +
4350 "displayId=" + mActivityDisplay.mDisplayId);
4351 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004352 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004353 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004354 }
4355
4356 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004357 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004358 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004359 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4360 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004361 return;
4362 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004363 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004364 }
4365 }
4366
4367 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004368 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004369 synchronized (mService) {
4370 if (mActivityDisplay != null) {
4371 return mActivityDisplay.mDisplayId;
4372 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004373 }
4374 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004375 }
4376
Jeff Brownca9bc702014-02-11 14:32:56 -08004377 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004378 public int getStackId() {
4379 synchronized (mService) {
4380 return mStackId;
4381 }
4382 }
4383
4384 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004385 public boolean injectEvent(InputEvent event) {
4386 final long origId = Binder.clearCallingIdentity();
4387 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004388 synchronized (mService) {
4389 if (mActivityDisplay != null) {
4390 return mInputManagerInternal.injectInputEvent(event,
4391 mActivityDisplay.mDisplayId,
4392 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4393 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004394 }
4395 return false;
4396 } finally {
4397 Binder.restoreCallingIdentity(origId);
4398 }
4399 }
4400
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004401 @Override
4402 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004403 synchronized (mService) {
4404 if (mContainerState == CONTAINER_STATE_FINISHING) {
4405 return;
4406 }
4407 mContainerState = CONTAINER_STATE_FINISHING;
4408
Craig Mautnerd163e752014-06-13 17:18:47 -07004409 long origId = Binder.clearCallingIdentity();
4410 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004411 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004412 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004413 } finally {
4414 Binder.restoreCallingIdentity(origId);
4415 }
4416 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004417 }
4418
Andrii Kulian03c403d2016-11-11 11:14:12 -08004419 /**
4420 * Remove the stack completely. Must be called only when there are no tasks left in it,
4421 * as this method does not finish running activities.
4422 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004423 void removeLocked() {
4424 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004425 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004426 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004427 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004428 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004429 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004430 }
4431
Andrii Kulian839def92016-11-02 10:58:58 -07004432 /**
4433 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4434 * completely or re-parented.
4435 */
4436 private void removeFromDisplayLocked() {
4437 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4438 + " current displayId=" + mActivityDisplay.mDisplayId);
4439
Wale Ogunwale1666e312016-12-16 11:27:18 -08004440 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004441 mActivityDisplay = null;
4442 }
4443
4444 /**
4445 * Move the stack to specified display.
4446 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004447 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004448 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004449 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004450 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4451 + mActivityDisplay + " to display=" + activityDisplay
4452 + " Callers=" + Debug.getCallers(2));
4453
4454 removeFromDisplayLocked();
4455
4456 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004457 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004458 }
4459
Craig Mautner4a1cb222013-12-04 16:14:06 -08004460 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004461 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004462 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004463 }
4464
4465 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004466 public final int startActivityIntentSender(IIntentSender intentSender)
4467 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004468 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4469
4470 if (!(intentSender instanceof PendingIntentRecord)) {
4471 throw new IllegalArgumentException("Bad PendingIntent object");
4472 }
4473
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004474 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004475 Binder.getCallingUid(), mCurrentUser, false,
4476 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004477
4478 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4479 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4480 pendingIntent.key.requestResolvedType);
4481
4482 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4483 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4484 }
4485
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004486 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004487 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004488 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004489 throw new SecurityException(
4490 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4491 }
4492 }
4493
Craig Mautnerdf88d732014-01-27 09:21:32 -08004494 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004495 public IBinder asBinder() {
4496 return this;
4497 }
4498
Craig Mautner4504de52013-12-20 09:06:56 -08004499 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004500 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004501 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004502 }
4503
Craig Mautner4a1cb222013-12-04 16:14:06 -08004504 ActivityStackSupervisor getOuter() {
4505 return ActivityStackSupervisor.this;
4506 }
4507
Craig Mautnerd163e752014-06-13 17:18:47 -07004508 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004509 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004510 }
4511
Craig Mautner6985bad2014-04-21 15:22:06 -07004512 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004513 void setVisible(boolean visible) {
4514 if (mVisible != visible) {
4515 mVisible = visible;
4516 if (mCallback != null) {
4517 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4518 0 /* unused */, this).sendToTarget();
4519 }
4520 }
4521 }
4522
Craig Mautner6985bad2014-04-21 15:22:06 -07004523 void setDrawn() {
4524 }
4525
Craig Mautner1b4bf852014-05-26 15:06:32 -07004526 // You can always start a new task on a regular ActivityStack.
4527 boolean isEligibleForNewTasks() {
4528 return true;
4529 }
4530
Craig Mautnerd163e752014-06-13 17:18:47 -07004531 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004532 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004533 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004534 }
4535
Craig Mautner34b73df2014-01-12 21:11:08 -08004536 @Override
4537 public String toString() {
4538 return mIdString + (mActivityDisplay == null ? "N" : "A");
4539 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004540 }
4541
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004542 private class VirtualActivityContainer extends ActivityContainer {
4543 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004544 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004545
4546 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004547 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4548 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004549 mParentActivity = parent;
4550 mCallback = callback;
4551 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004552 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004553 }
4554
4555 @Override
4556 public void setSurface(Surface surface, int width, int height, int density) {
4557 super.setSurface(surface, width, height, density);
4558
4559 synchronized (mService) {
4560 final long origId = Binder.clearCallingIdentity();
4561 try {
4562 setSurfaceLocked(surface, width, height, density);
4563 } finally {
4564 Binder.restoreCallingIdentity(origId);
4565 }
4566 }
4567 }
4568
4569 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4570 if (mContainerState == CONTAINER_STATE_FINISHING) {
4571 return;
4572 }
4573 VirtualActivityDisplay virtualActivityDisplay =
4574 (VirtualActivityDisplay) mActivityDisplay;
4575 if (virtualActivityDisplay == null) {
4576 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004577 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004578 mActivityDisplay = virtualActivityDisplay;
4579 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004580 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004581 }
4582
4583 if (mSurface != null) {
4584 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004585 }
4586
Craig Mautner6985bad2014-04-21 15:22:06 -07004587 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004588 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004589 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004590 } else {
4591 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004592 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004593 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004594 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004595 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004596 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004597
Craig Mautnerd163e752014-06-13 17:18:47 -07004598 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004599
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004600 if (DEBUG_STACK) Slog.d(TAG_STACK,
4601 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004602 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004603
Craig Mautner6985bad2014-04-21 15:22:06 -07004604 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004605 boolean isAttachedLocked() {
4606 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004607 }
4608
4609 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004610 void setDrawn() {
4611 synchronized (mService) {
4612 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004613 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004614 }
4615 }
4616
Craig Mautner1b4bf852014-05-26 15:06:32 -07004617 // Never start a new task on an ActivityView if it isn't explicitly specified.
4618 @Override
4619 boolean isEligibleForNewTasks() {
4620 return false;
4621 }
4622
Craig Mautnerd163e752014-06-13 17:18:47 -07004623 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004624 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004625 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4626 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4627 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4628 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4629 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004630 }
4631 }
4632
Craig Mautner4a1cb222013-12-04 16:14:06 -08004633 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4634 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004635 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004636 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004637 int mDisplayId;
4638 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004639
Craig Mautner4a1cb222013-12-04 16:14:06 -08004640 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4641 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004642 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004643
Jose Lima4b6c6692014-08-12 17:41:12 -07004644 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004645
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004646 /** Array of all UIDs that are present on the display. */
4647 private IntArray mDisplayAccessUIDs = new IntArray();
4648
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004649 ActivityDisplay() {
4650 }
Craig Mautner4504de52013-12-20 09:06:56 -08004651
Craig Mautner1a70a162014-09-13 12:09:31 -07004652 // After instantiation, check that mDisplay is not null before using this. The alternative
4653 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004654 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004655 final Display display = mDisplayManager.getDisplay(displayId);
4656 if (display == null) {
4657 return;
4658 }
4659 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004660 }
4661
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004662 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004663 mDisplay = display;
4664 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004665 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004666
Winson Chung7c3ede32017-03-14 14:18:34 -07004667 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004668 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004669 + " to displayId=" + mDisplayId + " position=" + position);
4670 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004671 }
4672
Wale Ogunwale1666e312016-12-16 11:27:18 -08004673 void detachStack(ActivityStack stack) {
4674 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004675 + " from displayId=" + mDisplayId);
4676 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004677 }
4678
Jose Lima4b6c6692014-08-12 17:41:12 -07004679 void setVisibleBehindActivity(ActivityRecord r) {
4680 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004681 }
4682
Jose Lima4b6c6692014-08-12 17:41:12 -07004683 boolean hasVisibleBehindActivity() {
4684 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004685 }
4686
Craig Mautner34b73df2014-01-12 21:11:08 -08004687 @Override
4688 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004689 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4690 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004691
4692 @Override
4693 protected int getChildCount() {
4694 return mStacks.size();
4695 }
4696
4697 @Override
4698 protected ConfigurationContainer getChildAt(int index) {
4699 return mStacks.get(index);
4700 }
4701
4702 @Override
4703 protected ConfigurationContainer getParent() {
4704 return ActivityStackSupervisor.this;
4705 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004706
4707 boolean isPrivate() {
4708 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4709 }
4710
4711 boolean isUidPresent(int uid) {
4712 for (ActivityStack stack : mStacks) {
4713 if (stack.isUidPresent(uid)) {
4714 return true;
4715 }
4716 }
4717 return false;
4718 }
4719
4720 /** Update and get all UIDs that are present on the display and have access to it. */
4721 private IntArray getPresentUIDs() {
4722 mDisplayAccessUIDs.clear();
4723 for (ActivityStack stack : mStacks) {
4724 stack.getPresentUIDs(mDisplayAccessUIDs);
4725 }
4726 return mDisplayAccessUIDs;
4727 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004728
4729 boolean shouldDestroyContentOnRemove() {
4730 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4731 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004732 }
4733
4734 class VirtualActivityDisplay extends ActivityDisplay {
4735 VirtualDisplay mVirtualDisplay;
4736
Craig Mautner6985bad2014-04-21 15:22:06 -07004737 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004738 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004739 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4740 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4741 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4742 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004743
4744 init(mVirtualDisplay.getDisplay());
4745
Bryce Leeaf3a4002017-03-20 10:37:12 -07004746 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004747 }
4748
4749 void setSurface(Surface surface) {
4750 if (mVirtualDisplay != null) {
4751 mVirtualDisplay.setSurface(surface);
4752 }
4753 }
4754
4755 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004756 void detachStack(ActivityStack stack) {
4757 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004758 if (mVirtualDisplay != null) {
4759 mVirtualDisplay.release();
4760 mVirtualDisplay = null;
4761 }
4762 }
4763
4764 @Override
4765 public String toString() {
4766 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004767 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004768 }
Jose Lima58e66d62014-05-27 20:00:27 -07004769
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004770 ActivityStack findStackBehind(ActivityStack stack) {
4771 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004772 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004773 if (display == null) {
4774 return null;
4775 }
4776 final ArrayList<ActivityStack> stacks = display.mStacks;
4777 for (int i = stacks.size() - 1; i >= 0; i--) {
4778 if (stacks.get(i) == stack && i > 0) {
4779 return stacks.get(i - 1);
4780 }
4781 }
4782 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4783 + " in=" + stacks);
4784 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004785
Jorim Jaggic69bd222016-03-15 14:38:37 +01004786 /**
4787 * Puts a task into resizing mode during the next app transition.
4788 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004789 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004790 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004791 private void setResizingDuringAnimation(TaskRecord task) {
4792 mResizingTasksDuringAnimation.add(task.taskId);
4793 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004794 }
4795
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004796 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4797 final TaskRecord task;
4798 final int callingUid;
4799 final String callingPackage;
4800 final Intent intent;
4801 final int userId;
4802 final ActivityOptions activityOptions = (bOptions != null)
4803 ? new ActivityOptions(bOptions) : null;
4804 final int launchStackId = (activityOptions != null)
4805 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004806 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004807 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004808 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004809 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004810
4811 if (launchStackId == DOCKED_STACK_ID) {
4812 mWindowManager.setDockedStackCreateState(
4813 activityOptions.getDockCreateMode(), null /* initialBounds */);
4814
4815 // Defer updating the stack in which recents is until the app transition is done, to
4816 // not run into issues where we still need to draw the task in recents but the
4817 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004818 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004819 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4820 }
4821
Winson Chung7900bee2017-03-10 08:59:25 -08004822 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4823 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004824 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004825 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004826 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004827 throw new IllegalArgumentException(
4828 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4829 }
4830
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004831 // Since we don't have an actual source record here, we assume that the currently focused
4832 // activity was the source.
4833 final ActivityStack focusedStack = getFocusedStack();
4834 final ActivityRecord sourceRecord =
4835 focusedStack != null ? focusedStack.topActivity() : null;
4836
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004837 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004838 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004839 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4840 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004841 }
4842 }
4843
4844 // If the user must confirm credentials (e.g. when first launching a work app and the
4845 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4846 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4847 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004848 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004849 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004850 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004851 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4852 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004853
4854 // If we are launching the task in the docked stack, put it into resizing mode so
4855 // the window renders full-screen with the background filling the void. Also only
4856 // call this at the end to make sure that tasks exists on the window manager side.
4857 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004858 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004859 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004860
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004861 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004862 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004863 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4864 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004865 return ActivityManager.START_TASK_TO_FRONT;
4866 }
4867 callingUid = task.mCallingUid;
4868 callingPackage = task.mCallingPackage;
4869 intent = task.intent;
4870 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4871 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004872 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4873 null, null, 0, 0, bOptions, userId, null, task);
4874 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004875 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004876 }
4877 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004878 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004879
4880 /**
4881 * @return a list of activities which are the top ones in each visible stack. The first
4882 * entry will be the focused activity.
4883 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004884 List<IBinder> getTopVisibleActivities() {
4885 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4886 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004887 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004888 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4889 // Traverse all stacks on a display.
4890 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4891 final ActivityStack stack = display.mStacks.get(j);
4892 // Get top activity from a visible stack and add it to the list.
4893 if (stack.getStackVisibilityLocked(null /* starting */)
4894 == ActivityStack.STACK_VISIBLE) {
4895 final ActivityRecord top = stack.topActivity();
4896 if (top != null) {
4897 if (stack == mFocusedStack) {
4898 topActivityTokens.add(0, top.appToken);
4899 } else {
4900 topActivityTokens.add(top.appToken);
4901 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004902 }
4903 }
4904 }
4905 }
4906 return topActivityTokens;
4907 }
Craig Mautner27084302013-03-25 08:05:25 -07004908}