blob: 6343dbc3b6c08b817bf69bb7bc9ca48072ae5188 [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
Jorim Jaggife762342016-10-13 14:33:27 +020019import static android.Manifest.permission.START_ANY_ACTIVITY;
20import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
21import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
22import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
23import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
24import static android.app.ActivityManager.RESIZE_MODE_FORCED;
25import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
26import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
36import 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;
Andrii Kulian16802aa2016-11-02 12:21:33 -070099import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100100import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700101import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800103import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700104import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import android.app.ActivityOptions;
107import android.app.AppGlobals;
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;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700181import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700182import java.util.Arrays;
Bryce Lee98689f72017-03-01 23:44:43 +0000183import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700184import java.util.List;
Andrii Kulian0864bbb2017-02-16 15:45:58 -0800185import java.util.Objects;
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 Ogunwale7e8184b2015-10-05 14:37:03 -0700251 // Restore task from the saved recents if it can't be found in any live stack.
252 static final boolean RESTORE_FROM_RECENTS = true;
253
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700254 // Don't execute any calls to resume.
255 static final boolean DEFER_RESUME = true;
256
Winson Chung010927a2016-12-15 16:12:35 -0800257 // Used to indicate that a task is removed it should also be removed from recents.
258 static final boolean REMOVE_FROM_RECENTS = true;
259
Svetoslav7008b512015-06-24 18:47:07 -0700260 // Activity actions an app cannot start if it uses a permission which is not granted.
261 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
262 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700263
Svetoslav7008b512015-06-24 18:47:07 -0700264 static {
265 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
266 Manifest.permission.CAMERA);
267 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
268 Manifest.permission.CAMERA);
269 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
270 Manifest.permission.CALL_PHONE);
271 }
272
Svet Ganov99b60432015-06-27 13:15:22 -0700273 /** Action restriction: launching the activity is not restricted. */
274 private static final int ACTIVITY_RESTRICTION_NONE = 0;
275 /** Action restriction: launching the activity is restricted by a permission. */
276 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
277 /** Action restriction: launching the activity is restricted by an app op. */
278 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700279
justinzhang5286d3f2014-05-12 17:06:01 -0400280 /** Status Bar Service **/
281 private IBinder mToken = new Binder();
282 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400283 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400284
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700285 // For debugging to make sure the caller when acquiring/releasing our
286 // wake lock is the system process.
287 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 /** The number of distinct task ids that can be assigned to the tasks of a single user */
289 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700290
Craig Mautner27084302013-03-25 08:05:25 -0700291 final ActivityManagerService mService;
292
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800293 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800294
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295 final ActivityStackSupervisorHandler mHandler;
296
297 /** Short cut */
298 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800299 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700300
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700301 /** Counter for next free stack ID to use for dynamic activity stacks. */
302 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700303
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800304 /**
305 * Maps the task identifier that activities are currently being started in to the userId of the
306 * task. Each time a new task is created, the entry for the userId of the task is incremented
307 */
308 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700309
Craig Mautner2420ead2013-04-01 17:13:20 -0700310 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack containing the launcher app. Assumed to always be attached to
314 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700316
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800318 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700319
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
321 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800322 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
325 /** List of activities that are waiting for a new activity to become visible before completing
326 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700330 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331
332 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700333 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700334
Craig Mautnerde4ef022013-04-07 19:01:33 -0700335 /** List of activities that are ready to be stopped, but waiting for the next activity to
336 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700338
Craig Mautnerf3333272013-04-22 10:55:53 -0700339 /** List of activities that are ready to be finished, but waiting for the previous activity to
340 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800341 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700342
Craig Mautner0eea92c2013-05-16 13:35:39 -0700343 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800344 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700345
Wale Ogunwale22e25262016-02-01 10:32:02 -0800346 /** List of activities whose multi-window mode changed that we need to report to the
347 * application */
348 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
349
350 /** List of activities whose picture-in-picture mode changed that we need to report to the
351 * application */
352 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
353
Craig Mautnerf3333272013-04-22 10:55:53 -0700354 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700355 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700356
Craig Mautnerde4ef022013-04-07 19:01:33 -0700357 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
358 * is being brought in front of us. */
359 boolean mUserLeaving = false;
360
Craig Mautner0eea92c2013-05-16 13:35:39 -0700361 /** Set when we have taken too long waiting to go to sleep. */
362 boolean mSleepTimeout = false;
363
364 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700365 * We don't want to allow the device to go to sleep while in the process
366 * of launching an activity. This is primarily to allow alarm intent
367 * receivers to launch an activity and get that to run before the device
368 * goes back to sleep.
369 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700370 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700371
372 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373 * Set when the system is going to sleep, until we have
374 * successfully paused the current activity and released our wake lock.
375 * At that point the system is allowed to actually sleep.
376 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700377 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700378
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700379 /** Stack id of the front stack when user switched, indexed by userId. */
380 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700381
Craig Mautner4504de52013-12-20 09:06:56 -0800382 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800384 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385
386 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700387 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800389 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
390
391 private DisplayManagerInternal mDisplayManagerInternal;
392 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800393
Craig Mautner15df08a2015-04-01 12:17:18 -0700394 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
395 * may be finished until there is only one entry left. If this is empty the system is not
396 * in lockTask mode. */
397 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000398 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700399 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
400 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000401 */
402 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400403 /**
404 * Notifies the user when entering/exiting lock-task.
405 */
406 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800407
Wale Ogunwaled046a012015-12-24 13:05:59 -0800408 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800409 boolean inResumeTopActivity;
410
Andrii Kulian1e32e022016-09-16 15:29:34 -0700411 /**
412 * Temporary rect used during docked stack resize calculation so we don't need to create a new
413 * object each time.
414 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700415 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700416
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700417 // The default minimal size that will be used if the activity doesn't specify its minimal size.
418 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700419 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700420
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700421 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
422 private boolean mTaskLayersChanged = true;
423
Jorim Jaggi275561a2016-02-23 10:11:02 -0500424 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800425
Andrii Kulian1779e612016-10-12 21:58:25 -0700426 @Override
427 protected int getChildCount() {
428 return mActivityDisplays.size();
429 }
430
431 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700432 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700433 return mActivityDisplays.valueAt(index);
434 }
435
436 @Override
437 protected ConfigurationContainer getParent() {
438 return null;
439 }
440
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700441 Configuration getDisplayOverrideConfiguration(int displayId) {
442 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
443 if (activityDisplay == null) {
444 throw new IllegalArgumentException("No display found with id: " + displayId);
445 }
446
447 return activityDisplay.getOverrideConfiguration();
448 }
449
450 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
451 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
452 if (activityDisplay == null) {
453 throw new IllegalArgumentException("No display found with id: " + displayId);
454 }
455
456 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
457 }
458
Wale Ogunwale39381972015-12-17 17:15:29 -0800459 static class FindTaskResult {
460 ActivityRecord r;
461 boolean matchedByRootAffinity;
462 }
463 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
464
Craig Mautneree36c772014-07-16 14:56:05 -0700465 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800466 * Temp storage for display ids sorted in focus order.
467 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
468 * it's more efficient, as the number of displays is usually small.
469 */
470 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
471
472 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100473 * Used to keep track whether app visibilities got changed since the last pause. Useful to
474 * determine whether to invoke the task stack change listener after pausing.
475 */
476 boolean mAppVisibilitiesChangedSinceLastPause;
477
478 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100479 * Set of tasks that are in resizing mode during an app transition to fill the "void".
480 */
481 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
482
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700483
484 /**
485 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
486 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
487 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
488 * like the docked stack going empty.
489 */
490 private boolean mAllowDockedStackResize = true;
491
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100492 /**
Tony Mak853304c2016-04-18 15:17:41 +0100493 * Is dock currently minimized.
494 */
495 boolean mIsDockMinimized;
496
Jorim Jaggife762342016-10-13 14:33:27 +0200497 final KeyguardController mKeyguardController;
498
Tony Mak853304c2016-04-18 15:17:41 +0100499 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700500 * Description of a request to start a new activity, which has been held
501 * due to app switches being disabled.
502 */
503 static class PendingActivityLaunch {
504 final ActivityRecord r;
505 final ActivityRecord sourceRecord;
506 final int startFlags;
507 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700508 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700509
510 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700511 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700512 r = _r;
513 sourceRecord = _sourceRecord;
514 startFlags = _startFlags;
515 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700516 callerApp = _callerApp;
517 }
518
519 void sendErrorResult(String message) {
520 try {
521 if (callerApp.thread != null) {
522 callerApp.thread.scheduleCrash(message);
523 }
524 } catch (RemoteException e) {
525 Slog.e(TAG, "Exception scheduling crash of failed "
526 + "activity launcher sourceRecord=" + sourceRecord, e);
527 }
Craig Mautneree36c772014-07-16 14:56:05 -0700528 }
529 }
530
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800531 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700532 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700533 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800534 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200535 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700536 }
537
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800538 void setRecentTasks(RecentTasks recentTasks) {
539 mRecentTasks = recentTasks;
540 }
541
Jeff Brown2c43c332014-06-12 22:38:59 -0700542 /**
543 * At the time when the constructor runs, the power manager has not yet been
544 * initialized. So we initialize our wakelocks afterwards.
545 */
546 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700548 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700549 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700550 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700551 }
552
justinzhang5286d3f2014-05-12 17:06:01 -0400553 // This function returns a IStatusBarService. The value is from ServiceManager.
554 // getService and is cached.
555 private IStatusBarService getStatusBarService() {
556 synchronized (mService) {
557 if (mStatusBarService == null) {
558 mStatusBarService = IStatusBarService.Stub.asInterface(
559 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
560 if (mStatusBarService == null) {
561 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
562 }
563 }
564 return mStatusBarService;
565 }
566 }
567
Jason Monk35c62a42014-06-17 10:24:47 -0400568 private IDevicePolicyManager getDevicePolicyManager() {
569 synchronized (mService) {
570 if (mDevicePolicyManager == null) {
571 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
572 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
573 if (mDevicePolicyManager == null) {
574 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
575 }
576 }
577 return mDevicePolicyManager;
578 }
579 }
580
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700581 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 synchronized (mService) {
583 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200584 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800585
586 mDisplayManager =
587 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
588 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800589 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590
591 Display[] displays = mDisplayManager.getDisplays();
592 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
593 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700595 if (activityDisplay.mDisplay == null) {
596 throw new IllegalStateException("Default Display does not exist");
597 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800598 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700599 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600 }
601
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800602 mHomeStack = mFocusedStack = mLastFocusedStack =
603 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800604
605 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606 }
Craig Mautner27084302013-03-25 08:05:25 -0700607 }
608
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700609 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700611 }
612
Craig Mautnerde4ef022013-04-07 19:01:33 -0700613 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700615 }
616
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700617 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700618 if (stack == null) {
619 return false;
620 }
621
Craig Mautnerdf88d732014-01-27 09:21:32 -0800622 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
623 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700624 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800625 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800626 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700627 }
628
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800629 /** The top most stack on its display. */
630 boolean isFrontStackOnDisplay(ActivityStack stack) {
631 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
632 }
633
634 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700635 if (stack == null) {
636 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700638
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700639 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
640 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700641 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800642 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800643 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700644 }
645
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800647 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
648 if (!focusCandidate.isFocusable()) {
649 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800650 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 }
652
653 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800655 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800656
Wale Ogunwaled046a012015-12-24 13:05:59 -0800657 EventLogTags.writeAmFocusedStack(
658 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
659 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
660 }
661
662 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800663 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800664 if (r != null && r.idle) {
665 checkFinishBootingLocked();
666 }
667 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700668 }
669
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700670 void moveHomeStackToFront(String reason) {
671 mHomeStack.moveToFront(reason);
672 }
673
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700674 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800675 boolean moveHomeStackTaskToTop(String reason) {
676 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700677
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700678 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700679 if (top == null) {
680 return false;
681 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700682 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700683 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700684 }
685
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800686 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700687 if (!mService.mBooting && !mService.mBooted) {
688 // Not ready yet!
689 return false;
690 }
691
Craig Mautner84984fa2014-06-19 11:19:20 -0700692 if (prev != null) {
693 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
694 }
695
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800696 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700697 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800698 final String myReason = reason + " resumeHomeStackTask";
699
Mark Lua56ea122015-10-08 13:31:01 +0800700 // Only resume home activity if isn't finishing.
701 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700702 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800703 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700704 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700706 }
707
Craig Mautner8d341ef2013-03-26 09:03:27 -0700708 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700709 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700710 }
711
712 /**
713 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
714 * @param id Id of the task we would like returned.
715 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
716 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700717 * @param stackId The stack to restore the task to (default launch stack will be used if
718 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700719 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700720 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800721 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 ActivityStack stack = stacks.get(stackNdx);
726 TaskRecord task = stack.taskForIdLocked(id);
727 if (task != null) {
728 return task;
729 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700730 }
731 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800732
733 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700734 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800735 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800736 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700737 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800738 return null;
739 }
740
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700741 if (!restoreFromRecents) {
742 return task;
743 }
744
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700745 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700746 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
747 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800748 return null;
749 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700750 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800751 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700752 }
753
Craig Mautner6170f732013-04-02 13:05:23 -0700754 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800755 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800757 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
759 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
760 if (r != null) {
761 return r;
762 }
Craig Mautner6170f732013-04-02 13:05:23 -0700763 }
764 }
765 return null;
766 }
767
Tony Mak853304c2016-04-18 15:17:41 +0100768 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000769 * Detects whether we should show a lock screen in front of this task for a locked user.
770 * <p>
771 * We'll do this if either of the following holds:
772 * <ul>
773 * <li>The top activity explicitly belongs to {@param userId}.</li>
774 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
775 * </ul>
776 *
777 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100778 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000779 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
780 // To handle the case that work app is in the task but just is not the top one.
781 final ActivityRecord activityRecord = task.getTopActivity();
782 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
783
784 return (activityRecord != null && activityRecord.userId == userId)
785 || (resultTo != null && resultTo.userId == userId);
786 }
787
788 /**
789 * Find all visible task stacks containing {@param userId} and intercept them with an activity
790 * to block out the contents and possibly start a credential-confirming intent.
791 *
792 * @param userId user handle for the locked managed profile.
793 */
794 void lockAllProfileTasks(@UserIdInt int userId) {
795 mWindowManager.deferSurfaceLayout();
796 try {
797 final List<ActivityStack> stacks = getStacks();
798 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
799 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
800 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
801 final TaskRecord task = tasks.get(taskNdx);
802
803 // Check the task for a top activity belonging to userId, or returning a result
804 // to an activity belonging to userId. Example case: a document picker for
805 // personal files, opened by a work app, should still get locked.
806 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000807 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
808 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000809 }
Tony Mak853304c2016-04-18 15:17:41 +0100810 }
811 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000812 } finally {
813 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100814 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000815 }
816
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800817 void setNextTaskIdForUserLocked(int taskId, int userId) {
818 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
819 if (taskId > currentTaskId) {
820 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700821 }
822 }
823
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700824 static int nextTaskIdForUser(int taskId, int userId) {
825 int nextTaskId = taskId + 1;
826 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
827 // Wrap around as there will be smaller task ids that are available now.
828 nextTaskId -= MAX_TASK_IDS_PER_USER;
829 }
830 return nextTaskId;
831 }
832
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800833 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800834 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
835 // for a userId u, a taskId can only be in the range
836 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
837 // 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 -0700838 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800839 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
840 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
841 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700842 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800843 if (candidateTaskId == currentTaskId) {
844 // Something wrong!
845 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
846 throw new IllegalStateException("Cannot get an available task id."
847 + " Reached limit of " + MAX_TASK_IDS_PER_USER
848 + " running tasks per user.");
849 }
850 }
851 mCurTaskIdForUser.put(userId, candidateTaskId);
852 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700853 }
854
Chong Zhang6cda19c2016-06-14 19:07:56 -0700855 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800856 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700857 if (stack == null) {
858 return null;
859 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700860 ActivityRecord resumedActivity = stack.mResumedActivity;
861 if (resumedActivity == null || resumedActivity.app == null) {
862 resumedActivity = stack.mPausingActivity;
863 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700864 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700865 }
866 }
867 return resumedActivity;
868 }
869
Dianne Hackbornff072722014-09-24 10:56:28 -0700870 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700871 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800873 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
874 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
876 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700877 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 continue;
879 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700880 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800881 if (hr != null) {
882 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
883 && processName.equals(hr.processName)) {
884 try {
George Mount2c92c972014-03-20 09:38:23 -0700885 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 didSomething = true;
887 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700888 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 Slog.w(TAG, "Exception in new application when starting activity "
890 + hr.intent.getComponent().flattenToShortString(), e);
891 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700892 }
Craig Mautner20e72272013-04-01 13:45:53 -0700893 }
Craig Mautner20e72272013-04-01 13:45:53 -0700894 }
895 }
896 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700897 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700898 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700899 }
Craig Mautner20e72272013-04-01 13:45:53 -0700900 return didSomething;
901 }
902
903 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800904 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
905 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800906 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
907 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700908 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 continue;
910 }
911 final ActivityRecord resumedActivity = stack.mResumedActivity;
912 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700913 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800914 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 return false;
916 }
Craig Mautner20e72272013-04-01 13:45:53 -0700917 }
918 }
Wei Wang65c7a152016-06-02 18:51:22 -0700919 // Send launch end powerhint when idle
920 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700921 return true;
922 }
923
Craig Mautnerde4ef022013-04-07 19:01:33 -0700924 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800925 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
926 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
928 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700929 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700931 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 return false;
933 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700934 }
935 }
936 }
937 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700938 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
940 mLastFocusedStack + " to=" + mFocusedStack);
941 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700942 return true;
943 }
944
945 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800946 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
948 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
950 final ActivityStack stack = stacks.get(stackNdx);
951 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800952 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700953 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800954 return false;
955 }
956 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 }
959 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800960 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 }
962
Craig Mautner2acc3892013-09-23 10:28:14 -0700963 /**
964 * Pause all activities in either all of the stacks or just the back stacks.
965 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700966 * @param resuming The resuming activity.
967 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
968 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700969 * @return true if any activity was paused as a result of this call.
970 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700971 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700972 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
974 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
976 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700977 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700978 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700980 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
981 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700983 }
984 }
985 return someActivityPaused;
986 }
987
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700989 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
991 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
994 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700995 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700997 Slog.d(TAG_STATES,
998 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 pausing = false;
1000 } else {
1001 return false;
1002 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001003 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 }
1005 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001006 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001007 }
1008
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001009 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001010 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001011 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1013 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1015 final ActivityStack stack = stacks.get(stackNdx);
1016 if (stack.mResumedActivity != null &&
1017 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001018 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001019 }
1020 }
1021 }
1022 }
1023
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001024 void cancelInitializingActivities() {
1025 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1026 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1027 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1028 stacks.get(stackNdx).cancelInitializingActivities();
1029 }
1030 }
1031 }
1032
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001033 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001034 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001035 }
1036
1037 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1038 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001039 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001041 if (w.who == null) {
1042 changed = true;
1043 w.timeout = false;
1044 if (r != null) {
1045 w.who = new ComponentName(r.info.packageName, r.info.name);
1046 }
1047 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1048 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001050 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001051 if (changed) {
1052 mService.notifyAll();
1053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001054 }
1055
Chong Zhang5022da32016-06-21 16:31:37 -07001056 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1057 boolean changed = false;
1058 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1059 WaitResult w = mWaitingActivityLaunched.remove(i);
1060 if (w.who == null) {
1061 changed = true;
1062 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1063 // the starting activity ends up moving another activity to front, and it should
1064 // wait for this new activity to become visible instead.
1065 // Do not modify other fields.
1066 w.result = START_TASK_TO_FRONT;
1067 }
1068 }
1069 if (changed) {
1070 mService.notifyAll();
1071 }
1072 }
1073
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1075 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001077 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001078 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001079 if (w.who == null) {
1080 changed = true;
1081 w.timeout = timeout;
1082 if (r != null) {
1083 w.who = new ComponentName(r.info.packageName, r.info.name);
1084 }
1085 w.thisTime = thisTime;
1086 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001087 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001088 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001089 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001090 if (changed) {
1091 mService.notifyAll();
1092 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 }
1094
Craig Mautner29219d92013-04-16 20:19:12 -07001095 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001096 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001097 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001098 if (r != null) {
1099 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001100 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001101
Andrii Kulian7318d632016-07-20 18:59:28 -07001102 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001103 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1104
1105 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1106 final int displayId = mTmpOrderedDisplayIds.get(i);
1107 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1108 if (stacks == null) {
1109 continue;
1110 }
1111 for (int j = stacks.size() - 1; j >= 0; --j) {
1112 final ActivityStack stack = stacks.get(j);
1113 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1114 r = stack.topRunningActivityLocked();
1115 if (r != null) {
1116 return r;
1117 }
Craig Mautner29219d92013-04-16 20:19:12 -07001118 }
1119 }
1120 }
1121 return null;
1122 }
1123
Dianne Hackborn09233282014-04-30 11:33:59 -07001124 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001125 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001126 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1127 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001128 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001129 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001130 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001131 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1132 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001133 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001134 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001135 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001136 }
1137 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001138
1139 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1140 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1141 while (maxNum > 0) {
1142 long mostRecentActiveTime = Long.MIN_VALUE;
1143 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001144 final int numTaskLists = runningTaskLists.size();
1145 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1146 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001147 if (!stackTaskList.isEmpty()) {
1148 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1149 if (lastActiveTime > mostRecentActiveTime) {
1150 mostRecentActiveTime = lastActiveTime;
1151 selectedStackList = stackTaskList;
1152 }
1153 }
1154 }
1155 if (selectedStackList != null) {
1156 list.add(selectedStackList.remove(0));
1157 --maxNum;
1158 } else {
1159 break;
1160 }
1161 }
Craig Mautner20e72272013-04-01 13:45:53 -07001162 }
1163
Todd Kennedy7440f172015-12-09 14:31:22 -08001164 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1165 ProfilerInfo profilerInfo) {
1166 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001167 if (aInfo != null) {
1168 // Store the found target back into the intent, because now that
1169 // we have it we never want to do this again. For example, if the
1170 // user navigates back to this point in the history, we should
1171 // always restart the exact same activity.
1172 intent.setComponent(new ComponentName(
1173 aInfo.applicationInfo.packageName, aInfo.name));
1174
1175 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001176 if (!aInfo.processName.equals("system")) {
1177 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001178 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001179 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001181 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1182 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1183 }
1184
Man Caocfa78b22015-06-11 20:14:34 -07001185 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1186 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1187 }
1188
1189 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001190 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001191 }
1192 }
1193 }
1194 return aInfo;
1195 }
1196
Todd Kennedy7440f172015-12-09 14:31:22 -08001197 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001198 return resolveIntent(intent, resolvedType, userId, 0);
1199 }
1200
1201 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001202 synchronized (mService) {
1203 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001204 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001205 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001206 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001207 }
1208
1209 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1210 ProfilerInfo profilerInfo, int userId) {
1211 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1212 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1213 }
1214
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001215 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1216 boolean andResume, boolean checkConfig) throws RemoteException {
1217
1218 if (!allPausedActivitiesComplete()) {
1219 // While there are activities pausing we skipping starting any new activities until
1220 // pauses are complete. NOTE: that we also do this for activities that are starting in
1221 // the paused state because they will first be resumed then paused on the client side.
1222 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1223 "realStartActivityLocked: Skipping start of r=" + r
1224 + " some activities pausing...");
1225 return false;
1226 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001227
Andrii Kulianada10292017-02-09 23:19:29 -08001228 r.startFreezingScreenLocked(app, 0);
1229 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001230
Andrii Kulianada10292017-02-09 23:19:29 -08001231 // schedule launch ticks to collect information about slow apps.
1232 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001233
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001234 // Have the window manager re-evaluate the orientation of the screen based on the new
1235 // activity order. Note that as a result of this, it can call back into the activity
1236 // manager with a new orientation. We don't care about that, because the activity is not
1237 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001238 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001239 final int displayId = r.getDisplayId();
1240 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1241 getDisplayOverrideConfiguration(displayId),
1242 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001243 // Deferring resume here because we're going to launch new activity shortly.
1244 // We don't want to perform a redundant launch of the same record while ensuring
1245 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001246 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1247 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 }
1249
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001250 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001251 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001252 }
1253
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 r.app = app;
1255 app.waitingToKill = null;
1256 r.launchCount++;
1257 r.lastLaunchTime = SystemClock.uptimeMillis();
1258
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001259 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001260
1261 int idx = app.activities.indexOf(r);
1262 if (idx < 0) {
1263 app.activities.add(r);
1264 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001265 mService.updateLruProcessLocked(app, true, null);
1266 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001267
Craig Mautner15df08a2015-04-01 12:17:18 -07001268 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001269 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1270 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001271 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001272 }
1273
Andrii Kulian02b7a832016-10-06 23:11:56 -07001274 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 try {
1276 if (app.thread == null) {
1277 throw new RemoteException();
1278 }
1279 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001280 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001282 // We don't need to deliver new intents and/or set results if activity is going
1283 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 results = r.results;
1285 newIntents = r.newIntents;
1286 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001287 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1288 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1289 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001290 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1291 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001292 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001293 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001294 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001296 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1297 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 r.sleeping = false;
1299 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001300 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 mService.showAskCompatModeDialogLocked(r);
1302 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001303 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1305 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1306 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001307 final String profileFile = mService.mProfileFile;
1308 if (profileFile != null) {
1309 ParcelFileDescriptor profileFd = mService.mProfileFd;
1310 if (profileFd != null) {
1311 try {
1312 profileFd = profileFd.dup();
1313 } catch (IOException e) {
1314 if (profileFd != null) {
1315 try {
1316 profileFd.close();
1317 } catch (IOException o) {
1318 }
1319 profileFd = null;
1320 }
1321 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001323
1324 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001325 mService.mSamplingInterval, mService.mAutoStopProfiler,
1326 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 }
1328 }
1329 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001330
Andrii Kulianada10292017-02-09 23:19:29 -08001331 app.hasShownUi = true;
1332 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001333 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001334 // Because we could be starting an Activity in the system process this may not go across
1335 // a Binder interface which would create a new Configuration. Consequently we have to
1336 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001337
1338 final Configuration globalConfiguration =
1339 new Configuration(mService.getGlobalConfiguration());
1340 r.setLastReportedGlobalConfiguration(globalConfiguration);
1341 final Configuration mergedOverrideConfiguration =
1342 new Configuration(task.getMergedOverrideConfiguration());
1343 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1344
Craig Mautner2420ead2013-04-01 17:13:20 -07001345 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001346 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001347 globalConfiguration,
1348 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001349 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1350 r.persistentState, results, newIntents, !andResume,
1351 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001352
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001353 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001354 // This may be a heavy-weight process! Note that the package
1355 // manager will ensure that only activity can run in the main
1356 // process of the .apk, which is the only thing that will be
1357 // considered heavy-weight.
1358 if (app.processName.equals(app.info.packageName)) {
1359 if (mService.mHeavyWeightProcess != null
1360 && mService.mHeavyWeightProcess != app) {
1361 Slog.w(TAG, "Starting new heavy weight process " + app
1362 + " when already running "
1363 + mService.mHeavyWeightProcess);
1364 }
1365 mService.mHeavyWeightProcess = app;
1366 Message msg = mService.mHandler.obtainMessage(
1367 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1368 msg.obj = r;
1369 mService.mHandler.sendMessage(msg);
1370 }
1371 }
1372
1373 } catch (RemoteException e) {
1374 if (r.launchFailed) {
1375 // This is the second time we failed -- finish activity
1376 // and give up.
1377 Slog.e(TAG, "Second failure launching "
1378 + r.intent.getComponent().flattenToShortString()
1379 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001380 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001381 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1382 "2nd-crash", false);
1383 return false;
1384 }
1385
1386 // This is the first time we failed -- restart process and
1387 // retry.
1388 app.activities.remove(r);
1389 throw e;
1390 }
1391
1392 r.launchFailed = false;
1393 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001394 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 }
1396
1397 if (andResume) {
1398 // As part of the process of launching, ActivityThread also performs
1399 // a resume.
1400 stack.minimalResumeActivityLocked(r);
1401 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001402 // This activity is not starting in the resumed state... which should look like we asked
1403 // it to pause+stop (but remain visible), and it has done so and reported back the
1404 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001405 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001406 "Moving to PAUSED: " + r + " (starting in paused state)");
1407 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001408 }
1409
1410 // Launch the new version setup screen if needed. We do this -after-
1411 // launching the initial activity (that is, home), so that it can have
1412 // a chance to initialize itself while in the background, making the
1413 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001414 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001415 mService.startSetupActivityLocked();
1416 }
1417
Dianne Hackborn465fa392014-09-14 14:21:18 -07001418 // Update any services we are bound to that might care about whether
1419 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001420 if (r.app != null) {
1421 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1422 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001423
Craig Mautner2420ead2013-04-01 17:13:20 -07001424 return true;
1425 }
1426
Craig Mautnere79d42682013-04-01 19:01:53 -07001427 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001428 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001429 // Is this activity's application already running?
1430 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001431 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001432
Andrii Kulian02b7a832016-10-06 23:11:56 -07001433 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001434
1435 if (app != null && app.thread != null) {
1436 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001437 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1438 || !"android".equals(r.info.packageName)) {
1439 // Don't add this if it is a platform component that is marked
1440 // to run in multiple processes, because this is actually
1441 // part of the framework so doesn't make sense to track as a
1442 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001443 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1444 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001445 }
George Mount2c92c972014-03-20 09:38:23 -07001446 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001447 return;
1448 } catch (RemoteException e) {
1449 Slog.w(TAG, "Exception when starting activity "
1450 + r.intent.getComponent().flattenToShortString(), e);
1451 }
1452
1453 // If a dead object exception was thrown -- fall through to
1454 // restart the application.
1455 }
1456
1457 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001458 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001459 }
1460
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001461 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001462 String resultWho, int requestCode, int callingPid, int callingUid,
1463 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001464 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001465 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1466 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001467 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001468 return true;
1469 }
1470 final int componentRestriction = getComponentRestrictionForCallingPackage(
1471 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1472 final int actionRestriction = getActionRestrictionForCallingPackage(
1473 intent.getAction(), callingPackage, callingPid, callingUid);
1474 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1475 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1476 if (resultRecord != null) {
1477 resultStack.sendActivityResultLocked(-1,
1478 resultRecord, resultWho, requestCode,
1479 Activity.RESULT_CANCELED, null);
1480 }
1481 final String msg;
1482 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1483 msg = "Permission Denial: starting " + intent.toString()
1484 + " from " + callerApp + " (pid=" + callingPid
1485 + ", uid=" + callingUid + ")" + " with revoked permission "
1486 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1487 } else if (!aInfo.exported) {
1488 msg = "Permission Denial: starting " + intent.toString()
1489 + " from " + callerApp + " (pid=" + callingPid
1490 + ", uid=" + callingUid + ")"
1491 + " not exported from uid " + aInfo.applicationInfo.uid;
1492 } else {
1493 msg = "Permission Denial: starting " + intent.toString()
1494 + " from " + callerApp + " (pid=" + callingPid
1495 + ", uid=" + callingUid + ")"
1496 + " requires " + aInfo.permission;
1497 }
1498 Slog.w(TAG, msg);
1499 throw new SecurityException(msg);
1500 }
1501
1502 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1503 final String message = "Appop Denial: starting " + intent.toString()
1504 + " from " + callerApp + " (pid=" + callingPid
1505 + ", uid=" + callingUid + ")"
1506 + " requires " + AppOpsManager.permissionToOp(
1507 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1508 Slog.w(TAG, message);
1509 return false;
1510 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1511 final String message = "Appop Denial: starting " + intent.toString()
1512 + " from " + callerApp + " (pid=" + callingPid
1513 + ", uid=" + callingUid + ")"
1514 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1515 Slog.w(TAG, message);
1516 return false;
1517 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001518 if (options != null) {
1519 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1520 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1521 callingPid, callingUid);
1522 if (startInTaskPerm != PERMISSION_GRANTED) {
1523 final String msg = "Permission Denial: starting " + intent.toString()
1524 + " from " + callerApp + " (pid=" + callingPid
1525 + ", uid=" + callingUid + ") with launchTaskId="
1526 + options.getLaunchTaskId();
1527 Slog.w(TAG, msg);
1528 throw new SecurityException(msg);
1529 }
1530 }
1531 // Check if someone tries to launch an activity on a private display with a different
1532 // owner.
1533 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001534 if (launchDisplayId != INVALID_DISPLAY
1535 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1536 final String msg = "Permission Denial: starting " + intent.toString()
1537 + " from " + callerApp + " (pid=" + callingPid
1538 + ", uid=" + callingUid + ") with launchDisplayId="
1539 + launchDisplayId;
1540 Slog.w(TAG, msg);
1541 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001542 }
1543 }
1544
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001545 return true;
1546 }
1547
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001548 /** Check if caller is allowed to launch activities on specified display. */
1549 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1550 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1551 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1552
1553 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1554 if (activityDisplay == null) {
1555 Slog.w(TAG, "Launch on display check: display not found");
1556 return false;
1557 }
1558
1559 if (!activityDisplay.isPrivate()) {
1560 // Anyone can launch on a public display.
1561 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1562 + " allow launch on public display");
1563 return true;
1564 }
1565
1566 // Check if the caller is the owner of the display.
1567 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1568 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1569 + " allow launch for owner of the display");
1570 return true;
1571 }
1572
1573 // Check if caller is present on display
1574 if (activityDisplay.isUidPresent(callingUid)) {
1575 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1576 + " allow launch for caller present on the display");
1577 return true;
1578 }
1579
1580 // Check if the caller can launch anything.
1581 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1582 callingUid);
1583 if (startAnyPerm == PERMISSION_GRANTED) {
1584 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1585 + " allow launch any on display");
1586 return true;
1587 }
1588
1589 Slog.w(TAG, "Launch on display check: denied");
1590 return false;
1591 }
1592
1593 /** Update lists of UIDs that are present on displays and have access to them. */
1594 void updateUIDsPresentOnDisplay() {
1595 mDisplayAccessUIDs.clear();
1596 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1597 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1598 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1599 }
1600 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1601 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1602 }
1603
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001604 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001605 final long identity = Binder.clearCallingIdentity();
1606 try {
1607 return UserManager.get(mService.mContext).getUserInfo(userId);
1608 } finally {
1609 Binder.restoreCallingIdentity(identity);
1610 }
1611 }
1612
Svet Ganov99b60432015-06-27 13:15:22 -07001613 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001614 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001615 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1616 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001617 == PackageManager.PERMISSION_DENIED) {
1618 return ACTIVITY_RESTRICTION_PERMISSION;
1619 }
1620
Christopher Tateff7add02015-08-17 10:23:22 -07001621 if (activityInfo.permission == null) {
1622 return ACTIVITY_RESTRICTION_NONE;
1623 }
1624
Svet Ganov99b60432015-06-27 13:15:22 -07001625 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1626 if (opCode == AppOpsManager.OP_NONE) {
1627 return ACTIVITY_RESTRICTION_NONE;
1628 }
1629
1630 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1631 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001632 if (!ignoreTargetSecurity) {
1633 return ACTIVITY_RESTRICTION_APPOP;
1634 }
Svet Ganov99b60432015-06-27 13:15:22 -07001635 }
1636
1637 return ACTIVITY_RESTRICTION_NONE;
1638 }
1639
Svetoslav7008b512015-06-24 18:47:07 -07001640 private int getActionRestrictionForCallingPackage(String action,
1641 String callingPackage, int callingPid, int callingUid) {
1642 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001643 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001644 }
1645
1646 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1647 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001648 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001649 }
1650
1651 final PackageInfo packageInfo;
1652 try {
1653 packageInfo = mService.mContext.getPackageManager()
1654 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1655 } catch (PackageManager.NameNotFoundException e) {
1656 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001657 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001658 }
1659
1660 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001661 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001662 }
1663
1664 if (mService.checkPermission(permission, callingPid, callingUid) ==
1665 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001666 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001667 }
1668
1669 final int opCode = AppOpsManager.permissionToOpCode(permission);
1670 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001671 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001672 }
1673
1674 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1675 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001676 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001677 }
1678
Svet Ganov99b60432015-06-27 13:15:22 -07001679 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001680 }
1681
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001682 void setLaunchSource(int uid) {
1683 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1684 }
1685
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001686 void acquireLaunchWakelock() {
1687 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1688 throw new IllegalStateException("Calling must be system uid");
1689 }
1690 mLaunchingActivity.acquire();
1691 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1692 // To be safe, don't allow the wake lock to be held for too long.
1693 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1694 }
1695 }
1696
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001697 /**
1698 * Called when the frontmost task is idle.
1699 * @return the state of mService.mBooting before this was called.
1700 */
1701 private boolean checkFinishBootingLocked() {
1702 final boolean booting = mService.mBooting;
1703 boolean enableScreen = false;
1704 mService.mBooting = false;
1705 if (!mService.mBooted) {
1706 mService.mBooted = true;
1707 enableScreen = true;
1708 }
1709 if (booting || enableScreen) {
1710 mService.postFinishBooting(booting, enableScreen);
1711 }
1712 return booting;
1713 }
1714
Craig Mautnerf3333272013-04-22 10:55:53 -07001715 // Checked.
1716 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001717 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001718 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001719
Craig Mautnerf3333272013-04-22 10:55:53 -07001720 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001721 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001722 int NS = 0;
1723 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001724 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001725 boolean activityRemoved = false;
1726
Wale Ogunwale7d701172015-03-11 15:36:30 -07001727 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001728 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001729 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1730 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001731 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1732 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001733 if (fromTimeout) {
1734 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001735 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001736
1737 // This is a hack to semi-deal with a race condition
1738 // in the client where it can be constructed with a
1739 // newer configuration from when we asked it to launch.
1740 // We'll update with whatever configuration it now says
1741 // it used to launch.
1742 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001743 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001744 }
1745
1746 // We are now idle. If someone is waiting for a thumbnail from
1747 // us, we can now deliver.
1748 r.idle = true;
1749
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001750 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001751 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001752 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001753 }
1754 }
1755
1756 if (allResumedActivitiesIdle()) {
1757 if (r != null) {
1758 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001759 }
1760
1761 if (mLaunchingActivity.isHeld()) {
1762 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1763 if (VALIDATE_WAKE_LOCK_CALLER &&
1764 Binder.getCallingUid() != Process.myUid()) {
1765 throw new IllegalStateException("Calling must be system uid");
1766 }
1767 mLaunchingActivity.release();
1768 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001769 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001770 }
1771
1772 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001773 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1774 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001775 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001776 if ((NF = mFinishingActivities.size()) > 0) {
1777 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001778 mFinishingActivities.clear();
1779 }
1780
Craig Mautnerf3333272013-04-22 10:55:53 -07001781 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001782 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001783 mStartingUsers.clear();
1784 }
1785
Craig Mautnerf3333272013-04-22 10:55:53 -07001786 // Stop any activities that are scheduled to do so but have been
1787 // waiting for the next one to start.
1788 for (int i = 0; i < NS; i++) {
1789 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001790 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001791 if (stack != null) {
1792 if (r.finishing) {
1793 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1794 } else {
1795 stack.stopActivityLocked(r);
1796 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001797 }
1798 }
1799
1800 // Finish any activities that are scheduled to do so but have been
1801 // waiting for the next one to start.
1802 for (int i = 0; i < NF; i++) {
1803 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001804 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001805 if (stack != null) {
1806 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1807 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001808 }
1809
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001810 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001811 // Complete user switch
1812 if (startingUsers != null) {
1813 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001814 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001815 }
1816 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001817 }
1818
1819 mService.trimApplications();
1820 //dump();
1821 //mWindowManager.dump();
1822
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001824 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001825 }
1826
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001827 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001828 }
1829
Craig Mautner8e569572013-10-11 17:36:59 -07001830 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001831 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001832 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1833 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001834 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1835 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1836 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001837 }
Craig Mautner19091252013-10-05 00:03:53 -07001838 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001839 }
1840
1841 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001842 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1843 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001844 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1845 stacks.get(stackNdx).closeSystemDialogsLocked();
1846 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001847 }
1848 }
1849
Craig Mautner93529a42013-10-04 15:03:13 -07001850 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001851 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001852 }
1853
Craig Mautner8d341ef2013-03-26 09:03:27 -07001854 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001855 * Update the last used stack id for non-current user (current user's last
1856 * used stack is the focused stack)
1857 */
1858 void updateUserStackLocked(int userId, ActivityStack stack) {
1859 if (userId != mCurrentUser) {
1860 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1861 }
1862 }
1863
1864 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001865 * @return true if some activity was finished (or would have finished if doit were true).
1866 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001867 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1868 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001869 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1871 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001873 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001874 if (stack.finishDisabledPackageActivitiesLocked(
1875 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001876 didSomething = true;
1877 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001878 }
1879 }
1880 return didSomething;
1881 }
1882
Dianne Hackborna413dc02013-07-12 12:02:55 -07001883 void updatePreviousProcessLocked(ActivityRecord r) {
1884 // Now that this process has stopped, we may want to consider
1885 // it to be the previous app to try to keep around in case
1886 // the user wants to return to it.
1887
1888 // First, found out what is currently the foreground app, so that
1889 // we don't blow away the previous app if this activity is being
1890 // hosted by the process that is actually still the foreground.
1891 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001892 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1893 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001894 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1895 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001896 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001897 if (stack.mResumedActivity != null) {
1898 fgApp = stack.mResumedActivity.app;
1899 } else if (stack.mPausingActivity != null) {
1900 fgApp = stack.mPausingActivity.app;
1901 }
1902 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001903 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001904 }
1905 }
1906
1907 // Now set this one as the previous process, only if that really
1908 // makes sense to.
1909 if (r.app != null && fgApp != null && r.app != fgApp
1910 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001911 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001912 mService.mPreviousProcess = r.app;
1913 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1914 }
1915 }
1916
Wale Ogunwaled046a012015-12-24 13:05:59 -08001917 boolean resumeFocusedStackTopActivityLocked() {
1918 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001919 }
1920
Wale Ogunwaled046a012015-12-24 13:05:59 -08001921 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001922 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001923 if (targetStack != null && isFocusedStack(targetStack)) {
1924 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001925 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001926 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1927 if (r == null || r.state != RESUMED) {
1928 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001929 } else if (r.state == RESUMED) {
1930 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1931 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001932 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001933 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001934 }
1935
Todd Kennedy39bfee52016-02-24 10:28:21 -08001936 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1937 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1938 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1939 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1940 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1941 }
1942 }
1943 }
1944
Adrian Roos20d7df32016-01-12 18:59:43 +01001945 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1946 TaskRecord finishedTask = null;
1947 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001948 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1949 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001950 final int numStacks = stacks.size();
1951 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1952 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001953 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1954 if (stack == focusedStack || finishedTask == null) {
1955 finishedTask = t;
1956 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001957 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001958 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001959 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001960 }
1961
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001962 void finishVoiceTask(IVoiceInteractionSession session) {
1963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1964 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1965 final int numStacks = stacks.size();
1966 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1967 final ActivityStack stack = stacks.get(stackNdx);
1968 stack.finishVoiceTask(session);
1969 }
1970 }
1971 }
1972
Andrii Kulianc27916642016-04-12 17:59:27 -07001973 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1974 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001975 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1976 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001977 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001978 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1979 // Caller wants the home activity moved with it. To accomplish this,
1980 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001981 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001982 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001983 ActivityStack currentStack = task.getStack();
1984 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001985 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1986 + task + " to front. Stack is null");
1987 return;
1988 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001989
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001990 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001991 int stackId = options.getLaunchStackId();
1992 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001993 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001994 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001995 if (stackId == INVALID_STACK_ID) {
1996 stackId = task.getLaunchStackId();
1997 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001998 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08001999 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2000 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002001 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002002 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2003 // still need moveTaskToFrontLocked() below for any transition settings.
2004 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002005 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2006 resizeStackLocked(stackId, bounds,
2007 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002008 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002009 } else {
2010 // WM resizeTask must be done after the task is moved to the correct stack,
2011 // because Task's setBounds() also updates dim layer's bounds, but that has
2012 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002013 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002014 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002015 }
2016 }
2017
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002018 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002019 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002020 r == null ? null : r.appTimeTracker, reason);
2021
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002022 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002023 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002024
Andrii Kulian02b7a832016-10-06 23:11:56 -07002025 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002026 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002027 }
2028
Wale Ogunwale854809c2015-12-27 16:18:19 -08002029 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002030 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002031 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002032 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002033 return false;
2034 }
2035 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2036 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002037 }
2038
Winson Chung55893332017-02-17 17:13:10 -08002039 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002040 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002041 }
2042
Winson Chung55893332017-02-17 17:13:10 -08002043 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2044 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002045 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002046 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002047 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002048 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002049 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002050 return null;
2051 }
Winson Chung55893332017-02-17 17:13:10 -08002052 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002053 }
2054
Andrii Kulian16802aa2016-11-02 12:21:33 -07002055 /**
2056 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2057 * If there is no such stack, new dynamic stack can be created.
2058 * @param displayId Target display.
2059 * @param r Activity that should be launched there.
2060 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2061 */
2062 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2063 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2064 if (activityDisplay == null) {
2065 throw new IllegalArgumentException(
2066 "Display with displayId=" + displayId + " not found.");
2067 }
2068
2069 // Return the topmost valid stack on the display.
2070 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2071 final ActivityStack stack = activityDisplay.mStacks.get(i);
2072 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2073 return stack;
2074 }
2075 }
2076
2077 // If there is no valid stack on the external display - check if new dynamic stack will do.
2078 if (displayId != Display.DEFAULT_DISPLAY) {
2079 final int newDynamicStackId = getNextStackId();
2080 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2081 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2082 }
2083 }
2084
2085 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2086 return null;
2087 }
2088
Craig Mautner967212c2013-04-13 21:10:58 -07002089 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002090 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002091 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2092 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002093 }
2094 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002095 }
2096
Jorim Jaggife762342016-10-13 14:33:27 +02002097 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2098 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2099 }
2100
Andrii Kulian7fc22812016-12-28 13:04:11 -08002101 /**
2102 * Get next focusable stack in the system. This will search across displays and stacks
2103 * in last-focused order for a focusable and visible stack, different from the target stack.
2104 *
2105 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2106 * searching for next candidate.
2107 * @return Next focusable {@link ActivityStack}, null if not found.
2108 */
2109 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2110 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2111
2112 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2113 final int displayId = mTmpOrderedDisplayIds.get(i);
2114 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2115 if (stacks == null) {
2116 continue;
2117 }
2118 for (int j = stacks.size() - 1; j >= 0; --j) {
2119 final ActivityStack stack = stacks.get(j);
2120 if (stack != currentFocus && stack.isFocusable()
2121 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2122 return stack;
2123 }
2124 }
2125 }
2126
2127 return null;
2128 }
2129
Craig Mautneree2e45a2014-06-27 12:10:03 -07002130 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002131 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002132 }
2133
2134 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002135 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2136 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2137 final TaskRecord task = tasks.get(taskNdx);
2138 if (task.isHomeTask()) {
2139 final ArrayList<ActivityRecord> activities = task.mActivities;
2140 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2141 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002142 if (r.isHomeActivity()
2143 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002144 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002145 }
2146 }
2147 }
2148 }
2149 return null;
2150 }
2151
Chong Zhangb15758a2015-11-17 12:12:03 -08002152 /**
2153 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2154 * the docked stack itself, or if it's side-by-side to the docked stack.
2155 */
2156 boolean isStackDockedInEffect(int stackId) {
2157 return stackId == DOCKED_STACK_ID ||
2158 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2159 }
2160
Todd Kennedyca4d8422015-01-15 15:19:22 -08002161 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002162 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002163 ActivityContainer activityContainer =
2164 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002165 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002166 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2167 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002168 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002169 return activityContainer;
2170 }
2171
Craig Mautner34b73df2014-01-12 21:11:08 -08002172 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002173 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2174 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2175 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002176 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2177 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002178 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002179 }
2180 }
2181
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002182 void deleteActivityContainerRecord(int stackId) {
2183 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2184 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2185 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002186 }
2187
Jorim Jaggidc249c42015-12-15 14:57:31 -08002188 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002189 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002190 if (stackId == DOCKED_STACK_ID) {
2191 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002192 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002193 return;
2194 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002195 final ActivityStack stack = getStack(stackId);
2196 if (stack == null) {
2197 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2198 return;
2199 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002200
Jorim Jaggidc249c42015-12-15 14:57:31 -08002201 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002202 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2203 // stack size changing so things don't get out of sync.
2204 return;
2205 }
2206
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002207 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002208 mWindowManager.deferSurfaceLayout();
2209 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002210 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002211 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002212 stack.ensureVisibleActivitiesConfigurationLocked(
2213 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002214 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002215 } finally {
2216 mWindowManager.continueSurfaceLayout();
2217 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002218 }
2219 }
2220
Jorim Jaggi192086e2016-03-11 17:17:03 +01002221 void deferUpdateBounds(int stackId) {
2222 final ActivityStack stack = getStack(stackId);
2223 if (stack != null) {
2224 stack.deferUpdateBounds();
2225 }
2226 }
2227
2228 void continueUpdateBounds(int stackId) {
2229 final ActivityStack stack = getStack(stackId);
2230 if (stack != null) {
2231 stack.continueUpdateBounds();
2232 }
2233 }
2234
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002235 void notifyAppTransitionDone() {
2236 continueUpdateBounds(HOME_STACK_ID);
2237 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2238 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002239 final TaskRecord task =
2240 anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2241 if (task != null) {
2242 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002243 }
2244 }
2245 mResizingTasksDuringAnimation.clear();
2246 }
2247
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002248 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2249 final ActivityStack stack = getStack(fromStackId);
2250 if (stack == null) {
2251 return;
2252 }
2253
2254 mWindowManager.deferSurfaceLayout();
2255 try {
2256 if (fromStackId == DOCKED_STACK_ID) {
2257
2258 // We are moving all tasks from the docked stack to the fullscreen stack,
2259 // which is dismissing the docked stack, so resize all other stacks to
2260 // fullscreen here already so we don't end up with resize trashing.
2261 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2262 if (StackId.isResizeableByDockedStack(i)) {
2263 ActivityStack otherStack = getStack(i);
2264 if (otherStack != null) {
2265 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2266 true /* allowResizeInDockedMode */, DEFER_RESUME);
2267 }
2268 }
2269 }
2270
2271 // Also disable docked stack resizing since we have manually adjusted the
2272 // size of other stacks above and we don't want to trigger a docked stack
2273 // resize when we remove task from it below and it is detached from the
2274 // display because it no longer contains any tasks.
2275 mAllowDockedStackResize = false;
2276 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002277 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002278 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2279 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002280 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2281 final int size = tasks.size();
2282 if (onTop) {
2283 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002284 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002285 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002286 if (fromStackId == PINNED_STACK_ID) {
2287 // Update the return-to to reflect where the pinned stack task was moved
2288 // from so that we retain the stack that was previously visible if the
2289 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002290 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2291 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002292 MetricsLogger.action(mService.mContext,
2293 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002294 }
Winson Chung74666102017-02-22 17:49:24 -08002295 // Defer resume until all the tasks have been moved to the fullscreen stack
2296 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2297 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
2298 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002299 }
2300 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002301 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002302 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002303 final int position = fullscreenStack != null
2304 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2305 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002306 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung74666102017-02-22 17:49:24 -08002307 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
2308 DEFER_RESUME, "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002309 }
2310 }
Winson Chung74666102017-02-22 17:49:24 -08002311
2312 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2313 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002314 } finally {
2315 mAllowDockedStackResize = true;
2316 mWindowManager.continueSurfaceLayout();
2317 }
2318 }
2319
Jorim Jaggidc249c42015-12-15 14:57:31 -08002320 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002321 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2322 boolean preserveWindows) {
2323 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2324 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2325 false /* deferResume */);
2326 }
2327
2328 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2329 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2330 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002331
2332 if (!mAllowDockedStackResize) {
2333 // Docked stack resize currently disabled.
2334 return;
2335 }
2336
Jorim Jaggidc249c42015-12-15 14:57:31 -08002337 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2338 if (stack == null) {
2339 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2340 return;
2341 }
2342
2343 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2344 mWindowManager.deferSurfaceLayout();
2345 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002346 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2347 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002348 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002349 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002350
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002351 // TODO: Checking for isAttached might not be needed as if the user passes in null
2352 // dockedBounds then they want the docked stack to be dismissed.
2353 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2354 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002355 // In this case we make all other static stacks fullscreen and move all
2356 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002357 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002358
2359 // stack shouldn't contain anymore activities, so nothing to resume.
2360 r = null;
2361 } else {
2362 // Docked stacks occupy a dedicated region on screen so the size of all other
2363 // static stacks need to be adjusted so they don't overlap with the docked stack.
2364 // We get the bounds to use from window manager which has been adjusted for any
2365 // screen controls and is also the same for all stacks.
Matthew Nge15352e2016-12-20 15:36:29 -08002366 final Rect tempOtherTaskRect = new Rect();
2367 final Rect tempOtherTaskInsetRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002368 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002369 final ActivityStack current = getStack(i);
2370 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Nge15352e2016-12-20 15:36:29 -08002371 current.getStackDockedModeBounds(tempRect, tempOtherTaskRect,
2372 tempOtherTaskInsetRect, true /* ignoreVisibility */);
2373 resizeStackLocked(i, tempRect,
2374 !tempOtherTaskRect.isEmpty() ? tempOtherTaskRect :
2375 tempOtherTaskBounds,
2376 !tempOtherTaskInsetRect.isEmpty() ? tempOtherTaskInsetRect :
2377 tempOtherTaskInsetBounds,
2378 preserveWindows, true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002379 }
2380 }
2381 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002382 if (!deferResume) {
2383 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2384 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002385 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002386 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002387 mWindowManager.continueSurfaceLayout();
2388 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2389 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002390 }
2391
Robert Carr0d00c2e2016-02-29 17:45:02 -08002392 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2393 final ActivityStack stack = getStack(PINNED_STACK_ID);
2394 if (stack == null) {
2395 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2396 return;
2397 }
2398 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2399 mWindowManager.deferSurfaceLayout();
2400 try {
2401 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002402 Rect insetBounds = null;
2403 if (tempPinnedTaskBounds != null) {
2404 // We always use 0,0 as the position for the inset rect because
2405 // if we are getting insets at all in the pinned stack it must mean
2406 // we are headed for fullscreen.
2407 insetBounds = tempRect;
2408 insetBounds.top = 0;
2409 insetBounds.left = 0;
2410 insetBounds.right = tempPinnedTaskBounds.width();
2411 insetBounds.bottom = tempPinnedTaskBounds.height();
2412 }
2413 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002414 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002415 } finally {
2416 mWindowManager.continueSurfaceLayout();
2417 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2418 }
2419 }
2420
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002421 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002422 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002423 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002424 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002425 }
2426
Wale Ogunwale1666e312016-12-16 11:27:18 -08002427 final ActivityContainer activityContainer =
2428 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002429 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002430 }
2431
Winson Chung010927a2016-12-15 16:12:35 -08002432 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002433 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002434 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2435 * instead moved back onto the fullscreen stack.
2436 */
2437 void removeStackLocked(int stackId) {
2438 final ActivityStack stack = getStack(stackId);
2439 if (stack == null) {
2440 return;
2441 }
2442
2443 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2444 if (stack.getStackId() == PINNED_STACK_ID) {
2445 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2446 if (fullscreenStack != null) {
2447 final boolean isFullscreenStackVisible =
2448 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2449 for (int i = 0; i < tasks.size(); i++) {
2450 // Insert the task either at the top of the fullscreen stack if it is hidden,
2451 // or just under the top task if it is currently visible
2452 final int insertPosition = isFullscreenStackVisible
2453 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2454 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002455 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002456 // Defer resume until we remove all the tasks
2457 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2458 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002459 }
2460 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2461 resumeFocusedStackTopActivityLocked();
2462 } else {
2463 // If there is no fullscreen stack, then create the stack and move all the tasks
2464 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002465 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002466 }
2467 } else {
2468 for (int i = tasks.size() - 1; i >= 0; i--) {
2469 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2470 REMOVE_FROM_RECENTS);
2471 }
2472 }
2473 }
2474
2475 /**
2476 * Removes the task with the specified task id.
2477 *
2478 * @param taskId Identifier of the task to be removed.
2479 * @param killProcess Kill any process associated with the task if possible.
2480 * @param removeFromRecents Whether to also remove the task from recents.
2481 * @return Returns true if the given task was found and removed.
2482 */
2483 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2484 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2485 if (tr != null) {
2486 tr.removeTaskActivitiesLocked();
2487 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2488 if (tr.isPersistable) {
2489 mService.notifyTaskPersisterLocked(null, true);
2490 }
2491 return true;
2492 }
2493 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2494 return false;
2495 }
2496
2497 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2498 if (removeFromRecents) {
2499 mRecentTasks.remove(tr);
2500 tr.removedFromRecents();
2501 }
2502 ComponentName component = tr.getBaseIntent().getComponent();
2503 if (component == null) {
2504 Slog.w(TAG, "No component for base intent of task: " + tr);
2505 return;
2506 }
2507
2508 // Find any running services associated with this app and stop if needed.
2509 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2510
2511 if (!killProcess) {
2512 return;
2513 }
2514
2515 // Determine if the process(es) for this task should be killed.
2516 final String pkg = component.getPackageName();
2517 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2518 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2519 for (int i = 0; i < pmap.size(); i++) {
2520
2521 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2522 for (int j = 0; j < uids.size(); j++) {
2523 ProcessRecord proc = uids.valueAt(j);
2524 if (proc.userId != tr.userId) {
2525 // Don't kill process for a different user.
2526 continue;
2527 }
2528 if (proc == mService.mHomeProcess) {
2529 // Don't kill the home process along with tasks from the same package.
2530 continue;
2531 }
2532 if (!proc.pkgList.containsKey(pkg)) {
2533 // Don't kill process that is not associated with this task.
2534 continue;
2535 }
2536
2537 for (int k = 0; k < proc.activities.size(); k++) {
2538 TaskRecord otherTask = proc.activities.get(k).task;
2539 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2540 // Don't kill process(es) that has an activity in a different task that is
2541 // also in recents.
2542 return;
2543 }
2544 }
2545
2546 if (proc.foregroundServices) {
2547 // Don't kill process(es) with foreground service.
2548 return;
2549 }
2550
2551 // Add process to kill list.
2552 procsToKill.add(proc);
2553 }
2554 }
2555
2556 // Kill the running processes.
2557 for (int i = 0; i < procsToKill.size(); i++) {
2558 ProcessRecord pr = procsToKill.get(i);
2559 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2560 && pr.curReceivers.isEmpty()) {
2561 pr.kill("remove task", true);
2562 } else {
2563 // We delay killing processes that are not in the background or running a receiver.
2564 pr.waitingToKill = "remove task";
2565 }
2566 }
2567 }
2568
Craig Mautner4a1cb222013-12-04 16:14:06 -08002569 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002570 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002571 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2572 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002573 break;
2574 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002575 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002576 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002577 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002578 }
2579
Chong Zhang5dcb2752015-08-18 13:50:26 -07002580 /**
2581 * Restores a recent task to a stack
2582 * @param task The recent task to be restored.
2583 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002584 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2585 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002586 * @return true if the task has been restored successfully.
2587 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002588 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002589 if (!StackId.isStaticStack(stackId)) {
2590 // If stack is not static (or stack id is invalid) - use the default one.
2591 // This means that tasks that were on external displays will be restored on the
2592 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002593 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002594 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002595 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2596 // Put it in the fullscreen stack.
2597 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002598 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002599
Andrii Kulian02b7a832016-10-06 23:11:56 -07002600 final ActivityStack currentStack = task.getStack();
2601 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002602 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002603 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002604 // Nothing else to do since it is already restored in the right stack.
2605 return true;
2606 }
2607 // Remove current stack association, so we can re-associate the task with the
2608 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002609 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002610 }
2611
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002612 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002613 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002614
2615 if (stack == null) {
2616 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002617 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2618 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002619 return false;
2620 }
2621
Wale Ogunwale72919d22016-12-08 18:58:50 -08002622 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002623 // TODO: move call for creation here and other place into Stack.addTask()
2624 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002625 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2626 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002627 final ArrayList<ActivityRecord> activities = task.mActivities;
2628 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002629 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002630 }
2631 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002632 }
2633
Wale Ogunwale040b4702015-08-06 18:10:50 -07002634 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002635 * Move stack with all its existing content to specified display.
2636 * @param stackId Id of stack to move.
2637 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002638 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002639 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002640 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002641 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2642 if (activityDisplay == null) {
2643 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2644 + displayId);
2645 }
2646 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2647 if (activityContainer != null) {
2648 if (activityContainer.isAttachedLocked()) {
2649 if (activityContainer.getDisplayId() == displayId) {
2650 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2651 + " to its current displayId=" + displayId);
2652 }
2653
Andrii Kulian250d6532017-02-08 23:30:45 -08002654 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002655 } else {
2656 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2657 + stackId + " is not attached to any display.");
2658 }
2659 } else {
2660 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2661 + stackId);
2662 }
2663
2664 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2665 !PRESERVE_WINDOWS);
2666 // TODO(multi-display): resize stacks properly if moved from split-screen.
2667 }
2668
2669 /**
Winson Chung74666102017-02-22 17:49:24 -08002670 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2671 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002672 */
Winson Chung74666102017-02-22 17:49:24 -08002673 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002674 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002675
Winson Chung74666102017-02-22 17:49:24 -08002676 // Check that we aren't reparenting to the same stack that the task is already in
2677 if (prevStack != null && prevStack.mStackId == stackId) {
2678 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2679 + " already in stackId=" + stackId);
2680 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002681 }
2682
Winson Chung74666102017-02-22 17:49:24 -08002683 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2684 // support
2685 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2686 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2687 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002688 }
2689
Winson Chung74666102017-02-22 17:49:24 -08002690 // Ensure that we aren't trying to move into a freeform stack without freeform
2691 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002692 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002693 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2694 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002695 }
2696
Winson Chung74666102017-02-22 17:49:24 -08002697 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2698 // used for split-screen mode and will cause things like the docked divider to show up. We
2699 // instead leave the task in its current stack or move it to the fullscreen stack if it
2700 // isn't currently in a stack.
2701 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2702 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2703 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2704 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002705 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002706
Winson Chung74666102017-02-22 17:49:24 -08002707 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2708 // example, a docked stack is created which will lead to the stack we are moving from being
2709 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002710 try {
Winson Chung74666102017-02-22 17:49:24 -08002711 task.mTemporarilyUnresizable = true;
2712 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002713 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002714 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002715 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002716 }
2717
Wale Ogunwale079a0042015-10-24 11:44:07 -07002718 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2719 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2720 if (stack == null) {
2721 throw new IllegalArgumentException(
2722 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2723 }
2724
2725 final ActivityRecord r = stack.topRunningActivityLocked();
2726 if (r == null) {
2727 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2728 + " in stack=" + stack);
2729 return false;
2730 }
2731
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002732 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002733 Slog.w(TAG,
2734 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002735 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002736 return false;
2737 }
2738
Winson Chungb5c41b72016-12-07 15:00:47 -08002739 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2740 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002741 return true;
2742 }
2743
Winson Chungb5c41b72016-12-07 15:00:47 -08002744 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2745 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002746 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002747
Wale Ogunwale1666e312016-12-16 11:27:18 -08002748 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002749 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002750
Wale Ogunwale480dca02016-02-06 13:58:29 -08002751 try {
2752 final TaskRecord task = r.task;
2753
Andrii Kulian02b7a832016-10-06 23:11:56 -07002754 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002755 // An activity can't be pinned and visible behind at the same time. Go ahead and
2756 // release it from been visible behind before pinning.
2757 requestVisibleBehindLocked(r, false);
2758 }
2759
Wale Ogunwale480dca02016-02-06 13:58:29 -08002760 // Resize the pinned stack to match the current size of the task the activity we are
2761 // going to be moving is currently contained in. We do this to have the right starting
2762 // animation bounds for the pinned stack to the desired bounds the caller wants.
2763 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2764 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002765 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002766
2767 if (task.mActivities.size() == 1) {
2768 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002769 // the stack without re-parenting the activity in a different task. We don't
2770 // move the home stack forward if we are currently entering picture-in-picture
2771 // while pausing because that changes the focused stack and may prevent the new
2772 // starting activity from resuming.
2773 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
2774 && !r.supportsPictureInPictureWhilePausing) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002775 // Move the home stack forward if the task we just moved to the pinned stack
2776 // was launched from home so home should be visible behind it.
2777 moveHomeStackToFront(reason);
2778 }
Winson Chung74666102017-02-22 17:49:24 -08002779 // Defer resume until below
2780 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2781 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002782 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002783 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002784 // reveal/leave the other activities in their original task.
2785
2786 // Currently, we don't support reparenting activities across tasks in two different
2787 // stacks, so instead, just create a new task in the same stack, reparent the
2788 // activity into that task, and then reparent the whole task to the new stack. This
2789 // ensures that all the necessary work to migrate states in the old and new stacks
2790 // is also done.
2791 final TaskRecord newTask = task.getStack().createTaskRecord(
2792 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2793 r.mActivityType);
2794 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2795
2796 // Defer resume until below
2797 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2798 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002799 }
Winson Chungc2baac02017-01-11 13:34:47 -08002800
2801 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2802 // to the pinned stack
2803 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002804 } finally {
2805 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002806 }
2807
Wale Ogunwale480dca02016-02-06 13:58:29 -08002808 // The task might have already been running and its visibility needs to be synchronized
2809 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002810 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002811 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002812
Winson Chung55893332017-02-17 17:13:10 -08002813 stack.animateResizePinnedStack(bounds, -1 /* animationDuration */);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002814 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002815 }
2816
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002817 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002818 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2819 if (r == null || !r.isFocusable()) {
2820 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2821 "moveActivityStackToFront: unfocusable r=" + r);
2822 return false;
2823 }
2824
2825 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002826 final ActivityStack stack = r.getStack();
2827 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002828 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2829 + r + " task=" + task);
2830 return false;
2831 }
2832
Chong Zhang6cda19c2016-06-14 19:07:56 -07002833 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2834 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2835 "moveActivityStackToFront: already on top, r=" + r);
2836 return false;
2837 }
2838
2839 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2840 "moveActivityStackToFront: r=" + r);
2841
2842 stack.moveToFront(reason, task);
2843 return true;
2844 }
2845
Craig Mautnerac6f8432013-07-17 13:24:59 -07002846 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002847 mTmpFindTaskResult.r = null;
2848 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002849 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002850 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2851 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002852 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2853 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002854 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002855 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002856 continue;
2857 }
2858 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002859 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2860 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002861 continue;
2862 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002863 stack.findTaskLocked(r, mTmpFindTaskResult);
2864 // It is possible to have task in multiple stacks with the same root affinity.
2865 // If the match we found was based on root affinity we keep on looking to see if
2866 // there is a better match in another stack. We eventually return the match based
2867 // on root affinity if we don't find a better match.
2868 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2869 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002870 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002871 }
2872 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002873 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2874 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002875 }
2876
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002877 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2878 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002879 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2880 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002882 final ActivityRecord ar = stacks.get(stackNdx)
2883 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002884 if (ar != null) {
2885 return ar;
2886 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002887 }
2888 }
2889 return null;
2890 }
2891
Craig Mautner8d341ef2013-03-26 09:03:27 -07002892 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002893 scheduleSleepTimeout();
2894 if (!mGoingToSleep.isHeld()) {
2895 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002896 if (mLaunchingActivity.isHeld()) {
2897 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2898 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002899 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002900 mLaunchingActivity.release();
2901 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002902 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002903 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002904 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002905 }
2906
2907 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002908 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002909
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002910 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002911 final long endTime = System.currentTimeMillis() + timeout;
2912 while (true) {
2913 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002914 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2915 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002916 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2917 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2918 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002919 }
2920 if (cantShutdown) {
2921 long timeRemaining = endTime - System.currentTimeMillis();
2922 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002923 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002924 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002925 } catch (InterruptedException e) {
2926 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002927 } else {
2928 Slog.w(TAG, "Activity manager shutdown timed out");
2929 timedout = true;
2930 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002931 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002932 } else {
2933 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002934 }
2935 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002936
2937 // Force checkReadyForSleep to complete.
2938 mSleepTimeout = true;
2939 checkReadyForSleepLocked();
2940
Craig Mautner8d341ef2013-03-26 09:03:27 -07002941 return timedout;
2942 }
2943
2944 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002945 removeSleepTimeouts();
2946 if (mGoingToSleep.isHeld()) {
2947 mGoingToSleep.release();
2948 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2952 final ActivityStack stack = stacks.get(stackNdx);
2953 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002954 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002955 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002956 }
Craig Mautner5314a402013-09-26 12:40:16 -07002957 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002958 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002959 mGoingToSleepActivities.clear();
2960 }
2961
2962 void activitySleptLocked(ActivityRecord r) {
2963 mGoingToSleepActivities.remove(r);
2964 checkReadyForSleepLocked();
2965 }
2966
2967 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002968 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002969 // Do not care.
2970 return;
2971 }
2972
2973 if (!mSleepTimeout) {
2974 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2976 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2978 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2979 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002980 }
2981
2982 if (mStoppingActivities.size() > 0) {
2983 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002984 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002985 + mStoppingActivities.size() + " activities");
2986 scheduleIdleLocked();
2987 dontSleep = true;
2988 }
2989
2990 if (mGoingToSleepActivities.size() > 0) {
2991 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002992 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002993 + mGoingToSleepActivities.size() + " activities");
2994 dontSleep = true;
2995 }
2996
2997 if (dontSleep) {
2998 return;
2999 }
3000 }
3001
Wei Wang65c7a152016-06-02 18:51:22 -07003002 // Send launch end powerhint before going sleep
3003 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3004
Craig Mautnere0a38842013-12-16 16:14:02 -08003005 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3006 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003007 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3008 stacks.get(stackNdx).goToSleep();
3009 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003010 }
3011
3012 removeSleepTimeouts();
3013
3014 if (mGoingToSleep.isHeld()) {
3015 mGoingToSleep.release();
3016 }
3017 if (mService.mShuttingDown) {
3018 mService.notifyAll();
3019 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003020 }
3021
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003022 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003023 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003024 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003025 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003026 }
3027 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003028 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003029 mWindowManager.executeAppTransition();
3030 return true;
3031 }
3032 return false;
3033 }
3034
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003036 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3037 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003038 int stackNdx = stacks.size() - 1;
3039 while (stackNdx >= 0) {
3040 stacks.get(stackNdx).handleAppCrashLocked(app);
3041 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003042 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003043 }
3044 }
3045
Jose Lima4b6c6692014-08-12 17:41:12 -07003046 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003047 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003048 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003049 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3050 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003051 return false;
3052 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003053
3054 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3055 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3056 + " visible=" + visible + " stackId=" + stack.mStackId
3057 + " can't contain visible behind activities");
3058 return false;
3059 }
3060
Jose Lima4b6c6692014-08-12 17:41:12 -07003061 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003062 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3063 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003064
3065 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003066 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003067 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003068 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003069 return true;
3070 }
3071
3072 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003073 if (visible && top.fullscreen) {
3074 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003075 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3076 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3077 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3078 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003079 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003080 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3081 // Only the activity set as currently visible behind should actively reset its
3082 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003083 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3084 "requestVisibleBehind: returning visible=" + visible
3085 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3086 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003087 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003088 }
3089
Jose Lima4b6c6692014-08-12 17:41:12 -07003090 stack.setVisibleBehindActivity(visible ? r : null);
3091 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003092 // If there is a translucent home activity, we need to force it stop being translucent,
3093 // because we can't depend on the application to necessarily perform that operation.
3094 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003095 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003096 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003097 mService.convertFromTranslucent(next.appToken);
3098 }
3099 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003100 if (top.app != null && top.app.thread != null) {
3101 // Notify the top app of the change.
3102 try {
3103 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3104 } catch (RemoteException e) {
3105 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003106 }
3107 return true;
3108 }
3109
Craig Mautnerbb742462014-07-07 15:28:55 -07003110 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003111 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003112 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003113 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003114
3115 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003116 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003117 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003118 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003119 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003120
3121 // When launching tasks behind, update the last active time of the top task after the new
3122 // task has been shown briefly
3123 final ActivityRecord top = stack.topActivity();
3124 if (top != null) {
3125 top.task.touchActiveTime();
3126 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003127 }
3128
3129 void scheduleLaunchTaskBehindComplete(IBinder token) {
3130 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3131 }
3132
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003133 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3134 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003135 mKeyguardController.beginActivityVisibilityUpdate();
3136 try {
3137 // First the front stacks. In case any are not fullscreen and are in front of home.
3138 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3139 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3140 final int topStackNdx = stacks.size() - 1;
3141 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3142 final ActivityStack stack = stacks.get(stackNdx);
3143 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3144 }
Craig Mautner580ea812013-04-25 12:58:38 -07003145 }
Jorim Jaggife762342016-10-13 14:33:27 +02003146 } finally {
3147 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003148 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003149 }
3150
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003151 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3152 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3153 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3154 final int topStackNdx = stacks.size() - 1;
3155 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3156 final ActivityStack stack = stacks.get(stackNdx);
3157 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3158 }
3159 }
3160 }
3161
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003162 void invalidateTaskLayers() {
3163 mTaskLayersChanged = true;
3164 }
3165
3166 void rankTaskLayersIfNeeded() {
3167 if (!mTaskLayersChanged) {
3168 return;
3169 }
3170 mTaskLayersChanged = false;
3171 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3172 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3173 int baseLayer = 0;
3174 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3175 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3176 }
3177 }
3178 }
3179
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003180 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3181 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3182 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3183 final int topStackNdx = stacks.size() - 1;
3184 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3185 final ActivityStack stack = stacks.get(stackNdx);
3186 stack.clearOtherAppTimeTrackers(except);
3187 }
3188 }
3189 }
3190
Craig Mautner8d341ef2013-03-26 09:03:27 -07003191 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003192 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3193 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003194 final int numStacks = stacks.size();
3195 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3196 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003197 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003198 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003199 }
3200 }
3201
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003202 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3203 // Examine all activities currently running in the process.
3204 TaskRecord firstTask = null;
3205 // Tasks is non-null only if two or more tasks are found.
3206 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003207 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3208 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003209 ActivityRecord r = app.activities.get(i);
3210 // First, if we find an activity that is in the process of being destroyed,
3211 // then we just aren't going to do anything for now; we want things to settle
3212 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003213 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003214 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003215 return;
3216 }
3217 // Don't consider any activies that are currently not in a state where they
3218 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003219 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3220 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003221 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003222 continue;
3223 }
3224 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003225 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003226 + " from " + r);
3227 if (firstTask == null) {
3228 firstTask = r.task;
3229 } else if (firstTask != r.task) {
3230 if (tasks == null) {
3231 tasks = new ArraySet<>();
3232 tasks.add(firstTask);
3233 }
3234 tasks.add(r.task);
3235 }
3236 }
3237 }
3238 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003239 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003240 return;
3241 }
3242 // If we have activities in multiple tasks that are in a position to be destroyed,
3243 // let's iterate through the tasks and release the oldest one.
3244 final int numDisplays = mActivityDisplays.size();
3245 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3246 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3247 // Step through all stacks starting from behind, to hit the oldest things first.
3248 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3249 final ActivityStack stack = stacks.get(stackNdx);
3250 // Try to release activities in this stack; if we manage to, we are done.
3251 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3252 return;
3253 }
3254 }
3255 }
3256 }
3257
Amith Yamasani37a40c22015-06-17 13:25:42 -07003258 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003259 final int focusStackId = mFocusedStack.getStackId();
3260 // We dismiss the docked stack whenever we switch users.
3261 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003262 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3263 // also cause all tasks to be moved to the fullscreen stack at a position that is
3264 // appropriate.
3265 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003266
3267 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003268 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003269 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003270
Craig Mautner858d8a62013-04-23 17:08:34 -07003271 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003272 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3273 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003274 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003275 final ActivityStack stack = stacks.get(stackNdx);
3276 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003277 TaskRecord task = stack.topTask();
3278 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003279 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003280 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003281 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003282 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003283
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003284 ActivityStack stack = getStack(restoreStackId);
3285 if (stack == null) {
3286 stack = mHomeStack;
3287 }
3288 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003289 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003290 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003291 } else {
3292 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003293 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003294 }
Craig Mautner93529a42013-10-04 15:03:13 -07003295 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003296 }
3297
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003298 /** Checks whether the userid is a profile of the current user. */
3299 boolean isCurrentProfileLocked(int userId) {
3300 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003301 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003302 }
3303
Winson Chung4dabf232017-01-25 13:25:22 -08003304 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3305 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003306 ArrayList<ActivityRecord> stops = null;
3307
3308 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003309 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3310 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003311 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3312 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003313 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003314 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003315 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3316 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003317 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003318 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003319 if (s.finishing) {
3320 // If this activity is finishing, it is sitting on top of
3321 // everyone else but we now know it is no longer needed...
3322 // so get rid of it. Otherwise, we need to go through the
3323 // normal flow and hide it once we determine that it is
3324 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003325 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003326 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003327 }
3328 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003329 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003330 if (!processPausingActivities && s.state == PAUSING) {
3331 // Defer processing pausing activities in this iteration and reschedule
3332 // a delayed idle to reprocess it again
3333 removeTimeoutsForActivityLocked(idleActivity);
3334 scheduleIdleTimeoutLocked(idleActivity);
3335 continue;
3336 }
3337
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003338 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003339 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003340 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003341 }
3342 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003343 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003344 }
3345 }
3346
3347 return stops;
3348 }
3349
Craig Mautnercf910b02013-04-23 11:23:27 -07003350 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003351 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3352 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3353 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3354 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003355 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003356 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003357 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003358 if (r == null) Slog.e(TAG,
3359 "validateTop...: null top activity, stack=" + stack);
3360 else {
3361 final ActivityRecord pausing = stack.mPausingActivity;
3362 if (pausing != null && pausing == r) Slog.e(TAG,
3363 "validateTop...: top stack has pausing activity r=" + r
3364 + " state=" + state);
3365 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3366 "validateTop...: activity in front not resumed r=" + r
3367 + " state=" + state);
3368 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003369 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003370 final ActivityRecord resumed = stack.mResumedActivity;
3371 if (resumed != null && resumed == r) Slog.e(TAG,
3372 "validateTop...: back stack has resumed activity r=" + r
3373 + " state=" + state);
3374 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3375 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003376 }
3377 }
3378 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003379 }
3380
Craig Mautnere0570202015-05-13 13:06:11 -07003381 private String lockTaskModeToString() {
3382 switch (mLockTaskModeState) {
3383 case LOCK_TASK_MODE_LOCKED:
3384 return "LOCKED";
3385 case LOCK_TASK_MODE_PINNED:
3386 return "PINNED";
3387 case LOCK_TASK_MODE_NONE:
3388 return "NONE";
3389 default: return "unknown=" + mLockTaskModeState;
3390 }
3391 }
3392
Craig Mautner27084302013-03-25 08:05:25 -07003393 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003394 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003396 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003397 pw.print(prefix);
3398 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003399 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003400 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003401 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003402 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3403 if (packages.size() > 0) {
3404 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3405 for (int i = 0; i < packages.size(); ++i) {
3406 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3407 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3408 }
3409 }
3410 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3411 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003412 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003413
Winson43d1f262016-06-14 16:05:55 -07003414 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003415 * Dump all connected displays' configurations.
3416 * @param prefix Prefix to apply to each line of the dump.
3417 */
3418 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3419 pw.print(prefix); pw.println("Display override configurations:");
3420 final int displayCount = mActivityDisplays.size();
3421 for (int i = 0; i < displayCount; i++) {
3422 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3423 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3424 pw.println(activityDisplay.getOverrideConfiguration());
3425 }
3426 }
3427
3428 /**
Winson43d1f262016-06-14 16:05:55 -07003429 * Dumps the activities matching the given {@param name} in the either the focused stack
3430 * or all visible stacks if {@param dumpVisibleStacks} is true.
3431 */
Winson Chung6998bc42017-02-28 17:07:05 -08003432 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3433 boolean dumpFocusedStackOnly) {
3434 if (dumpFocusedStackOnly) {
3435 return mFocusedStack.getDumpActivitiesLocked(name);
3436 } else {
Winson43d1f262016-06-14 16:05:55 -07003437 ArrayList<ActivityRecord> activities = new ArrayList<>();
3438 int numDisplays = mActivityDisplays.size();
3439 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3440 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3441 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3442 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003443 if (!dumpVisibleStacksOnly ||
3444 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003445 activities.addAll(stack.getDumpActivitiesLocked(name));
3446 }
3447 }
3448 }
3449 return activities;
Winson43d1f262016-06-14 16:05:55 -07003450 }
Craig Mautner20e72272013-04-01 13:45:53 -07003451 }
3452
Dianne Hackborn390517b2013-05-30 15:03:32 -07003453 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3454 boolean needSep, String prefix) {
3455 if (activity != null) {
3456 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3457 if (needSep) {
3458 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003459 }
3460 pw.print(prefix);
3461 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003462 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003463 }
3464 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003465 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003466 }
3467
Craig Mautner8d341ef2013-03-26 09:03:27 -07003468 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3469 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003470 boolean printed = false;
3471 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003472 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3473 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003474 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003475 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003476 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003477 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003478 final ActivityStack stack = stacks.get(stackNdx);
3479 StringBuilder stackHeader = new StringBuilder(128);
3480 stackHeader.append(" Stack #");
3481 stackHeader.append(stack.mStackId);
3482 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003483 stackHeader.append("\n");
3484 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3485 stackHeader.append("\n");
3486 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003487 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3488 needSep, stackHeader.toString());
3489 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3490 !dumpAll, false, dumpPackage, true,
3491 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003492
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 needSep = printed;
3494 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3495 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003496 if (pr) {
3497 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003498 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003499 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003500 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3501 " mResumedActivity: ");
3502 if (pr) {
3503 printed = true;
3504 needSep = false;
3505 }
3506 if (dumpAll) {
3507 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3508 " mLastPausedActivity: ");
3509 if (pr) {
3510 printed = true;
3511 needSep = true;
3512 }
3513 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3514 needSep, " mLastNoHistoryActivity: ");
3515 }
3516 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 }
3518 }
3519
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003520 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3521 false, dumpPackage, true, " Activities waiting to finish:", null);
3522 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3523 false, dumpPackage, true, " Activities waiting to stop:", null);
3524 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3525 false, dumpPackage, true, " Activities waiting for another to become visible:",
3526 null);
3527 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3528 false, dumpPackage, true, " Activities waiting to sleep:", null);
3529 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3530 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003531
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003532 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003533 }
3534
Dianne Hackborn390517b2013-05-30 15:03:32 -07003535 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003536 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003537 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003538 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003539 String innerPrefix = null;
3540 String[] args = null;
3541 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003542 for (int i=list.size()-1; i>=0; i--) {
3543 final ActivityRecord r = list.get(i);
3544 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3545 continue;
3546 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003547 if (innerPrefix == null) {
3548 innerPrefix = prefix + " ";
3549 args = new String[0];
3550 }
3551 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003552 final boolean full = !brief && (complete || !r.isInHistory());
3553 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003554 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003555 needNL = false;
3556 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003557 if (header1 != null) {
3558 pw.println(header1);
3559 header1 = null;
3560 }
3561 if (header2 != null) {
3562 pw.println(header2);
3563 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003564 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003565 if (lastTask != r.task) {
3566 lastTask = r.task;
3567 pw.print(prefix);
3568 pw.print(full ? "* " : " ");
3569 pw.println(lastTask);
3570 if (full) {
3571 lastTask.dump(pw, prefix + " ");
3572 } else if (complete) {
3573 // Complete + brief == give a summary. Isn't that obvious?!?
3574 if (lastTask.intent != null) {
3575 pw.print(prefix); pw.print(" ");
3576 pw.println(lastTask.intent.toInsecureStringWithClip());
3577 }
3578 }
3579 }
3580 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3581 pw.print(" #"); pw.print(i); pw.print(": ");
3582 pw.println(r);
3583 if (full) {
3584 r.dump(pw, innerPrefix);
3585 } else if (complete) {
3586 // Complete + brief == give a summary. Isn't that obvious?!?
3587 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3588 if (r.app != null) {
3589 pw.print(innerPrefix); pw.println(r.app);
3590 }
3591 }
3592 if (client && r.app != null && r.app.thread != null) {
3593 // flush anything that is already in the PrintWriter since the thread is going
3594 // to write to the file descriptor directly
3595 pw.flush();
3596 try {
3597 TransferPipe tp = new TransferPipe();
3598 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003599 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003600 // Short timeout, since blocking here can
3601 // deadlock with the application.
3602 tp.go(fd, 2000);
3603 } finally {
3604 tp.kill();
3605 }
3606 } catch (IOException e) {
3607 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3608 } catch (RemoteException e) {
3609 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3610 }
3611 needNL = true;
3612 }
3613 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003614 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003615 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003616
Craig Mautnerf3333272013-04-22 10:55:53 -07003617 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003618 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3619 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003620 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3621 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003622 }
3623
3624 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003625 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003626 }
3627
3628 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003629 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3630 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003631 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3632 }
3633
Craig Mautner05d29032013-05-03 13:40:13 -07003634 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003635 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3636 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3637 }
Craig Mautner05d29032013-05-03 13:40:13 -07003638 }
3639
Craig Mautner0eea92c2013-05-16 13:35:39 -07003640 void removeSleepTimeouts() {
3641 mSleepTimeout = false;
3642 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3643 }
3644
3645 final void scheduleSleepTimeout() {
3646 removeSleepTimeouts();
3647 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3648 }
3649
Craig Mautner4a1cb222013-12-04 16:14:06 -08003650 @Override
3651 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003652 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3654 }
3655
3656 @Override
3657 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003658 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003659 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3660 }
3661
3662 @Override
3663 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003664 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003665 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3666 }
3667
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003668 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003669 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003670 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003671 newDisplay = mActivityDisplays.get(displayId) == null;
3672 if (newDisplay) {
3673 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003674 if (activityDisplay.mDisplay == null) {
3675 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3676 return;
3677 }
Craig Mautner4504de52013-12-20 09:06:56 -08003678 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003679 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003680 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 }
Craig Mautner4504de52013-12-20 09:06:56 -08003682 if (newDisplay) {
3683 mWindowManager.onDisplayAdded(displayId);
3684 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003685 }
3686
Andrii Kulianca007a62016-11-22 16:33:28 -08003687 /** Check if display with specified id is added to the list. */
3688 boolean isDisplayAdded(int displayId) {
3689 return mActivityDisplays.get(displayId) != null;
3690 }
3691
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003692 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003693 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003694 mService.mContext.getResources().getDimensionPixelSize(
3695 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003696 }
3697
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003698 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003699 if (displayId == DEFAULT_DISPLAY) {
3700 throw new IllegalArgumentException("Can't remove the primary display.");
3701 }
3702
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003704 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3705 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003706 final boolean destroyContentOnRemoval
3707 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003708 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3709 while (!stacks.isEmpty()) {
3710 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003711 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003712 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3713 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003714 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003715 } else {
3716 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3717 // a valid launch stack for all activities. This way the task history from
3718 // external display will be preserved on primary after move.
3719 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003720 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003722 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723 }
3724 }
3725 mWindowManager.onDisplayRemoved(displayId);
3726 }
3727
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003728 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003729 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003730 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3731 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003732 // TODO: Update the bounds.
3733 }
3734 }
3735 mWindowManager.onDisplayChanged(displayId);
3736 }
3737
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003738 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003739 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003741 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003742 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003743 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003744 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003745 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003746 info.position = display != null
3747 ? display.mStacks.indexOf(stack)
3748 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003749
3750 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3751 final int numTasks = tasks.size();
3752 int[] taskIds = new int[numTasks];
3753 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003754 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003755 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003756 for (int i = 0; i < numTasks; ++i) {
3757 final TaskRecord task = tasks.get(i);
3758 taskIds[i] = task.taskId;
3759 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3760 : task.realActivity != null ? task.realActivity.flattenToString()
3761 : task.getTopActivity() != null ? task.getTopActivity().packageName
3762 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003763 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003764 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003765 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003766 }
3767 info.taskIds = taskIds;
3768 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003769 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003770 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003771
3772 final ActivityRecord top = stack.topRunningActivityLocked();
3773 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003774 return info;
3775 }
3776
3777 StackInfo getStackInfoLocked(int stackId) {
3778 ActivityStack stack = getStack(stackId);
3779 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003780 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003781 }
3782 return null;
3783 }
3784
3785 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003786 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003787 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3788 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003789 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003790 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 }
3792 }
3793 return list;
3794 }
3795
Craig Mautner15df08a2015-04-01 12:17:18 -07003796 TaskRecord getLockedTaskLocked() {
3797 final int top = mLockTaskModeTasks.size() - 1;
3798 if (top >= 0) {
3799 return mLockTaskModeTasks.get(top);
3800 }
3801 return null;
3802 }
3803
3804 boolean isLockedTask(TaskRecord task) {
3805 return mLockTaskModeTasks.contains(task);
3806 }
3807
3808 boolean isLastLockedTask(TaskRecord task) {
3809 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3810 }
3811
3812 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003813 if (!mLockTaskModeTasks.remove(task)) {
3814 return;
3815 }
3816 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3817 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003818 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003819 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3820 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003821 final Message lockTaskMsg = Message.obtain();
3822 lockTaskMsg.arg1 = task.userId;
3823 lockTaskMsg.what = LOCK_TASK_END_MSG;
3824 mHandler.sendMessage(lockTaskMsg);
3825 }
3826 }
3827
Andrii Kulianc27916642016-04-12 17:59:27 -07003828 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3829 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3830 false /* forceNonResizable */);
3831 }
3832
Jorim Jaggid53f0922016-04-06 22:16:23 -07003833 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003834 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003835 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3836 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003837 return;
3838 }
3839
Jorim Jaggicd13d332016-04-27 15:40:20 -07003840 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003841 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003842 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003843 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003844
Andrii Kulianc27916642016-04-12 17:59:27 -07003845 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3846 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003847 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003848 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003849 && !topActivity.noDisplay) {
3850 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003851 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3852 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003853 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003854 }
3855
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003856 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003857 if (mLockTaskNotify != null) {
3858 mLockTaskNotify.showToast(mLockTaskModeState);
3859 }
Jason Monka8f569c2014-07-07 12:15:21 -04003860 }
3861
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003862 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3863 if (mLockTaskModeTasks.contains(task)) {
3864 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3865 }
3866 }
3867
Craig Mautner432f64e2015-05-20 14:59:57 -07003868 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3869 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003870 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003871 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003872 final TaskRecord lockedTask = getLockedTaskLocked();
3873 if (lockedTask != null) {
3874 removeLockedTaskLocked(lockedTask);
3875 if (!mLockTaskModeTasks.isEmpty()) {
3876 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003877 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3878 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003879 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003880 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003881 return;
3882 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003883 }
Craig Mautnere0570202015-05-13 13:06:11 -07003884 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3885 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003886 return;
3887 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003888
3889 // Should have already been checked, but do it again.
3890 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003891 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3892 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003893 return;
3894 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003895 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003896 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003897 return;
3898 }
3899
3900 if (mLockTaskModeTasks.isEmpty()) {
3901 // First locktask.
3902 final Message lockTaskMsg = Message.obtain();
3903 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3904 lockTaskMsg.arg1 = task.userId;
3905 lockTaskMsg.what = LOCK_TASK_START_MSG;
3906 lockTaskMsg.arg2 = lockTaskModeState;
3907 mHandler.sendMessage(lockTaskMsg);
3908 }
3909 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003910 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3911 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003912 mLockTaskModeTasks.remove(task);
3913 mLockTaskModeTasks.add(task);
3914
3915 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003916 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003917 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003918
3919 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003920 findTaskToMoveToFrontLocked(task, 0, null, reason,
3921 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003922 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003923 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003924 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003925 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003926 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003927 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003928 }
3929
3930 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003931 return isLockTaskModeViolation(task, false);
3932 }
3933
3934 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3935 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003936 return false;
3937 }
3938 final int lockTaskAuth = task.mLockTaskAuth;
3939 switch (lockTaskAuth) {
3940 case LOCK_TASK_AUTH_DONT_LOCK:
3941 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003942 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003943 case LOCK_TASK_AUTH_LAUNCHABLE:
3944 case LOCK_TASK_AUTH_WHITELISTED:
3945 return false;
3946 case LOCK_TASK_AUTH_PINNABLE:
3947 // Pinnable tasks can't be launched on top of locktask tasks.
3948 return !mLockTaskModeTasks.isEmpty();
3949 default:
3950 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3951 return true;
3952 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003953 }
3954
Craig Mautner15df08a2015-04-01 12:17:18 -07003955 void onLockTaskPackagesUpdatedLocked() {
3956 boolean didSomething = false;
3957 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3958 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003959 final boolean wasWhitelisted =
3960 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3961 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003962 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003963 final boolean isWhitelisted =
3964 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3965 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3966 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003967 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003968 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3969 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 removeLockedTaskLocked(lockedTask);
3971 lockedTask.performClearTaskLocked();
3972 didSomething = true;
3973 }
3974 }
3975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3976 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3978 final ActivityStack stack = stacks.get(stackNdx);
3979 stack.onLockTaskPackagesUpdatedLocked();
3980 }
3981 }
Craig Mautnere0570202015-05-13 13:06:11 -07003982 final ActivityRecord r = topRunningActivityLocked();
3983 final TaskRecord task = r != null ? r.task : null;
3984 if (mLockTaskModeTasks.isEmpty() && task != null
3985 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3986 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003987 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3988 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3989 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3990 false);
3991 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003992 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003993 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003994 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003995 }
3996 }
3997
Benjamin Franz43261142015-02-11 15:59:44 +00003998 int getLockTaskModeState() {
3999 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004000 }
4001
Jorim Jaggife89d122015-12-22 16:28:44 +01004002 void activityRelaunchedLocked(IBinder token) {
4003 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004004 if (mService.isSleepingOrShuttingDownLocked()) {
4005 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4006 if (r != null) {
4007 r.setSleeping(true, true);
4008 }
4009 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004010 }
4011
4012 void activityRelaunchingLocked(ActivityRecord r) {
4013 mWindowManager.notifyAppRelaunching(r.appToken);
4014 }
4015
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004016 void logStackState() {
4017 mActivityMetricsLogger.logWindowState();
4018 }
4019
Andrii Kulian933076d2016-03-29 17:04:42 -07004020 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004021 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4022 final ActivityRecord r = task.mActivities.get(i);
4023 if (r.app != null && r.app.thread != null) {
4024 mMultiWindowModeChangedActivities.add(r);
4025 }
4026 }
4027
4028 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4029 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4030 }
4031 }
4032
Andrii Kulian933076d2016-03-29 17:04:42 -07004033 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004034 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004035 if (prevStack == null || prevStack == stack
4036 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4037 return;
4038 }
4039
4040 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4041 final ActivityRecord r = task.mActivities.get(i);
4042 if (r.app != null && r.app.thread != null) {
4043 mPipModeChangedActivities.add(r);
4044 }
4045 }
4046
4047 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4048 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4049 }
4050 }
4051
Tony Mak853304c2016-04-18 15:17:41 +01004052 void setDockedStackMinimized(boolean minimized) {
4053 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004054 }
4055
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004056 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004057
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004058 public ActivityStackSupervisorHandler(Looper looper) {
4059 super(looper);
4060 }
4061
Winson Chung4dabf232017-01-25 13:25:22 -08004062 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004063 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004064 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4065 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004066 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004067 }
4068
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004069 @Override
4070 public void handleMessage(Message msg) {
4071 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004072 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4073 synchronized (mService) {
4074 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4075 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004076 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004077 }
4078 }
4079 } break;
4080 case REPORT_PIP_MODE_CHANGED_MSG: {
4081 synchronized (mService) {
4082 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4083 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004084 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004085 }
4086 }
4087 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004088 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004089 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4090 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004091 if (mService.mDidDexOpt) {
4092 mService.mDidDexOpt = false;
4093 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4094 nmsg.obj = msg.obj;
4095 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4096 return;
4097 }
4098 // We don't at this point know if the activity is fullscreen,
4099 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004100 activityIdleInternal((ActivityRecord) msg.obj,
4101 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004102 } break;
4103 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004104 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004105 activityIdleInternal((ActivityRecord) msg.obj,
4106 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004107 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004108 case RESUME_TOP_ACTIVITY_MSG: {
4109 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004110 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004111 }
4112 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004113 case SLEEP_TIMEOUT_MSG: {
4114 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004115 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004116 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4117 mSleepTimeout = true;
4118 checkReadyForSleepLocked();
4119 }
4120 }
4121 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004122 case LAUNCH_TIMEOUT_MSG: {
4123 if (mService.mDidDexOpt) {
4124 mService.mDidDexOpt = false;
4125 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4126 return;
4127 }
4128 synchronized (mService) {
4129 if (mLaunchingActivity.isHeld()) {
4130 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4131 if (VALIDATE_WAKE_LOCK_CALLER
4132 && Binder.getCallingUid() != Process.myUid()) {
4133 throw new IllegalStateException("Calling must be system uid");
4134 }
4135 mLaunchingActivity.release();
4136 }
4137 }
4138 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004139 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004140 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004141 } break;
4142 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004143 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144 } break;
4145 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004146 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004147 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004148 case CONTAINER_CALLBACK_VISIBILITY: {
4149 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004150 final IActivityContainerCallback callback = container.mCallback;
4151 if (callback != null) {
4152 try {
4153 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4154 } catch (RemoteException e) {
4155 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004156 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004157 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004158 case LOCK_TASK_START_MSG: {
4159 // When lock task starts, we disable the status bars.
4160 try {
Jason Monk62515be2014-05-21 16:06:19 -04004161 if (mLockTaskNotify == null) {
4162 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004163 }
Jason Monk62515be2014-05-21 16:06:19 -04004164 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004165 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004166 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004167 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004168 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004169 flags = StatusBarManager.DISABLE_MASK
4170 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004171 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004172 flags = StatusBarManager.DISABLE_MASK
4173 & (~StatusBarManager.DISABLE_BACK)
4174 & (~StatusBarManager.DISABLE_HOME)
4175 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004176 }
4177 getStatusBarService().disable(flags, mToken,
4178 mService.mContext.getPackageName());
4179 }
4180 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004181 if (getDevicePolicyManager() != null) {
4182 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004183 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004184 }
justinzhang5286d3f2014-05-12 17:06:01 -04004185 } catch (RemoteException ex) {
4186 throw new RuntimeException(ex);
4187 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004188 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004189 case LOCK_TASK_END_MSG: {
4190 // When lock task ends, we enable the status bars.
4191 try {
Jason Monk62515be2014-05-21 16:06:19 -04004192 if (getStatusBarService() != null) {
4193 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4194 mService.mContext.getPackageName());
4195 }
4196 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004197 if (getDevicePolicyManager() != null) {
4198 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4199 msg.arg1);
4200 }
Jason Monk62515be2014-05-21 16:06:19 -04004201 if (mLockTaskNotify == null) {
4202 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4203 }
4204 mLockTaskNotify.show(false);
4205 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004206 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004207 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004208 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004209 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004210 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004211 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004212 new LockPatternUtils(mService.mContext)
4213 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004214 }
4215 } catch (SettingNotFoundException e) {
4216 // No setting, don't lock.
4217 }
justinzhang5286d3f2014-05-12 17:06:01 -04004218 } catch (RemoteException ex) {
4219 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004220 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004221 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004222 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004223 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004224 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4225 if (mLockTaskNotify == null) {
4226 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4227 }
4228 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4229 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004230 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4231 final ActivityContainer container = (ActivityContainer) msg.obj;
4232 final IActivityContainerCallback callback = container.mCallback;
4233 if (callback != null) {
4234 try {
4235 callback.onAllActivitiesComplete(container.asBinder());
4236 } catch (RemoteException e) {
4237 }
4238 }
4239 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004240 case LAUNCH_TASK_BEHIND_COMPLETE: {
4241 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004242 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004243 if (r != null) {
4244 handleLaunchTaskBehindCompleteLocked(r);
4245 }
4246 }
4247 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004248
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004249 }
4250 }
4251 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004252
Ying Wangb081a592014-04-22 15:20:16 -07004253 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004254 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4255 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004257 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004258 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004259 ActivityRecord mParentActivity = null;
4260 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004261
Craig Mautnere3a00d72014-04-16 08:31:19 -07004262 boolean mVisible = true;
4263
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004265 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004266
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004267 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4268 final static int CONTAINER_STATE_NO_SURFACE = 1;
4269 final static int CONTAINER_STATE_FINISHING = 2;
4270 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4271
Wale Ogunwale1666e312016-12-16 11:27:18 -08004272 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004273 synchronized (mService) {
4274 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004275 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004276 switch (mStackId) {
4277 case PINNED_STACK_ID:
4278 new PinnedActivityStack(this, mRecentTasks, onTop);
4279 break;
4280 default:
4281 new ActivityStack(this, mRecentTasks, onTop);
4282 break;
4283 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004284 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004285 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004286 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004287 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004288
Andrii Kulian839def92016-11-02 10:58:58 -07004289 /**
4290 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004291 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004292 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004293 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004294 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004295 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004296 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004297 if (mActivityDisplay != null) {
4298 throw new IllegalStateException("ActivityContainer is already attached, " +
4299 "displayId=" + mActivityDisplay.mDisplayId);
4300 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004301 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004302 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004303 }
4304
4305 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004306 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004307 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004308 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4309 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004310 return;
4311 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004312 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004313 }
4314 }
4315
4316 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004317 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004318 synchronized (mService) {
4319 if (mActivityDisplay != null) {
4320 return mActivityDisplay.mDisplayId;
4321 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004322 }
4323 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004324 }
4325
Jeff Brownca9bc702014-02-11 14:32:56 -08004326 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004327 public int getStackId() {
4328 synchronized (mService) {
4329 return mStackId;
4330 }
4331 }
4332
4333 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004334 public boolean injectEvent(InputEvent event) {
4335 final long origId = Binder.clearCallingIdentity();
4336 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004337 synchronized (mService) {
4338 if (mActivityDisplay != null) {
4339 return mInputManagerInternal.injectInputEvent(event,
4340 mActivityDisplay.mDisplayId,
4341 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4342 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004343 }
4344 return false;
4345 } finally {
4346 Binder.restoreCallingIdentity(origId);
4347 }
4348 }
4349
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004350 @Override
4351 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004352 synchronized (mService) {
4353 if (mContainerState == CONTAINER_STATE_FINISHING) {
4354 return;
4355 }
4356 mContainerState = CONTAINER_STATE_FINISHING;
4357
Craig Mautnerd163e752014-06-13 17:18:47 -07004358 long origId = Binder.clearCallingIdentity();
4359 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004360 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004361 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004362 } finally {
4363 Binder.restoreCallingIdentity(origId);
4364 }
4365 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004366 }
4367
Andrii Kulian03c403d2016-11-11 11:14:12 -08004368 /**
4369 * Remove the stack completely. Must be called only when there are no tasks left in it,
4370 * as this method does not finish running activities.
4371 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004372 void removeLocked() {
4373 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004374 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004375 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004376 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004377 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004378 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004379 }
4380
Andrii Kulian839def92016-11-02 10:58:58 -07004381 /**
4382 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4383 * completely or re-parented.
4384 */
4385 private void removeFromDisplayLocked() {
4386 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4387 + " current displayId=" + mActivityDisplay.mDisplayId);
4388
Wale Ogunwale1666e312016-12-16 11:27:18 -08004389 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004390 mActivityDisplay = null;
4391 }
4392
4393 /**
4394 * Move the stack to specified display.
4395 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004396 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004397 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004398 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004399 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4400 + mActivityDisplay + " to display=" + activityDisplay
4401 + " Callers=" + Debug.getCallers(2));
4402
4403 removeFromDisplayLocked();
4404
4405 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004406 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004407 }
4408
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004410 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004411 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004412 }
4413
4414 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004415 public final int startActivityIntentSender(IIntentSender intentSender)
4416 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004417 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4418
4419 if (!(intentSender instanceof PendingIntentRecord)) {
4420 throw new IllegalArgumentException("Bad PendingIntent object");
4421 }
4422
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004423 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004424 Binder.getCallingUid(), mCurrentUser, false,
4425 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004426
4427 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4428 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4429 pendingIntent.key.requestResolvedType);
4430
4431 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4432 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4433 }
4434
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004435 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004436 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004437 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004438 throw new SecurityException(
4439 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4440 }
4441 }
4442
Craig Mautnerdf88d732014-01-27 09:21:32 -08004443 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444 public IBinder asBinder() {
4445 return this;
4446 }
4447
Craig Mautner4504de52013-12-20 09:06:56 -08004448 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004449 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004450 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004451 }
4452
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 ActivityStackSupervisor getOuter() {
4454 return ActivityStackSupervisor.this;
4455 }
4456
Craig Mautnerd163e752014-06-13 17:18:47 -07004457 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004458 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004459 }
4460
Craig Mautner6985bad2014-04-21 15:22:06 -07004461 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004462 void setVisible(boolean visible) {
4463 if (mVisible != visible) {
4464 mVisible = visible;
4465 if (mCallback != null) {
4466 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4467 0 /* unused */, this).sendToTarget();
4468 }
4469 }
4470 }
4471
Craig Mautner6985bad2014-04-21 15:22:06 -07004472 void setDrawn() {
4473 }
4474
Craig Mautner1b4bf852014-05-26 15:06:32 -07004475 // You can always start a new task on a regular ActivityStack.
4476 boolean isEligibleForNewTasks() {
4477 return true;
4478 }
4479
Craig Mautnerd163e752014-06-13 17:18:47 -07004480 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004481 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004482 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004483 }
4484
Craig Mautner34b73df2014-01-12 21:11:08 -08004485 @Override
4486 public String toString() {
4487 return mIdString + (mActivityDisplay == null ? "N" : "A");
4488 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004489 }
4490
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004491 private class VirtualActivityContainer extends ActivityContainer {
4492 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004493 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004494
4495 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004496 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4497 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004498 mParentActivity = parent;
4499 mCallback = callback;
4500 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004501 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004502 }
4503
4504 @Override
4505 public void setSurface(Surface surface, int width, int height, int density) {
4506 super.setSurface(surface, width, height, density);
4507
4508 synchronized (mService) {
4509 final long origId = Binder.clearCallingIdentity();
4510 try {
4511 setSurfaceLocked(surface, width, height, density);
4512 } finally {
4513 Binder.restoreCallingIdentity(origId);
4514 }
4515 }
4516 }
4517
4518 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4519 if (mContainerState == CONTAINER_STATE_FINISHING) {
4520 return;
4521 }
4522 VirtualActivityDisplay virtualActivityDisplay =
4523 (VirtualActivityDisplay) mActivityDisplay;
4524 if (virtualActivityDisplay == null) {
4525 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004526 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004527 mActivityDisplay = virtualActivityDisplay;
4528 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004529 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004530 }
4531
4532 if (mSurface != null) {
4533 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004534 }
4535
Craig Mautner6985bad2014-04-21 15:22:06 -07004536 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004537 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004538 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004539 } else {
4540 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004541 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004542 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004543 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004544 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004545 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004546
Craig Mautnerd163e752014-06-13 17:18:47 -07004547 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004548
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004549 if (DEBUG_STACK) Slog.d(TAG_STACK,
4550 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004551 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004552
Craig Mautner6985bad2014-04-21 15:22:06 -07004553 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004554 boolean isAttachedLocked() {
4555 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004556 }
4557
4558 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004559 void setDrawn() {
4560 synchronized (mService) {
4561 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004562 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004563 }
4564 }
4565
Craig Mautner1b4bf852014-05-26 15:06:32 -07004566 // Never start a new task on an ActivityView if it isn't explicitly specified.
4567 @Override
4568 boolean isEligibleForNewTasks() {
4569 return false;
4570 }
4571
Craig Mautnerd163e752014-06-13 17:18:47 -07004572 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004573 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004574 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4575 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4576 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4577 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4578 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004579 }
4580 }
4581
Craig Mautner4a1cb222013-12-04 16:14:06 -08004582 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4583 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004584 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004585 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004586 int mDisplayId;
4587 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004588
Craig Mautner4a1cb222013-12-04 16:14:06 -08004589 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4590 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004591 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004592
Jose Lima4b6c6692014-08-12 17:41:12 -07004593 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004594
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004595 /** Array of all UIDs that are present on the display. */
4596 private IntArray mDisplayAccessUIDs = new IntArray();
4597
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004598 ActivityDisplay() {
4599 }
Craig Mautner4504de52013-12-20 09:06:56 -08004600
Craig Mautner1a70a162014-09-13 12:09:31 -07004601 // After instantiation, check that mDisplay is not null before using this. The alternative
4602 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004603 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004604 final Display display = mDisplayManager.getDisplay(displayId);
4605 if (display == null) {
4606 return;
4607 }
4608 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004609 }
4610
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004611 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004612 mDisplay = display;
4613 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004614 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004615
Wale Ogunwale1666e312016-12-16 11:27:18 -08004616 void attachStack(ActivityStack stack, boolean onTop) {
4617 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
4618 + " to displayId=" + mDisplayId + " onTop=" + onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004619 if (onTop) {
4620 mStacks.add(stack);
4621 } else {
4622 mStacks.add(0, stack);
4623 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004624 }
4625
Wale Ogunwale1666e312016-12-16 11:27:18 -08004626 void detachStack(ActivityStack stack) {
4627 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004628 + " from displayId=" + mDisplayId);
4629 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004630 }
4631
Jose Lima4b6c6692014-08-12 17:41:12 -07004632 void setVisibleBehindActivity(ActivityRecord r) {
4633 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004634 }
4635
Jose Lima4b6c6692014-08-12 17:41:12 -07004636 boolean hasVisibleBehindActivity() {
4637 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004638 }
4639
Craig Mautner34b73df2014-01-12 21:11:08 -08004640 @Override
4641 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004642 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4643 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004644
4645 @Override
4646 protected int getChildCount() {
4647 return mStacks.size();
4648 }
4649
4650 @Override
4651 protected ConfigurationContainer getChildAt(int index) {
4652 return mStacks.get(index);
4653 }
4654
4655 @Override
4656 protected ConfigurationContainer getParent() {
4657 return ActivityStackSupervisor.this;
4658 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004659
4660 boolean isPrivate() {
4661 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4662 }
4663
4664 boolean isUidPresent(int uid) {
4665 for (ActivityStack stack : mStacks) {
4666 if (stack.isUidPresent(uid)) {
4667 return true;
4668 }
4669 }
4670 return false;
4671 }
4672
4673 /** Update and get all UIDs that are present on the display and have access to it. */
4674 private IntArray getPresentUIDs() {
4675 mDisplayAccessUIDs.clear();
4676 for (ActivityStack stack : mStacks) {
4677 stack.getPresentUIDs(mDisplayAccessUIDs);
4678 }
4679 return mDisplayAccessUIDs;
4680 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004681
4682 boolean shouldDestroyContentOnRemove() {
4683 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4684 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004685 }
4686
4687 class VirtualActivityDisplay extends ActivityDisplay {
4688 VirtualDisplay mVirtualDisplay;
4689
Craig Mautner6985bad2014-04-21 15:22:06 -07004690 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004691 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004692 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4693 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4694 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4695 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004696
4697 init(mVirtualDisplay.getDisplay());
4698
4699 mWindowManager.handleDisplayAdded(mDisplayId);
4700 }
4701
4702 void setSurface(Surface surface) {
4703 if (mVirtualDisplay != null) {
4704 mVirtualDisplay.setSurface(surface);
4705 }
4706 }
4707
4708 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004709 void detachStack(ActivityStack stack) {
4710 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004711 if (mVirtualDisplay != null) {
4712 mVirtualDisplay.release();
4713 mVirtualDisplay = null;
4714 }
4715 }
4716
4717 @Override
4718 public String toString() {
4719 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004720 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004721 }
Jose Lima58e66d62014-05-27 20:00:27 -07004722
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004723 ActivityStack findStackBehind(ActivityStack stack) {
4724 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004725 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004726 if (display == null) {
4727 return null;
4728 }
4729 final ArrayList<ActivityStack> stacks = display.mStacks;
4730 for (int i = stacks.size() - 1; i >= 0; i--) {
4731 if (stacks.get(i) == stack && i > 0) {
4732 return stacks.get(i - 1);
4733 }
4734 }
4735 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4736 + " in=" + stacks);
4737 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004738
Jorim Jaggic69bd222016-03-15 14:38:37 +01004739 /**
4740 * Puts a task into resizing mode during the next app transition.
4741 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004742 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004743 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004744 private void setResizingDuringAnimation(TaskRecord task) {
4745 mResizingTasksDuringAnimation.add(task.taskId);
4746 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004747 }
4748
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004749 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4750 final TaskRecord task;
4751 final int callingUid;
4752 final String callingPackage;
4753 final Intent intent;
4754 final int userId;
4755 final ActivityOptions activityOptions = (bOptions != null)
4756 ? new ActivityOptions(bOptions) : null;
4757 final int launchStackId = (activityOptions != null)
4758 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004759 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004760 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004761 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004762 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004763
4764 if (launchStackId == DOCKED_STACK_ID) {
4765 mWindowManager.setDockedStackCreateState(
4766 activityOptions.getDockCreateMode(), null /* initialBounds */);
4767
4768 // Defer updating the stack in which recents is until the app transition is done, to
4769 // not run into issues where we still need to draw the task in recents but the
4770 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004771 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004772 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4773 }
4774
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004775 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4776 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004777 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004778 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004779 throw new IllegalArgumentException(
4780 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4781 }
4782
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004783 // Since we don't have an actual source record here, we assume that the currently focused
4784 // activity was the source.
4785 final ActivityStack focusedStack = getFocusedStack();
4786 final ActivityRecord sourceRecord =
4787 focusedStack != null ? focusedStack.topActivity() : null;
4788
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004789 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004790 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004791 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4792 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004793 }
4794 }
4795
4796 // If the user must confirm credentials (e.g. when first launching a work app and the
4797 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4798 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4799 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004800 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004801 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004802 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004803 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4804 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004805
4806 // If we are launching the task in the docked stack, put it into resizing mode so
4807 // the window renders full-screen with the background filling the void. Also only
4808 // call this at the end to make sure that tasks exists on the window manager side.
4809 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004810 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004811 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004812
Bryce Lee98689f72017-03-01 23:44:43 +00004813 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004814 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004815 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4816 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004817 return ActivityManager.START_TASK_TO_FRONT;
4818 }
4819 callingUid = task.mCallingUid;
4820 callingPackage = task.mCallingPackage;
4821 intent = task.intent;
4822 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4823 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004824 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4825 null, null, 0, 0, bOptions, userId, null, task);
4826 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004827 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004828 }
4829 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004830 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004831
4832 /**
4833 * @return a list of activities which are the top ones in each visible stack. The first
4834 * entry will be the focused activity.
4835 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004836 List<IBinder> getTopVisibleActivities() {
4837 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4838 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004839 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004840 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4841 // Traverse all stacks on a display.
4842 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4843 final ActivityStack stack = display.mStacks.get(j);
4844 // Get top activity from a visible stack and add it to the list.
4845 if (stack.getStackVisibilityLocked(null /* starting */)
4846 == ActivityStack.STACK_VISIBLE) {
4847 final ActivityRecord top = stack.topActivity();
4848 if (top != null) {
4849 if (stack == mFocusedStack) {
4850 topActivityTokens.add(0, top.appToken);
4851 } else {
4852 topActivityTokens.add(top.appToken);
4853 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004854 }
4855 }
4856 }
4857 }
4858 return topActivityTokens;
4859 }
Craig Mautner27084302013-03-25 08:05:25 -07004860}