blob: 1c5233e0dcb00079124f11c253e2ce983f747eff [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;
92import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080093import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020094
Svetoslav7008b512015-06-24 18:47:07 -070095import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -070096import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +010097import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070098import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800100import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700101import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800102import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityOptions;
104import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700105import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700107import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400109import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700110import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400111import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700113import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import android.content.Intent;
116import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700118import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.pm.PackageManager;
120import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100121import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700123import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800124import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800125import android.hardware.display.DisplayManager;
126import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800127import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800128import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800129import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800130import android.hardware.input.InputManager;
131import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100133import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700134import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700135import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700136import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700137import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700138import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700140import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700141import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700142import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400143import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700145import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700146import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700147import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100148import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700149import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700150import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400151import android.provider.Settings;
152import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700153import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700154import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700155import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700156import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700157import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800158import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700159import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800162import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800163import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800164import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800165
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800166import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800167import com.android.internal.logging.MetricsLogger;
168import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700169import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400170import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700171import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400172import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800173import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700174import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700175import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700176
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177import java.io.FileDescriptor;
178import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700179import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700180import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700181import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700182import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700183import java.util.List;
Andrii Kulian0864bbb2017-02-16 15:45:58 -0800184import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700185import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700186
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800187public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800188 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700190 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700192 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700193 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800199 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800201
Craig Mautnerf3333272013-04-22 10:55:53 -0700202 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700204
Craig Mautner0eea92c2013-05-16 13:35:39 -0700205 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210
Craig Mautner05d29032013-05-03 13:40:13 -0700211 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
212 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
213 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700214 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700215 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800216 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
217 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
218 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700219 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400220 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
221 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700222 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700223 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700224 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800225 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
226 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700229
Jason Monk62515be2014-05-21 16:06:19 -0400230 private static final String LOCK_TASK_TAG = "Lock-to-App";
231
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232 // Used to indicate if an object (e.g. stack) that we are trying to get
233 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800234 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700236 // Used to indicate that windows of activities should be preserved during the resize.
237 static final boolean PRESERVE_WINDOWS = true;
238
Wale Ogunwale040b4702015-08-06 18:10:50 -0700239 // Used to indicate if an object (e.g. task) should be moved/created
240 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700241 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700242
243 // Used to indicate that an objects (e.g. task) removal from its container
244 // (e.g. stack) is due to it moving to another container.
245 static final boolean MOVING = true;
246
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700247 // Force the focus to change to the stack we are moving a task to..
248 static final boolean FORCE_FOCUS = true;
249
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700250 // Restore task from the saved recents if it can't be found in any live stack.
251 static final boolean RESTORE_FROM_RECENTS = true;
252
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700253 // Don't execute any calls to resume.
254 static final boolean DEFER_RESUME = true;
255
Winson Chung010927a2016-12-15 16:12:35 -0800256 // Used to indicate that a task is removed it should also be removed from recents.
257 static final boolean REMOVE_FROM_RECENTS = true;
258
Svetoslav7008b512015-06-24 18:47:07 -0700259 // Activity actions an app cannot start if it uses a permission which is not granted.
260 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
261 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700262
Svetoslav7008b512015-06-24 18:47:07 -0700263 static {
264 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
265 Manifest.permission.CAMERA);
266 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
267 Manifest.permission.CAMERA);
268 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
269 Manifest.permission.CALL_PHONE);
270 }
271
Svet Ganov99b60432015-06-27 13:15:22 -0700272 /** Action restriction: launching the activity is not restricted. */
273 private static final int ACTIVITY_RESTRICTION_NONE = 0;
274 /** Action restriction: launching the activity is restricted by a permission. */
275 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
276 /** Action restriction: launching the activity is restricted by an app op. */
277 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700278
justinzhang5286d3f2014-05-12 17:06:01 -0400279 /** Status Bar Service **/
280 private IBinder mToken = new Binder();
281 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400282 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400283
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700284 // For debugging to make sure the caller when acquiring/releasing our
285 // wake lock is the system process.
286 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800287 /** The number of distinct task ids that can be assigned to the tasks of a single user */
288 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700289
Craig Mautner27084302013-03-25 08:05:25 -0700290 final ActivityManagerService mService;
291
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800292 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800293
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700294 final ActivityStackSupervisorHandler mHandler;
295
296 /** Short cut */
297 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800298 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700299
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700300 /** Counter for next free stack ID to use for dynamic activity stacks. */
301 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800303 /**
304 * Maps the task identifier that activities are currently being started in to the userId of the
305 * task. Each time a new task is created, the entry for the userId of the task is incremented
306 */
307 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700308
Craig Mautner2420ead2013-04-01 17:13:20 -0700309 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack containing the launcher app. Assumed to always be attached to
313 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700315
Craig Mautnere0a38842013-12-16 16:14:02 -0800316 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800317 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700318
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
320 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800321 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
324 /** List of activities that are waiting for a new activity to become visible before completing
325 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700327
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700328 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700329 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330
331 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700332 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334 /** List of activities that are ready to be stopped, but waiting for the next activity to
335 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337
Craig Mautnerf3333272013-04-22 10:55:53 -0700338 /** List of activities that are ready to be finished, but waiting for the previous activity to
339 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800340 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700341
Craig Mautner0eea92c2013-05-16 13:35:39 -0700342 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800343 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344
Wale Ogunwale22e25262016-02-01 10:32:02 -0800345 /** List of activities whose multi-window mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
348
349 /** List of activities whose picture-in-picture mode changed that we need to report to the
350 * application */
351 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
352
Craig Mautnerf3333272013-04-22 10:55:53 -0700353 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700354 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700355
Craig Mautnerde4ef022013-04-07 19:01:33 -0700356 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
357 * is being brought in front of us. */
358 boolean mUserLeaving = false;
359
Craig Mautner0eea92c2013-05-16 13:35:39 -0700360 /** Set when we have taken too long waiting to go to sleep. */
361 boolean mSleepTimeout = false;
362
363 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700364 * We don't want to allow the device to go to sleep while in the process
365 * of launching an activity. This is primarily to allow alarm intent
366 * receivers to launch an activity and get that to run before the device
367 * goes back to sleep.
368 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700369 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700370
371 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372 * Set when the system is going to sleep, until we have
373 * successfully paused the current activity and released our wake lock.
374 * At that point the system is allowed to actually sleep.
375 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700376 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700377
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700378 /** Stack id of the front stack when user switched, indexed by userId. */
379 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700380
Craig Mautner4504de52013-12-20 09:06:56 -0800381 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800383 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384
385 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700386 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800388 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
389
390 private DisplayManagerInternal mDisplayManagerInternal;
391 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800392
Craig Mautner15df08a2015-04-01 12:17:18 -0700393 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
394 * may be finished until there is only one entry left. If this is empty the system is not
395 * in lockTask mode. */
396 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000397 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700398 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
399 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000400 */
401 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400402 /**
403 * Notifies the user when entering/exiting lock-task.
404 */
405 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800406
Wale Ogunwaled046a012015-12-24 13:05:59 -0800407 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800408 boolean inResumeTopActivity;
409
Andrii Kulian1e32e022016-09-16 15:29:34 -0700410 /**
411 * Temporary rect used during docked stack resize calculation so we don't need to create a new
412 * object each time.
413 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700414 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700415
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700416 // The default minimal size that will be used if the activity doesn't specify its minimal size.
417 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700418 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700419
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700420 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
421 private boolean mTaskLayersChanged = true;
422
Jorim Jaggi275561a2016-02-23 10:11:02 -0500423 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800424
Andrii Kulian1779e612016-10-12 21:58:25 -0700425 @Override
426 protected int getChildCount() {
427 return mActivityDisplays.size();
428 }
429
430 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700431 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700432 return mActivityDisplays.valueAt(index);
433 }
434
435 @Override
436 protected ConfigurationContainer getParent() {
437 return null;
438 }
439
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700440 Configuration getDisplayOverrideConfiguration(int displayId) {
441 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
442 if (activityDisplay == null) {
443 throw new IllegalArgumentException("No display found with id: " + displayId);
444 }
445
446 return activityDisplay.getOverrideConfiguration();
447 }
448
449 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
450 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
451 if (activityDisplay == null) {
452 throw new IllegalArgumentException("No display found with id: " + displayId);
453 }
454
455 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
456 }
457
Wale Ogunwale39381972015-12-17 17:15:29 -0800458 static class FindTaskResult {
459 ActivityRecord r;
460 boolean matchedByRootAffinity;
461 }
462 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
463
Craig Mautneree36c772014-07-16 14:56:05 -0700464 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800465 * Temp storage for display ids sorted in focus order.
466 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
467 * it's more efficient, as the number of displays is usually small.
468 */
469 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
470
471 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100472 * Used to keep track whether app visibilities got changed since the last pause. Useful to
473 * determine whether to invoke the task stack change listener after pausing.
474 */
475 boolean mAppVisibilitiesChangedSinceLastPause;
476
477 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100478 * Set of tasks that are in resizing mode during an app transition to fill the "void".
479 */
480 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
481
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700482
483 /**
484 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
485 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
486 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
487 * like the docked stack going empty.
488 */
489 private boolean mAllowDockedStackResize = true;
490
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100491 /**
Tony Mak853304c2016-04-18 15:17:41 +0100492 * Is dock currently minimized.
493 */
494 boolean mIsDockMinimized;
495
Jorim Jaggife762342016-10-13 14:33:27 +0200496 final KeyguardController mKeyguardController;
497
Tony Mak853304c2016-04-18 15:17:41 +0100498 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700499 * Description of a request to start a new activity, which has been held
500 * due to app switches being disabled.
501 */
502 static class PendingActivityLaunch {
503 final ActivityRecord r;
504 final ActivityRecord sourceRecord;
505 final int startFlags;
506 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700507 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700508
509 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700510 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700511 r = _r;
512 sourceRecord = _sourceRecord;
513 startFlags = _startFlags;
514 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700515 callerApp = _callerApp;
516 }
517
518 void sendErrorResult(String message) {
519 try {
520 if (callerApp.thread != null) {
521 callerApp.thread.scheduleCrash(message);
522 }
523 } catch (RemoteException e) {
524 Slog.e(TAG, "Exception scheduling crash of failed "
525 + "activity launcher sourceRecord=" + sourceRecord, e);
526 }
Craig Mautneree36c772014-07-16 14:56:05 -0700527 }
528 }
529
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800530 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700531 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700532 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800533 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200534 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700535 }
536
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800537 void setRecentTasks(RecentTasks recentTasks) {
538 mRecentTasks = recentTasks;
539 }
540
Jeff Brown2c43c332014-06-12 22:38:59 -0700541 /**
542 * At the time when the constructor runs, the power manager has not yet been
543 * initialized. So we initialize our wakelocks afterwards.
544 */
545 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800546 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700547 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700548 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700549 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700550 }
551
justinzhang5286d3f2014-05-12 17:06:01 -0400552 // This function returns a IStatusBarService. The value is from ServiceManager.
553 // getService and is cached.
554 private IStatusBarService getStatusBarService() {
555 synchronized (mService) {
556 if (mStatusBarService == null) {
557 mStatusBarService = IStatusBarService.Stub.asInterface(
558 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
559 if (mStatusBarService == null) {
560 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
561 }
562 }
563 return mStatusBarService;
564 }
565 }
566
Jason Monk35c62a42014-06-17 10:24:47 -0400567 private IDevicePolicyManager getDevicePolicyManager() {
568 synchronized (mService) {
569 if (mDevicePolicyManager == null) {
570 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
571 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
572 if (mDevicePolicyManager == null) {
573 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
574 }
575 }
576 return mDevicePolicyManager;
577 }
578 }
579
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700580 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 synchronized (mService) {
582 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200583 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584
585 mDisplayManager =
586 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
587 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800588 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800589
590 Display[] displays = mDisplayManager.getDisplays();
591 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
592 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700594 if (activityDisplay.mDisplay == null) {
595 throw new IllegalStateException("Default Display does not exist");
596 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800597 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700598 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 }
600
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800601 mHomeStack = mFocusedStack = mLastFocusedStack =
602 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800603
604 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 }
Craig Mautner27084302013-03-25 08:05:25 -0700606 }
607
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700608 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700610 }
611
Craig Mautnerde4ef022013-04-07 19:01:33 -0700612 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700614 }
615
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700616 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700617 if (stack == null) {
618 return false;
619 }
620
Craig Mautnerdf88d732014-01-27 09:21:32 -0800621 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
622 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700623 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800624 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800625 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700626 }
627
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800628 /** The top most stack on its display. */
629 boolean isFrontStackOnDisplay(ActivityStack stack) {
630 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
631 }
632
633 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700634 if (stack == null) {
635 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800636 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700637
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700638 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
639 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700640 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800641 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800642 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700643 }
644
Wale Ogunwaled046a012015-12-24 13:05:59 -0800645 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800646 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
647 if (!focusCandidate.isFocusable()) {
648 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800649 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800650 }
651
652 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800653 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800654 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800655
Wale Ogunwaled046a012015-12-24 13:05:59 -0800656 EventLogTags.writeAmFocusedStack(
657 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
658 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
659 }
660
661 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800662 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800663 if (r != null && r.idle) {
664 checkFinishBootingLocked();
665 }
666 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700667 }
668
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700669 void moveHomeStackToFront(String reason) {
670 mHomeStack.moveToFront(reason);
671 }
672
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700673 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800674 boolean moveHomeStackTaskToTop(String reason) {
675 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700676
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700677 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678 if (top == null) {
679 return false;
680 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700681 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700682 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700683 }
684
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800685 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700686 if (!mService.mBooting && !mService.mBooted) {
687 // Not ready yet!
688 return false;
689 }
690
Craig Mautner84984fa2014-06-19 11:19:20 -0700691 if (prev != null) {
692 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
693 }
694
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800695 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700696 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800697 final String myReason = reason + " resumeHomeStackTask";
698
Mark Lua56ea122015-10-08 13:31:01 +0800699 // Only resume home activity if isn't finishing.
700 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700701 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700703 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800704 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700705 }
706
Craig Mautner8d341ef2013-03-26 09:03:27 -0700707 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700708 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700709 }
710
711 /**
712 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
713 * @param id Id of the task we would like returned.
714 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
715 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700716 * @param stackId The stack to restore the task to (default launch stack will be used if
717 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700718 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700719 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
724 ActivityStack stack = stacks.get(stackNdx);
725 TaskRecord task = stack.taskForIdLocked(id);
726 if (task != null) {
727 return task;
728 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700729 }
730 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800731
732 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700733 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800734 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800735 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700736 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800737 return null;
738 }
739
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700740 if (!restoreFromRecents) {
741 return task;
742 }
743
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700744 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700745 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
746 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800747 return null;
748 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700749 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800750 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700751 }
752
Craig Mautner6170f732013-04-02 13:05:23 -0700753 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800756 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800757 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
758 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
759 if (r != null) {
760 return r;
761 }
Craig Mautner6170f732013-04-02 13:05:23 -0700762 }
763 }
764 return null;
765 }
766
Tony Mak853304c2016-04-18 15:17:41 +0100767 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000768 * Detects whether we should show a lock screen in front of this task for a locked user.
769 * <p>
770 * We'll do this if either of the following holds:
771 * <ul>
772 * <li>The top activity explicitly belongs to {@param userId}.</li>
773 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
774 * </ul>
775 *
776 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100777 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000778 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
779 // To handle the case that work app is in the task but just is not the top one.
780 final ActivityRecord activityRecord = task.getTopActivity();
781 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
782
783 return (activityRecord != null && activityRecord.userId == userId)
784 || (resultTo != null && resultTo.userId == userId);
785 }
786
787 /**
788 * Find all visible task stacks containing {@param userId} and intercept them with an activity
789 * to block out the contents and possibly start a credential-confirming intent.
790 *
791 * @param userId user handle for the locked managed profile.
792 */
793 void lockAllProfileTasks(@UserIdInt int userId) {
794 mWindowManager.deferSurfaceLayout();
795 try {
796 final List<ActivityStack> stacks = getStacks();
797 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
798 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
799 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
800 final TaskRecord task = tasks.get(taskNdx);
801
802 // Check the task for a top activity belonging to userId, or returning a result
803 // to an activity belonging to userId. Example case: a document picker for
804 // personal files, opened by a work app, should still get locked.
805 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000806 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
807 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000808 }
Tony Mak853304c2016-04-18 15:17:41 +0100809 }
810 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000811 } finally {
812 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100813 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000814 }
815
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800816 void setNextTaskIdForUserLocked(int taskId, int userId) {
817 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
818 if (taskId > currentTaskId) {
819 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700820 }
821 }
822
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700823 static int nextTaskIdForUser(int taskId, int userId) {
824 int nextTaskId = taskId + 1;
825 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
826 // Wrap around as there will be smaller task ids that are available now.
827 nextTaskId -= MAX_TASK_IDS_PER_USER;
828 }
829 return nextTaskId;
830 }
831
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800832 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800833 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
834 // for a userId u, a taskId can only be in the range
835 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
836 // 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 -0700837 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800838 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
839 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
840 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700841 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800842 if (candidateTaskId == currentTaskId) {
843 // Something wrong!
844 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
845 throw new IllegalStateException("Cannot get an available task id."
846 + " Reached limit of " + MAX_TASK_IDS_PER_USER
847 + " running tasks per user.");
848 }
849 }
850 mCurTaskIdForUser.put(userId, candidateTaskId);
851 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700852 }
853
Chong Zhang6cda19c2016-06-14 19:07:56 -0700854 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800855 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700856 if (stack == null) {
857 return null;
858 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700859 ActivityRecord resumedActivity = stack.mResumedActivity;
860 if (resumedActivity == null || resumedActivity.app == null) {
861 resumedActivity = stack.mPausingActivity;
862 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700863 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 }
865 }
866 return resumedActivity;
867 }
868
Dianne Hackbornff072722014-09-24 10:56:28 -0700869 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700870 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800872 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
873 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800874 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
875 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700876 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800877 continue;
878 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700879 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800880 if (hr != null) {
881 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
882 && processName.equals(hr.processName)) {
883 try {
George Mount2c92c972014-03-20 09:38:23 -0700884 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 didSomething = true;
886 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700887 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800888 Slog.w(TAG, "Exception in new application when starting activity "
889 + hr.intent.getComponent().flattenToShortString(), e);
890 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700891 }
Craig Mautner20e72272013-04-01 13:45:53 -0700892 }
Craig Mautner20e72272013-04-01 13:45:53 -0700893 }
894 }
895 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700896 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700897 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700898 }
Craig Mautner20e72272013-04-01 13:45:53 -0700899 return didSomething;
900 }
901
902 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800903 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
904 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800905 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
906 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700907 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 continue;
909 }
910 final ActivityRecord resumedActivity = stack.mResumedActivity;
911 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700912 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800913 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800914 return false;
915 }
Craig Mautner20e72272013-04-01 13:45:53 -0700916 }
917 }
Wei Wang65c7a152016-06-02 18:51:22 -0700918 // Send launch end powerhint when idle
919 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700920 return true;
921 }
922
Craig Mautnerde4ef022013-04-07 19:01:33 -0700923 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
925 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
927 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700928 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700930 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 return false;
932 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700933 }
934 }
935 }
936 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700937 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
939 mLastFocusedStack + " to=" + mFocusedStack);
940 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700941 return true;
942 }
943
944 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800945 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
947 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800948 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
949 final ActivityStack stack = stacks.get(stackNdx);
950 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800951 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700952 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800953 return false;
954 }
955 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700957 }
958 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800959 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 }
961
Craig Mautner2acc3892013-09-23 10:28:14 -0700962 /**
963 * Pause all activities in either all of the stacks or just the back stacks.
964 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700965 * @param resuming The resuming activity.
966 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
967 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700968 * @return true if any activity was paused as a result of this call.
969 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700970 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700971 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
973 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
975 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700976 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700977 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800978 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700979 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
980 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700982 }
983 }
984 return someActivityPaused;
985 }
986
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700988 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
990 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800991 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
992 final ActivityStack stack = stacks.get(stackNdx);
993 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700994 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800995 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700996 Slog.d(TAG_STATES,
997 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800998 pausing = false;
999 } else {
1000 return false;
1001 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001002 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001003 }
1004 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001005 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001006 }
1007
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001008 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001009 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001010 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001011 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1012 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1013 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1014 final ActivityStack stack = stacks.get(stackNdx);
1015 if (stack.mResumedActivity != null &&
1016 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001017 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001018 }
1019 }
1020 }
1021 }
1022
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001023 void cancelInitializingActivities() {
1024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1025 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1026 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1027 stacks.get(stackNdx).cancelInitializingActivities();
1028 }
1029 }
1030 }
1031
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001032 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001033 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001034 }
1035
1036 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1037 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001038 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001039 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001040 if (w.who == null) {
1041 changed = true;
1042 w.timeout = false;
1043 if (r != null) {
1044 w.who = new ComponentName(r.info.packageName, r.info.name);
1045 }
1046 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1047 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001048 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001050 if (changed) {
1051 mService.notifyAll();
1052 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001053 }
1054
Chong Zhang5022da32016-06-21 16:31:37 -07001055 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1056 boolean changed = false;
1057 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1058 WaitResult w = mWaitingActivityLaunched.remove(i);
1059 if (w.who == null) {
1060 changed = true;
1061 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1062 // the starting activity ends up moving another activity to front, and it should
1063 // wait for this new activity to become visible instead.
1064 // Do not modify other fields.
1065 w.result = START_TASK_TO_FRONT;
1066 }
1067 }
1068 if (changed) {
1069 mService.notifyAll();
1070 }
1071 }
1072
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001073 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1074 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001075 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001076 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001077 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001078 if (w.who == null) {
1079 changed = true;
1080 w.timeout = timeout;
1081 if (r != null) {
1082 w.who = new ComponentName(r.info.packageName, r.info.name);
1083 }
1084 w.thisTime = thisTime;
1085 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001086 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001087 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001088 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001089 if (changed) {
1090 mService.notifyAll();
1091 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001092 }
1093
Craig Mautner29219d92013-04-16 20:19:12 -07001094 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001095 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001096 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001097 if (r != null) {
1098 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001099 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001100
Andrii Kulian7318d632016-07-20 18:59:28 -07001101 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001102 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1103
1104 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1105 final int displayId = mTmpOrderedDisplayIds.get(i);
1106 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1107 if (stacks == null) {
1108 continue;
1109 }
1110 for (int j = stacks.size() - 1; j >= 0; --j) {
1111 final ActivityStack stack = stacks.get(j);
1112 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1113 r = stack.topRunningActivityLocked();
1114 if (r != null) {
1115 return r;
1116 }
Craig Mautner29219d92013-04-16 20:19:12 -07001117 }
1118 }
1119 }
1120 return null;
1121 }
1122
Dianne Hackborn09233282014-04-30 11:33:59 -07001123 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001124 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001125 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1126 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001127 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001128 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001129 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001130 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1131 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001132 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001133 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001134 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001135 }
1136 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001137
1138 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1139 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1140 while (maxNum > 0) {
1141 long mostRecentActiveTime = Long.MIN_VALUE;
1142 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001143 final int numTaskLists = runningTaskLists.size();
1144 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1145 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001146 if (!stackTaskList.isEmpty()) {
1147 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1148 if (lastActiveTime > mostRecentActiveTime) {
1149 mostRecentActiveTime = lastActiveTime;
1150 selectedStackList = stackTaskList;
1151 }
1152 }
1153 }
1154 if (selectedStackList != null) {
1155 list.add(selectedStackList.remove(0));
1156 --maxNum;
1157 } else {
1158 break;
1159 }
1160 }
Craig Mautner20e72272013-04-01 13:45:53 -07001161 }
1162
Todd Kennedy7440f172015-12-09 14:31:22 -08001163 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1164 ProfilerInfo profilerInfo) {
1165 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166 if (aInfo != null) {
1167 // Store the found target back into the intent, because now that
1168 // we have it we never want to do this again. For example, if the
1169 // user navigates back to this point in the history, we should
1170 // always restart the exact same activity.
1171 intent.setComponent(new ComponentName(
1172 aInfo.applicationInfo.packageName, aInfo.name));
1173
1174 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001175 if (!aInfo.processName.equals("system")) {
1176 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001177 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001178 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001179
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001180 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1181 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1182 }
1183
Man Caocfa78b22015-06-11 20:14:34 -07001184 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1185 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1186 }
1187
1188 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001189 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001190 }
1191 }
1192 }
1193 return aInfo;
1194 }
1195
Todd Kennedy7440f172015-12-09 14:31:22 -08001196 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001197 return resolveIntent(intent, resolvedType, userId, 0);
1198 }
1199
1200 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001201 try {
1202 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001203 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001204 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001205 } catch (RemoteException e) {
1206 }
1207 return null;
1208 }
1209
1210 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1211 ProfilerInfo profilerInfo, int userId) {
1212 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1213 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1214 }
1215
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001216 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1217 boolean andResume, boolean checkConfig) throws RemoteException {
1218
1219 if (!allPausedActivitiesComplete()) {
1220 // While there are activities pausing we skipping starting any new activities until
1221 // pauses are complete. NOTE: that we also do this for activities that are starting in
1222 // the paused state because they will first be resumed then paused on the client side.
1223 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1224 "realStartActivityLocked: Skipping start of r=" + r
1225 + " some activities pausing...");
1226 return false;
1227 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001228
Andrii Kulianada10292017-02-09 23:19:29 -08001229 r.startFreezingScreenLocked(app, 0);
1230 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001231
Andrii Kulianada10292017-02-09 23:19:29 -08001232 // schedule launch ticks to collect information about slow apps.
1233 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001234
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001235 // Have the window manager re-evaluate the orientation of the screen based on the new
1236 // activity order. Note that as a result of this, it can call back into the activity
1237 // manager with a new orientation. We don't care about that, because the activity is not
1238 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001240 final int displayId = r.getDisplayId();
1241 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1242 getDisplayOverrideConfiguration(displayId),
1243 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001244 // Deferring resume here because we're going to launch new activity shortly.
1245 // We don't want to perform a redundant launch of the same record while ensuring
1246 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001247 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1248 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 }
1250
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001251 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001252 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001253 }
1254
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 r.app = app;
1256 app.waitingToKill = null;
1257 r.launchCount++;
1258 r.lastLaunchTime = SystemClock.uptimeMillis();
1259
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001260 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001261
1262 int idx = app.activities.indexOf(r);
1263 if (idx < 0) {
1264 app.activities.add(r);
1265 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001266 mService.updateLruProcessLocked(app, true, null);
1267 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001268
Craig Mautner15df08a2015-04-01 12:17:18 -07001269 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001270 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1271 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001272 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001273 }
1274
Andrii Kulian02b7a832016-10-06 23:11:56 -07001275 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 try {
1277 if (app.thread == null) {
1278 throw new RemoteException();
1279 }
1280 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001281 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001283 // We don't need to deliver new intents and/or set results if activity is going
1284 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 results = r.results;
1286 newIntents = r.newIntents;
1287 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001288 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1289 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1290 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001291 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1292 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001293 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001294 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001295 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001297 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1298 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 r.sleeping = false;
1300 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001301 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 mService.showAskCompatModeDialogLocked(r);
1303 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1306 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1307 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001308 final String profileFile = mService.mProfileFile;
1309 if (profileFile != null) {
1310 ParcelFileDescriptor profileFd = mService.mProfileFd;
1311 if (profileFd != null) {
1312 try {
1313 profileFd = profileFd.dup();
1314 } catch (IOException e) {
1315 if (profileFd != null) {
1316 try {
1317 profileFd.close();
1318 } catch (IOException o) {
1319 }
1320 profileFd = null;
1321 }
1322 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001324
1325 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001326 mService.mSamplingInterval, mService.mAutoStopProfiler,
1327 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001328 }
1329 }
1330 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001331
Andrii Kulianada10292017-02-09 23:19:29 -08001332 app.hasShownUi = true;
1333 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001334 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001335 // Because we could be starting an Activity in the system process this may not go across
1336 // a Binder interface which would create a new Configuration. Consequently we have to
1337 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001338
1339 final Configuration globalConfiguration =
1340 new Configuration(mService.getGlobalConfiguration());
1341 r.setLastReportedGlobalConfiguration(globalConfiguration);
1342 final Configuration mergedOverrideConfiguration =
1343 new Configuration(task.getMergedOverrideConfiguration());
1344 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1345
Craig Mautner2420ead2013-04-01 17:13:20 -07001346 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001347 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001348 globalConfiguration,
1349 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001350 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1351 r.persistentState, results, newIntents, !andResume,
1352 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001353
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001354 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001355 // This may be a heavy-weight process! Note that the package
1356 // manager will ensure that only activity can run in the main
1357 // process of the .apk, which is the only thing that will be
1358 // considered heavy-weight.
1359 if (app.processName.equals(app.info.packageName)) {
1360 if (mService.mHeavyWeightProcess != null
1361 && mService.mHeavyWeightProcess != app) {
1362 Slog.w(TAG, "Starting new heavy weight process " + app
1363 + " when already running "
1364 + mService.mHeavyWeightProcess);
1365 }
1366 mService.mHeavyWeightProcess = app;
1367 Message msg = mService.mHandler.obtainMessage(
1368 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1369 msg.obj = r;
1370 mService.mHandler.sendMessage(msg);
1371 }
1372 }
1373
1374 } catch (RemoteException e) {
1375 if (r.launchFailed) {
1376 // This is the second time we failed -- finish activity
1377 // and give up.
1378 Slog.e(TAG, "Second failure launching "
1379 + r.intent.getComponent().flattenToShortString()
1380 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001381 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1383 "2nd-crash", false);
1384 return false;
1385 }
1386
1387 // This is the first time we failed -- restart process and
1388 // retry.
1389 app.activities.remove(r);
1390 throw e;
1391 }
1392
1393 r.launchFailed = false;
1394 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001395 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001396 }
1397
1398 if (andResume) {
1399 // As part of the process of launching, ActivityThread also performs
1400 // a resume.
1401 stack.minimalResumeActivityLocked(r);
1402 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001403 // This activity is not starting in the resumed state... which should look like we asked
1404 // it to pause+stop (but remain visible), and it has done so and reported back the
1405 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001406 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001407 "Moving to PAUSED: " + r + " (starting in paused state)");
1408 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001409 }
1410
1411 // Launch the new version setup screen if needed. We do this -after-
1412 // launching the initial activity (that is, home), so that it can have
1413 // a chance to initialize itself while in the background, making the
1414 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001415 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001416 mService.startSetupActivityLocked();
1417 }
1418
Dianne Hackborn465fa392014-09-14 14:21:18 -07001419 // Update any services we are bound to that might care about whether
1420 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001421 if (r.app != null) {
1422 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1423 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001424
Craig Mautner2420ead2013-04-01 17:13:20 -07001425 return true;
1426 }
1427
Craig Mautnere79d42682013-04-01 19:01:53 -07001428 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001429 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001430 // Is this activity's application already running?
1431 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001432 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001433
Andrii Kulian02b7a832016-10-06 23:11:56 -07001434 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001435
1436 if (app != null && app.thread != null) {
1437 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001438 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1439 || !"android".equals(r.info.packageName)) {
1440 // Don't add this if it is a platform component that is marked
1441 // to run in multiple processes, because this is actually
1442 // part of the framework so doesn't make sense to track as a
1443 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001444 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1445 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001446 }
George Mount2c92c972014-03-20 09:38:23 -07001447 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001448 return;
1449 } catch (RemoteException e) {
1450 Slog.w(TAG, "Exception when starting activity "
1451 + r.intent.getComponent().flattenToShortString(), e);
1452 }
1453
1454 // If a dead object exception was thrown -- fall through to
1455 // restart the application.
1456 }
1457
1458 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001459 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001460 }
1461
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001462 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001463 String resultWho, int requestCode, int callingPid, int callingUid,
1464 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001465 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001466 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1467 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001468 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001469 return true;
1470 }
1471 final int componentRestriction = getComponentRestrictionForCallingPackage(
1472 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1473 final int actionRestriction = getActionRestrictionForCallingPackage(
1474 intent.getAction(), callingPackage, callingPid, callingUid);
1475 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1476 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1477 if (resultRecord != null) {
1478 resultStack.sendActivityResultLocked(-1,
1479 resultRecord, resultWho, requestCode,
1480 Activity.RESULT_CANCELED, null);
1481 }
1482 final String msg;
1483 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1484 msg = "Permission Denial: starting " + intent.toString()
1485 + " from " + callerApp + " (pid=" + callingPid
1486 + ", uid=" + callingUid + ")" + " with revoked permission "
1487 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1488 } else if (!aInfo.exported) {
1489 msg = "Permission Denial: starting " + intent.toString()
1490 + " from " + callerApp + " (pid=" + callingPid
1491 + ", uid=" + callingUid + ")"
1492 + " not exported from uid " + aInfo.applicationInfo.uid;
1493 } else {
1494 msg = "Permission Denial: starting " + intent.toString()
1495 + " from " + callerApp + " (pid=" + callingPid
1496 + ", uid=" + callingUid + ")"
1497 + " requires " + aInfo.permission;
1498 }
1499 Slog.w(TAG, msg);
1500 throw new SecurityException(msg);
1501 }
1502
1503 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1504 final String message = "Appop Denial: starting " + intent.toString()
1505 + " from " + callerApp + " (pid=" + callingPid
1506 + ", uid=" + callingUid + ")"
1507 + " requires " + AppOpsManager.permissionToOp(
1508 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1509 Slog.w(TAG, message);
1510 return false;
1511 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1512 final String message = "Appop Denial: starting " + intent.toString()
1513 + " from " + callerApp + " (pid=" + callingPid
1514 + ", uid=" + callingUid + ")"
1515 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1516 Slog.w(TAG, message);
1517 return false;
1518 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001519 if (options != null) {
1520 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1521 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1522 callingPid, callingUid);
1523 if (startInTaskPerm != PERMISSION_GRANTED) {
1524 final String msg = "Permission Denial: starting " + intent.toString()
1525 + " from " + callerApp + " (pid=" + callingPid
1526 + ", uid=" + callingUid + ") with launchTaskId="
1527 + options.getLaunchTaskId();
1528 Slog.w(TAG, msg);
1529 throw new SecurityException(msg);
1530 }
1531 }
1532 // Check if someone tries to launch an activity on a private display with a different
1533 // owner.
1534 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001535 if (launchDisplayId != INVALID_DISPLAY
1536 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1537 final String msg = "Permission Denial: starting " + intent.toString()
1538 + " from " + callerApp + " (pid=" + callingPid
1539 + ", uid=" + callingUid + ") with launchDisplayId="
1540 + launchDisplayId;
1541 Slog.w(TAG, msg);
1542 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001543 }
1544 }
1545
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001546 return true;
1547 }
1548
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001549 /** Check if caller is allowed to launch activities on specified display. */
1550 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1551 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1552 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1553
1554 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1555 if (activityDisplay == null) {
1556 Slog.w(TAG, "Launch on display check: display not found");
1557 return false;
1558 }
1559
1560 if (!activityDisplay.isPrivate()) {
1561 // Anyone can launch on a public display.
1562 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1563 + " allow launch on public display");
1564 return true;
1565 }
1566
1567 // Check if the caller is the owner of the display.
1568 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1569 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1570 + " allow launch for owner of the display");
1571 return true;
1572 }
1573
1574 // Check if caller is present on display
1575 if (activityDisplay.isUidPresent(callingUid)) {
1576 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1577 + " allow launch for caller present on the display");
1578 return true;
1579 }
1580
1581 // Check if the caller can launch anything.
1582 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1583 callingUid);
1584 if (startAnyPerm == PERMISSION_GRANTED) {
1585 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1586 + " allow launch any on display");
1587 return true;
1588 }
1589
1590 Slog.w(TAG, "Launch on display check: denied");
1591 return false;
1592 }
1593
1594 /** Update lists of UIDs that are present on displays and have access to them. */
1595 void updateUIDsPresentOnDisplay() {
1596 mDisplayAccessUIDs.clear();
1597 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1598 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1599 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1600 }
1601 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1602 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1603 }
1604
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001605 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001606 final long identity = Binder.clearCallingIdentity();
1607 try {
1608 return UserManager.get(mService.mContext).getUserInfo(userId);
1609 } finally {
1610 Binder.restoreCallingIdentity(identity);
1611 }
1612 }
1613
Svet Ganov99b60432015-06-27 13:15:22 -07001614 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001615 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001616 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1617 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001618 == PackageManager.PERMISSION_DENIED) {
1619 return ACTIVITY_RESTRICTION_PERMISSION;
1620 }
1621
Christopher Tateff7add02015-08-17 10:23:22 -07001622 if (activityInfo.permission == null) {
1623 return ACTIVITY_RESTRICTION_NONE;
1624 }
1625
Svet Ganov99b60432015-06-27 13:15:22 -07001626 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1627 if (opCode == AppOpsManager.OP_NONE) {
1628 return ACTIVITY_RESTRICTION_NONE;
1629 }
1630
1631 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1632 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001633 if (!ignoreTargetSecurity) {
1634 return ACTIVITY_RESTRICTION_APPOP;
1635 }
Svet Ganov99b60432015-06-27 13:15:22 -07001636 }
1637
1638 return ACTIVITY_RESTRICTION_NONE;
1639 }
1640
Svetoslav7008b512015-06-24 18:47:07 -07001641 private int getActionRestrictionForCallingPackage(String action,
1642 String callingPackage, int callingPid, int callingUid) {
1643 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001644 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001645 }
1646
1647 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1648 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001649 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001650 }
1651
1652 final PackageInfo packageInfo;
1653 try {
1654 packageInfo = mService.mContext.getPackageManager()
1655 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1656 } catch (PackageManager.NameNotFoundException e) {
1657 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001658 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001659 }
1660
1661 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001662 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001663 }
1664
1665 if (mService.checkPermission(permission, callingPid, callingUid) ==
1666 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001667 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001668 }
1669
1670 final int opCode = AppOpsManager.permissionToOpCode(permission);
1671 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001672 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001673 }
1674
1675 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1676 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001677 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001678 }
1679
Svet Ganov99b60432015-06-27 13:15:22 -07001680 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001681 }
1682
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001683 void setLaunchSource(int uid) {
1684 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1685 }
1686
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001687 void acquireLaunchWakelock() {
1688 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1689 throw new IllegalStateException("Calling must be system uid");
1690 }
1691 mLaunchingActivity.acquire();
1692 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1693 // To be safe, don't allow the wake lock to be held for too long.
1694 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1695 }
1696 }
1697
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001698 /**
1699 * Called when the frontmost task is idle.
1700 * @return the state of mService.mBooting before this was called.
1701 */
1702 private boolean checkFinishBootingLocked() {
1703 final boolean booting = mService.mBooting;
1704 boolean enableScreen = false;
1705 mService.mBooting = false;
1706 if (!mService.mBooted) {
1707 mService.mBooted = true;
1708 enableScreen = true;
1709 }
1710 if (booting || enableScreen) {
1711 mService.postFinishBooting(booting, enableScreen);
1712 }
1713 return booting;
1714 }
1715
Craig Mautnerf3333272013-04-22 10:55:53 -07001716 // Checked.
1717 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001718 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001719 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001720
Craig Mautnerf3333272013-04-22 10:55:53 -07001721 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001722 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001723 int NS = 0;
1724 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001725 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001726 boolean activityRemoved = false;
1727
Wale Ogunwale7d701172015-03-11 15:36:30 -07001728 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001729 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001730 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1731 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001732 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1733 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001734 if (fromTimeout) {
1735 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001736 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001737
1738 // This is a hack to semi-deal with a race condition
1739 // in the client where it can be constructed with a
1740 // newer configuration from when we asked it to launch.
1741 // We'll update with whatever configuration it now says
1742 // it used to launch.
1743 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001744 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001745 }
1746
1747 // We are now idle. If someone is waiting for a thumbnail from
1748 // us, we can now deliver.
1749 r.idle = true;
1750
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001751 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001752 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001753 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001754 }
1755 }
1756
1757 if (allResumedActivitiesIdle()) {
1758 if (r != null) {
1759 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001760 }
1761
1762 if (mLaunchingActivity.isHeld()) {
1763 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1764 if (VALIDATE_WAKE_LOCK_CALLER &&
1765 Binder.getCallingUid() != Process.myUid()) {
1766 throw new IllegalStateException("Calling must be system uid");
1767 }
1768 mLaunchingActivity.release();
1769 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001770 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001771 }
1772
1773 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001774 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1775 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001776 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001777 if ((NF = mFinishingActivities.size()) > 0) {
1778 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001779 mFinishingActivities.clear();
1780 }
1781
Craig Mautnerf3333272013-04-22 10:55:53 -07001782 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001783 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001784 mStartingUsers.clear();
1785 }
1786
Craig Mautnerf3333272013-04-22 10:55:53 -07001787 // Stop any activities that are scheduled to do so but have been
1788 // waiting for the next one to start.
1789 for (int i = 0; i < NS; i++) {
1790 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001791 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001792 if (stack != null) {
1793 if (r.finishing) {
1794 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1795 } else {
1796 stack.stopActivityLocked(r);
1797 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001798 }
1799 }
1800
1801 // Finish any activities that are scheduled to do so but have been
1802 // waiting for the next one to start.
1803 for (int i = 0; i < NF; i++) {
1804 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001805 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001806 if (stack != null) {
1807 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1808 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001809 }
1810
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001811 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001812 // Complete user switch
1813 if (startingUsers != null) {
1814 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001815 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001816 }
1817 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 }
1819
1820 mService.trimApplications();
1821 //dump();
1822 //mWindowManager.dump();
1823
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001825 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001826 }
1827
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001828 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001829 }
1830
Craig Mautner8e569572013-10-11 17:36:59 -07001831 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001832 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001833 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1834 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001835 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1836 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1837 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001838 }
Craig Mautner19091252013-10-05 00:03:53 -07001839 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001840 }
1841
1842 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001843 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1844 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001845 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1846 stacks.get(stackNdx).closeSystemDialogsLocked();
1847 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001848 }
1849 }
1850
Craig Mautner93529a42013-10-04 15:03:13 -07001851 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001852 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001853 }
1854
Craig Mautner8d341ef2013-03-26 09:03:27 -07001855 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001856 * Update the last used stack id for non-current user (current user's last
1857 * used stack is the focused stack)
1858 */
1859 void updateUserStackLocked(int userId, ActivityStack stack) {
1860 if (userId != mCurrentUser) {
1861 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1862 }
1863 }
1864
1865 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001866 * @return true if some activity was finished (or would have finished if doit were true).
1867 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001868 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1869 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001870 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001871 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1872 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001873 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001874 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001875 if (stack.finishDisabledPackageActivitiesLocked(
1876 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001877 didSomething = true;
1878 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001879 }
1880 }
1881 return didSomething;
1882 }
1883
Dianne Hackborna413dc02013-07-12 12:02:55 -07001884 void updatePreviousProcessLocked(ActivityRecord r) {
1885 // Now that this process has stopped, we may want to consider
1886 // it to be the previous app to try to keep around in case
1887 // the user wants to return to it.
1888
1889 // First, found out what is currently the foreground app, so that
1890 // we don't blow away the previous app if this activity is being
1891 // hosted by the process that is actually still the foreground.
1892 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001893 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1894 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001895 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1896 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001897 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001898 if (stack.mResumedActivity != null) {
1899 fgApp = stack.mResumedActivity.app;
1900 } else if (stack.mPausingActivity != null) {
1901 fgApp = stack.mPausingActivity.app;
1902 }
1903 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001904 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001905 }
1906 }
1907
1908 // Now set this one as the previous process, only if that really
1909 // makes sense to.
1910 if (r.app != null && fgApp != null && r.app != fgApp
1911 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001912 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001913 mService.mPreviousProcess = r.app;
1914 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1915 }
1916 }
1917
Wale Ogunwaled046a012015-12-24 13:05:59 -08001918 boolean resumeFocusedStackTopActivityLocked() {
1919 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001920 }
1921
Wale Ogunwaled046a012015-12-24 13:05:59 -08001922 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001923 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001924 if (targetStack != null && isFocusedStack(targetStack)) {
1925 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001926 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001927 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1928 if (r == null || r.state != RESUMED) {
1929 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001930 } else if (r.state == RESUMED) {
1931 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1932 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001933 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001934 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001935 }
1936
Todd Kennedy39bfee52016-02-24 10:28:21 -08001937 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1938 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1939 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1940 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1941 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1942 }
1943 }
1944 }
1945
Adrian Roos20d7df32016-01-12 18:59:43 +01001946 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1947 TaskRecord finishedTask = null;
1948 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001951 final int numStacks = stacks.size();
1952 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1953 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001954 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1955 if (stack == focusedStack || finishedTask == null) {
1956 finishedTask = t;
1957 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001959 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001960 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001961 }
1962
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001963 void finishVoiceTask(IVoiceInteractionSession session) {
1964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1965 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1966 final int numStacks = stacks.size();
1967 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1968 final ActivityStack stack = stacks.get(stackNdx);
1969 stack.finishVoiceTask(session);
1970 }
1971 }
1972 }
1973
Andrii Kulianc27916642016-04-12 17:59:27 -07001974 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1975 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001976 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1977 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001978 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001979 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1980 // Caller wants the home activity moved with it. To accomplish this,
1981 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001982 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001983 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001984 ActivityStack currentStack = task.getStack();
1985 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001986 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1987 + task + " to front. Stack is null");
1988 return;
1989 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001990
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001991 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001992 int stackId = options.getLaunchStackId();
1993 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001994 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001995 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001996 if (stackId == INVALID_STACK_ID) {
1997 stackId = task.getLaunchStackId();
1998 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001999 if (stackId != currentStack.mStackId) {
2000 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002001 !FORCE_FOCUS, reason);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002002 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002003 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2004 // still need moveTaskToFrontLocked() below for any transition settings.
2005 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002006 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2007 resizeStackLocked(stackId, bounds,
2008 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002009 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002010 } else {
2011 // WM resizeTask must be done after the task is moved to the correct stack,
2012 // because Task's setBounds() also updates dim layer's bounds, but that has
2013 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002014 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002015 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002016 }
2017 }
2018
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002019 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002020 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002021 r == null ? null : r.appTimeTracker, reason);
2022
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002023 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002024 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002025
Andrii Kulian02b7a832016-10-06 23:11:56 -07002026 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002027 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002028 }
2029
Wale Ogunwale854809c2015-12-27 16:18:19 -08002030 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002031 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002032 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002033 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002034 return false;
2035 }
2036 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2037 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002038 }
2039
Craig Mautner967212c2013-04-13 21:10:58 -07002040 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002041 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002042 }
2043
2044 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, 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) {
2047 return 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 }
Jorim Jaggife762342016-10-13 14:33:27 +02002052 return 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);
2285 if (fromStackId == PINNED_STACK_ID) {
2286 // Update the return-to to reflect where the pinned stack task was moved
2287 // from so that we retain the stack that was previously visible if the
2288 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002289 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2290 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002291 MetricsLogger.action(mService.mContext,
2292 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002293 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002294 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002295 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002296 "moveTasksToFullscreenStack - onTop", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002297 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002298
2299 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2300 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002301 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002302 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002303 final TaskRecord task = tasks.get(i);
2304 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, MAX_VALUE,
2305 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002306 }
2307 }
2308 } finally {
2309 mAllowDockedStackResize = true;
2310 mWindowManager.continueSurfaceLayout();
2311 }
2312 }
2313
Jorim Jaggidc249c42015-12-15 14:57:31 -08002314 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002315 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2316 boolean preserveWindows) {
2317 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2318 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2319 false /* deferResume */);
2320 }
2321
2322 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2323 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2324 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002325
2326 if (!mAllowDockedStackResize) {
2327 // Docked stack resize currently disabled.
2328 return;
2329 }
2330
Jorim Jaggidc249c42015-12-15 14:57:31 -08002331 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2332 if (stack == null) {
2333 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2334 return;
2335 }
2336
2337 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2338 mWindowManager.deferSurfaceLayout();
2339 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002340 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2341 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002342 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002343 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002344
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002345 // TODO: Checking for isAttached might not be needed as if the user passes in null
2346 // dockedBounds then they want the docked stack to be dismissed.
2347 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2348 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002349 // In this case we make all other static stacks fullscreen and move all
2350 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002351 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002352
2353 // stack shouldn't contain anymore activities, so nothing to resume.
2354 r = null;
2355 } else {
2356 // Docked stacks occupy a dedicated region on screen so the size of all other
2357 // static stacks need to be adjusted so they don't overlap with the docked stack.
2358 // We get the bounds to use from window manager which has been adjusted for any
2359 // screen controls and is also the same for all stacks.
Matthew Nge15352e2016-12-20 15:36:29 -08002360 final Rect tempOtherTaskRect = new Rect();
2361 final Rect tempOtherTaskInsetRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002362 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002363 final ActivityStack current = getStack(i);
2364 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Nge15352e2016-12-20 15:36:29 -08002365 current.getStackDockedModeBounds(tempRect, tempOtherTaskRect,
2366 tempOtherTaskInsetRect, true /* ignoreVisibility */);
2367 resizeStackLocked(i, tempRect,
2368 !tempOtherTaskRect.isEmpty() ? tempOtherTaskRect :
2369 tempOtherTaskBounds,
2370 !tempOtherTaskInsetRect.isEmpty() ? tempOtherTaskInsetRect :
2371 tempOtherTaskInsetBounds,
2372 preserveWindows, true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002373 }
2374 }
2375 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002376 if (!deferResume) {
2377 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2378 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002379 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002380 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002381 mWindowManager.continueSurfaceLayout();
2382 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2383 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002384 }
2385
Robert Carr0d00c2e2016-02-29 17:45:02 -08002386 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2387 final ActivityStack stack = getStack(PINNED_STACK_ID);
2388 if (stack == null) {
2389 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2390 return;
2391 }
2392 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2393 mWindowManager.deferSurfaceLayout();
2394 try {
2395 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002396 Rect insetBounds = null;
2397 if (tempPinnedTaskBounds != null) {
2398 // We always use 0,0 as the position for the inset rect because
2399 // if we are getting insets at all in the pinned stack it must mean
2400 // we are headed for fullscreen.
2401 insetBounds = tempRect;
2402 insetBounds.top = 0;
2403 insetBounds.left = 0;
2404 insetBounds.right = tempPinnedTaskBounds.width();
2405 insetBounds.bottom = tempPinnedTaskBounds.height();
2406 }
2407 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002408 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002409 } finally {
2410 mWindowManager.continueSurfaceLayout();
2411 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2412 }
2413 }
2414
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002415 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002416 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002417 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002418 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002419 }
2420
Wale Ogunwale1666e312016-12-16 11:27:18 -08002421 final ActivityContainer activityContainer =
2422 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002423 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002424 }
2425
Winson Chung010927a2016-12-15 16:12:35 -08002426 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002427 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002428 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2429 * instead moved back onto the fullscreen stack.
2430 */
2431 void removeStackLocked(int stackId) {
2432 final ActivityStack stack = getStack(stackId);
2433 if (stack == null) {
2434 return;
2435 }
2436
2437 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2438 if (stack.getStackId() == PINNED_STACK_ID) {
2439 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2440 if (fullscreenStack != null) {
2441 final boolean isFullscreenStackVisible =
2442 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2443 for (int i = 0; i < tasks.size(); i++) {
2444 // Insert the task either at the top of the fullscreen stack if it is hidden,
2445 // or just under the top task if it is currently visible
2446 final int insertPosition = isFullscreenStackVisible
2447 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2448 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002449 final TaskRecord task = tasks.get(i);
2450 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002451 }
2452 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2453 resumeFocusedStackTopActivityLocked();
2454 } else {
2455 // If there is no fullscreen stack, then create the stack and move all the tasks
2456 // onto the stack
2457 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, false /* onTop */);
2458 }
2459 } else {
2460 for (int i = tasks.size() - 1; i >= 0; i--) {
2461 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2462 REMOVE_FROM_RECENTS);
2463 }
2464 }
2465 }
2466
2467 /**
2468 * Removes the task with the specified task id.
2469 *
2470 * @param taskId Identifier of the task to be removed.
2471 * @param killProcess Kill any process associated with the task if possible.
2472 * @param removeFromRecents Whether to also remove the task from recents.
2473 * @return Returns true if the given task was found and removed.
2474 */
2475 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2476 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2477 if (tr != null) {
2478 tr.removeTaskActivitiesLocked();
2479 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2480 if (tr.isPersistable) {
2481 mService.notifyTaskPersisterLocked(null, true);
2482 }
2483 return true;
2484 }
2485 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2486 return false;
2487 }
2488
2489 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2490 if (removeFromRecents) {
2491 mRecentTasks.remove(tr);
2492 tr.removedFromRecents();
2493 }
2494 ComponentName component = tr.getBaseIntent().getComponent();
2495 if (component == null) {
2496 Slog.w(TAG, "No component for base intent of task: " + tr);
2497 return;
2498 }
2499
2500 // Find any running services associated with this app and stop if needed.
2501 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2502
2503 if (!killProcess) {
2504 return;
2505 }
2506
2507 // Determine if the process(es) for this task should be killed.
2508 final String pkg = component.getPackageName();
2509 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2510 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2511 for (int i = 0; i < pmap.size(); i++) {
2512
2513 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2514 for (int j = 0; j < uids.size(); j++) {
2515 ProcessRecord proc = uids.valueAt(j);
2516 if (proc.userId != tr.userId) {
2517 // Don't kill process for a different user.
2518 continue;
2519 }
2520 if (proc == mService.mHomeProcess) {
2521 // Don't kill the home process along with tasks from the same package.
2522 continue;
2523 }
2524 if (!proc.pkgList.containsKey(pkg)) {
2525 // Don't kill process that is not associated with this task.
2526 continue;
2527 }
2528
2529 for (int k = 0; k < proc.activities.size(); k++) {
2530 TaskRecord otherTask = proc.activities.get(k).task;
2531 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2532 // Don't kill process(es) that has an activity in a different task that is
2533 // also in recents.
2534 return;
2535 }
2536 }
2537
2538 if (proc.foregroundServices) {
2539 // Don't kill process(es) with foreground service.
2540 return;
2541 }
2542
2543 // Add process to kill list.
2544 procsToKill.add(proc);
2545 }
2546 }
2547
2548 // Kill the running processes.
2549 for (int i = 0; i < procsToKill.size(); i++) {
2550 ProcessRecord pr = procsToKill.get(i);
2551 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2552 && pr.curReceivers.isEmpty()) {
2553 pr.kill("remove task", true);
2554 } else {
2555 // We delay killing processes that are not in the background or running a receiver.
2556 pr.waitingToKill = "remove task";
2557 }
2558 }
2559 }
2560
Craig Mautner4a1cb222013-12-04 16:14:06 -08002561 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002562 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002563 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2564 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002565 break;
2566 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002567 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002568 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002569 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002570 }
2571
Chong Zhang5dcb2752015-08-18 13:50:26 -07002572 /**
2573 * Restores a recent task to a stack
2574 * @param task The recent task to be restored.
2575 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002576 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2577 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002578 * @return true if the task has been restored successfully.
2579 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002580 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002581 if (!StackId.isStaticStack(stackId)) {
2582 // If stack is not static (or stack id is invalid) - use the default one.
2583 // This means that tasks that were on external displays will be restored on the
2584 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002585 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002586 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002587 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2588 // Put it in the fullscreen stack.
2589 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002590 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002591
Andrii Kulian02b7a832016-10-06 23:11:56 -07002592 final ActivityStack currentStack = task.getStack();
2593 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002594 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002595 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002596 // Nothing else to do since it is already restored in the right stack.
2597 return true;
2598 }
2599 // Remove current stack association, so we can re-associate the task with the
2600 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002601 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002602 }
2603
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002604 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002605 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002606
2607 if (stack == null) {
2608 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002609 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2610 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002611 return false;
2612 }
2613
Wale Ogunwale72919d22016-12-08 18:58:50 -08002614 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002615 // TODO: move call for creation here and other place into Stack.addTask()
2616 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002617 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2618 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002619 final ArrayList<ActivityRecord> activities = task.mActivities;
2620 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002621 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002622 }
2623 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002624 }
2625
Wale Ogunwale040b4702015-08-06 18:10:50 -07002626 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002627 * Move stack with all its existing content to specified display.
2628 * @param stackId Id of stack to move.
2629 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002630 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002631 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002632 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002633 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2634 if (activityDisplay == null) {
2635 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2636 + displayId);
2637 }
2638 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2639 if (activityContainer != null) {
2640 if (activityContainer.isAttachedLocked()) {
2641 if (activityContainer.getDisplayId() == displayId) {
2642 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2643 + " to its current displayId=" + displayId);
2644 }
2645
Andrii Kulian250d6532017-02-08 23:30:45 -08002646 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002647 } else {
2648 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2649 + stackId + " is not attached to any display.");
2650 }
2651 } else {
2652 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2653 + stackId);
2654 }
2655
2656 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2657 !PRESERVE_WINDOWS);
2658 // TODO(multi-display): resize stacks properly if moved from split-screen.
2659 }
2660
2661 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002662 * Moves the specified task record to the input stack id.
2663 * WARNING: This method performs an unchecked/raw move of the task and
2664 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002665 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002666 * @param task Task to move.
2667 * @param stackId Id of stack to move task to.
2668 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002669 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002670 * @param reason Reason the task is been moved.
2671 * @return The stack the task was moved to.
2672 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002673 ActivityStack moveTaskToStackUncheckedLocked(TaskRecord task, int stackId, boolean toTop,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002674 boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002675
2676 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2677 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2678 + "support multi-window task=" + task + " to stackId=" + stackId);
2679 }
2680
Wale Ogunwale06579d62016-04-30 15:29:06 -07002681 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002682 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002683 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002684 final boolean wasResumed = prevStack.mResumedActivity == r;
Winson Chungc2baac02017-01-11 13:34:47 -08002685 final boolean wasPaused = prevStack.mPausingActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002686 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2687 // Whenever we are moving the top activity from the front stack we want to make sure to move
2688 // the stack to the front.
Andrii Kulian7d95df42017-02-15 10:11:48 -08002689 final boolean wasFront = isFrontStackOnDisplay(prevStack)
Wale Ogunwaled046a012015-12-24 13:05:59 -08002690 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002691
Chong Zhangd545dce2016-02-29 18:09:17 -08002692 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002693 // We don't allow moving a unresizeable task to the docked stack since the docked
2694 // stack is used for split-screen mode and will cause things like the docked divider to
2695 // show up. We instead leave the task in its current stack or move it to the fullscreen
2696 // stack if it isn't currently in a stack.
2697 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002698 Slog.w(TAG, "Can not move unresizeable task=" + task
2699 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2700 }
2701
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002702 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2703 // if a docked stack is created below which will lead to the stack we are moving from and
2704 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002705 task.mTemporarilyUnresizable = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002706 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002707 task.mTemporarilyUnresizable = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002708 task.reparent(stack.mStackId, toTop ? MAX_VALUE : 0, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002709
Winson Chungc2baac02017-01-11 13:34:47 -08002710 // Reset the resumed activity on the previous stack
2711 if (wasResumed) {
2712 prevStack.mResumedActivity = null;
2713 }
2714 // Reset the paused activity on the previous stack
2715 if (wasPaused) {
2716 prevStack.mPausingActivity = null;
Winson Chung4dabf232017-01-25 13:25:22 -08002717 prevStack.removeTimeoutsForActivityLocked(r);
Winson Chungc2baac02017-01-11 13:34:47 -08002718 }
2719
Chong Zhang02898352015-08-21 17:27:14 -07002720 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002721 // move focus to the new stack by moving the stack to the front.
Winson Chungc2baac02017-01-11 13:34:47 -08002722 stack.moveToFrontAndResumeStateIfNeeded(r, forceFocus || wasFocused || wasFront, wasResumed,
2723 wasPaused, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002724
Wale Ogunwale040b4702015-08-06 18:10:50 -07002725 return stack;
2726 }
2727
Chong Zhange4fbd322016-03-01 14:44:03 -08002728 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002729 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002730 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002731 false /* deferResume */);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002732 }
2733
2734 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002735 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002736 final TaskRecord task = anyTaskForIdLocked(taskId);
2737 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002738 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002739 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002740 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002741
Andrii Kulian02b7a832016-10-06 23:11:56 -07002742 final ActivityStack currentStack = task.getStack();
2743 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002744 // You are already in the right stack silly...
2745 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002746 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002747 }
2748
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002749 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2750 throw new IllegalArgumentException("moveTaskToStack:"
2751 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2752 }
2753
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002754 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002755 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002756 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002757 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002758 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002759 // We are about to relaunch the activity because its configuration changed due to
2760 // being maximized, i.e. size change. The activity will first remove the old window
2761 // and then add a new one. This call will tell window manager about this, so it can
2762 // preserve the old window until the new one is drawn. This prevents having a gap
2763 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002764 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002765 // Note here we always set the replacing window first, as the flags might be needed
2766 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002767 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002768 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002769
2770 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002771 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002772 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002773 try {
2774 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002775 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002776 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002777
Wale Ogunwale961f4852016-02-01 20:25:54 -08002778 if (!animate) {
2779 stack.mNoAnimActivities.add(topActivity);
2780 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002781
Wale Ogunwale961f4852016-02-01 20:25:54 -08002782 // We might trigger a configuration change. Save the current task bounds for freezing.
Wale Ogunwale1666e312016-12-16 11:27:18 -08002783 // TODO: Should this call be moved inside the resize method in WM?
2784 stack.prepareFreezingTaskBounds();
Wale Ogunwale961f4852016-02-01 20:25:54 -08002785
2786 // Make sure the task has the appropriate bounds/size for the stack it is in.
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002787 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID
2788 && !Objects.equals(task.mBounds, stack.mBounds)) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002789 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2790 deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002791 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2792 Rect bounds = task.getLaunchBounds();
2793 if (bounds == null) {
2794 stack.layoutTaskInStack(task, null);
2795 bounds = task.mBounds;
2796 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002797 kept = task.resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002798 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002799 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2800 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002801 }
2802 } finally {
2803 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002804 }
2805
2806 if (mightReplaceWindow) {
2807 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2808 // window), we need to clear the replace window settings. Otherwise, we schedule a
2809 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002810 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002811 }
2812
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002813 if (!deferResume) {
2814
2815 // The task might have already been running and its visibility needs to be synchronized with
2816 // the visibility of the stack / windows.
2817 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2818 resumeFocusedStackTopActivityLocked();
2819 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002820
Jorim Jaggid53f0922016-04-06 22:16:23 -07002821 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002822
2823 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002824 }
2825
Wale Ogunwale079a0042015-10-24 11:44:07 -07002826 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2827 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2828 if (stack == null) {
2829 throw new IllegalArgumentException(
2830 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2831 }
2832
2833 final ActivityRecord r = stack.topRunningActivityLocked();
2834 if (r == null) {
2835 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2836 + " in stack=" + stack);
2837 return false;
2838 }
2839
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002840 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002841 Slog.w(TAG,
2842 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002843 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002844 return false;
2845 }
2846
Winson Chungb5c41b72016-12-07 15:00:47 -08002847 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2848 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002849 return true;
2850 }
2851
Winson Chungb5c41b72016-12-07 15:00:47 -08002852 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2853 boolean moveHomeStackToFront) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002854
Wale Ogunwale480dca02016-02-06 13:58:29 -08002855 mWindowManager.deferSurfaceLayout();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002856 // Need to make sure the pinned stack exist so we can resize it below...
2857 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2858
Wale Ogunwale480dca02016-02-06 13:58:29 -08002859 try {
2860 final TaskRecord task = r.task;
2861
Andrii Kulian02b7a832016-10-06 23:11:56 -07002862 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002863 // An activity can't be pinned and visible behind at the same time. Go ahead and
2864 // release it from been visible behind before pinning.
2865 requestVisibleBehindLocked(r, false);
2866 }
2867
Wale Ogunwale480dca02016-02-06 13:58:29 -08002868 // Resize the pinned stack to match the current size of the task the activity we are
2869 // going to be moving is currently contained in. We do this to have the right starting
2870 // animation bounds for the pinned stack to the desired bounds the caller wants.
2871 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2872 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002873 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002874
2875 if (task.mActivities.size() == 1) {
2876 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002877 // the stack without re-parenting the activity in a different task. We don't
2878 // move the home stack forward if we are currently entering picture-in-picture
2879 // while pausing because that changes the focused stack and may prevent the new
2880 // starting activity from resuming.
2881 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
2882 && !r.supportsPictureInPictureWhilePausing) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002883 // Move the home stack forward if the task we just moved to the pinned stack
2884 // was launched from home so home should be visible behind it.
2885 moveHomeStackToFront(reason);
2886 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002887 moveTaskToStackLocked(
2888 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2889 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002890 // There are multiple activities in the task and moving the top activity should
2891 // reveal/leave the other activities in their original task
Wale Ogunwale480dca02016-02-06 13:58:29 -08002892 stack.moveActivityToStack(r);
2893 }
Winson Chungc2baac02017-01-11 13:34:47 -08002894
2895 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2896 // to the pinned stack
2897 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002898 } finally {
2899 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002900 }
2901
Wale Ogunwale480dca02016-02-06 13:58:29 -08002902 // The task might have already been running and its visibility needs to be synchronized
2903 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002904 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002905 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002906
Wale Ogunwale1666e312016-12-16 11:27:18 -08002907 stack.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002908 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002909 }
2910
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002911 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002912 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2913 if (r == null || !r.isFocusable()) {
2914 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2915 "moveActivityStackToFront: unfocusable r=" + r);
2916 return false;
2917 }
2918
2919 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002920 final ActivityStack stack = r.getStack();
2921 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002922 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2923 + r + " task=" + task);
2924 return false;
2925 }
2926
Chong Zhang6cda19c2016-06-14 19:07:56 -07002927 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2928 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2929 "moveActivityStackToFront: already on top, r=" + r);
2930 return false;
2931 }
2932
2933 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2934 "moveActivityStackToFront: r=" + r);
2935
2936 stack.moveToFront(reason, task);
2937 return true;
2938 }
2939
Craig Mautnerac6f8432013-07-17 13:24:59 -07002940 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002941 mTmpFindTaskResult.r = null;
2942 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002943 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2947 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002948 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002949 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002950 continue;
2951 }
2952 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002953 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2954 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002955 continue;
2956 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002957 stack.findTaskLocked(r, mTmpFindTaskResult);
2958 // It is possible to have task in multiple stacks with the same root affinity.
2959 // If the match we found was based on root affinity we keep on looking to see if
2960 // there is a better match in another stack. We eventually return the match based
2961 // on root affinity if we don't find a better match.
2962 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2963 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002964 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002965 }
2966 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002967 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2968 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002969 }
2970
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002971 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2972 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2974 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002976 final ActivityRecord ar = stacks.get(stackNdx)
2977 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002978 if (ar != null) {
2979 return ar;
2980 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002981 }
2982 }
2983 return null;
2984 }
2985
Craig Mautner8d341ef2013-03-26 09:03:27 -07002986 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002987 scheduleSleepTimeout();
2988 if (!mGoingToSleep.isHeld()) {
2989 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002990 if (mLaunchingActivity.isHeld()) {
2991 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2992 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002993 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002994 mLaunchingActivity.release();
2995 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002997 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002998 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002999 }
3000
3001 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003002 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003003
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003004 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005 final long endTime = System.currentTimeMillis() + timeout;
3006 while (true) {
3007 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3009 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003010 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3011 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3012 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013 }
3014 if (cantShutdown) {
3015 long timeRemaining = endTime - System.currentTimeMillis();
3016 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003017 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003018 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 } catch (InterruptedException e) {
3020 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021 } else {
3022 Slog.w(TAG, "Activity manager shutdown timed out");
3023 timedout = true;
3024 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003025 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003026 } else {
3027 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003028 }
3029 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003030
3031 // Force checkReadyForSleep to complete.
3032 mSleepTimeout = true;
3033 checkReadyForSleepLocked();
3034
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 return timedout;
3036 }
3037
3038 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003039 removeSleepTimeouts();
3040 if (mGoingToSleep.isHeld()) {
3041 mGoingToSleep.release();
3042 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3044 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003045 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3046 final ActivityStack stack = stacks.get(stackNdx);
3047 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003048 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003049 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 }
Craig Mautner5314a402013-09-26 12:40:16 -07003051 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003052 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003053 mGoingToSleepActivities.clear();
3054 }
3055
3056 void activitySleptLocked(ActivityRecord r) {
3057 mGoingToSleepActivities.remove(r);
3058 checkReadyForSleepLocked();
3059 }
3060
3061 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003062 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003063 // Do not care.
3064 return;
3065 }
3066
3067 if (!mSleepTimeout) {
3068 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003069 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3070 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3072 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3073 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003074 }
3075
3076 if (mStoppingActivities.size() > 0) {
3077 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003078 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003079 + mStoppingActivities.size() + " activities");
3080 scheduleIdleLocked();
3081 dontSleep = true;
3082 }
3083
3084 if (mGoingToSleepActivities.size() > 0) {
3085 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003086 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003087 + mGoingToSleepActivities.size() + " activities");
3088 dontSleep = true;
3089 }
3090
3091 if (dontSleep) {
3092 return;
3093 }
3094 }
3095
Wei Wang65c7a152016-06-02 18:51:22 -07003096 // Send launch end powerhint before going sleep
3097 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3098
Craig Mautnere0a38842013-12-16 16:14:02 -08003099 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3100 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3102 stacks.get(stackNdx).goToSleep();
3103 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003104 }
3105
3106 removeSleepTimeouts();
3107
3108 if (mGoingToSleep.isHeld()) {
3109 mGoingToSleep.release();
3110 }
3111 if (mService.mShuttingDown) {
3112 mService.notifyAll();
3113 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003114 }
3115
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003116 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003117 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003118 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003119 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003120 }
3121 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003122 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003123 mWindowManager.executeAppTransition();
3124 return true;
3125 }
3126 return false;
3127 }
3128
Craig Mautner8d341ef2013-03-26 09:03:27 -07003129 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003130 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3131 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003132 int stackNdx = stacks.size() - 1;
3133 while (stackNdx >= 0) {
3134 stacks.get(stackNdx).handleAppCrashLocked(app);
3135 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137 }
3138 }
3139
Jose Lima4b6c6692014-08-12 17:41:12 -07003140 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003141 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003142 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003143 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3144 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003145 return false;
3146 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003147
3148 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3149 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3150 + " visible=" + visible + " stackId=" + stack.mStackId
3151 + " can't contain visible behind activities");
3152 return false;
3153 }
3154
Jose Lima4b6c6692014-08-12 17:41:12 -07003155 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003156 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3157 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003158
3159 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003160 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003161 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003162 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003163 return true;
3164 }
3165
3166 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003167 if (visible && top.fullscreen) {
3168 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003169 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3170 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3171 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3172 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003173 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003174 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3175 // Only the activity set as currently visible behind should actively reset its
3176 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003177 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3178 "requestVisibleBehind: returning visible=" + visible
3179 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3180 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003181 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003182 }
3183
Jose Lima4b6c6692014-08-12 17:41:12 -07003184 stack.setVisibleBehindActivity(visible ? r : null);
3185 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003186 // If there is a translucent home activity, we need to force it stop being translucent,
3187 // because we can't depend on the application to necessarily perform that operation.
3188 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003189 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003190 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003191 mService.convertFromTranslucent(next.appToken);
3192 }
3193 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003194 if (top.app != null && top.app.thread != null) {
3195 // Notify the top app of the change.
3196 try {
3197 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3198 } catch (RemoteException e) {
3199 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003200 }
3201 return true;
3202 }
3203
Craig Mautnerbb742462014-07-07 15:28:55 -07003204 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003205 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003206 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003207 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003208
3209 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003210 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003211 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003212 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003213 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003214
3215 // When launching tasks behind, update the last active time of the top task after the new
3216 // task has been shown briefly
3217 final ActivityRecord top = stack.topActivity();
3218 if (top != null) {
3219 top.task.touchActiveTime();
3220 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003221 }
3222
3223 void scheduleLaunchTaskBehindComplete(IBinder token) {
3224 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3225 }
3226
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003227 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3228 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003229 mKeyguardController.beginActivityVisibilityUpdate();
3230 try {
3231 // First the front stacks. In case any are not fullscreen and are in front of home.
3232 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3233 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3234 final int topStackNdx = stacks.size() - 1;
3235 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3236 final ActivityStack stack = stacks.get(stackNdx);
3237 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3238 }
Craig Mautner580ea812013-04-25 12:58:38 -07003239 }
Jorim Jaggife762342016-10-13 14:33:27 +02003240 } finally {
3241 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003242 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243 }
3244
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003245 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3246 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3247 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3248 final int topStackNdx = stacks.size() - 1;
3249 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3250 final ActivityStack stack = stacks.get(stackNdx);
3251 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3252 }
3253 }
3254 }
3255
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003256 void invalidateTaskLayers() {
3257 mTaskLayersChanged = true;
3258 }
3259
3260 void rankTaskLayersIfNeeded() {
3261 if (!mTaskLayersChanged) {
3262 return;
3263 }
3264 mTaskLayersChanged = false;
3265 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3266 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3267 int baseLayer = 0;
3268 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3269 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3270 }
3271 }
3272 }
3273
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003274 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3275 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3276 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3277 final int topStackNdx = stacks.size() - 1;
3278 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3279 final ActivityStack stack = stacks.get(stackNdx);
3280 stack.clearOtherAppTimeTrackers(except);
3281 }
3282 }
3283 }
3284
Craig Mautner8d341ef2013-03-26 09:03:27 -07003285 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003286 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3287 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003288 final int numStacks = stacks.size();
3289 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3290 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003291 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003292 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 }
3294 }
3295
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003296 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3297 // Examine all activities currently running in the process.
3298 TaskRecord firstTask = null;
3299 // Tasks is non-null only if two or more tasks are found.
3300 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003301 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3302 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003303 ActivityRecord r = app.activities.get(i);
3304 // First, if we find an activity that is in the process of being destroyed,
3305 // then we just aren't going to do anything for now; we want things to settle
3306 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003307 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003308 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003309 return;
3310 }
3311 // Don't consider any activies that are currently not in a state where they
3312 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003313 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3314 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003315 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003316 continue;
3317 }
3318 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003319 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003320 + " from " + r);
3321 if (firstTask == null) {
3322 firstTask = r.task;
3323 } else if (firstTask != r.task) {
3324 if (tasks == null) {
3325 tasks = new ArraySet<>();
3326 tasks.add(firstTask);
3327 }
3328 tasks.add(r.task);
3329 }
3330 }
3331 }
3332 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003333 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003334 return;
3335 }
3336 // If we have activities in multiple tasks that are in a position to be destroyed,
3337 // let's iterate through the tasks and release the oldest one.
3338 final int numDisplays = mActivityDisplays.size();
3339 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3340 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3341 // Step through all stacks starting from behind, to hit the oldest things first.
3342 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3343 final ActivityStack stack = stacks.get(stackNdx);
3344 // Try to release activities in this stack; if we manage to, we are done.
3345 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3346 return;
3347 }
3348 }
3349 }
3350 }
3351
Amith Yamasani37a40c22015-06-17 13:25:42 -07003352 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003353 final int focusStackId = mFocusedStack.getStackId();
3354 // We dismiss the docked stack whenever we switch users.
3355 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003356 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3357 // also cause all tasks to be moved to the fullscreen stack at a position that is
3358 // appropriate.
3359 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003360
3361 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003362 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003363 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003364
Craig Mautner858d8a62013-04-23 17:08:34 -07003365 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003366 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3367 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003369 final ActivityStack stack = stacks.get(stackNdx);
3370 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003371 TaskRecord task = stack.topTask();
3372 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003373 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003374 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003376 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003377
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003378 ActivityStack stack = getStack(restoreStackId);
3379 if (stack == null) {
3380 stack = mHomeStack;
3381 }
3382 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003383 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003384 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003385 } else {
3386 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003387 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003388 }
Craig Mautner93529a42013-10-04 15:03:13 -07003389 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003390 }
3391
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003392 /** Checks whether the userid is a profile of the current user. */
3393 boolean isCurrentProfileLocked(int userId) {
3394 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003395 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003396 }
3397
Winson Chung4dabf232017-01-25 13:25:22 -08003398 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3399 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003400 ArrayList<ActivityRecord> stops = null;
3401
3402 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003403 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3404 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003405 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3406 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003407 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003408 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003409 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3410 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003411 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003412 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003413 if (s.finishing) {
3414 // If this activity is finishing, it is sitting on top of
3415 // everyone else but we now know it is no longer needed...
3416 // so get rid of it. Otherwise, we need to go through the
3417 // normal flow and hide it once we determine that it is
3418 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003419 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003420 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003421 }
3422 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003423 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003424 if (!processPausingActivities && s.state == PAUSING) {
3425 // Defer processing pausing activities in this iteration and reschedule
3426 // a delayed idle to reprocess it again
3427 removeTimeoutsForActivityLocked(idleActivity);
3428 scheduleIdleTimeoutLocked(idleActivity);
3429 continue;
3430 }
3431
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003432 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003433 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003434 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003435 }
3436 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003437 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003438 }
3439 }
3440
3441 return stops;
3442 }
3443
Craig Mautnercf910b02013-04-23 11:23:27 -07003444 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003445 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3446 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3447 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3448 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003449 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003450 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003451 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003452 if (r == null) Slog.e(TAG,
3453 "validateTop...: null top activity, stack=" + stack);
3454 else {
3455 final ActivityRecord pausing = stack.mPausingActivity;
3456 if (pausing != null && pausing == r) Slog.e(TAG,
3457 "validateTop...: top stack has pausing activity r=" + r
3458 + " state=" + state);
3459 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3460 "validateTop...: activity in front not resumed r=" + r
3461 + " state=" + state);
3462 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003463 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003464 final ActivityRecord resumed = stack.mResumedActivity;
3465 if (resumed != null && resumed == r) Slog.e(TAG,
3466 "validateTop...: back stack has resumed activity r=" + r
3467 + " state=" + state);
3468 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3469 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003470 }
3471 }
3472 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003473 }
3474
Craig Mautnere0570202015-05-13 13:06:11 -07003475 private String lockTaskModeToString() {
3476 switch (mLockTaskModeState) {
3477 case LOCK_TASK_MODE_LOCKED:
3478 return "LOCKED";
3479 case LOCK_TASK_MODE_PINNED:
3480 return "PINNED";
3481 case LOCK_TASK_MODE_NONE:
3482 return "NONE";
3483 default: return "unknown=" + mLockTaskModeState;
3484 }
3485 }
3486
Craig Mautner27084302013-03-25 08:05:25 -07003487 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003488 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003490 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003491 pw.print(prefix);
3492 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003493 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003494 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003495 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003496 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3497 if (packages.size() > 0) {
3498 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3499 for (int i = 0; i < packages.size(); ++i) {
3500 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3501 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3502 }
3503 }
3504 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3505 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003506 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003507
Winson43d1f262016-06-14 16:05:55 -07003508 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003509 * Dump all connected displays' configurations.
3510 * @param prefix Prefix to apply to each line of the dump.
3511 */
3512 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3513 pw.print(prefix); pw.println("Display override configurations:");
3514 final int displayCount = mActivityDisplays.size();
3515 for (int i = 0; i < displayCount; i++) {
3516 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3517 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3518 pw.println(activityDisplay.getOverrideConfiguration());
3519 }
3520 }
3521
3522 /**
Winson43d1f262016-06-14 16:05:55 -07003523 * Dumps the activities matching the given {@param name} in the either the focused stack
3524 * or all visible stacks if {@param dumpVisibleStacks} is true.
3525 */
3526 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3527 if (dumpVisibleStacks) {
3528 ArrayList<ActivityRecord> activities = new ArrayList<>();
3529 int numDisplays = mActivityDisplays.size();
3530 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3531 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3532 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3533 ActivityStack stack = stacks.get(stackNdx);
3534 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3535 activities.addAll(stack.getDumpActivitiesLocked(name));
3536 }
3537 }
3538 }
3539 return activities;
3540 } else {
3541 return mFocusedStack.getDumpActivitiesLocked(name);
3542 }
Craig Mautner20e72272013-04-01 13:45:53 -07003543 }
3544
Dianne Hackborn390517b2013-05-30 15:03:32 -07003545 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3546 boolean needSep, String prefix) {
3547 if (activity != null) {
3548 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3549 if (needSep) {
3550 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003551 }
3552 pw.print(prefix);
3553 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003554 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003555 }
3556 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003557 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003558 }
3559
Craig Mautner8d341ef2013-03-26 09:03:27 -07003560 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3561 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003562 boolean printed = false;
3563 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003564 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3565 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003566 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003567 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003568 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 final ActivityStack stack = stacks.get(stackNdx);
3571 StringBuilder stackHeader = new StringBuilder(128);
3572 stackHeader.append(" Stack #");
3573 stackHeader.append(stack.mStackId);
3574 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003575 stackHeader.append("\n");
3576 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3577 stackHeader.append("\n");
3578 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3580 needSep, stackHeader.toString());
3581 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3582 !dumpAll, false, dumpPackage, true,
3583 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003584
Craig Mautner4a1cb222013-12-04 16:14:06 -08003585 needSep = printed;
3586 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3587 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003588 if (pr) {
3589 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003591 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003592 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3593 " mResumedActivity: ");
3594 if (pr) {
3595 printed = true;
3596 needSep = false;
3597 }
3598 if (dumpAll) {
3599 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3600 " mLastPausedActivity: ");
3601 if (pr) {
3602 printed = true;
3603 needSep = true;
3604 }
3605 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3606 needSep, " mLastNoHistoryActivity: ");
3607 }
3608 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003609 }
3610 }
3611
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003612 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3613 false, dumpPackage, true, " Activities waiting to finish:", null);
3614 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3615 false, dumpPackage, true, " Activities waiting to stop:", null);
3616 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3617 false, dumpPackage, true, " Activities waiting for another to become visible:",
3618 null);
3619 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3620 false, dumpPackage, true, " Activities waiting to sleep:", null);
3621 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3622 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003623
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003624 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003625 }
3626
Dianne Hackborn390517b2013-05-30 15:03:32 -07003627 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003628 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003629 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003630 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003631 String innerPrefix = null;
3632 String[] args = null;
3633 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003634 for (int i=list.size()-1; i>=0; i--) {
3635 final ActivityRecord r = list.get(i);
3636 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3637 continue;
3638 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003639 if (innerPrefix == null) {
3640 innerPrefix = prefix + " ";
3641 args = new String[0];
3642 }
3643 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003644 final boolean full = !brief && (complete || !r.isInHistory());
3645 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003646 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003647 needNL = false;
3648 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003649 if (header1 != null) {
3650 pw.println(header1);
3651 header1 = null;
3652 }
3653 if (header2 != null) {
3654 pw.println(header2);
3655 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003656 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003657 if (lastTask != r.task) {
3658 lastTask = r.task;
3659 pw.print(prefix);
3660 pw.print(full ? "* " : " ");
3661 pw.println(lastTask);
3662 if (full) {
3663 lastTask.dump(pw, prefix + " ");
3664 } else if (complete) {
3665 // Complete + brief == give a summary. Isn't that obvious?!?
3666 if (lastTask.intent != null) {
3667 pw.print(prefix); pw.print(" ");
3668 pw.println(lastTask.intent.toInsecureStringWithClip());
3669 }
3670 }
3671 }
3672 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3673 pw.print(" #"); pw.print(i); pw.print(": ");
3674 pw.println(r);
3675 if (full) {
3676 r.dump(pw, innerPrefix);
3677 } else if (complete) {
3678 // Complete + brief == give a summary. Isn't that obvious?!?
3679 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3680 if (r.app != null) {
3681 pw.print(innerPrefix); pw.println(r.app);
3682 }
3683 }
3684 if (client && r.app != null && r.app.thread != null) {
3685 // flush anything that is already in the PrintWriter since the thread is going
3686 // to write to the file descriptor directly
3687 pw.flush();
3688 try {
3689 TransferPipe tp = new TransferPipe();
3690 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003691 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003692 // Short timeout, since blocking here can
3693 // deadlock with the application.
3694 tp.go(fd, 2000);
3695 } finally {
3696 tp.kill();
3697 }
3698 } catch (IOException e) {
3699 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3700 } catch (RemoteException e) {
3701 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3702 }
3703 needNL = true;
3704 }
3705 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003706 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003707 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003708
Craig Mautnerf3333272013-04-22 10:55:53 -07003709 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003710 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3711 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003712 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3713 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003714 }
3715
3716 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003717 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003718 }
3719
3720 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003721 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3722 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003723 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3724 }
3725
Craig Mautner05d29032013-05-03 13:40:13 -07003726 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003727 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3728 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3729 }
Craig Mautner05d29032013-05-03 13:40:13 -07003730 }
3731
Craig Mautner0eea92c2013-05-16 13:35:39 -07003732 void removeSleepTimeouts() {
3733 mSleepTimeout = false;
3734 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3735 }
3736
3737 final void scheduleSleepTimeout() {
3738 removeSleepTimeouts();
3739 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3740 }
3741
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 @Override
3743 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003744 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3746 }
3747
3748 @Override
3749 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003750 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3752 }
3753
3754 @Override
3755 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003756 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003757 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3758 }
3759
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003760 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003761 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003763 newDisplay = mActivityDisplays.get(displayId) == null;
3764 if (newDisplay) {
3765 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003766 if (activityDisplay.mDisplay == null) {
3767 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3768 return;
3769 }
Craig Mautner4504de52013-12-20 09:06:56 -08003770 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003771 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003772 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 }
Craig Mautner4504de52013-12-20 09:06:56 -08003774 if (newDisplay) {
3775 mWindowManager.onDisplayAdded(displayId);
3776 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 }
3778
Andrii Kulianca007a62016-11-22 16:33:28 -08003779 /** Check if display with specified id is added to the list. */
3780 boolean isDisplayAdded(int displayId) {
3781 return mActivityDisplays.get(displayId) != null;
3782 }
3783
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003784 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003785 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003786 mService.mContext.getResources().getDimensionPixelSize(
3787 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003788 }
3789
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003790 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003791 if (displayId == DEFAULT_DISPLAY) {
3792 throw new IllegalArgumentException("Can't remove the primary display.");
3793 }
3794
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003796 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3797 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003798 final boolean destroyContentOnRemoval
3799 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003800 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3801 while (!stacks.isEmpty()) {
3802 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003803 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003804 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3805 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003806 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003807 } else {
3808 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3809 // a valid launch stack for all activities. This way the task history from
3810 // external display will be preserved on primary after move.
3811 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003812 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003814 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 }
3816 }
3817 mWindowManager.onDisplayRemoved(displayId);
3818 }
3819
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003820 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003822 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3823 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824 // TODO: Update the bounds.
3825 }
3826 }
3827 mWindowManager.onDisplayChanged(displayId);
3828 }
3829
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003830 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003831 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003833 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003834 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003836 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003837 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003838 info.position = display != null
3839 ? display.mStacks.indexOf(stack)
3840 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841
3842 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3843 final int numTasks = tasks.size();
3844 int[] taskIds = new int[numTasks];
3845 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003846 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003847 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 for (int i = 0; i < numTasks; ++i) {
3849 final TaskRecord task = tasks.get(i);
3850 taskIds[i] = task.taskId;
3851 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3852 : task.realActivity != null ? task.realActivity.flattenToString()
3853 : task.getTopActivity() != null ? task.getTopActivity().packageName
3854 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003855 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003856 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003857 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 }
3859 info.taskIds = taskIds;
3860 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003861 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003862 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003863
3864 final ActivityRecord top = stack.topRunningActivityLocked();
3865 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 return info;
3867 }
3868
3869 StackInfo getStackInfoLocked(int stackId) {
3870 ActivityStack stack = getStack(stackId);
3871 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003872 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003873 }
3874 return null;
3875 }
3876
3877 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003878 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003879 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3880 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003881 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003882 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003883 }
3884 }
3885 return list;
3886 }
3887
Craig Mautner15df08a2015-04-01 12:17:18 -07003888 TaskRecord getLockedTaskLocked() {
3889 final int top = mLockTaskModeTasks.size() - 1;
3890 if (top >= 0) {
3891 return mLockTaskModeTasks.get(top);
3892 }
3893 return null;
3894 }
3895
3896 boolean isLockedTask(TaskRecord task) {
3897 return mLockTaskModeTasks.contains(task);
3898 }
3899
3900 boolean isLastLockedTask(TaskRecord task) {
3901 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3902 }
3903
3904 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003905 if (!mLockTaskModeTasks.remove(task)) {
3906 return;
3907 }
3908 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3909 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003910 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003911 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3912 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003913 final Message lockTaskMsg = Message.obtain();
3914 lockTaskMsg.arg1 = task.userId;
3915 lockTaskMsg.what = LOCK_TASK_END_MSG;
3916 mHandler.sendMessage(lockTaskMsg);
3917 }
3918 }
3919
Andrii Kulianc27916642016-04-12 17:59:27 -07003920 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3921 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3922 false /* forceNonResizable */);
3923 }
3924
Jorim Jaggid53f0922016-04-06 22:16:23 -07003925 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003926 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003927 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3928 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003929 return;
3930 }
3931
Jorim Jaggicd13d332016-04-27 15:40:20 -07003932 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003933 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003934 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003935 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003936
Andrii Kulianc27916642016-04-12 17:59:27 -07003937 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3938 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003939 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003940 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003941 && !topActivity.noDisplay) {
3942 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003943 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3944 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003945 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003946 }
3947
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003948 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003949 if (mLockTaskNotify != null) {
3950 mLockTaskNotify.showToast(mLockTaskModeState);
3951 }
Jason Monka8f569c2014-07-07 12:15:21 -04003952 }
3953
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003954 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3955 if (mLockTaskModeTasks.contains(task)) {
3956 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3957 }
3958 }
3959
Craig Mautner432f64e2015-05-20 14:59:57 -07003960 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3961 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003962 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003963 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003964 final TaskRecord lockedTask = getLockedTaskLocked();
3965 if (lockedTask != null) {
3966 removeLockedTaskLocked(lockedTask);
3967 if (!mLockTaskModeTasks.isEmpty()) {
3968 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003969 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3970 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003971 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003972 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003973 return;
3974 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003975 }
Craig Mautnere0570202015-05-13 13:06:11 -07003976 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3977 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003978 return;
3979 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003980
3981 // Should have already been checked, but do it again.
3982 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003983 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3984 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003985 return;
3986 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003987 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003988 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003989 return;
3990 }
3991
3992 if (mLockTaskModeTasks.isEmpty()) {
3993 // First locktask.
3994 final Message lockTaskMsg = Message.obtain();
3995 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3996 lockTaskMsg.arg1 = task.userId;
3997 lockTaskMsg.what = LOCK_TASK_START_MSG;
3998 lockTaskMsg.arg2 = lockTaskModeState;
3999 mHandler.sendMessage(lockTaskMsg);
4000 }
4001 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004002 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4003 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004004 mLockTaskModeTasks.remove(task);
4005 mLockTaskModeTasks.add(task);
4006
4007 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004008 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004009 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004010
4011 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004012 findTaskToMoveToFrontLocked(task, 0, null, reason,
4013 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004014 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004015 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004016 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004017 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004018 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004019 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004020 }
4021
4022 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004023 return isLockTaskModeViolation(task, false);
4024 }
4025
4026 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4027 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004028 return false;
4029 }
4030 final int lockTaskAuth = task.mLockTaskAuth;
4031 switch (lockTaskAuth) {
4032 case LOCK_TASK_AUTH_DONT_LOCK:
4033 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004034 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004035 case LOCK_TASK_AUTH_LAUNCHABLE:
4036 case LOCK_TASK_AUTH_WHITELISTED:
4037 return false;
4038 case LOCK_TASK_AUTH_PINNABLE:
4039 // Pinnable tasks can't be launched on top of locktask tasks.
4040 return !mLockTaskModeTasks.isEmpty();
4041 default:
4042 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4043 return true;
4044 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004045 }
4046
Craig Mautner15df08a2015-04-01 12:17:18 -07004047 void onLockTaskPackagesUpdatedLocked() {
4048 boolean didSomething = false;
4049 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4050 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004051 final boolean wasWhitelisted =
4052 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4053 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004054 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004055 final boolean isWhitelisted =
4056 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4057 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4058 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004059 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004060 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4061 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004062 removeLockedTaskLocked(lockedTask);
4063 lockedTask.performClearTaskLocked();
4064 didSomething = true;
4065 }
4066 }
4067 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4068 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4069 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4070 final ActivityStack stack = stacks.get(stackNdx);
4071 stack.onLockTaskPackagesUpdatedLocked();
4072 }
4073 }
Craig Mautnere0570202015-05-13 13:06:11 -07004074 final ActivityRecord r = topRunningActivityLocked();
4075 final TaskRecord task = r != null ? r.task : null;
4076 if (mLockTaskModeTasks.isEmpty() && task != null
4077 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4078 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004079 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4080 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4081 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4082 false);
4083 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004084 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004085 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004086 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004087 }
4088 }
4089
Benjamin Franz43261142015-02-11 15:59:44 +00004090 int getLockTaskModeState() {
4091 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004092 }
4093
Jorim Jaggife89d122015-12-22 16:28:44 +01004094 void activityRelaunchedLocked(IBinder token) {
4095 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004096 if (mService.isSleepingOrShuttingDownLocked()) {
4097 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4098 if (r != null) {
4099 r.setSleeping(true, true);
4100 }
4101 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004102 }
4103
4104 void activityRelaunchingLocked(ActivityRecord r) {
4105 mWindowManager.notifyAppRelaunching(r.appToken);
4106 }
4107
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004108 void logStackState() {
4109 mActivityMetricsLogger.logWindowState();
4110 }
4111
Andrii Kulian933076d2016-03-29 17:04:42 -07004112 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004113 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4114 final ActivityRecord r = task.mActivities.get(i);
4115 if (r.app != null && r.app.thread != null) {
4116 mMultiWindowModeChangedActivities.add(r);
4117 }
4118 }
4119
4120 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4121 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4122 }
4123 }
4124
Andrii Kulian933076d2016-03-29 17:04:42 -07004125 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004126 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004127 if (prevStack == null || prevStack == stack
4128 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4129 return;
4130 }
4131
4132 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4133 final ActivityRecord r = task.mActivities.get(i);
4134 if (r.app != null && r.app.thread != null) {
4135 mPipModeChangedActivities.add(r);
4136 }
4137 }
4138
4139 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4140 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4141 }
4142 }
4143
Tony Mak853304c2016-04-18 15:17:41 +01004144 void setDockedStackMinimized(boolean minimized) {
4145 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004146 }
4147
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004148 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004149
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004150 public ActivityStackSupervisorHandler(Looper looper) {
4151 super(looper);
4152 }
4153
Winson Chung4dabf232017-01-25 13:25:22 -08004154 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004155 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004156 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4157 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004158 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004159 }
4160
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004161 @Override
4162 public void handleMessage(Message msg) {
4163 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004164 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4165 synchronized (mService) {
4166 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4167 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004168 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004169 }
4170 }
4171 } break;
4172 case REPORT_PIP_MODE_CHANGED_MSG: {
4173 synchronized (mService) {
4174 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4175 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004176 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004177 }
4178 }
4179 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004180 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004181 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4182 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004183 if (mService.mDidDexOpt) {
4184 mService.mDidDexOpt = false;
4185 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4186 nmsg.obj = msg.obj;
4187 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4188 return;
4189 }
4190 // We don't at this point know if the activity is fullscreen,
4191 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004192 activityIdleInternal((ActivityRecord) msg.obj,
4193 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004194 } break;
4195 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004196 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004197 activityIdleInternal((ActivityRecord) msg.obj,
4198 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004199 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004200 case RESUME_TOP_ACTIVITY_MSG: {
4201 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004202 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004203 }
4204 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004205 case SLEEP_TIMEOUT_MSG: {
4206 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004207 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004208 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4209 mSleepTimeout = true;
4210 checkReadyForSleepLocked();
4211 }
4212 }
4213 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004214 case LAUNCH_TIMEOUT_MSG: {
4215 if (mService.mDidDexOpt) {
4216 mService.mDidDexOpt = false;
4217 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4218 return;
4219 }
4220 synchronized (mService) {
4221 if (mLaunchingActivity.isHeld()) {
4222 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4223 if (VALIDATE_WAKE_LOCK_CALLER
4224 && Binder.getCallingUid() != Process.myUid()) {
4225 throw new IllegalStateException("Calling must be system uid");
4226 }
4227 mLaunchingActivity.release();
4228 }
4229 }
4230 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004231 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004232 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004233 } break;
4234 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004235 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004236 } break;
4237 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004238 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004240 case CONTAINER_CALLBACK_VISIBILITY: {
4241 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004242 final IActivityContainerCallback callback = container.mCallback;
4243 if (callback != null) {
4244 try {
4245 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4246 } catch (RemoteException e) {
4247 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004248 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004249 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004250 case LOCK_TASK_START_MSG: {
4251 // When lock task starts, we disable the status bars.
4252 try {
Jason Monk62515be2014-05-21 16:06:19 -04004253 if (mLockTaskNotify == null) {
4254 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004255 }
Jason Monk62515be2014-05-21 16:06:19 -04004256 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004257 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004258 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004259 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004261 flags = StatusBarManager.DISABLE_MASK
4262 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004263 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004264 flags = StatusBarManager.DISABLE_MASK
4265 & (~StatusBarManager.DISABLE_BACK)
4266 & (~StatusBarManager.DISABLE_HOME)
4267 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004268 }
4269 getStatusBarService().disable(flags, mToken,
4270 mService.mContext.getPackageName());
4271 }
4272 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004273 if (getDevicePolicyManager() != null) {
4274 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004275 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004276 }
justinzhang5286d3f2014-05-12 17:06:01 -04004277 } catch (RemoteException ex) {
4278 throw new RuntimeException(ex);
4279 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004280 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004281 case LOCK_TASK_END_MSG: {
4282 // When lock task ends, we enable the status bars.
4283 try {
Jason Monk62515be2014-05-21 16:06:19 -04004284 if (getStatusBarService() != null) {
4285 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4286 mService.mContext.getPackageName());
4287 }
4288 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004289 if (getDevicePolicyManager() != null) {
4290 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4291 msg.arg1);
4292 }
Jason Monk62515be2014-05-21 16:06:19 -04004293 if (mLockTaskNotify == null) {
4294 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4295 }
4296 mLockTaskNotify.show(false);
4297 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004298 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004299 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004300 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004301 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004302 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004303 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004304 new LockPatternUtils(mService.mContext)
4305 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004306 }
4307 } catch (SettingNotFoundException e) {
4308 // No setting, don't lock.
4309 }
justinzhang5286d3f2014-05-12 17:06:01 -04004310 } catch (RemoteException ex) {
4311 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004312 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004313 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004314 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004315 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004316 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4317 if (mLockTaskNotify == null) {
4318 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4319 }
4320 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4321 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004322 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4323 final ActivityContainer container = (ActivityContainer) msg.obj;
4324 final IActivityContainerCallback callback = container.mCallback;
4325 if (callback != null) {
4326 try {
4327 callback.onAllActivitiesComplete(container.asBinder());
4328 } catch (RemoteException e) {
4329 }
4330 }
4331 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004332 case LAUNCH_TASK_BEHIND_COMPLETE: {
4333 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004334 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004335 if (r != null) {
4336 handleLaunchTaskBehindCompleteLocked(r);
4337 }
4338 }
4339 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004340
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004341 }
4342 }
4343 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004344
Ying Wangb081a592014-04-22 15:20:16 -07004345 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004346 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4347 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004349 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004350 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004351 ActivityRecord mParentActivity = null;
4352 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004353
Craig Mautnere3a00d72014-04-16 08:31:19 -07004354 boolean mVisible = true;
4355
Craig Mautner4a1cb222013-12-04 16:14:06 -08004356 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004357 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004358
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004359 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4360 final static int CONTAINER_STATE_NO_SURFACE = 1;
4361 final static int CONTAINER_STATE_FINISHING = 2;
4362 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4363
Wale Ogunwale1666e312016-12-16 11:27:18 -08004364 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004365 synchronized (mService) {
4366 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004367 mActivityDisplay = activityDisplay;
4368 new ActivityStack(this, mRecentTasks, onTop);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004369 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004370 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004372 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004373
Andrii Kulian839def92016-11-02 10:58:58 -07004374 /**
4375 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004376 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004377 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004378 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004379 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004380 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004381 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004382 if (mActivityDisplay != null) {
4383 throw new IllegalStateException("ActivityContainer is already attached, " +
4384 "displayId=" + mActivityDisplay.mDisplayId);
4385 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004386 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004387 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004388 }
4389
4390 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004391 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004392 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004393 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4394 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004395 return;
4396 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004397 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004398 }
4399 }
4400
4401 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004402 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004403 synchronized (mService) {
4404 if (mActivityDisplay != null) {
4405 return mActivityDisplay.mDisplayId;
4406 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004407 }
4408 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409 }
4410
Jeff Brownca9bc702014-02-11 14:32:56 -08004411 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004412 public int getStackId() {
4413 synchronized (mService) {
4414 return mStackId;
4415 }
4416 }
4417
4418 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004419 public boolean injectEvent(InputEvent event) {
4420 final long origId = Binder.clearCallingIdentity();
4421 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004422 synchronized (mService) {
4423 if (mActivityDisplay != null) {
4424 return mInputManagerInternal.injectInputEvent(event,
4425 mActivityDisplay.mDisplayId,
4426 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4427 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004428 }
4429 return false;
4430 } finally {
4431 Binder.restoreCallingIdentity(origId);
4432 }
4433 }
4434
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004435 @Override
4436 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004437 synchronized (mService) {
4438 if (mContainerState == CONTAINER_STATE_FINISHING) {
4439 return;
4440 }
4441 mContainerState = CONTAINER_STATE_FINISHING;
4442
Craig Mautnerd163e752014-06-13 17:18:47 -07004443 long origId = Binder.clearCallingIdentity();
4444 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004445 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004446 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004447 } finally {
4448 Binder.restoreCallingIdentity(origId);
4449 }
4450 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004451 }
4452
Andrii Kulian03c403d2016-11-11 11:14:12 -08004453 /**
4454 * Remove the stack completely. Must be called only when there are no tasks left in it,
4455 * as this method does not finish running activities.
4456 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004457 void removeLocked() {
4458 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004459 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004460 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004461 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004462 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004463 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 }
4465
Andrii Kulian839def92016-11-02 10:58:58 -07004466 /**
4467 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4468 * completely or re-parented.
4469 */
4470 private void removeFromDisplayLocked() {
4471 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4472 + " current displayId=" + mActivityDisplay.mDisplayId);
4473
Wale Ogunwale1666e312016-12-16 11:27:18 -08004474 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004475 mActivityDisplay = null;
4476 }
4477
4478 /**
4479 * Move the stack to specified display.
4480 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004481 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004482 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004483 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004484 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4485 + mActivityDisplay + " to display=" + activityDisplay
4486 + " Callers=" + Debug.getCallers(2));
4487
4488 removeFromDisplayLocked();
4489
4490 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004491 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004492 }
4493
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004495 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004496 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004497 }
4498
4499 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004500 public final int startActivityIntentSender(IIntentSender intentSender)
4501 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004502 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4503
4504 if (!(intentSender instanceof PendingIntentRecord)) {
4505 throw new IllegalArgumentException("Bad PendingIntent object");
4506 }
4507
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004508 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004509 Binder.getCallingUid(), mCurrentUser, false,
4510 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004511
4512 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4513 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4514 pendingIntent.key.requestResolvedType);
4515
4516 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4517 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4518 }
4519
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004520 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004521 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004522 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004523 throw new SecurityException(
4524 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4525 }
4526 }
4527
Craig Mautnerdf88d732014-01-27 09:21:32 -08004528 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004529 public IBinder asBinder() {
4530 return this;
4531 }
4532
Craig Mautner4504de52013-12-20 09:06:56 -08004533 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004534 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004535 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004536 }
4537
Craig Mautner4a1cb222013-12-04 16:14:06 -08004538 ActivityStackSupervisor getOuter() {
4539 return ActivityStackSupervisor.this;
4540 }
4541
Craig Mautnerd163e752014-06-13 17:18:47 -07004542 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004543 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004544 }
4545
Craig Mautner6985bad2014-04-21 15:22:06 -07004546 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004547 void setVisible(boolean visible) {
4548 if (mVisible != visible) {
4549 mVisible = visible;
4550 if (mCallback != null) {
4551 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4552 0 /* unused */, this).sendToTarget();
4553 }
4554 }
4555 }
4556
Craig Mautner6985bad2014-04-21 15:22:06 -07004557 void setDrawn() {
4558 }
4559
Craig Mautner1b4bf852014-05-26 15:06:32 -07004560 // You can always start a new task on a regular ActivityStack.
4561 boolean isEligibleForNewTasks() {
4562 return true;
4563 }
4564
Craig Mautnerd163e752014-06-13 17:18:47 -07004565 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004566 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004567 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004568 }
4569
Craig Mautner34b73df2014-01-12 21:11:08 -08004570 @Override
4571 public String toString() {
4572 return mIdString + (mActivityDisplay == null ? "N" : "A");
4573 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004574 }
4575
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004576 private class VirtualActivityContainer extends ActivityContainer {
4577 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004578 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004579
4580 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004581 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4582 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004583 mParentActivity = parent;
4584 mCallback = callback;
4585 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004586 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004587 }
4588
4589 @Override
4590 public void setSurface(Surface surface, int width, int height, int density) {
4591 super.setSurface(surface, width, height, density);
4592
4593 synchronized (mService) {
4594 final long origId = Binder.clearCallingIdentity();
4595 try {
4596 setSurfaceLocked(surface, width, height, density);
4597 } finally {
4598 Binder.restoreCallingIdentity(origId);
4599 }
4600 }
4601 }
4602
4603 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4604 if (mContainerState == CONTAINER_STATE_FINISHING) {
4605 return;
4606 }
4607 VirtualActivityDisplay virtualActivityDisplay =
4608 (VirtualActivityDisplay) mActivityDisplay;
4609 if (virtualActivityDisplay == null) {
4610 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004611 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004612 mActivityDisplay = virtualActivityDisplay;
4613 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004614 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004615 }
4616
4617 if (mSurface != null) {
4618 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004619 }
4620
Craig Mautner6985bad2014-04-21 15:22:06 -07004621 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004622 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004623 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004624 } else {
4625 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004626 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004627 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004628 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004629 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004630 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004631
Craig Mautnerd163e752014-06-13 17:18:47 -07004632 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004633
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004634 if (DEBUG_STACK) Slog.d(TAG_STACK,
4635 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004636 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004637
Craig Mautner6985bad2014-04-21 15:22:06 -07004638 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004639 boolean isAttachedLocked() {
4640 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004641 }
4642
4643 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004644 void setDrawn() {
4645 synchronized (mService) {
4646 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004647 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004648 }
4649 }
4650
Craig Mautner1b4bf852014-05-26 15:06:32 -07004651 // Never start a new task on an ActivityView if it isn't explicitly specified.
4652 @Override
4653 boolean isEligibleForNewTasks() {
4654 return false;
4655 }
4656
Craig Mautnerd163e752014-06-13 17:18:47 -07004657 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004658 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004659 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4660 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4661 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4662 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4663 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004664 }
4665 }
4666
Craig Mautner4a1cb222013-12-04 16:14:06 -08004667 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4668 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004669 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004670 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004671 int mDisplayId;
4672 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004673
Craig Mautner4a1cb222013-12-04 16:14:06 -08004674 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4675 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004676 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004677
Jose Lima4b6c6692014-08-12 17:41:12 -07004678 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004679
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004680 /** Array of all UIDs that are present on the display. */
4681 private IntArray mDisplayAccessUIDs = new IntArray();
4682
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004683 ActivityDisplay() {
4684 }
Craig Mautner4504de52013-12-20 09:06:56 -08004685
Craig Mautner1a70a162014-09-13 12:09:31 -07004686 // After instantiation, check that mDisplay is not null before using this. The alternative
4687 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004688 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004689 final Display display = mDisplayManager.getDisplay(displayId);
4690 if (display == null) {
4691 return;
4692 }
4693 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004694 }
4695
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004696 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004697 mDisplay = display;
4698 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004699 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004700
Wale Ogunwale1666e312016-12-16 11:27:18 -08004701 void attachStack(ActivityStack stack, boolean onTop) {
4702 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
4703 + " to displayId=" + mDisplayId + " onTop=" + onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004704 if (onTop) {
4705 mStacks.add(stack);
4706 } else {
4707 mStacks.add(0, stack);
4708 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004709 }
4710
Wale Ogunwale1666e312016-12-16 11:27:18 -08004711 void detachStack(ActivityStack stack) {
4712 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004713 + " from displayId=" + mDisplayId);
4714 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004715 }
4716
Jose Lima4b6c6692014-08-12 17:41:12 -07004717 void setVisibleBehindActivity(ActivityRecord r) {
4718 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004719 }
4720
Jose Lima4b6c6692014-08-12 17:41:12 -07004721 boolean hasVisibleBehindActivity() {
4722 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004723 }
4724
Craig Mautner34b73df2014-01-12 21:11:08 -08004725 @Override
4726 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004727 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4728 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004729
4730 @Override
4731 protected int getChildCount() {
4732 return mStacks.size();
4733 }
4734
4735 @Override
4736 protected ConfigurationContainer getChildAt(int index) {
4737 return mStacks.get(index);
4738 }
4739
4740 @Override
4741 protected ConfigurationContainer getParent() {
4742 return ActivityStackSupervisor.this;
4743 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004744
4745 boolean isPrivate() {
4746 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4747 }
4748
4749 boolean isUidPresent(int uid) {
4750 for (ActivityStack stack : mStacks) {
4751 if (stack.isUidPresent(uid)) {
4752 return true;
4753 }
4754 }
4755 return false;
4756 }
4757
4758 /** Update and get all UIDs that are present on the display and have access to it. */
4759 private IntArray getPresentUIDs() {
4760 mDisplayAccessUIDs.clear();
4761 for (ActivityStack stack : mStacks) {
4762 stack.getPresentUIDs(mDisplayAccessUIDs);
4763 }
4764 return mDisplayAccessUIDs;
4765 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004766
4767 boolean shouldDestroyContentOnRemove() {
4768 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4769 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004770 }
4771
4772 class VirtualActivityDisplay extends ActivityDisplay {
4773 VirtualDisplay mVirtualDisplay;
4774
Craig Mautner6985bad2014-04-21 15:22:06 -07004775 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004776 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004777 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4778 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4779 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4780 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004781
4782 init(mVirtualDisplay.getDisplay());
4783
4784 mWindowManager.handleDisplayAdded(mDisplayId);
4785 }
4786
4787 void setSurface(Surface surface) {
4788 if (mVirtualDisplay != null) {
4789 mVirtualDisplay.setSurface(surface);
4790 }
4791 }
4792
4793 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004794 void detachStack(ActivityStack stack) {
4795 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004796 if (mVirtualDisplay != null) {
4797 mVirtualDisplay.release();
4798 mVirtualDisplay = null;
4799 }
4800 }
4801
4802 @Override
4803 public String toString() {
4804 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004805 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004806 }
Jose Lima58e66d62014-05-27 20:00:27 -07004807
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004808 ActivityStack findStackBehind(ActivityStack stack) {
4809 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004810 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004811 if (display == null) {
4812 return null;
4813 }
4814 final ArrayList<ActivityStack> stacks = display.mStacks;
4815 for (int i = stacks.size() - 1; i >= 0; i--) {
4816 if (stacks.get(i) == stack && i > 0) {
4817 return stacks.get(i - 1);
4818 }
4819 }
4820 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4821 + " in=" + stacks);
4822 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004823
Jorim Jaggic69bd222016-03-15 14:38:37 +01004824 /**
4825 * Puts a task into resizing mode during the next app transition.
4826 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004827 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004828 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004829 private void setResizingDuringAnimation(TaskRecord task) {
4830 mResizingTasksDuringAnimation.add(task.taskId);
4831 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004832 }
4833
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004834 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4835 final TaskRecord task;
4836 final int callingUid;
4837 final String callingPackage;
4838 final Intent intent;
4839 final int userId;
4840 final ActivityOptions activityOptions = (bOptions != null)
4841 ? new ActivityOptions(bOptions) : null;
4842 final int launchStackId = (activityOptions != null)
4843 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004844 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004845 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004846 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004847 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004848
4849 if (launchStackId == DOCKED_STACK_ID) {
4850 mWindowManager.setDockedStackCreateState(
4851 activityOptions.getDockCreateMode(), null /* initialBounds */);
4852
4853 // Defer updating the stack in which recents is until the app transition is done, to
4854 // not run into issues where we still need to draw the task in recents but the
4855 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004856 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004857 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4858 }
4859
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004860 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4861 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004862 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004863 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004864 throw new IllegalArgumentException(
4865 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4866 }
4867
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004868 // Since we don't have an actual source record here, we assume that the currently focused
4869 // activity was the source.
4870 final ActivityStack focusedStack = getFocusedStack();
4871 final ActivityRecord sourceRecord =
4872 focusedStack != null ? focusedStack.topActivity() : null;
4873
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004874 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004875 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004876 moveTaskToStackLocked(
4877 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4878 ANIMATE);
4879 }
4880 }
4881
4882 // If the user must confirm credentials (e.g. when first launching a work app and the
4883 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4884 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4885 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004886 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004887 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004888 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004889 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4890 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004891
4892 // If we are launching the task in the docked stack, put it into resizing mode so
4893 // the window renders full-screen with the background filling the void. Also only
4894 // call this at the end to make sure that tasks exists on the window manager side.
4895 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004896 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004897 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004898
4899 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4900 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004901 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4902 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004903 return ActivityManager.START_TASK_TO_FRONT;
4904 }
4905 callingUid = task.mCallingUid;
4906 callingPackage = task.mCallingPackage;
4907 intent = task.intent;
4908 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4909 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004910 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4911 null, null, 0, 0, bOptions, userId, null, task);
4912 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004913 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004914 }
4915 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004916 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004917
4918 /**
4919 * @return a list of activities which are the top ones in each visible stack. The first
4920 * entry will be the focused activity.
4921 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004922 List<IBinder> getTopVisibleActivities() {
4923 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4924 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004925 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004926 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4927 // Traverse all stacks on a display.
4928 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4929 final ActivityStack stack = display.mStacks.get(j);
4930 // Get top activity from a visible stack and add it to the list.
4931 if (stack.getStackVisibilityLocked(null /* starting */)
4932 == ActivityStack.STACK_VISIBLE) {
4933 final ActivityRecord top = stack.topActivity();
4934 if (top != null) {
4935 if (stack == mFocusedStack) {
4936 topActivityTokens.add(0, top.appToken);
4937 } else {
4938 topActivityTokens.add(top.appToken);
4939 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004940 }
4941 }
4942 }
4943 }
4944 return topActivityTokens;
4945 }
Craig Mautner27084302013-03-25 08:05:25 -07004946}