blob: 9b0600a39328ea799ac540f4ecd2e9eef7863ad3 [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;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800123import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124import android.hardware.display.DisplayManager;
125import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800126import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800127import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800128import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800129import android.hardware.input.InputManager;
130import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100132import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700133import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700134import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700136import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700137import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700139import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700140import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700141import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400142import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700143import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700144import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700145import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700146import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100147import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700148import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700149import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400150import android.provider.Settings;
151import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700152import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700153import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700154import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700155import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800156import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700157import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800158import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800159import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800160import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800161import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800162import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800163
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800164import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800165import com.android.internal.logging.MetricsLogger;
166import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700167import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400168import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700169import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400170import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800171import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700172import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700173import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700174
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175import java.io.FileDescriptor;
176import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700177import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700178import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700179import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700180import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700181import java.util.List;
Andrii Kulian0864bbb2017-02-16 15:45:58 -0800182import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700183import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700184
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800185public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800186 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700188 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700190 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800197 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800199
Craig Mautnerf3333272013-04-22 10:55:53 -0700200 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700202
Craig Mautner0eea92c2013-05-16 13:35:39 -0700203 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700205
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208
Craig Mautner05d29032013-05-03 13:40:13 -0700209 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
210 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
211 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700212 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
215 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
216 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700217 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400218 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
219 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700220 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700221 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700222 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800223 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
224 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700227
Jason Monk62515be2014-05-21 16:06:19 -0400228 private static final String LOCK_TASK_TAG = "Lock-to-App";
229
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230 // Used to indicate if an object (e.g. stack) that we are trying to get
231 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800232 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700234 // Used to indicate that windows of activities should be preserved during the resize.
235 static final boolean PRESERVE_WINDOWS = true;
236
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237 // Used to indicate if an object (e.g. task) should be moved/created
238 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700239 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240
241 // Used to indicate that an objects (e.g. task) removal from its container
242 // (e.g. stack) is due to it moving to another container.
243 static final boolean MOVING = true;
244
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700245 // Force the focus to change to the stack we are moving a task to..
246 static final boolean FORCE_FOCUS = true;
247
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700248 // Restore task from the saved recents if it can't be found in any live stack.
249 static final boolean RESTORE_FROM_RECENTS = true;
250
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700251 // Don't execute any calls to resume.
252 static final boolean DEFER_RESUME = true;
253
Winson Chung010927a2016-12-15 16:12:35 -0800254 // Used to indicate that a task is removed it should also be removed from recents.
255 static final boolean REMOVE_FROM_RECENTS = true;
256
Svetoslav7008b512015-06-24 18:47:07 -0700257 // Activity actions an app cannot start if it uses a permission which is not granted.
258 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
259 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700260
Svetoslav7008b512015-06-24 18:47:07 -0700261 static {
262 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
263 Manifest.permission.CAMERA);
264 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
265 Manifest.permission.CAMERA);
266 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
267 Manifest.permission.CALL_PHONE);
268 }
269
Svet Ganov99b60432015-06-27 13:15:22 -0700270 /** Action restriction: launching the activity is not restricted. */
271 private static final int ACTIVITY_RESTRICTION_NONE = 0;
272 /** Action restriction: launching the activity is restricted by a permission. */
273 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
274 /** Action restriction: launching the activity is restricted by an app op. */
275 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700276
justinzhang5286d3f2014-05-12 17:06:01 -0400277 /** Status Bar Service **/
278 private IBinder mToken = new Binder();
279 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400280 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400281
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700282 // For debugging to make sure the caller when acquiring/releasing our
283 // wake lock is the system process.
284 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800285 /** The number of distinct task ids that can be assigned to the tasks of a single user */
286 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700287
Craig Mautner27084302013-03-25 08:05:25 -0700288 final ActivityManagerService mService;
289
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800290 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800291
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700292 final ActivityStackSupervisorHandler mHandler;
293
294 /** Short cut */
295 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700297
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700298 /** Counter for next free stack ID to use for dynamic activity stacks. */
299 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800301 /**
302 * Maps the task identifier that activities are currently being started in to the userId of the
303 * task. Each time a new task is created, the entry for the userId of the task is incremented
304 */
305 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700306
Craig Mautner2420ead2013-04-01 17:13:20 -0700307 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700309
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 /** The stack containing the launcher app. Assumed to always be attached to
311 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800312 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700313
Craig Mautnere0a38842013-12-16 16:14:02 -0800314 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700316
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
318 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800319 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
322 /** List of activities that are waiting for a new activity to become visible before completing
323 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700327 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700328
329 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700330 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331
Craig Mautnerde4ef022013-04-07 19:01:33 -0700332 /** List of activities that are ready to be stopped, but waiting for the next activity to
333 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800334 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700335
Craig Mautnerf3333272013-04-22 10:55:53 -0700336 /** List of activities that are ready to be finished, but waiting for the previous activity to
337 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800338 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700339
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800341 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700342
Wale Ogunwale22e25262016-02-01 10:32:02 -0800343 /** List of activities whose multi-window mode changed that we need to report to the
344 * application */
345 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
346
347 /** List of activities whose picture-in-picture mode changed that we need to report to the
348 * application */
349 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
350
Craig Mautnerf3333272013-04-22 10:55:53 -0700351 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700352 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700353
Craig Mautnerde4ef022013-04-07 19:01:33 -0700354 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
355 * is being brought in front of us. */
356 boolean mUserLeaving = false;
357
Craig Mautner0eea92c2013-05-16 13:35:39 -0700358 /** Set when we have taken too long waiting to go to sleep. */
359 boolean mSleepTimeout = false;
360
361 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700362 * We don't want to allow the device to go to sleep while in the process
363 * of launching an activity. This is primarily to allow alarm intent
364 * receivers to launch an activity and get that to run before the device
365 * goes back to sleep.
366 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700367 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700368
369 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700370 * Set when the system is going to sleep, until we have
371 * successfully paused the current activity and released our wake lock.
372 * At that point the system is allowed to actually sleep.
373 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700374 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700375
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700376 /** Stack id of the front stack when user switched, indexed by userId. */
377 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700378
Craig Mautner4504de52013-12-20 09:06:56 -0800379 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800381 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382
383 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700384 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800386 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
387
388 private DisplayManagerInternal mDisplayManagerInternal;
389 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800390
Craig Mautner15df08a2015-04-01 12:17:18 -0700391 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
392 * may be finished until there is only one entry left. If this is empty the system is not
393 * in lockTask mode. */
394 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000395 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700396 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
397 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000398 */
399 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400400 /**
401 * Notifies the user when entering/exiting lock-task.
402 */
403 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800404
Wale Ogunwaled046a012015-12-24 13:05:59 -0800405 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800406 boolean inResumeTopActivity;
407
Andrii Kulian1e32e022016-09-16 15:29:34 -0700408 /**
409 * Temporary rect used during docked stack resize calculation so we don't need to create a new
410 * object each time.
411 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700412 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700413
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700414 // The default minimal size that will be used if the activity doesn't specify its minimal size.
415 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700416 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700417
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700418 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
419 private boolean mTaskLayersChanged = true;
420
Jorim Jaggi275561a2016-02-23 10:11:02 -0500421 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800422
Andrii Kulian1779e612016-10-12 21:58:25 -0700423 @Override
424 protected int getChildCount() {
425 return mActivityDisplays.size();
426 }
427
428 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700429 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700430 return mActivityDisplays.valueAt(index);
431 }
432
433 @Override
434 protected ConfigurationContainer getParent() {
435 return null;
436 }
437
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700438 Configuration getDisplayOverrideConfiguration(int displayId) {
439 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
440 if (activityDisplay == null) {
441 throw new IllegalArgumentException("No display found with id: " + displayId);
442 }
443
444 return activityDisplay.getOverrideConfiguration();
445 }
446
447 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
448 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
449 if (activityDisplay == null) {
450 throw new IllegalArgumentException("No display found with id: " + displayId);
451 }
452
453 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
454 }
455
Wale Ogunwale39381972015-12-17 17:15:29 -0800456 static class FindTaskResult {
457 ActivityRecord r;
458 boolean matchedByRootAffinity;
459 }
460 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
461
Craig Mautneree36c772014-07-16 14:56:05 -0700462 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800463 * Temp storage for display ids sorted in focus order.
464 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
465 * it's more efficient, as the number of displays is usually small.
466 */
467 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
468
469 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100470 * Used to keep track whether app visibilities got changed since the last pause. Useful to
471 * determine whether to invoke the task stack change listener after pausing.
472 */
473 boolean mAppVisibilitiesChangedSinceLastPause;
474
475 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100476 * Set of tasks that are in resizing mode during an app transition to fill the "void".
477 */
478 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
479
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700480
481 /**
482 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
483 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
484 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
485 * like the docked stack going empty.
486 */
487 private boolean mAllowDockedStackResize = true;
488
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100489 /**
Tony Mak853304c2016-04-18 15:17:41 +0100490 * Is dock currently minimized.
491 */
492 boolean mIsDockMinimized;
493
Jorim Jaggife762342016-10-13 14:33:27 +0200494 final KeyguardController mKeyguardController;
495
Tony Mak853304c2016-04-18 15:17:41 +0100496 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700497 * Description of a request to start a new activity, which has been held
498 * due to app switches being disabled.
499 */
500 static class PendingActivityLaunch {
501 final ActivityRecord r;
502 final ActivityRecord sourceRecord;
503 final int startFlags;
504 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700505 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700506
507 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700508 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700509 r = _r;
510 sourceRecord = _sourceRecord;
511 startFlags = _startFlags;
512 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700513 callerApp = _callerApp;
514 }
515
516 void sendErrorResult(String message) {
517 try {
518 if (callerApp.thread != null) {
519 callerApp.thread.scheduleCrash(message);
520 }
521 } catch (RemoteException e) {
522 Slog.e(TAG, "Exception scheduling crash of failed "
523 + "activity launcher sourceRecord=" + sourceRecord, e);
524 }
Craig Mautneree36c772014-07-16 14:56:05 -0700525 }
526 }
527
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800528 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700529 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700530 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800531 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200532 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700533 }
534
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800535 void setRecentTasks(RecentTasks recentTasks) {
536 mRecentTasks = recentTasks;
537 }
538
Jeff Brown2c43c332014-06-12 22:38:59 -0700539 /**
540 * At the time when the constructor runs, the power manager has not yet been
541 * initialized. So we initialize our wakelocks afterwards.
542 */
543 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700545 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700546 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700547 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700548 }
549
justinzhang5286d3f2014-05-12 17:06:01 -0400550 // This function returns a IStatusBarService. The value is from ServiceManager.
551 // getService and is cached.
552 private IStatusBarService getStatusBarService() {
553 synchronized (mService) {
554 if (mStatusBarService == null) {
555 mStatusBarService = IStatusBarService.Stub.asInterface(
556 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
557 if (mStatusBarService == null) {
558 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
559 }
560 }
561 return mStatusBarService;
562 }
563 }
564
Jason Monk35c62a42014-06-17 10:24:47 -0400565 private IDevicePolicyManager getDevicePolicyManager() {
566 synchronized (mService) {
567 if (mDevicePolicyManager == null) {
568 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
569 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
570 if (mDevicePolicyManager == null) {
571 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
572 }
573 }
574 return mDevicePolicyManager;
575 }
576 }
577
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700578 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800579 synchronized (mService) {
580 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200581 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582
583 mDisplayManager =
584 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
585 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800586 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587
588 Display[] displays = mDisplayManager.getDisplays();
589 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
590 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700592 if (activityDisplay.mDisplay == null) {
593 throw new IllegalStateException("Default Display does not exist");
594 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800595 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700596 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 }
598
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800599 mHomeStack = mFocusedStack = mLastFocusedStack =
600 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800601
602 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800603 }
Craig Mautner27084302013-03-25 08:05:25 -0700604 }
605
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700606 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
609
Craig Mautnerde4ef022013-04-07 19:01:33 -0700610 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700612 }
613
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700614 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700615 if (stack == null) {
616 return false;
617 }
618
Craig Mautnerdf88d732014-01-27 09:21:32 -0800619 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
620 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700621 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800622 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800623 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700624 }
625
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800626 /** The top most stack on its display. */
627 boolean isFrontStackOnDisplay(ActivityStack stack) {
628 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
629 }
630
631 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700632 if (stack == null) {
633 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700635
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700636 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
637 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700638 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800639 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800640 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700641 }
642
Wale Ogunwaled046a012015-12-24 13:05:59 -0800643 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800644 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
645 if (!focusCandidate.isFocusable()) {
646 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800647 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800648 }
649
650 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800651 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800652 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800653
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 EventLogTags.writeAmFocusedStack(
655 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
656 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
657 }
658
659 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800660 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800661 if (r != null && r.idle) {
662 checkFinishBootingLocked();
663 }
664 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700665 }
666
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700667 void moveHomeStackToFront(String reason) {
668 mHomeStack.moveToFront(reason);
669 }
670
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700671 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800672 boolean moveHomeStackTaskToTop(String reason) {
673 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700674
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700675 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700676 if (top == null) {
677 return false;
678 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700679 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700680 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700681 }
682
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800683 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700684 if (!mService.mBooting && !mService.mBooted) {
685 // Not ready yet!
686 return false;
687 }
688
Craig Mautner84984fa2014-06-19 11:19:20 -0700689 if (prev != null) {
690 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
691 }
692
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800693 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700694 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800695 final String myReason = reason + " resumeHomeStackTask";
696
Mark Lua56ea122015-10-08 13:31:01 +0800697 // Only resume home activity if isn't finishing.
698 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700699 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800700 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700701 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700703 }
704
Craig Mautner8d341ef2013-03-26 09:03:27 -0700705 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700706 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700707 }
708
709 /**
710 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
711 * @param id Id of the task we would like returned.
712 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
713 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700714 * @param stackId The stack to restore the task to (default launch stack will be used if
715 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700716 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700717 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800718 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800719 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
722 ActivityStack stack = stacks.get(stackNdx);
723 TaskRecord task = stack.taskForIdLocked(id);
724 if (task != null) {
725 return task;
726 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700727 }
728 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800729
730 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700731 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800732 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800733 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700734 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800735 return null;
736 }
737
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700738 if (!restoreFromRecents) {
739 return task;
740 }
741
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700742 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700743 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
744 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800745 return null;
746 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700747 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800748 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700749 }
750
Craig Mautner6170f732013-04-02 13:05:23 -0700751 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800752 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
756 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
757 if (r != null) {
758 return r;
759 }
Craig Mautner6170f732013-04-02 13:05:23 -0700760 }
761 }
762 return null;
763 }
764
Tony Mak853304c2016-04-18 15:17:41 +0100765 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000766 * Detects whether we should show a lock screen in front of this task for a locked user.
767 * <p>
768 * We'll do this if either of the following holds:
769 * <ul>
770 * <li>The top activity explicitly belongs to {@param userId}.</li>
771 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
772 * </ul>
773 *
774 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100775 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000776 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
777 // To handle the case that work app is in the task but just is not the top one.
778 final ActivityRecord activityRecord = task.getTopActivity();
779 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
780
781 return (activityRecord != null && activityRecord.userId == userId)
782 || (resultTo != null && resultTo.userId == userId);
783 }
784
785 /**
786 * Find all visible task stacks containing {@param userId} and intercept them with an activity
787 * to block out the contents and possibly start a credential-confirming intent.
788 *
789 * @param userId user handle for the locked managed profile.
790 */
791 void lockAllProfileTasks(@UserIdInt int userId) {
792 mWindowManager.deferSurfaceLayout();
793 try {
794 final List<ActivityStack> stacks = getStacks();
795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
796 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
797 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
798 final TaskRecord task = tasks.get(taskNdx);
799
800 // Check the task for a top activity belonging to userId, or returning a result
801 // to an activity belonging to userId. Example case: a document picker for
802 // personal files, opened by a work app, should still get locked.
803 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000804 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
805 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000806 }
Tony Mak853304c2016-04-18 15:17:41 +0100807 }
808 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000809 } finally {
810 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100811 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000812 }
813
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800814 void setNextTaskIdForUserLocked(int taskId, int userId) {
815 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
816 if (taskId > currentTaskId) {
817 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700818 }
819 }
820
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700821 static int nextTaskIdForUser(int taskId, int userId) {
822 int nextTaskId = taskId + 1;
823 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
824 // Wrap around as there will be smaller task ids that are available now.
825 nextTaskId -= MAX_TASK_IDS_PER_USER;
826 }
827 return nextTaskId;
828 }
829
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800830 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800831 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
832 // for a userId u, a taskId can only be in the range
833 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
834 // 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 -0700835 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800836 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
837 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
838 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700839 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800840 if (candidateTaskId == currentTaskId) {
841 // Something wrong!
842 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
843 throw new IllegalStateException("Cannot get an available task id."
844 + " Reached limit of " + MAX_TASK_IDS_PER_USER
845 + " running tasks per user.");
846 }
847 }
848 mCurTaskIdForUser.put(userId, candidateTaskId);
849 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700850 }
851
Chong Zhang6cda19c2016-06-14 19:07:56 -0700852 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800853 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700854 if (stack == null) {
855 return null;
856 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700857 ActivityRecord resumedActivity = stack.mResumedActivity;
858 if (resumedActivity == null || resumedActivity.app == null) {
859 resumedActivity = stack.mPausingActivity;
860 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700861 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700862 }
863 }
864 return resumedActivity;
865 }
866
Dianne Hackbornff072722014-09-24 10:56:28 -0700867 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700868 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
871 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
873 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700874 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 continue;
876 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700877 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 if (hr != null) {
879 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
880 && processName.equals(hr.processName)) {
881 try {
George Mount2c92c972014-03-20 09:38:23 -0700882 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800883 didSomething = true;
884 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700885 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 Slog.w(TAG, "Exception in new application when starting activity "
887 + hr.intent.getComponent().flattenToShortString(), e);
888 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700889 }
Craig Mautner20e72272013-04-01 13:45:53 -0700890 }
Craig Mautner20e72272013-04-01 13:45:53 -0700891 }
892 }
893 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700894 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700895 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700896 }
Craig Mautner20e72272013-04-01 13:45:53 -0700897 return didSomething;
898 }
899
900 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800901 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
902 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
904 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700905 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800906 continue;
907 }
908 final ActivityRecord resumedActivity = stack.mResumedActivity;
909 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700910 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800911 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 return false;
913 }
Craig Mautner20e72272013-04-01 13:45:53 -0700914 }
915 }
Wei Wang65c7a152016-06-02 18:51:22 -0700916 // Send launch end powerhint when idle
917 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700918 return true;
919 }
920
Craig Mautnerde4ef022013-04-07 19:01:33 -0700921 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
923 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
925 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700926 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700928 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 return false;
930 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700931 }
932 }
933 }
934 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700935 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
937 mLastFocusedStack + " to=" + mFocusedStack);
938 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700939 return true;
940 }
941
942 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800943 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
945 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
947 final ActivityStack stack = stacks.get(stackNdx);
948 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800949 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700950 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800951 return false;
952 }
953 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800954 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700955 }
956 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800957 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 }
959
Craig Mautner2acc3892013-09-23 10:28:14 -0700960 /**
961 * Pause all activities in either all of the stacks or just the back stacks.
962 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700963 * @param resuming The resuming activity.
964 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
965 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700966 * @return true if any activity was paused as a result of this call.
967 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700968 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700969 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
971 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
973 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700974 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700975 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700977 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
978 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700980 }
981 }
982 return someActivityPaused;
983 }
984
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700986 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800987 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
988 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800989 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
990 final ActivityStack stack = stacks.get(stackNdx);
991 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700992 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700994 Slog.d(TAG_STATES,
995 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 pausing = false;
997 } else {
998 return false;
999 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001000 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 }
1002 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001003 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 }
1005
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001006 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001007 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001008 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001009 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1010 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1011 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1012 final ActivityStack stack = stacks.get(stackNdx);
1013 if (stack.mResumedActivity != null &&
1014 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001015 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001016 }
1017 }
1018 }
1019 }
1020
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001021 void cancelInitializingActivities() {
1022 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1023 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1024 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1025 stacks.get(stackNdx).cancelInitializingActivities();
1026 }
1027 }
1028 }
1029
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001030 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001031 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001032 }
1033
1034 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1035 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001036 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001038 if (w.who == null) {
1039 changed = true;
1040 w.timeout = false;
1041 if (r != null) {
1042 w.who = new ComponentName(r.info.packageName, r.info.name);
1043 }
1044 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1045 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001046 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001047 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001048 if (changed) {
1049 mService.notifyAll();
1050 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001051 }
1052
Chong Zhang5022da32016-06-21 16:31:37 -07001053 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1054 boolean changed = false;
1055 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1056 WaitResult w = mWaitingActivityLaunched.remove(i);
1057 if (w.who == null) {
1058 changed = true;
1059 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1060 // the starting activity ends up moving another activity to front, and it should
1061 // wait for this new activity to become visible instead.
1062 // Do not modify other fields.
1063 w.result = START_TASK_TO_FRONT;
1064 }
1065 }
1066 if (changed) {
1067 mService.notifyAll();
1068 }
1069 }
1070
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001071 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1072 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001073 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001075 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 if (w.who == null) {
1077 changed = true;
1078 w.timeout = timeout;
1079 if (r != null) {
1080 w.who = new ComponentName(r.info.packageName, r.info.name);
1081 }
1082 w.thisTime = thisTime;
1083 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001084 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001087 if (changed) {
1088 mService.notifyAll();
1089 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001090 }
1091
Craig Mautner29219d92013-04-16 20:19:12 -07001092 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001093 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001094 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001095 if (r != null) {
1096 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001097 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001098
Andrii Kulian7318d632016-07-20 18:59:28 -07001099 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001100 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1101
1102 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1103 final int displayId = mTmpOrderedDisplayIds.get(i);
1104 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1105 if (stacks == null) {
1106 continue;
1107 }
1108 for (int j = stacks.size() - 1; j >= 0; --j) {
1109 final ActivityStack stack = stacks.get(j);
1110 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1111 r = stack.topRunningActivityLocked();
1112 if (r != null) {
1113 return r;
1114 }
Craig Mautner29219d92013-04-16 20:19:12 -07001115 }
1116 }
1117 }
1118 return null;
1119 }
1120
Dianne Hackborn09233282014-04-30 11:33:59 -07001121 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001122 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001123 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1124 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001125 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001126 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001127 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001128 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1129 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001130 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001131 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001132 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001133 }
1134 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001135
1136 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1137 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1138 while (maxNum > 0) {
1139 long mostRecentActiveTime = Long.MIN_VALUE;
1140 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001141 final int numTaskLists = runningTaskLists.size();
1142 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1143 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001144 if (!stackTaskList.isEmpty()) {
1145 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1146 if (lastActiveTime > mostRecentActiveTime) {
1147 mostRecentActiveTime = lastActiveTime;
1148 selectedStackList = stackTaskList;
1149 }
1150 }
1151 }
1152 if (selectedStackList != null) {
1153 list.add(selectedStackList.remove(0));
1154 --maxNum;
1155 } else {
1156 break;
1157 }
1158 }
Craig Mautner20e72272013-04-01 13:45:53 -07001159 }
1160
Todd Kennedy7440f172015-12-09 14:31:22 -08001161 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1162 ProfilerInfo profilerInfo) {
1163 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001164 if (aInfo != null) {
1165 // Store the found target back into the intent, because now that
1166 // we have it we never want to do this again. For example, if the
1167 // user navigates back to this point in the history, we should
1168 // always restart the exact same activity.
1169 intent.setComponent(new ComponentName(
1170 aInfo.applicationInfo.packageName, aInfo.name));
1171
1172 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001173 if (!aInfo.processName.equals("system")) {
1174 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001175 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001176 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001177
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001178 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1179 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1180 }
1181
Man Caocfa78b22015-06-11 20:14:34 -07001182 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1183 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1184 }
1185
1186 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001187 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001188 }
1189 }
1190 }
1191 return aInfo;
1192 }
1193
Todd Kennedy7440f172015-12-09 14:31:22 -08001194 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001195 return resolveIntent(intent, resolvedType, userId, 0);
1196 }
1197
1198 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001199 try {
1200 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001201 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001202 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001203 } catch (RemoteException e) {
1204 }
1205 return null;
1206 }
1207
1208 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1209 ProfilerInfo profilerInfo, int userId) {
1210 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1211 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1212 }
1213
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001214 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1215 boolean andResume, boolean checkConfig) throws RemoteException {
1216
1217 if (!allPausedActivitiesComplete()) {
1218 // While there are activities pausing we skipping starting any new activities until
1219 // pauses are complete. NOTE: that we also do this for activities that are starting in
1220 // the paused state because they will first be resumed then paused on the client side.
1221 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1222 "realStartActivityLocked: Skipping start of r=" + r
1223 + " some activities pausing...");
1224 return false;
1225 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001226
Andrii Kulianada10292017-02-09 23:19:29 -08001227 r.startFreezingScreenLocked(app, 0);
1228 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001229
Andrii Kulianada10292017-02-09 23:19:29 -08001230 // schedule launch ticks to collect information about slow apps.
1231 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001232
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001233 // Have the window manager re-evaluate the orientation of the screen based on the new
1234 // activity order. Note that as a result of this, it can call back into the activity
1235 // manager with a new orientation. We don't care about that, because the activity is not
1236 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001238 final int displayId = r.getDisplayId();
1239 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1240 getDisplayOverrideConfiguration(displayId),
1241 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001242 // Deferring resume here because we're going to launch new activity shortly.
1243 // We don't want to perform a redundant launch of the same record while ensuring
1244 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001245 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1246 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 }
1248
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001249 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001250 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001251 }
1252
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 r.app = app;
1254 app.waitingToKill = null;
1255 r.launchCount++;
1256 r.lastLaunchTime = SystemClock.uptimeMillis();
1257
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001258 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001259
1260 int idx = app.activities.indexOf(r);
1261 if (idx < 0) {
1262 app.activities.add(r);
1263 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001264 mService.updateLruProcessLocked(app, true, null);
1265 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001266
Craig Mautner15df08a2015-04-01 12:17:18 -07001267 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001268 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1269 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001270 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 }
1272
Andrii Kulian02b7a832016-10-06 23:11:56 -07001273 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001274 try {
1275 if (app.thread == null) {
1276 throw new RemoteException();
1277 }
1278 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001279 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001281 // We don't need to deliver new intents and/or set results if activity is going
1282 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 results = r.results;
1284 newIntents = r.newIntents;
1285 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001286 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1287 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1288 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001289 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1290 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001291 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001292 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001293 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001294 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001295 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1296 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 r.sleeping = false;
1298 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001299 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 mService.showAskCompatModeDialogLocked(r);
1301 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001302 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1304 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1305 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001306 final String profileFile = mService.mProfileFile;
1307 if (profileFile != null) {
1308 ParcelFileDescriptor profileFd = mService.mProfileFd;
1309 if (profileFd != null) {
1310 try {
1311 profileFd = profileFd.dup();
1312 } catch (IOException e) {
1313 if (profileFd != null) {
1314 try {
1315 profileFd.close();
1316 } catch (IOException o) {
1317 }
1318 profileFd = null;
1319 }
1320 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001321 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001322
1323 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001324 mService.mSamplingInterval, mService.mAutoStopProfiler,
1325 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001326 }
1327 }
1328 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001329
Andrii Kulianada10292017-02-09 23:19:29 -08001330 app.hasShownUi = true;
1331 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001332 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001333 // Because we could be starting an Activity in the system process this may not go across
1334 // a Binder interface which would create a new Configuration. Consequently we have to
1335 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001336
1337 final Configuration globalConfiguration =
1338 new Configuration(mService.getGlobalConfiguration());
1339 r.setLastReportedGlobalConfiguration(globalConfiguration);
1340 final Configuration mergedOverrideConfiguration =
1341 new Configuration(task.getMergedOverrideConfiguration());
1342 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1343
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001345 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001346 globalConfiguration,
1347 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001348 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1349 r.persistentState, results, newIntents, !andResume,
1350 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001351
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001352 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 // This may be a heavy-weight process! Note that the package
1354 // manager will ensure that only activity can run in the main
1355 // process of the .apk, which is the only thing that will be
1356 // considered heavy-weight.
1357 if (app.processName.equals(app.info.packageName)) {
1358 if (mService.mHeavyWeightProcess != null
1359 && mService.mHeavyWeightProcess != app) {
1360 Slog.w(TAG, "Starting new heavy weight process " + app
1361 + " when already running "
1362 + mService.mHeavyWeightProcess);
1363 }
1364 mService.mHeavyWeightProcess = app;
1365 Message msg = mService.mHandler.obtainMessage(
1366 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1367 msg.obj = r;
1368 mService.mHandler.sendMessage(msg);
1369 }
1370 }
1371
1372 } catch (RemoteException e) {
1373 if (r.launchFailed) {
1374 // This is the second time we failed -- finish activity
1375 // and give up.
1376 Slog.e(TAG, "Second failure launching "
1377 + r.intent.getComponent().flattenToShortString()
1378 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001379 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1381 "2nd-crash", false);
1382 return false;
1383 }
1384
1385 // This is the first time we failed -- restart process and
1386 // retry.
1387 app.activities.remove(r);
1388 throw e;
1389 }
1390
1391 r.launchFailed = false;
1392 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001393 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001394 }
1395
1396 if (andResume) {
1397 // As part of the process of launching, ActivityThread also performs
1398 // a resume.
1399 stack.minimalResumeActivityLocked(r);
1400 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001401 // This activity is not starting in the resumed state... which should look like we asked
1402 // it to pause+stop (but remain visible), and it has done so and reported back the
1403 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001404 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001405 "Moving to PAUSED: " + r + " (starting in paused state)");
1406 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001407 }
1408
1409 // Launch the new version setup screen if needed. We do this -after-
1410 // launching the initial activity (that is, home), so that it can have
1411 // a chance to initialize itself while in the background, making the
1412 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001413 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001414 mService.startSetupActivityLocked();
1415 }
1416
Dianne Hackborn465fa392014-09-14 14:21:18 -07001417 // Update any services we are bound to that might care about whether
1418 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001419 if (r.app != null) {
1420 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1421 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001422
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 return true;
1424 }
1425
Craig Mautnere79d42682013-04-01 19:01:53 -07001426 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001427 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001428 // Is this activity's application already running?
1429 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001430 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001431
Andrii Kulian02b7a832016-10-06 23:11:56 -07001432 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001433
1434 if (app != null && app.thread != null) {
1435 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001436 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1437 || !"android".equals(r.info.packageName)) {
1438 // Don't add this if it is a platform component that is marked
1439 // to run in multiple processes, because this is actually
1440 // part of the framework so doesn't make sense to track as a
1441 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001442 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1443 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001444 }
George Mount2c92c972014-03-20 09:38:23 -07001445 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001446 return;
1447 } catch (RemoteException e) {
1448 Slog.w(TAG, "Exception when starting activity "
1449 + r.intent.getComponent().flattenToShortString(), e);
1450 }
1451
1452 // If a dead object exception was thrown -- fall through to
1453 // restart the application.
1454 }
1455
1456 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001457 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001458 }
1459
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001460 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001461 String resultWho, int requestCode, int callingPid, int callingUid,
1462 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001463 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001464 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1465 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001466 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001467 return true;
1468 }
1469 final int componentRestriction = getComponentRestrictionForCallingPackage(
1470 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1471 final int actionRestriction = getActionRestrictionForCallingPackage(
1472 intent.getAction(), callingPackage, callingPid, callingUid);
1473 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1474 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1475 if (resultRecord != null) {
1476 resultStack.sendActivityResultLocked(-1,
1477 resultRecord, resultWho, requestCode,
1478 Activity.RESULT_CANCELED, null);
1479 }
1480 final String msg;
1481 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1482 msg = "Permission Denial: starting " + intent.toString()
1483 + " from " + callerApp + " (pid=" + callingPid
1484 + ", uid=" + callingUid + ")" + " with revoked permission "
1485 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1486 } else if (!aInfo.exported) {
1487 msg = "Permission Denial: starting " + intent.toString()
1488 + " from " + callerApp + " (pid=" + callingPid
1489 + ", uid=" + callingUid + ")"
1490 + " not exported from uid " + aInfo.applicationInfo.uid;
1491 } else {
1492 msg = "Permission Denial: starting " + intent.toString()
1493 + " from " + callerApp + " (pid=" + callingPid
1494 + ", uid=" + callingUid + ")"
1495 + " requires " + aInfo.permission;
1496 }
1497 Slog.w(TAG, msg);
1498 throw new SecurityException(msg);
1499 }
1500
1501 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1502 final String message = "Appop Denial: starting " + intent.toString()
1503 + " from " + callerApp + " (pid=" + callingPid
1504 + ", uid=" + callingUid + ")"
1505 + " requires " + AppOpsManager.permissionToOp(
1506 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1507 Slog.w(TAG, message);
1508 return false;
1509 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1510 final String message = "Appop Denial: starting " + intent.toString()
1511 + " from " + callerApp + " (pid=" + callingPid
1512 + ", uid=" + callingUid + ")"
1513 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1514 Slog.w(TAG, message);
1515 return false;
1516 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001517 if (options != null) {
1518 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1519 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1520 callingPid, callingUid);
1521 if (startInTaskPerm != PERMISSION_GRANTED) {
1522 final String msg = "Permission Denial: starting " + intent.toString()
1523 + " from " + callerApp + " (pid=" + callingPid
1524 + ", uid=" + callingUid + ") with launchTaskId="
1525 + options.getLaunchTaskId();
1526 Slog.w(TAG, msg);
1527 throw new SecurityException(msg);
1528 }
1529 }
1530 // Check if someone tries to launch an activity on a private display with a different
1531 // owner.
1532 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001533 if (launchDisplayId != INVALID_DISPLAY
1534 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1535 final String msg = "Permission Denial: starting " + intent.toString()
1536 + " from " + callerApp + " (pid=" + callingPid
1537 + ", uid=" + callingUid + ") with launchDisplayId="
1538 + launchDisplayId;
1539 Slog.w(TAG, msg);
1540 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001541 }
1542 }
1543
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001544 return true;
1545 }
1546
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001547 /** Check if caller is allowed to launch activities on specified display. */
1548 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1549 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1550 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1551
1552 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1553 if (activityDisplay == null) {
1554 Slog.w(TAG, "Launch on display check: display not found");
1555 return false;
1556 }
1557
1558 if (!activityDisplay.isPrivate()) {
1559 // Anyone can launch on a public display.
1560 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1561 + " allow launch on public display");
1562 return true;
1563 }
1564
1565 // Check if the caller is the owner of the display.
1566 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1567 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1568 + " allow launch for owner of the display");
1569 return true;
1570 }
1571
1572 // Check if caller is present on display
1573 if (activityDisplay.isUidPresent(callingUid)) {
1574 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1575 + " allow launch for caller present on the display");
1576 return true;
1577 }
1578
1579 // Check if the caller can launch anything.
1580 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1581 callingUid);
1582 if (startAnyPerm == PERMISSION_GRANTED) {
1583 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1584 + " allow launch any on display");
1585 return true;
1586 }
1587
1588 Slog.w(TAG, "Launch on display check: denied");
1589 return false;
1590 }
1591
1592 /** Update lists of UIDs that are present on displays and have access to them. */
1593 void updateUIDsPresentOnDisplay() {
1594 mDisplayAccessUIDs.clear();
1595 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1596 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1597 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1598 }
1599 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1600 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1601 }
1602
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001603 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001604 final long identity = Binder.clearCallingIdentity();
1605 try {
1606 return UserManager.get(mService.mContext).getUserInfo(userId);
1607 } finally {
1608 Binder.restoreCallingIdentity(identity);
1609 }
1610 }
1611
Svet Ganov99b60432015-06-27 13:15:22 -07001612 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001613 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001614 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1615 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001616 == PackageManager.PERMISSION_DENIED) {
1617 return ACTIVITY_RESTRICTION_PERMISSION;
1618 }
1619
Christopher Tateff7add02015-08-17 10:23:22 -07001620 if (activityInfo.permission == null) {
1621 return ACTIVITY_RESTRICTION_NONE;
1622 }
1623
Svet Ganov99b60432015-06-27 13:15:22 -07001624 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1625 if (opCode == AppOpsManager.OP_NONE) {
1626 return ACTIVITY_RESTRICTION_NONE;
1627 }
1628
1629 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1630 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001631 if (!ignoreTargetSecurity) {
1632 return ACTIVITY_RESTRICTION_APPOP;
1633 }
Svet Ganov99b60432015-06-27 13:15:22 -07001634 }
1635
1636 return ACTIVITY_RESTRICTION_NONE;
1637 }
1638
Svetoslav7008b512015-06-24 18:47:07 -07001639 private int getActionRestrictionForCallingPackage(String action,
1640 String callingPackage, int callingPid, int callingUid) {
1641 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001642 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001643 }
1644
1645 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1646 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001647 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001648 }
1649
1650 final PackageInfo packageInfo;
1651 try {
1652 packageInfo = mService.mContext.getPackageManager()
1653 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1654 } catch (PackageManager.NameNotFoundException e) {
1655 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001656 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001657 }
1658
1659 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001660 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001661 }
1662
1663 if (mService.checkPermission(permission, callingPid, callingUid) ==
1664 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001665 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001666 }
1667
1668 final int opCode = AppOpsManager.permissionToOpCode(permission);
1669 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001670 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001671 }
1672
1673 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1674 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001675 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001676 }
1677
Svet Ganov99b60432015-06-27 13:15:22 -07001678 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001679 }
1680
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001681 void setLaunchSource(int uid) {
1682 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1683 }
1684
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001685 void acquireLaunchWakelock() {
1686 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1687 throw new IllegalStateException("Calling must be system uid");
1688 }
1689 mLaunchingActivity.acquire();
1690 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1691 // To be safe, don't allow the wake lock to be held for too long.
1692 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1693 }
1694 }
1695
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001696 /**
1697 * Called when the frontmost task is idle.
1698 * @return the state of mService.mBooting before this was called.
1699 */
1700 private boolean checkFinishBootingLocked() {
1701 final boolean booting = mService.mBooting;
1702 boolean enableScreen = false;
1703 mService.mBooting = false;
1704 if (!mService.mBooted) {
1705 mService.mBooted = true;
1706 enableScreen = true;
1707 }
1708 if (booting || enableScreen) {
1709 mService.postFinishBooting(booting, enableScreen);
1710 }
1711 return booting;
1712 }
1713
Craig Mautnerf3333272013-04-22 10:55:53 -07001714 // Checked.
1715 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001716 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001717 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001718
Craig Mautnerf3333272013-04-22 10:55:53 -07001719 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001720 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001721 int NS = 0;
1722 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001723 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001724 boolean activityRemoved = false;
1725
Wale Ogunwale7d701172015-03-11 15:36:30 -07001726 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001727 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001728 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1729 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001730 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1731 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001732 if (fromTimeout) {
1733 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001734 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001735
1736 // This is a hack to semi-deal with a race condition
1737 // in the client where it can be constructed with a
1738 // newer configuration from when we asked it to launch.
1739 // We'll update with whatever configuration it now says
1740 // it used to launch.
1741 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001742 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001743 }
1744
1745 // We are now idle. If someone is waiting for a thumbnail from
1746 // us, we can now deliver.
1747 r.idle = true;
1748
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001749 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001750 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001751 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001752 }
1753 }
1754
1755 if (allResumedActivitiesIdle()) {
1756 if (r != null) {
1757 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001758 }
1759
1760 if (mLaunchingActivity.isHeld()) {
1761 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1762 if (VALIDATE_WAKE_LOCK_CALLER &&
1763 Binder.getCallingUid() != Process.myUid()) {
1764 throw new IllegalStateException("Calling must be system uid");
1765 }
1766 mLaunchingActivity.release();
1767 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001768 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001769 }
1770
1771 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001772 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1773 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001774 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001775 if ((NF = mFinishingActivities.size()) > 0) {
1776 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001777 mFinishingActivities.clear();
1778 }
1779
Craig Mautnerf3333272013-04-22 10:55:53 -07001780 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001781 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001782 mStartingUsers.clear();
1783 }
1784
Craig Mautnerf3333272013-04-22 10:55:53 -07001785 // Stop any activities that are scheduled to do so but have been
1786 // waiting for the next one to start.
1787 for (int i = 0; i < NS; i++) {
1788 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001789 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001790 if (stack != null) {
1791 if (r.finishing) {
1792 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1793 } else {
1794 stack.stopActivityLocked(r);
1795 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001796 }
1797 }
1798
1799 // Finish any activities that are scheduled to do so but have been
1800 // waiting for the next one to start.
1801 for (int i = 0; i < NF; i++) {
1802 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001803 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001804 if (stack != null) {
1805 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1806 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001807 }
1808
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001809 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001810 // Complete user switch
1811 if (startingUsers != null) {
1812 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001813 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001814 }
1815 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 }
1817
1818 mService.trimApplications();
1819 //dump();
1820 //mWindowManager.dump();
1821
Craig Mautnerf3333272013-04-22 10:55:53 -07001822 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001823 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 }
1825
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001826 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001827 }
1828
Craig Mautner8e569572013-10-11 17:36:59 -07001829 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001830 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1832 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001833 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1834 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1835 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001836 }
Craig Mautner19091252013-10-05 00:03:53 -07001837 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001838 }
1839
1840 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001841 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1842 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001843 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1844 stacks.get(stackNdx).closeSystemDialogsLocked();
1845 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001846 }
1847 }
1848
Craig Mautner93529a42013-10-04 15:03:13 -07001849 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001850 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001851 }
1852
Craig Mautner8d341ef2013-03-26 09:03:27 -07001853 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001854 * Update the last used stack id for non-current user (current user's last
1855 * used stack is the focused stack)
1856 */
1857 void updateUserStackLocked(int userId, ActivityStack stack) {
1858 if (userId != mCurrentUser) {
1859 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1860 }
1861 }
1862
1863 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001864 * @return true if some activity was finished (or would have finished if doit were true).
1865 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001866 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1867 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001868 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001869 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1870 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001871 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001872 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001873 if (stack.finishDisabledPackageActivitiesLocked(
1874 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001875 didSomething = true;
1876 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001877 }
1878 }
1879 return didSomething;
1880 }
1881
Dianne Hackborna413dc02013-07-12 12:02:55 -07001882 void updatePreviousProcessLocked(ActivityRecord r) {
1883 // Now that this process has stopped, we may want to consider
1884 // it to be the previous app to try to keep around in case
1885 // the user wants to return to it.
1886
1887 // First, found out what is currently the foreground app, so that
1888 // we don't blow away the previous app if this activity is being
1889 // hosted by the process that is actually still the foreground.
1890 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001891 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1892 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001893 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1894 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001895 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001896 if (stack.mResumedActivity != null) {
1897 fgApp = stack.mResumedActivity.app;
1898 } else if (stack.mPausingActivity != null) {
1899 fgApp = stack.mPausingActivity.app;
1900 }
1901 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001902 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001903 }
1904 }
1905
1906 // Now set this one as the previous process, only if that really
1907 // makes sense to.
1908 if (r.app != null && fgApp != null && r.app != fgApp
1909 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001910 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001911 mService.mPreviousProcess = r.app;
1912 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1913 }
1914 }
1915
Wale Ogunwaled046a012015-12-24 13:05:59 -08001916 boolean resumeFocusedStackTopActivityLocked() {
1917 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001918 }
1919
Wale Ogunwaled046a012015-12-24 13:05:59 -08001920 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001921 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001922 if (targetStack != null && isFocusedStack(targetStack)) {
1923 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001924 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001925 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1926 if (r == null || r.state != RESUMED) {
1927 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001928 } else if (r.state == RESUMED) {
1929 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1930 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001931 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001932 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001933 }
1934
Todd Kennedy39bfee52016-02-24 10:28:21 -08001935 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1936 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1937 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1938 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1939 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1940 }
1941 }
1942 }
1943
Adrian Roos20d7df32016-01-12 18:59:43 +01001944 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1945 TaskRecord finishedTask = null;
1946 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1948 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001949 final int numStacks = stacks.size();
1950 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1951 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001952 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1953 if (stack == focusedStack || finishedTask == null) {
1954 finishedTask = t;
1955 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001956 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001957 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001958 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001959 }
1960
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001961 void finishVoiceTask(IVoiceInteractionSession session) {
1962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1963 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1964 final int numStacks = stacks.size();
1965 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1966 final ActivityStack stack = stacks.get(stackNdx);
1967 stack.finishVoiceTask(session);
1968 }
1969 }
1970 }
1971
Andrii Kulianc27916642016-04-12 17:59:27 -07001972 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1973 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001974 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1975 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001976 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001977 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1978 // Caller wants the home activity moved with it. To accomplish this,
1979 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001980 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001981 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001982 ActivityStack currentStack = task.getStack();
1983 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001984 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1985 + task + " to front. Stack is null");
1986 return;
1987 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001988
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001989 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001990 int stackId = options.getLaunchStackId();
1991 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001992 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001993 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001994 if (stackId == INVALID_STACK_ID) {
1995 stackId = task.getLaunchStackId();
1996 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001997 if (stackId != currentStack.mStackId) {
1998 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08001999 !FORCE_FOCUS, reason);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002000 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002001 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2002 // still need moveTaskToFrontLocked() below for any transition settings.
2003 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002004 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2005 resizeStackLocked(stackId, bounds,
2006 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002007 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002008 } else {
2009 // WM resizeTask must be done after the task is moved to the correct stack,
2010 // because Task's setBounds() also updates dim layer's bounds, but that has
2011 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002012 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002013 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002014 }
2015 }
2016
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002017 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002018 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002019 r == null ? null : r.appTimeTracker, reason);
2020
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002021 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002022 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002023
Andrii Kulian02b7a832016-10-06 23:11:56 -07002024 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002025 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002026 }
2027
Wale Ogunwale854809c2015-12-27 16:18:19 -08002028 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002029 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002030 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002031 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002032 return false;
2033 }
2034 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2035 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002036 }
2037
Winson Chung55893332017-02-17 17:13:10 -08002038 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002039 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002040 }
2041
Winson Chung55893332017-02-17 17:13:10 -08002042 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2043 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002044 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002045 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002046 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002047 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002048 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002049 return null;
2050 }
Winson Chung55893332017-02-17 17:13:10 -08002051 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002052 }
2053
Andrii Kulian16802aa2016-11-02 12:21:33 -07002054 /**
2055 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2056 * If there is no such stack, new dynamic stack can be created.
2057 * @param displayId Target display.
2058 * @param r Activity that should be launched there.
2059 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2060 */
2061 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2062 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2063 if (activityDisplay == null) {
2064 throw new IllegalArgumentException(
2065 "Display with displayId=" + displayId + " not found.");
2066 }
2067
2068 // Return the topmost valid stack on the display.
2069 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2070 final ActivityStack stack = activityDisplay.mStacks.get(i);
2071 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2072 return stack;
2073 }
2074 }
2075
2076 // If there is no valid stack on the external display - check if new dynamic stack will do.
2077 if (displayId != Display.DEFAULT_DISPLAY) {
2078 final int newDynamicStackId = getNextStackId();
2079 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2080 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2081 }
2082 }
2083
2084 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2085 return null;
2086 }
2087
Craig Mautner967212c2013-04-13 21:10:58 -07002088 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002089 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002090 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2091 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002092 }
2093 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002094 }
2095
Jorim Jaggife762342016-10-13 14:33:27 +02002096 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2097 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2098 }
2099
Andrii Kulian7fc22812016-12-28 13:04:11 -08002100 /**
2101 * Get next focusable stack in the system. This will search across displays and stacks
2102 * in last-focused order for a focusable and visible stack, different from the target stack.
2103 *
2104 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2105 * searching for next candidate.
2106 * @return Next focusable {@link ActivityStack}, null if not found.
2107 */
2108 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2109 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2110
2111 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2112 final int displayId = mTmpOrderedDisplayIds.get(i);
2113 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2114 if (stacks == null) {
2115 continue;
2116 }
2117 for (int j = stacks.size() - 1; j >= 0; --j) {
2118 final ActivityStack stack = stacks.get(j);
2119 if (stack != currentFocus && stack.isFocusable()
2120 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2121 return stack;
2122 }
2123 }
2124 }
2125
2126 return null;
2127 }
2128
Craig Mautneree2e45a2014-06-27 12:10:03 -07002129 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002130 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002131 }
2132
2133 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002134 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2135 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2136 final TaskRecord task = tasks.get(taskNdx);
2137 if (task.isHomeTask()) {
2138 final ArrayList<ActivityRecord> activities = task.mActivities;
2139 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2140 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002141 if (r.isHomeActivity()
2142 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002143 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002144 }
2145 }
2146 }
2147 }
2148 return null;
2149 }
2150
Chong Zhangb15758a2015-11-17 12:12:03 -08002151 /**
2152 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2153 * the docked stack itself, or if it's side-by-side to the docked stack.
2154 */
2155 boolean isStackDockedInEffect(int stackId) {
2156 return stackId == DOCKED_STACK_ID ||
2157 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2158 }
2159
Todd Kennedyca4d8422015-01-15 15:19:22 -08002160 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002161 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002162 ActivityContainer activityContainer =
2163 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002164 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002165 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2166 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002167 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002168 return activityContainer;
2169 }
2170
Craig Mautner34b73df2014-01-12 21:11:08 -08002171 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002172 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2173 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2174 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002175 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2176 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002177 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002178 }
2179 }
2180
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002181 void deleteActivityContainerRecord(int stackId) {
2182 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2183 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2184 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002185 }
2186
Jorim Jaggidc249c42015-12-15 14:57:31 -08002187 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002188 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002189 if (stackId == DOCKED_STACK_ID) {
2190 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002191 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002192 return;
2193 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002194 final ActivityStack stack = getStack(stackId);
2195 if (stack == null) {
2196 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2197 return;
2198 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002199
Jorim Jaggidc249c42015-12-15 14:57:31 -08002200 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002201 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2202 // stack size changing so things don't get out of sync.
2203 return;
2204 }
2205
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002206 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002207 mWindowManager.deferSurfaceLayout();
2208 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002209 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002210 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002211 stack.ensureVisibleActivitiesConfigurationLocked(
2212 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002213 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002214 } finally {
2215 mWindowManager.continueSurfaceLayout();
2216 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002217 }
2218 }
2219
Jorim Jaggi192086e2016-03-11 17:17:03 +01002220 void deferUpdateBounds(int stackId) {
2221 final ActivityStack stack = getStack(stackId);
2222 if (stack != null) {
2223 stack.deferUpdateBounds();
2224 }
2225 }
2226
2227 void continueUpdateBounds(int stackId) {
2228 final ActivityStack stack = getStack(stackId);
2229 if (stack != null) {
2230 stack.continueUpdateBounds();
2231 }
2232 }
2233
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002234 void notifyAppTransitionDone() {
2235 continueUpdateBounds(HOME_STACK_ID);
2236 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2237 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002238 final TaskRecord task =
2239 anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2240 if (task != null) {
2241 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002242 }
2243 }
2244 mResizingTasksDuringAnimation.clear();
2245 }
2246
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002247 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2248 final ActivityStack stack = getStack(fromStackId);
2249 if (stack == null) {
2250 return;
2251 }
2252
2253 mWindowManager.deferSurfaceLayout();
2254 try {
2255 if (fromStackId == DOCKED_STACK_ID) {
2256
2257 // We are moving all tasks from the docked stack to the fullscreen stack,
2258 // which is dismissing the docked stack, so resize all other stacks to
2259 // fullscreen here already so we don't end up with resize trashing.
2260 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2261 if (StackId.isResizeableByDockedStack(i)) {
2262 ActivityStack otherStack = getStack(i);
2263 if (otherStack != null) {
2264 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2265 true /* allowResizeInDockedMode */, DEFER_RESUME);
2266 }
2267 }
2268 }
2269
2270 // Also disable docked stack resizing since we have manually adjusted the
2271 // size of other stacks above and we don't want to trigger a docked stack
2272 // resize when we remove task from it below and it is detached from the
2273 // display because it no longer contains any tasks.
2274 mAllowDockedStackResize = false;
2275 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002276 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002277 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2278 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002279 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2280 final int size = tasks.size();
2281 if (onTop) {
2282 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002283 final TaskRecord task = tasks.get(i);
2284 if (fromStackId == PINNED_STACK_ID) {
2285 // Update the return-to to reflect where the pinned stack task was moved
2286 // from so that we retain the stack that was previously visible if the
2287 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002288 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2289 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002290 MetricsLogger.action(mService.mContext,
2291 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002292 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002293 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002294 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002295 "moveTasksToFullscreenStack - onTop", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002296 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002297
2298 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2299 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002300 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002301 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002302 final TaskRecord task = tasks.get(i);
2303 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, MAX_VALUE,
2304 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002305 }
2306 }
2307 } finally {
2308 mAllowDockedStackResize = true;
2309 mWindowManager.continueSurfaceLayout();
2310 }
2311 }
2312
Jorim Jaggidc249c42015-12-15 14:57:31 -08002313 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002314 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2315 boolean preserveWindows) {
2316 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2317 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2318 false /* deferResume */);
2319 }
2320
2321 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2322 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2323 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002324
2325 if (!mAllowDockedStackResize) {
2326 // Docked stack resize currently disabled.
2327 return;
2328 }
2329
Jorim Jaggidc249c42015-12-15 14:57:31 -08002330 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2331 if (stack == null) {
2332 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2333 return;
2334 }
2335
2336 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2337 mWindowManager.deferSurfaceLayout();
2338 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002339 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2340 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002341 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002342 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002343
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002344 // TODO: Checking for isAttached might not be needed as if the user passes in null
2345 // dockedBounds then they want the docked stack to be dismissed.
2346 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2347 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002348 // In this case we make all other static stacks fullscreen and move all
2349 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002350 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002351
2352 // stack shouldn't contain anymore activities, so nothing to resume.
2353 r = null;
2354 } else {
2355 // Docked stacks occupy a dedicated region on screen so the size of all other
2356 // static stacks need to be adjusted so they don't overlap with the docked stack.
2357 // We get the bounds to use from window manager which has been adjusted for any
2358 // screen controls and is also the same for all stacks.
Matthew Nge15352e2016-12-20 15:36:29 -08002359 final Rect tempOtherTaskRect = new Rect();
2360 final Rect tempOtherTaskInsetRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002361 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002362 final ActivityStack current = getStack(i);
2363 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Nge15352e2016-12-20 15:36:29 -08002364 current.getStackDockedModeBounds(tempRect, tempOtherTaskRect,
2365 tempOtherTaskInsetRect, true /* ignoreVisibility */);
2366 resizeStackLocked(i, tempRect,
2367 !tempOtherTaskRect.isEmpty() ? tempOtherTaskRect :
2368 tempOtherTaskBounds,
2369 !tempOtherTaskInsetRect.isEmpty() ? tempOtherTaskInsetRect :
2370 tempOtherTaskInsetBounds,
2371 preserveWindows, true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002372 }
2373 }
2374 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002375 if (!deferResume) {
2376 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2377 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002378 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002379 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002380 mWindowManager.continueSurfaceLayout();
2381 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2382 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002383 }
2384
Robert Carr0d00c2e2016-02-29 17:45:02 -08002385 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2386 final ActivityStack stack = getStack(PINNED_STACK_ID);
2387 if (stack == null) {
2388 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2389 return;
2390 }
2391 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2392 mWindowManager.deferSurfaceLayout();
2393 try {
2394 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002395 Rect insetBounds = null;
2396 if (tempPinnedTaskBounds != null) {
2397 // We always use 0,0 as the position for the inset rect because
2398 // if we are getting insets at all in the pinned stack it must mean
2399 // we are headed for fullscreen.
2400 insetBounds = tempRect;
2401 insetBounds.top = 0;
2402 insetBounds.left = 0;
2403 insetBounds.right = tempPinnedTaskBounds.width();
2404 insetBounds.bottom = tempPinnedTaskBounds.height();
2405 }
2406 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002407 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002408 } finally {
2409 mWindowManager.continueSurfaceLayout();
2410 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2411 }
2412 }
2413
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002414 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002415 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002416 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002417 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002418 }
2419
Wale Ogunwale1666e312016-12-16 11:27:18 -08002420 final ActivityContainer activityContainer =
2421 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002422 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002423 }
2424
Winson Chung010927a2016-12-15 16:12:35 -08002425 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002426 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002427 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2428 * instead moved back onto the fullscreen stack.
2429 */
2430 void removeStackLocked(int stackId) {
2431 final ActivityStack stack = getStack(stackId);
2432 if (stack == null) {
2433 return;
2434 }
2435
2436 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2437 if (stack.getStackId() == PINNED_STACK_ID) {
2438 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2439 if (fullscreenStack != null) {
2440 final boolean isFullscreenStackVisible =
2441 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2442 for (int i = 0; i < tasks.size(); i++) {
2443 // Insert the task either at the top of the fullscreen stack if it is hidden,
2444 // or just under the top task if it is currently visible
2445 final int insertPosition = isFullscreenStackVisible
2446 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2447 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002448 final TaskRecord task = tasks.get(i);
2449 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002450 }
2451 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2452 resumeFocusedStackTopActivityLocked();
2453 } else {
2454 // If there is no fullscreen stack, then create the stack and move all the tasks
2455 // onto the stack
2456 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, false /* onTop */);
2457 }
2458 } else {
2459 for (int i = tasks.size() - 1; i >= 0; i--) {
2460 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2461 REMOVE_FROM_RECENTS);
2462 }
2463 }
2464 }
2465
2466 /**
2467 * Removes the task with the specified task id.
2468 *
2469 * @param taskId Identifier of the task to be removed.
2470 * @param killProcess Kill any process associated with the task if possible.
2471 * @param removeFromRecents Whether to also remove the task from recents.
2472 * @return Returns true if the given task was found and removed.
2473 */
2474 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2475 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2476 if (tr != null) {
2477 tr.removeTaskActivitiesLocked();
2478 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2479 if (tr.isPersistable) {
2480 mService.notifyTaskPersisterLocked(null, true);
2481 }
2482 return true;
2483 }
2484 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2485 return false;
2486 }
2487
2488 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2489 if (removeFromRecents) {
2490 mRecentTasks.remove(tr);
2491 tr.removedFromRecents();
2492 }
2493 ComponentName component = tr.getBaseIntent().getComponent();
2494 if (component == null) {
2495 Slog.w(TAG, "No component for base intent of task: " + tr);
2496 return;
2497 }
2498
2499 // Find any running services associated with this app and stop if needed.
2500 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2501
2502 if (!killProcess) {
2503 return;
2504 }
2505
2506 // Determine if the process(es) for this task should be killed.
2507 final String pkg = component.getPackageName();
2508 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2509 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2510 for (int i = 0; i < pmap.size(); i++) {
2511
2512 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2513 for (int j = 0; j < uids.size(); j++) {
2514 ProcessRecord proc = uids.valueAt(j);
2515 if (proc.userId != tr.userId) {
2516 // Don't kill process for a different user.
2517 continue;
2518 }
2519 if (proc == mService.mHomeProcess) {
2520 // Don't kill the home process along with tasks from the same package.
2521 continue;
2522 }
2523 if (!proc.pkgList.containsKey(pkg)) {
2524 // Don't kill process that is not associated with this task.
2525 continue;
2526 }
2527
2528 for (int k = 0; k < proc.activities.size(); k++) {
2529 TaskRecord otherTask = proc.activities.get(k).task;
2530 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2531 // Don't kill process(es) that has an activity in a different task that is
2532 // also in recents.
2533 return;
2534 }
2535 }
2536
2537 if (proc.foregroundServices) {
2538 // Don't kill process(es) with foreground service.
2539 return;
2540 }
2541
2542 // Add process to kill list.
2543 procsToKill.add(proc);
2544 }
2545 }
2546
2547 // Kill the running processes.
2548 for (int i = 0; i < procsToKill.size(); i++) {
2549 ProcessRecord pr = procsToKill.get(i);
2550 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2551 && pr.curReceivers.isEmpty()) {
2552 pr.kill("remove task", true);
2553 } else {
2554 // We delay killing processes that are not in the background or running a receiver.
2555 pr.waitingToKill = "remove task";
2556 }
2557 }
2558 }
2559
Craig Mautner4a1cb222013-12-04 16:14:06 -08002560 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002561 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002562 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2563 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002564 break;
2565 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002566 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002567 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002568 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002569 }
2570
Chong Zhang5dcb2752015-08-18 13:50:26 -07002571 /**
2572 * Restores a recent task to a stack
2573 * @param task The recent task to be restored.
2574 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002575 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2576 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002577 * @return true if the task has been restored successfully.
2578 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002579 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002580 if (!StackId.isStaticStack(stackId)) {
2581 // If stack is not static (or stack id is invalid) - use the default one.
2582 // This means that tasks that were on external displays will be restored on the
2583 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002584 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002585 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002586 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2587 // Put it in the fullscreen stack.
2588 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002589 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002590
Andrii Kulian02b7a832016-10-06 23:11:56 -07002591 final ActivityStack currentStack = task.getStack();
2592 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002593 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002594 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002595 // Nothing else to do since it is already restored in the right stack.
2596 return true;
2597 }
2598 // Remove current stack association, so we can re-associate the task with the
2599 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002600 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002601 }
2602
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002603 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002604 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002605
2606 if (stack == null) {
2607 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002608 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2609 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002610 return false;
2611 }
2612
Wale Ogunwale72919d22016-12-08 18:58:50 -08002613 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002614 // TODO: move call for creation here and other place into Stack.addTask()
2615 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002616 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2617 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002618 final ArrayList<ActivityRecord> activities = task.mActivities;
2619 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002620 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002621 }
2622 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002623 }
2624
Wale Ogunwale040b4702015-08-06 18:10:50 -07002625 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002626 * Move stack with all its existing content to specified display.
2627 * @param stackId Id of stack to move.
2628 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002629 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002630 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002631 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002632 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2633 if (activityDisplay == null) {
2634 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2635 + displayId);
2636 }
2637 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2638 if (activityContainer != null) {
2639 if (activityContainer.isAttachedLocked()) {
2640 if (activityContainer.getDisplayId() == displayId) {
2641 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2642 + " to its current displayId=" + displayId);
2643 }
2644
Andrii Kulian250d6532017-02-08 23:30:45 -08002645 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002646 } else {
2647 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2648 + stackId + " is not attached to any display.");
2649 }
2650 } else {
2651 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2652 + stackId);
2653 }
2654
2655 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2656 !PRESERVE_WINDOWS);
2657 // TODO(multi-display): resize stacks properly if moved from split-screen.
2658 }
2659
2660 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002661 * Moves the specified task record to the input stack id.
2662 * WARNING: This method performs an unchecked/raw move of the task and
2663 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002664 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002665 * @param task Task to move.
2666 * @param stackId Id of stack to move task to.
2667 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002668 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002669 * @param reason Reason the task is been moved.
2670 * @return The stack the task was moved to.
2671 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002672 ActivityStack moveTaskToStackUncheckedLocked(TaskRecord task, int stackId, boolean toTop,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002673 boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002674
2675 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2676 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2677 + "support multi-window task=" + task + " to stackId=" + stackId);
2678 }
2679
Wale Ogunwale06579d62016-04-30 15:29:06 -07002680 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002681 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002682 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002683 final boolean wasResumed = prevStack.mResumedActivity == r;
Winson Chungc2baac02017-01-11 13:34:47 -08002684 final boolean wasPaused = prevStack.mPausingActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002685 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2686 // Whenever we are moving the top activity from the front stack we want to make sure to move
2687 // the stack to the front.
Andrii Kulian7d95df42017-02-15 10:11:48 -08002688 final boolean wasFront = isFrontStackOnDisplay(prevStack)
Wale Ogunwaled046a012015-12-24 13:05:59 -08002689 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002690
Chong Zhangd545dce2016-02-29 18:09:17 -08002691 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002692 // We don't allow moving a unresizeable task to the docked stack since the docked
2693 // stack is used for split-screen mode and will cause things like the docked divider to
2694 // show up. We instead leave the task in its current stack or move it to the fullscreen
2695 // stack if it isn't currently in a stack.
2696 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002697 Slog.w(TAG, "Can not move unresizeable task=" + task
2698 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2699 }
2700
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002701 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2702 // if a docked stack is created below which will lead to the stack we are moving from and
2703 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002704 task.mTemporarilyUnresizable = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002705 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002706 task.mTemporarilyUnresizable = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002707 task.reparent(stack.mStackId, toTop ? MAX_VALUE : 0, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002708
Winson Chungc2baac02017-01-11 13:34:47 -08002709 // Reset the resumed activity on the previous stack
2710 if (wasResumed) {
2711 prevStack.mResumedActivity = null;
2712 }
2713 // Reset the paused activity on the previous stack
2714 if (wasPaused) {
2715 prevStack.mPausingActivity = null;
Winson Chung4dabf232017-01-25 13:25:22 -08002716 prevStack.removeTimeoutsForActivityLocked(r);
Winson Chungc2baac02017-01-11 13:34:47 -08002717 }
2718
Chong Zhang02898352015-08-21 17:27:14 -07002719 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002720 // move focus to the new stack by moving the stack to the front.
Winson Chungc2baac02017-01-11 13:34:47 -08002721 stack.moveToFrontAndResumeStateIfNeeded(r, forceFocus || wasFocused || wasFront, wasResumed,
2722 wasPaused, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002723
Wale Ogunwale040b4702015-08-06 18:10:50 -07002724 return stack;
2725 }
2726
Chong Zhange4fbd322016-03-01 14:44:03 -08002727 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002728 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002729 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002730 false /* deferResume */);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002731 }
2732
2733 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002734 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002735 final TaskRecord task = anyTaskForIdLocked(taskId);
2736 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002737 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002738 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002739 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002740
Andrii Kulian02b7a832016-10-06 23:11:56 -07002741 final ActivityStack currentStack = task.getStack();
2742 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002743 // You are already in the right stack silly...
2744 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002745 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002746 }
2747
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002748 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2749 throw new IllegalArgumentException("moveTaskToStack:"
2750 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2751 }
2752
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002753 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002754 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002755 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002756 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002757 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002758 // We are about to relaunch the activity because its configuration changed due to
2759 // being maximized, i.e. size change. The activity will first remove the old window
2760 // and then add a new one. This call will tell window manager about this, so it can
2761 // preserve the old window until the new one is drawn. This prevents having a gap
2762 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002763 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002764 // Note here we always set the replacing window first, as the flags might be needed
2765 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002766 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002767 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002768
2769 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002770 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002771 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002772 try {
2773 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002774 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002775 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002776
Wale Ogunwale961f4852016-02-01 20:25:54 -08002777 if (!animate) {
2778 stack.mNoAnimActivities.add(topActivity);
2779 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002780
Wale Ogunwale961f4852016-02-01 20:25:54 -08002781 // We might trigger a configuration change. Save the current task bounds for freezing.
Wale Ogunwale1666e312016-12-16 11:27:18 -08002782 // TODO: Should this call be moved inside the resize method in WM?
2783 stack.prepareFreezingTaskBounds();
Wale Ogunwale961f4852016-02-01 20:25:54 -08002784
2785 // Make sure the task has the appropriate bounds/size for the stack it is in.
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002786 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID
2787 && !Objects.equals(task.mBounds, stack.mBounds)) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002788 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2789 deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002790 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2791 Rect bounds = task.getLaunchBounds();
2792 if (bounds == null) {
2793 stack.layoutTaskInStack(task, null);
2794 bounds = task.mBounds;
2795 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002796 kept = task.resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002797 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002798 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2799 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002800 }
2801 } finally {
2802 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002803 }
2804
2805 if (mightReplaceWindow) {
2806 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2807 // window), we need to clear the replace window settings. Otherwise, we schedule a
2808 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002809 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002810 }
2811
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002812 if (!deferResume) {
2813
2814 // The task might have already been running and its visibility needs to be synchronized with
2815 // the visibility of the stack / windows.
2816 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2817 resumeFocusedStackTopActivityLocked();
2818 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002819
Jorim Jaggid53f0922016-04-06 22:16:23 -07002820 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002821
2822 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002823 }
2824
Wale Ogunwale079a0042015-10-24 11:44:07 -07002825 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2826 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2827 if (stack == null) {
2828 throw new IllegalArgumentException(
2829 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2830 }
2831
2832 final ActivityRecord r = stack.topRunningActivityLocked();
2833 if (r == null) {
2834 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2835 + " in stack=" + stack);
2836 return false;
2837 }
2838
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002839 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002840 Slog.w(TAG,
2841 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002842 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002843 return false;
2844 }
2845
Winson Chungb5c41b72016-12-07 15:00:47 -08002846 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2847 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002848 return true;
2849 }
2850
Winson Chungb5c41b72016-12-07 15:00:47 -08002851 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2852 boolean moveHomeStackToFront) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002853
Wale Ogunwale480dca02016-02-06 13:58:29 -08002854 mWindowManager.deferSurfaceLayout();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002855 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002856 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002857
Wale Ogunwale480dca02016-02-06 13:58:29 -08002858 try {
2859 final TaskRecord task = r.task;
2860
Andrii Kulian02b7a832016-10-06 23:11:56 -07002861 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002862 // An activity can't be pinned and visible behind at the same time. Go ahead and
2863 // release it from been visible behind before pinning.
2864 requestVisibleBehindLocked(r, false);
2865 }
2866
Wale Ogunwale480dca02016-02-06 13:58:29 -08002867 // Resize the pinned stack to match the current size of the task the activity we are
2868 // going to be moving is currently contained in. We do this to have the right starting
2869 // animation bounds for the pinned stack to the desired bounds the caller wants.
2870 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2871 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002872 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002873
2874 if (task.mActivities.size() == 1) {
2875 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002876 // the stack without re-parenting the activity in a different task. We don't
2877 // move the home stack forward if we are currently entering picture-in-picture
2878 // while pausing because that changes the focused stack and may prevent the new
2879 // starting activity from resuming.
2880 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
2881 && !r.supportsPictureInPictureWhilePausing) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002882 // Move the home stack forward if the task we just moved to the pinned stack
2883 // was launched from home so home should be visible behind it.
2884 moveHomeStackToFront(reason);
2885 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002886 moveTaskToStackLocked(
2887 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2888 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002889 // There are multiple activities in the task and moving the top activity should
2890 // reveal/leave the other activities in their original task
Wale Ogunwale480dca02016-02-06 13:58:29 -08002891 stack.moveActivityToStack(r);
2892 }
Winson Chungc2baac02017-01-11 13:34:47 -08002893
2894 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2895 // to the pinned stack
2896 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002897 } finally {
2898 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002899 }
2900
Wale Ogunwale480dca02016-02-06 13:58:29 -08002901 // The task might have already been running and its visibility needs to be synchronized
2902 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002903 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002904 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002905
Winson Chung55893332017-02-17 17:13:10 -08002906 stack.animateResizePinnedStack(bounds, -1 /* animationDuration */);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002907 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002908 }
2909
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002910 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002911 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2912 if (r == null || !r.isFocusable()) {
2913 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2914 "moveActivityStackToFront: unfocusable r=" + r);
2915 return false;
2916 }
2917
2918 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002919 final ActivityStack stack = r.getStack();
2920 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002921 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2922 + r + " task=" + task);
2923 return false;
2924 }
2925
Chong Zhang6cda19c2016-06-14 19:07:56 -07002926 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2927 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2928 "moveActivityStackToFront: already on top, r=" + r);
2929 return false;
2930 }
2931
2932 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2933 "moveActivityStackToFront: r=" + r);
2934
2935 stack.moveToFront(reason, task);
2936 return true;
2937 }
2938
Craig Mautnerac6f8432013-07-17 13:24:59 -07002939 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002940 mTmpFindTaskResult.r = null;
2941 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002942 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002943 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2944 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002945 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2946 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002947 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002948 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002949 continue;
2950 }
2951 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002952 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2953 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002954 continue;
2955 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002956 stack.findTaskLocked(r, mTmpFindTaskResult);
2957 // It is possible to have task in multiple stacks with the same root affinity.
2958 // If the match we found was based on root affinity we keep on looking to see if
2959 // there is a better match in another stack. We eventually return the match based
2960 // on root affinity if we don't find a better match.
2961 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2962 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002963 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002964 }
2965 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002966 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2967 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002968 }
2969
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002970 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2971 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2973 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002974 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002975 final ActivityRecord ar = stacks.get(stackNdx)
2976 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 if (ar != null) {
2978 return ar;
2979 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002980 }
2981 }
2982 return null;
2983 }
2984
Craig Mautner8d341ef2013-03-26 09:03:27 -07002985 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002986 scheduleSleepTimeout();
2987 if (!mGoingToSleep.isHeld()) {
2988 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002989 if (mLaunchingActivity.isHeld()) {
2990 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2991 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002992 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002993 mLaunchingActivity.release();
2994 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002996 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002997 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002998 }
2999
3000 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003001 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003002
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003003 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003004 final long endTime = System.currentTimeMillis() + timeout;
3005 while (true) {
3006 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003007 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3008 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003009 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3010 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3011 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003012 }
3013 if (cantShutdown) {
3014 long timeRemaining = endTime - System.currentTimeMillis();
3015 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003016 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003018 } catch (InterruptedException e) {
3019 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003020 } else {
3021 Slog.w(TAG, "Activity manager shutdown timed out");
3022 timedout = true;
3023 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003024 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003025 } else {
3026 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 }
3028 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003029
3030 // Force checkReadyForSleep to complete.
3031 mSleepTimeout = true;
3032 checkReadyForSleepLocked();
3033
Craig Mautner8d341ef2013-03-26 09:03:27 -07003034 return timedout;
3035 }
3036
3037 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003038 removeSleepTimeouts();
3039 if (mGoingToSleep.isHeld()) {
3040 mGoingToSleep.release();
3041 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003042 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3043 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003044 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3045 final ActivityStack stack = stacks.get(stackNdx);
3046 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003047 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003048 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 }
Craig Mautner5314a402013-09-26 12:40:16 -07003050 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003051 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003052 mGoingToSleepActivities.clear();
3053 }
3054
3055 void activitySleptLocked(ActivityRecord r) {
3056 mGoingToSleepActivities.remove(r);
3057 checkReadyForSleepLocked();
3058 }
3059
3060 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003061 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003062 // Do not care.
3063 return;
3064 }
3065
3066 if (!mSleepTimeout) {
3067 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003068 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3069 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003070 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3071 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3072 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003073 }
3074
3075 if (mStoppingActivities.size() > 0) {
3076 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003077 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078 + mStoppingActivities.size() + " activities");
3079 scheduleIdleLocked();
3080 dontSleep = true;
3081 }
3082
3083 if (mGoingToSleepActivities.size() > 0) {
3084 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003085 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003086 + mGoingToSleepActivities.size() + " activities");
3087 dontSleep = true;
3088 }
3089
3090 if (dontSleep) {
3091 return;
3092 }
3093 }
3094
Wei Wang65c7a152016-06-02 18:51:22 -07003095 // Send launch end powerhint before going sleep
3096 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3097
Craig Mautnere0a38842013-12-16 16:14:02 -08003098 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3099 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3101 stacks.get(stackNdx).goToSleep();
3102 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003103 }
3104
3105 removeSleepTimeouts();
3106
3107 if (mGoingToSleep.isHeld()) {
3108 mGoingToSleep.release();
3109 }
3110 if (mService.mShuttingDown) {
3111 mService.notifyAll();
3112 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003113 }
3114
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003115 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003116 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003117 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003118 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003119 }
3120 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003121 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122 mWindowManager.executeAppTransition();
3123 return true;
3124 }
3125 return false;
3126 }
3127
Craig Mautner8d341ef2013-03-26 09:03:27 -07003128 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003129 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3130 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003131 int stackNdx = stacks.size() - 1;
3132 while (stackNdx >= 0) {
3133 stacks.get(stackNdx).handleAppCrashLocked(app);
3134 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003136 }
3137 }
3138
Jose Lima4b6c6692014-08-12 17:41:12 -07003139 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003140 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003141 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003142 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3143 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003144 return false;
3145 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003146
3147 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3148 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3149 + " visible=" + visible + " stackId=" + stack.mStackId
3150 + " can't contain visible behind activities");
3151 return false;
3152 }
3153
Jose Lima4b6c6692014-08-12 17:41:12 -07003154 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003155 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3156 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003157
3158 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003159 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003160 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003161 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003162 return true;
3163 }
3164
3165 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003166 if (visible && top.fullscreen) {
3167 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003168 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3169 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3170 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3171 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003172 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003173 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3174 // Only the activity set as currently visible behind should actively reset its
3175 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003176 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3177 "requestVisibleBehind: returning visible=" + visible
3178 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3179 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003180 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003181 }
3182
Jose Lima4b6c6692014-08-12 17:41:12 -07003183 stack.setVisibleBehindActivity(visible ? r : null);
3184 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003185 // If there is a translucent home activity, we need to force it stop being translucent,
3186 // because we can't depend on the application to necessarily perform that operation.
3187 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003188 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003189 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003190 mService.convertFromTranslucent(next.appToken);
3191 }
3192 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003193 if (top.app != null && top.app.thread != null) {
3194 // Notify the top app of the change.
3195 try {
3196 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3197 } catch (RemoteException e) {
3198 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003199 }
3200 return true;
3201 }
3202
Craig Mautnerbb742462014-07-07 15:28:55 -07003203 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003204 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003205 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003206 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003207
3208 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003209 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003210 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003211 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003212 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003213
3214 // When launching tasks behind, update the last active time of the top task after the new
3215 // task has been shown briefly
3216 final ActivityRecord top = stack.topActivity();
3217 if (top != null) {
3218 top.task.touchActiveTime();
3219 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003220 }
3221
3222 void scheduleLaunchTaskBehindComplete(IBinder token) {
3223 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3224 }
3225
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003226 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3227 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003228 mKeyguardController.beginActivityVisibilityUpdate();
3229 try {
3230 // First the front stacks. In case any are not fullscreen and are in front of home.
3231 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3232 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3233 final int topStackNdx = stacks.size() - 1;
3234 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3235 final ActivityStack stack = stacks.get(stackNdx);
3236 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3237 }
Craig Mautner580ea812013-04-25 12:58:38 -07003238 }
Jorim Jaggife762342016-10-13 14:33:27 +02003239 } finally {
3240 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003241 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003242 }
3243
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003244 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3245 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3246 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3247 final int topStackNdx = stacks.size() - 1;
3248 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3249 final ActivityStack stack = stacks.get(stackNdx);
3250 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3251 }
3252 }
3253 }
3254
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003255 void invalidateTaskLayers() {
3256 mTaskLayersChanged = true;
3257 }
3258
3259 void rankTaskLayersIfNeeded() {
3260 if (!mTaskLayersChanged) {
3261 return;
3262 }
3263 mTaskLayersChanged = false;
3264 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3265 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3266 int baseLayer = 0;
3267 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3268 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3269 }
3270 }
3271 }
3272
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003273 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3274 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3275 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3276 final int topStackNdx = stacks.size() - 1;
3277 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3278 final ActivityStack stack = stacks.get(stackNdx);
3279 stack.clearOtherAppTimeTrackers(except);
3280 }
3281 }
3282 }
3283
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003285 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3286 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003287 final int numStacks = stacks.size();
3288 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3289 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003290 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003292 }
3293 }
3294
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003295 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3296 // Examine all activities currently running in the process.
3297 TaskRecord firstTask = null;
3298 // Tasks is non-null only if two or more tasks are found.
3299 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003300 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3301 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003302 ActivityRecord r = app.activities.get(i);
3303 // First, if we find an activity that is in the process of being destroyed,
3304 // then we just aren't going to do anything for now; we want things to settle
3305 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003306 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003307 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003308 return;
3309 }
3310 // Don't consider any activies that are currently not in a state where they
3311 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003312 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3313 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003314 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003315 continue;
3316 }
3317 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003318 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003319 + " from " + r);
3320 if (firstTask == null) {
3321 firstTask = r.task;
3322 } else if (firstTask != r.task) {
3323 if (tasks == null) {
3324 tasks = new ArraySet<>();
3325 tasks.add(firstTask);
3326 }
3327 tasks.add(r.task);
3328 }
3329 }
3330 }
3331 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003332 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003333 return;
3334 }
3335 // If we have activities in multiple tasks that are in a position to be destroyed,
3336 // let's iterate through the tasks and release the oldest one.
3337 final int numDisplays = mActivityDisplays.size();
3338 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3339 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3340 // Step through all stacks starting from behind, to hit the oldest things first.
3341 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3342 final ActivityStack stack = stacks.get(stackNdx);
3343 // Try to release activities in this stack; if we manage to, we are done.
3344 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3345 return;
3346 }
3347 }
3348 }
3349 }
3350
Amith Yamasani37a40c22015-06-17 13:25:42 -07003351 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003352 final int focusStackId = mFocusedStack.getStackId();
3353 // We dismiss the docked stack whenever we switch users.
3354 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003355 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3356 // also cause all tasks to be moved to the fullscreen stack at a position that is
3357 // appropriate.
3358 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003359
3360 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003361 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003362 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003363
Craig Mautner858d8a62013-04-23 17:08:34 -07003364 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003365 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3366 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003367 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003368 final ActivityStack stack = stacks.get(stackNdx);
3369 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003370 TaskRecord task = stack.topTask();
3371 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003372 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003373 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003375 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003376
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003377 ActivityStack stack = getStack(restoreStackId);
3378 if (stack == null) {
3379 stack = mHomeStack;
3380 }
3381 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003382 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003383 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003384 } else {
3385 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003386 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003387 }
Craig Mautner93529a42013-10-04 15:03:13 -07003388 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003389 }
3390
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003391 /** Checks whether the userid is a profile of the current user. */
3392 boolean isCurrentProfileLocked(int userId) {
3393 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003394 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003395 }
3396
Winson Chung4dabf232017-01-25 13:25:22 -08003397 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3398 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003399 ArrayList<ActivityRecord> stops = null;
3400
3401 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003402 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3403 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003404 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3405 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003406 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003407 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003408 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3409 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003410 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003411 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003412 if (s.finishing) {
3413 // If this activity is finishing, it is sitting on top of
3414 // everyone else but we now know it is no longer needed...
3415 // so get rid of it. Otherwise, we need to go through the
3416 // normal flow and hide it once we determine that it is
3417 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003418 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003419 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003420 }
3421 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003422 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003423 if (!processPausingActivities && s.state == PAUSING) {
3424 // Defer processing pausing activities in this iteration and reschedule
3425 // a delayed idle to reprocess it again
3426 removeTimeoutsForActivityLocked(idleActivity);
3427 scheduleIdleTimeoutLocked(idleActivity);
3428 continue;
3429 }
3430
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003431 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003432 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003433 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003434 }
3435 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003436 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003437 }
3438 }
3439
3440 return stops;
3441 }
3442
Craig Mautnercf910b02013-04-23 11:23:27 -07003443 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003444 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3445 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3446 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3447 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003448 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003449 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003450 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003451 if (r == null) Slog.e(TAG,
3452 "validateTop...: null top activity, stack=" + stack);
3453 else {
3454 final ActivityRecord pausing = stack.mPausingActivity;
3455 if (pausing != null && pausing == r) Slog.e(TAG,
3456 "validateTop...: top stack has pausing activity r=" + r
3457 + " state=" + state);
3458 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3459 "validateTop...: activity in front not resumed r=" + r
3460 + " state=" + state);
3461 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003462 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003463 final ActivityRecord resumed = stack.mResumedActivity;
3464 if (resumed != null && resumed == r) Slog.e(TAG,
3465 "validateTop...: back stack has resumed activity r=" + r
3466 + " state=" + state);
3467 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3468 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003469 }
3470 }
3471 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003472 }
3473
Craig Mautnere0570202015-05-13 13:06:11 -07003474 private String lockTaskModeToString() {
3475 switch (mLockTaskModeState) {
3476 case LOCK_TASK_MODE_LOCKED:
3477 return "LOCKED";
3478 case LOCK_TASK_MODE_PINNED:
3479 return "PINNED";
3480 case LOCK_TASK_MODE_NONE:
3481 return "NONE";
3482 default: return "unknown=" + mLockTaskModeState;
3483 }
3484 }
3485
Craig Mautner27084302013-03-25 08:05:25 -07003486 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003487 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003488 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003489 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003490 pw.print(prefix);
3491 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003492 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003493 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003494 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003495 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3496 if (packages.size() > 0) {
3497 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3498 for (int i = 0; i < packages.size(); ++i) {
3499 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3500 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3501 }
3502 }
3503 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3504 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003505 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003506
Winson43d1f262016-06-14 16:05:55 -07003507 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003508 * Dump all connected displays' configurations.
3509 * @param prefix Prefix to apply to each line of the dump.
3510 */
3511 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3512 pw.print(prefix); pw.println("Display override configurations:");
3513 final int displayCount = mActivityDisplays.size();
3514 for (int i = 0; i < displayCount; i++) {
3515 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3516 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3517 pw.println(activityDisplay.getOverrideConfiguration());
3518 }
3519 }
3520
3521 /**
Winson43d1f262016-06-14 16:05:55 -07003522 * Dumps the activities matching the given {@param name} in the either the focused stack
3523 * or all visible stacks if {@param dumpVisibleStacks} is true.
3524 */
3525 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3526 if (dumpVisibleStacks) {
3527 ArrayList<ActivityRecord> activities = new ArrayList<>();
3528 int numDisplays = mActivityDisplays.size();
3529 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3530 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3531 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3532 ActivityStack stack = stacks.get(stackNdx);
3533 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3534 activities.addAll(stack.getDumpActivitiesLocked(name));
3535 }
3536 }
3537 }
3538 return activities;
3539 } else {
3540 return mFocusedStack.getDumpActivitiesLocked(name);
3541 }
Craig Mautner20e72272013-04-01 13:45:53 -07003542 }
3543
Dianne Hackborn390517b2013-05-30 15:03:32 -07003544 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3545 boolean needSep, String prefix) {
3546 if (activity != null) {
3547 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3548 if (needSep) {
3549 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003550 }
3551 pw.print(prefix);
3552 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003553 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003554 }
3555 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003556 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003557 }
3558
Craig Mautner8d341ef2013-03-26 09:03:27 -07003559 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3560 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003561 boolean printed = false;
3562 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003563 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3564 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003565 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003566 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003567 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003568 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003569 final ActivityStack stack = stacks.get(stackNdx);
3570 StringBuilder stackHeader = new StringBuilder(128);
3571 stackHeader.append(" Stack #");
3572 stackHeader.append(stack.mStackId);
3573 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003574 stackHeader.append("\n");
3575 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3576 stackHeader.append("\n");
3577 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3579 needSep, stackHeader.toString());
3580 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3581 !dumpAll, false, dumpPackage, true,
3582 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003583
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 needSep = printed;
3585 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3586 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003587 if (pr) {
3588 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003590 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3592 " mResumedActivity: ");
3593 if (pr) {
3594 printed = true;
3595 needSep = false;
3596 }
3597 if (dumpAll) {
3598 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3599 " mLastPausedActivity: ");
3600 if (pr) {
3601 printed = true;
3602 needSep = true;
3603 }
3604 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3605 needSep, " mLastNoHistoryActivity: ");
3606 }
3607 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003608 }
3609 }
3610
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003611 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3612 false, dumpPackage, true, " Activities waiting to finish:", null);
3613 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3614 false, dumpPackage, true, " Activities waiting to stop:", null);
3615 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3616 false, dumpPackage, true, " Activities waiting for another to become visible:",
3617 null);
3618 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3619 false, dumpPackage, true, " Activities waiting to sleep:", null);
3620 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3621 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003622
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003623 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003624 }
3625
Dianne Hackborn390517b2013-05-30 15:03:32 -07003626 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003627 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003628 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003630 String innerPrefix = null;
3631 String[] args = null;
3632 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003633 for (int i=list.size()-1; i>=0; i--) {
3634 final ActivityRecord r = list.get(i);
3635 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3636 continue;
3637 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003638 if (innerPrefix == null) {
3639 innerPrefix = prefix + " ";
3640 args = new String[0];
3641 }
3642 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003643 final boolean full = !brief && (complete || !r.isInHistory());
3644 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003645 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003646 needNL = false;
3647 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003648 if (header1 != null) {
3649 pw.println(header1);
3650 header1 = null;
3651 }
3652 if (header2 != null) {
3653 pw.println(header2);
3654 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003655 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003656 if (lastTask != r.task) {
3657 lastTask = r.task;
3658 pw.print(prefix);
3659 pw.print(full ? "* " : " ");
3660 pw.println(lastTask);
3661 if (full) {
3662 lastTask.dump(pw, prefix + " ");
3663 } else if (complete) {
3664 // Complete + brief == give a summary. Isn't that obvious?!?
3665 if (lastTask.intent != null) {
3666 pw.print(prefix); pw.print(" ");
3667 pw.println(lastTask.intent.toInsecureStringWithClip());
3668 }
3669 }
3670 }
3671 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3672 pw.print(" #"); pw.print(i); pw.print(": ");
3673 pw.println(r);
3674 if (full) {
3675 r.dump(pw, innerPrefix);
3676 } else if (complete) {
3677 // Complete + brief == give a summary. Isn't that obvious?!?
3678 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3679 if (r.app != null) {
3680 pw.print(innerPrefix); pw.println(r.app);
3681 }
3682 }
3683 if (client && r.app != null && r.app.thread != null) {
3684 // flush anything that is already in the PrintWriter since the thread is going
3685 // to write to the file descriptor directly
3686 pw.flush();
3687 try {
3688 TransferPipe tp = new TransferPipe();
3689 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003690 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003691 // Short timeout, since blocking here can
3692 // deadlock with the application.
3693 tp.go(fd, 2000);
3694 } finally {
3695 tp.kill();
3696 }
3697 } catch (IOException e) {
3698 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3699 } catch (RemoteException e) {
3700 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3701 }
3702 needNL = true;
3703 }
3704 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003705 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003706 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003707
Craig Mautnerf3333272013-04-22 10:55:53 -07003708 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003709 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3710 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003711 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3712 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003713 }
3714
3715 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003716 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003717 }
3718
3719 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003720 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3721 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003722 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3723 }
3724
Craig Mautner05d29032013-05-03 13:40:13 -07003725 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003726 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3727 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3728 }
Craig Mautner05d29032013-05-03 13:40:13 -07003729 }
3730
Craig Mautner0eea92c2013-05-16 13:35:39 -07003731 void removeSleepTimeouts() {
3732 mSleepTimeout = false;
3733 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3734 }
3735
3736 final void scheduleSleepTimeout() {
3737 removeSleepTimeouts();
3738 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3739 }
3740
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 @Override
3742 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003743 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003744 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3745 }
3746
3747 @Override
3748 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003749 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003750 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3751 }
3752
3753 @Override
3754 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003755 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003756 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3757 }
3758
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003759 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003760 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003762 newDisplay = mActivityDisplays.get(displayId) == null;
3763 if (newDisplay) {
3764 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003765 if (activityDisplay.mDisplay == null) {
3766 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3767 return;
3768 }
Craig Mautner4504de52013-12-20 09:06:56 -08003769 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003770 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003771 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003772 }
Craig Mautner4504de52013-12-20 09:06:56 -08003773 if (newDisplay) {
3774 mWindowManager.onDisplayAdded(displayId);
3775 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 }
3777
Andrii Kulianca007a62016-11-22 16:33:28 -08003778 /** Check if display with specified id is added to the list. */
3779 boolean isDisplayAdded(int displayId) {
3780 return mActivityDisplays.get(displayId) != null;
3781 }
3782
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003783 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003784 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003785 mService.mContext.getResources().getDimensionPixelSize(
3786 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003787 }
3788
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003789 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003790 if (displayId == DEFAULT_DISPLAY) {
3791 throw new IllegalArgumentException("Can't remove the primary display.");
3792 }
3793
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003795 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3796 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003797 final boolean destroyContentOnRemoval
3798 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003799 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3800 while (!stacks.isEmpty()) {
3801 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003802 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003803 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3804 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003805 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003806 } else {
3807 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3808 // a valid launch stack for all activities. This way the task history from
3809 // external display will be preserved on primary after move.
3810 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003811 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003813 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003814 }
3815 }
3816 mWindowManager.onDisplayRemoved(displayId);
3817 }
3818
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003819 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003821 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3822 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 // TODO: Update the bounds.
3824 }
3825 }
3826 mWindowManager.onDisplayChanged(displayId);
3827 }
3828
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003829 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003830 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003832 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003833 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003835 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003836 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003837 info.position = display != null
3838 ? display.mStacks.indexOf(stack)
3839 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840
3841 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3842 final int numTasks = tasks.size();
3843 int[] taskIds = new int[numTasks];
3844 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003845 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003846 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 for (int i = 0; i < numTasks; ++i) {
3848 final TaskRecord task = tasks.get(i);
3849 taskIds[i] = task.taskId;
3850 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3851 : task.realActivity != null ? task.realActivity.flattenToString()
3852 : task.getTopActivity() != null ? task.getTopActivity().packageName
3853 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003854 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003855 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003856 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003857 }
3858 info.taskIds = taskIds;
3859 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003860 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003861 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003862
3863 final ActivityRecord top = stack.topRunningActivityLocked();
3864 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 return info;
3866 }
3867
3868 StackInfo getStackInfoLocked(int stackId) {
3869 ActivityStack stack = getStack(stackId);
3870 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003871 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 }
3873 return null;
3874 }
3875
3876 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003877 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003878 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3879 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003881 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003882 }
3883 }
3884 return list;
3885 }
3886
Craig Mautner15df08a2015-04-01 12:17:18 -07003887 TaskRecord getLockedTaskLocked() {
3888 final int top = mLockTaskModeTasks.size() - 1;
3889 if (top >= 0) {
3890 return mLockTaskModeTasks.get(top);
3891 }
3892 return null;
3893 }
3894
3895 boolean isLockedTask(TaskRecord task) {
3896 return mLockTaskModeTasks.contains(task);
3897 }
3898
3899 boolean isLastLockedTask(TaskRecord task) {
3900 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3901 }
3902
3903 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003904 if (!mLockTaskModeTasks.remove(task)) {
3905 return;
3906 }
3907 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3908 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003909 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003910 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3911 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003912 final Message lockTaskMsg = Message.obtain();
3913 lockTaskMsg.arg1 = task.userId;
3914 lockTaskMsg.what = LOCK_TASK_END_MSG;
3915 mHandler.sendMessage(lockTaskMsg);
3916 }
3917 }
3918
Andrii Kulianc27916642016-04-12 17:59:27 -07003919 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3920 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3921 false /* forceNonResizable */);
3922 }
3923
Jorim Jaggid53f0922016-04-06 22:16:23 -07003924 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003925 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003926 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3927 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003928 return;
3929 }
3930
Jorim Jaggicd13d332016-04-27 15:40:20 -07003931 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003932 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003933 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003934 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003935
Andrii Kulianc27916642016-04-12 17:59:27 -07003936 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3937 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003938 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003939 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003940 && !topActivity.noDisplay) {
3941 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003942 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3943 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003944 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003945 }
3946
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003947 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003948 if (mLockTaskNotify != null) {
3949 mLockTaskNotify.showToast(mLockTaskModeState);
3950 }
Jason Monka8f569c2014-07-07 12:15:21 -04003951 }
3952
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003953 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3954 if (mLockTaskModeTasks.contains(task)) {
3955 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3956 }
3957 }
3958
Craig Mautner432f64e2015-05-20 14:59:57 -07003959 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3960 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003961 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003962 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003963 final TaskRecord lockedTask = getLockedTaskLocked();
3964 if (lockedTask != null) {
3965 removeLockedTaskLocked(lockedTask);
3966 if (!mLockTaskModeTasks.isEmpty()) {
3967 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003968 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3969 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003971 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003972 return;
3973 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003974 }
Craig Mautnere0570202015-05-13 13:06:11 -07003975 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3976 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003977 return;
3978 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003979
3980 // Should have already been checked, but do it again.
3981 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003982 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3983 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003984 return;
3985 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003986 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003987 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003988 return;
3989 }
3990
3991 if (mLockTaskModeTasks.isEmpty()) {
3992 // First locktask.
3993 final Message lockTaskMsg = Message.obtain();
3994 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3995 lockTaskMsg.arg1 = task.userId;
3996 lockTaskMsg.what = LOCK_TASK_START_MSG;
3997 lockTaskMsg.arg2 = lockTaskModeState;
3998 mHandler.sendMessage(lockTaskMsg);
3999 }
4000 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004001 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4002 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004003 mLockTaskModeTasks.remove(task);
4004 mLockTaskModeTasks.add(task);
4005
4006 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004007 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004008 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004009
4010 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004011 findTaskToMoveToFrontLocked(task, 0, null, reason,
4012 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004013 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004014 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004015 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004016 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004017 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004018 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004019 }
4020
4021 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004022 return isLockTaskModeViolation(task, false);
4023 }
4024
4025 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4026 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004027 return false;
4028 }
4029 final int lockTaskAuth = task.mLockTaskAuth;
4030 switch (lockTaskAuth) {
4031 case LOCK_TASK_AUTH_DONT_LOCK:
4032 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004033 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004034 case LOCK_TASK_AUTH_LAUNCHABLE:
4035 case LOCK_TASK_AUTH_WHITELISTED:
4036 return false;
4037 case LOCK_TASK_AUTH_PINNABLE:
4038 // Pinnable tasks can't be launched on top of locktask tasks.
4039 return !mLockTaskModeTasks.isEmpty();
4040 default:
4041 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4042 return true;
4043 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004044 }
4045
Craig Mautner15df08a2015-04-01 12:17:18 -07004046 void onLockTaskPackagesUpdatedLocked() {
4047 boolean didSomething = false;
4048 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4049 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004050 final boolean wasWhitelisted =
4051 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4052 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004053 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004054 final boolean isWhitelisted =
4055 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4056 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4057 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004058 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004059 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4060 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004061 removeLockedTaskLocked(lockedTask);
4062 lockedTask.performClearTaskLocked();
4063 didSomething = true;
4064 }
4065 }
4066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4067 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4069 final ActivityStack stack = stacks.get(stackNdx);
4070 stack.onLockTaskPackagesUpdatedLocked();
4071 }
4072 }
Craig Mautnere0570202015-05-13 13:06:11 -07004073 final ActivityRecord r = topRunningActivityLocked();
4074 final TaskRecord task = r != null ? r.task : null;
4075 if (mLockTaskModeTasks.isEmpty() && task != null
4076 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4077 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004078 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4079 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4080 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4081 false);
4082 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004083 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004084 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004085 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004086 }
4087 }
4088
Benjamin Franz43261142015-02-11 15:59:44 +00004089 int getLockTaskModeState() {
4090 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004091 }
4092
Jorim Jaggife89d122015-12-22 16:28:44 +01004093 void activityRelaunchedLocked(IBinder token) {
4094 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004095 if (mService.isSleepingOrShuttingDownLocked()) {
4096 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4097 if (r != null) {
4098 r.setSleeping(true, true);
4099 }
4100 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004101 }
4102
4103 void activityRelaunchingLocked(ActivityRecord r) {
4104 mWindowManager.notifyAppRelaunching(r.appToken);
4105 }
4106
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004107 void logStackState() {
4108 mActivityMetricsLogger.logWindowState();
4109 }
4110
Andrii Kulian933076d2016-03-29 17:04:42 -07004111 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004112 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4113 final ActivityRecord r = task.mActivities.get(i);
4114 if (r.app != null && r.app.thread != null) {
4115 mMultiWindowModeChangedActivities.add(r);
4116 }
4117 }
4118
4119 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4120 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4121 }
4122 }
4123
Andrii Kulian933076d2016-03-29 17:04:42 -07004124 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004125 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004126 if (prevStack == null || prevStack == stack
4127 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4128 return;
4129 }
4130
4131 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4132 final ActivityRecord r = task.mActivities.get(i);
4133 if (r.app != null && r.app.thread != null) {
4134 mPipModeChangedActivities.add(r);
4135 }
4136 }
4137
4138 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4139 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4140 }
4141 }
4142
Tony Mak853304c2016-04-18 15:17:41 +01004143 void setDockedStackMinimized(boolean minimized) {
4144 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004145 }
4146
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004147 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004148
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004149 public ActivityStackSupervisorHandler(Looper looper) {
4150 super(looper);
4151 }
4152
Winson Chung4dabf232017-01-25 13:25:22 -08004153 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004154 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004155 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4156 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004157 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004158 }
4159
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004160 @Override
4161 public void handleMessage(Message msg) {
4162 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004163 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4164 synchronized (mService) {
4165 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4166 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004167 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004168 }
4169 }
4170 } break;
4171 case REPORT_PIP_MODE_CHANGED_MSG: {
4172 synchronized (mService) {
4173 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4174 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004175 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004176 }
4177 }
4178 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004179 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004180 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4181 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004182 if (mService.mDidDexOpt) {
4183 mService.mDidDexOpt = false;
4184 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4185 nmsg.obj = msg.obj;
4186 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4187 return;
4188 }
4189 // We don't at this point know if the activity is fullscreen,
4190 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004191 activityIdleInternal((ActivityRecord) msg.obj,
4192 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004193 } break;
4194 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004195 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004196 activityIdleInternal((ActivityRecord) msg.obj,
4197 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004198 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004199 case RESUME_TOP_ACTIVITY_MSG: {
4200 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004201 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004202 }
4203 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004204 case SLEEP_TIMEOUT_MSG: {
4205 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004206 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004207 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4208 mSleepTimeout = true;
4209 checkReadyForSleepLocked();
4210 }
4211 }
4212 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004213 case LAUNCH_TIMEOUT_MSG: {
4214 if (mService.mDidDexOpt) {
4215 mService.mDidDexOpt = false;
4216 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4217 return;
4218 }
4219 synchronized (mService) {
4220 if (mLaunchingActivity.isHeld()) {
4221 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4222 if (VALIDATE_WAKE_LOCK_CALLER
4223 && Binder.getCallingUid() != Process.myUid()) {
4224 throw new IllegalStateException("Calling must be system uid");
4225 }
4226 mLaunchingActivity.release();
4227 }
4228 }
4229 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004231 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232 } break;
4233 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004234 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004235 } break;
4236 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004237 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004238 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004239 case CONTAINER_CALLBACK_VISIBILITY: {
4240 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004241 final IActivityContainerCallback callback = container.mCallback;
4242 if (callback != null) {
4243 try {
4244 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4245 } catch (RemoteException e) {
4246 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004247 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004248 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004249 case LOCK_TASK_START_MSG: {
4250 // When lock task starts, we disable the status bars.
4251 try {
Jason Monk62515be2014-05-21 16:06:19 -04004252 if (mLockTaskNotify == null) {
4253 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004254 }
Jason Monk62515be2014-05-21 16:06:19 -04004255 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004256 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004257 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004258 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004259 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004260 flags = StatusBarManager.DISABLE_MASK
4261 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004262 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004263 flags = StatusBarManager.DISABLE_MASK
4264 & (~StatusBarManager.DISABLE_BACK)
4265 & (~StatusBarManager.DISABLE_HOME)
4266 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004267 }
4268 getStatusBarService().disable(flags, mToken,
4269 mService.mContext.getPackageName());
4270 }
4271 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004272 if (getDevicePolicyManager() != null) {
4273 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004274 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004275 }
justinzhang5286d3f2014-05-12 17:06:01 -04004276 } catch (RemoteException ex) {
4277 throw new RuntimeException(ex);
4278 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004279 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004280 case LOCK_TASK_END_MSG: {
4281 // When lock task ends, we enable the status bars.
4282 try {
Jason Monk62515be2014-05-21 16:06:19 -04004283 if (getStatusBarService() != null) {
4284 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4285 mService.mContext.getPackageName());
4286 }
4287 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004288 if (getDevicePolicyManager() != null) {
4289 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4290 msg.arg1);
4291 }
Jason Monk62515be2014-05-21 16:06:19 -04004292 if (mLockTaskNotify == null) {
4293 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4294 }
4295 mLockTaskNotify.show(false);
4296 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004297 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004298 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004299 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004300 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004301 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004302 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004303 new LockPatternUtils(mService.mContext)
4304 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004305 }
4306 } catch (SettingNotFoundException e) {
4307 // No setting, don't lock.
4308 }
justinzhang5286d3f2014-05-12 17:06:01 -04004309 } catch (RemoteException ex) {
4310 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004311 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004312 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004313 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004314 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004315 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4316 if (mLockTaskNotify == null) {
4317 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4318 }
4319 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4320 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004321 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4322 final ActivityContainer container = (ActivityContainer) msg.obj;
4323 final IActivityContainerCallback callback = container.mCallback;
4324 if (callback != null) {
4325 try {
4326 callback.onAllActivitiesComplete(container.asBinder());
4327 } catch (RemoteException e) {
4328 }
4329 }
4330 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004331 case LAUNCH_TASK_BEHIND_COMPLETE: {
4332 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004333 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004334 if (r != null) {
4335 handleLaunchTaskBehindCompleteLocked(r);
4336 }
4337 }
4338 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004339
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004340 }
4341 }
4342 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004343
Ying Wangb081a592014-04-22 15:20:16 -07004344 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004345 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4346 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004348 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004349 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004350 ActivityRecord mParentActivity = null;
4351 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004352
Craig Mautnere3a00d72014-04-16 08:31:19 -07004353 boolean mVisible = true;
4354
Craig Mautner4a1cb222013-12-04 16:14:06 -08004355 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004356 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004357
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004358 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4359 final static int CONTAINER_STATE_NO_SURFACE = 1;
4360 final static int CONTAINER_STATE_FINISHING = 2;
4361 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4362
Wale Ogunwale1666e312016-12-16 11:27:18 -08004363 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004364 synchronized (mService) {
4365 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004366 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004367 switch (mStackId) {
4368 case PINNED_STACK_ID:
4369 new PinnedActivityStack(this, mRecentTasks, onTop);
4370 break;
4371 default:
4372 new ActivityStack(this, mRecentTasks, onTop);
4373 break;
4374 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004375 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004376 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004377 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004378 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004379
Andrii Kulian839def92016-11-02 10:58:58 -07004380 /**
4381 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004382 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004383 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004384 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004385 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004386 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004387 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004388 if (mActivityDisplay != null) {
4389 throw new IllegalStateException("ActivityContainer is already attached, " +
4390 "displayId=" + mActivityDisplay.mDisplayId);
4391 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004392 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004393 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004394 }
4395
4396 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004397 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004398 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004399 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4400 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004401 return;
4402 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004403 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004404 }
4405 }
4406
4407 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004408 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004409 synchronized (mService) {
4410 if (mActivityDisplay != null) {
4411 return mActivityDisplay.mDisplayId;
4412 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004413 }
4414 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004415 }
4416
Jeff Brownca9bc702014-02-11 14:32:56 -08004417 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004418 public int getStackId() {
4419 synchronized (mService) {
4420 return mStackId;
4421 }
4422 }
4423
4424 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004425 public boolean injectEvent(InputEvent event) {
4426 final long origId = Binder.clearCallingIdentity();
4427 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004428 synchronized (mService) {
4429 if (mActivityDisplay != null) {
4430 return mInputManagerInternal.injectInputEvent(event,
4431 mActivityDisplay.mDisplayId,
4432 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4433 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004434 }
4435 return false;
4436 } finally {
4437 Binder.restoreCallingIdentity(origId);
4438 }
4439 }
4440
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004441 @Override
4442 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004443 synchronized (mService) {
4444 if (mContainerState == CONTAINER_STATE_FINISHING) {
4445 return;
4446 }
4447 mContainerState = CONTAINER_STATE_FINISHING;
4448
Craig Mautnerd163e752014-06-13 17:18:47 -07004449 long origId = Binder.clearCallingIdentity();
4450 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004451 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004452 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004453 } finally {
4454 Binder.restoreCallingIdentity(origId);
4455 }
4456 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004457 }
4458
Andrii Kulian03c403d2016-11-11 11:14:12 -08004459 /**
4460 * Remove the stack completely. Must be called only when there are no tasks left in it,
4461 * as this method does not finish running activities.
4462 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004463 void removeLocked() {
4464 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004465 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004466 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004467 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004468 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004469 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470 }
4471
Andrii Kulian839def92016-11-02 10:58:58 -07004472 /**
4473 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4474 * completely or re-parented.
4475 */
4476 private void removeFromDisplayLocked() {
4477 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4478 + " current displayId=" + mActivityDisplay.mDisplayId);
4479
Wale Ogunwale1666e312016-12-16 11:27:18 -08004480 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004481 mActivityDisplay = null;
4482 }
4483
4484 /**
4485 * Move the stack to specified display.
4486 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004487 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004488 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004489 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004490 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4491 + mActivityDisplay + " to display=" + activityDisplay
4492 + " Callers=" + Debug.getCallers(2));
4493
4494 removeFromDisplayLocked();
4495
4496 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004497 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004498 }
4499
Craig Mautner4a1cb222013-12-04 16:14:06 -08004500 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004501 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004502 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004503 }
4504
4505 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004506 public final int startActivityIntentSender(IIntentSender intentSender)
4507 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004508 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4509
4510 if (!(intentSender instanceof PendingIntentRecord)) {
4511 throw new IllegalArgumentException("Bad PendingIntent object");
4512 }
4513
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004514 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004515 Binder.getCallingUid(), mCurrentUser, false,
4516 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004517
4518 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4519 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4520 pendingIntent.key.requestResolvedType);
4521
4522 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4523 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4524 }
4525
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004526 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004527 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004528 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004529 throw new SecurityException(
4530 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4531 }
4532 }
4533
Craig Mautnerdf88d732014-01-27 09:21:32 -08004534 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004535 public IBinder asBinder() {
4536 return this;
4537 }
4538
Craig Mautner4504de52013-12-20 09:06:56 -08004539 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004540 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004541 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004542 }
4543
Craig Mautner4a1cb222013-12-04 16:14:06 -08004544 ActivityStackSupervisor getOuter() {
4545 return ActivityStackSupervisor.this;
4546 }
4547
Craig Mautnerd163e752014-06-13 17:18:47 -07004548 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004549 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004550 }
4551
Craig Mautner6985bad2014-04-21 15:22:06 -07004552 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004553 void setVisible(boolean visible) {
4554 if (mVisible != visible) {
4555 mVisible = visible;
4556 if (mCallback != null) {
4557 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4558 0 /* unused */, this).sendToTarget();
4559 }
4560 }
4561 }
4562
Craig Mautner6985bad2014-04-21 15:22:06 -07004563 void setDrawn() {
4564 }
4565
Craig Mautner1b4bf852014-05-26 15:06:32 -07004566 // You can always start a new task on a regular ActivityStack.
4567 boolean isEligibleForNewTasks() {
4568 return true;
4569 }
4570
Craig Mautnerd163e752014-06-13 17:18:47 -07004571 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004572 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004573 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004574 }
4575
Craig Mautner34b73df2014-01-12 21:11:08 -08004576 @Override
4577 public String toString() {
4578 return mIdString + (mActivityDisplay == null ? "N" : "A");
4579 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004580 }
4581
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004582 private class VirtualActivityContainer extends ActivityContainer {
4583 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004584 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004585
4586 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004587 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4588 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004589 mParentActivity = parent;
4590 mCallback = callback;
4591 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004592 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004593 }
4594
4595 @Override
4596 public void setSurface(Surface surface, int width, int height, int density) {
4597 super.setSurface(surface, width, height, density);
4598
4599 synchronized (mService) {
4600 final long origId = Binder.clearCallingIdentity();
4601 try {
4602 setSurfaceLocked(surface, width, height, density);
4603 } finally {
4604 Binder.restoreCallingIdentity(origId);
4605 }
4606 }
4607 }
4608
4609 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4610 if (mContainerState == CONTAINER_STATE_FINISHING) {
4611 return;
4612 }
4613 VirtualActivityDisplay virtualActivityDisplay =
4614 (VirtualActivityDisplay) mActivityDisplay;
4615 if (virtualActivityDisplay == null) {
4616 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004617 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004618 mActivityDisplay = virtualActivityDisplay;
4619 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004620 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004621 }
4622
4623 if (mSurface != null) {
4624 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004625 }
4626
Craig Mautner6985bad2014-04-21 15:22:06 -07004627 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004628 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004629 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004630 } else {
4631 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004632 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004633 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004634 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004635 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004637
Craig Mautnerd163e752014-06-13 17:18:47 -07004638 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004639
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004640 if (DEBUG_STACK) Slog.d(TAG_STACK,
4641 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004642 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004643
Craig Mautner6985bad2014-04-21 15:22:06 -07004644 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004645 boolean isAttachedLocked() {
4646 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004647 }
4648
4649 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004650 void setDrawn() {
4651 synchronized (mService) {
4652 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004653 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004654 }
4655 }
4656
Craig Mautner1b4bf852014-05-26 15:06:32 -07004657 // Never start a new task on an ActivityView if it isn't explicitly specified.
4658 @Override
4659 boolean isEligibleForNewTasks() {
4660 return false;
4661 }
4662
Craig Mautnerd163e752014-06-13 17:18:47 -07004663 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004664 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004665 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4666 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4667 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4668 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4669 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004670 }
4671 }
4672
Craig Mautner4a1cb222013-12-04 16:14:06 -08004673 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4674 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004675 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004676 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004677 int mDisplayId;
4678 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004679
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4681 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004682 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004683
Jose Lima4b6c6692014-08-12 17:41:12 -07004684 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004685
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004686 /** Array of all UIDs that are present on the display. */
4687 private IntArray mDisplayAccessUIDs = new IntArray();
4688
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004689 ActivityDisplay() {
4690 }
Craig Mautner4504de52013-12-20 09:06:56 -08004691
Craig Mautner1a70a162014-09-13 12:09:31 -07004692 // After instantiation, check that mDisplay is not null before using this. The alternative
4693 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004694 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004695 final Display display = mDisplayManager.getDisplay(displayId);
4696 if (display == null) {
4697 return;
4698 }
4699 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004700 }
4701
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004702 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004703 mDisplay = display;
4704 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004705 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004706
Wale Ogunwale1666e312016-12-16 11:27:18 -08004707 void attachStack(ActivityStack stack, boolean onTop) {
4708 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
4709 + " to displayId=" + mDisplayId + " onTop=" + onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004710 if (onTop) {
4711 mStacks.add(stack);
4712 } else {
4713 mStacks.add(0, stack);
4714 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004715 }
4716
Wale Ogunwale1666e312016-12-16 11:27:18 -08004717 void detachStack(ActivityStack stack) {
4718 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004719 + " from displayId=" + mDisplayId);
4720 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004721 }
4722
Jose Lima4b6c6692014-08-12 17:41:12 -07004723 void setVisibleBehindActivity(ActivityRecord r) {
4724 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004725 }
4726
Jose Lima4b6c6692014-08-12 17:41:12 -07004727 boolean hasVisibleBehindActivity() {
4728 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004729 }
4730
Craig Mautner34b73df2014-01-12 21:11:08 -08004731 @Override
4732 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004733 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4734 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004735
4736 @Override
4737 protected int getChildCount() {
4738 return mStacks.size();
4739 }
4740
4741 @Override
4742 protected ConfigurationContainer getChildAt(int index) {
4743 return mStacks.get(index);
4744 }
4745
4746 @Override
4747 protected ConfigurationContainer getParent() {
4748 return ActivityStackSupervisor.this;
4749 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004750
4751 boolean isPrivate() {
4752 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4753 }
4754
4755 boolean isUidPresent(int uid) {
4756 for (ActivityStack stack : mStacks) {
4757 if (stack.isUidPresent(uid)) {
4758 return true;
4759 }
4760 }
4761 return false;
4762 }
4763
4764 /** Update and get all UIDs that are present on the display and have access to it. */
4765 private IntArray getPresentUIDs() {
4766 mDisplayAccessUIDs.clear();
4767 for (ActivityStack stack : mStacks) {
4768 stack.getPresentUIDs(mDisplayAccessUIDs);
4769 }
4770 return mDisplayAccessUIDs;
4771 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004772
4773 boolean shouldDestroyContentOnRemove() {
4774 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4775 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004776 }
4777
4778 class VirtualActivityDisplay extends ActivityDisplay {
4779 VirtualDisplay mVirtualDisplay;
4780
Craig Mautner6985bad2014-04-21 15:22:06 -07004781 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004782 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004783 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4784 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4785 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4786 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004787
4788 init(mVirtualDisplay.getDisplay());
4789
4790 mWindowManager.handleDisplayAdded(mDisplayId);
4791 }
4792
4793 void setSurface(Surface surface) {
4794 if (mVirtualDisplay != null) {
4795 mVirtualDisplay.setSurface(surface);
4796 }
4797 }
4798
4799 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004800 void detachStack(ActivityStack stack) {
4801 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004802 if (mVirtualDisplay != null) {
4803 mVirtualDisplay.release();
4804 mVirtualDisplay = null;
4805 }
4806 }
4807
4808 @Override
4809 public String toString() {
4810 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004811 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004812 }
Jose Lima58e66d62014-05-27 20:00:27 -07004813
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004814 ActivityStack findStackBehind(ActivityStack stack) {
4815 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004816 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004817 if (display == null) {
4818 return null;
4819 }
4820 final ArrayList<ActivityStack> stacks = display.mStacks;
4821 for (int i = stacks.size() - 1; i >= 0; i--) {
4822 if (stacks.get(i) == stack && i > 0) {
4823 return stacks.get(i - 1);
4824 }
4825 }
4826 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4827 + " in=" + stacks);
4828 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004829
Jorim Jaggic69bd222016-03-15 14:38:37 +01004830 /**
4831 * Puts a task into resizing mode during the next app transition.
4832 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004833 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004834 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004835 private void setResizingDuringAnimation(TaskRecord task) {
4836 mResizingTasksDuringAnimation.add(task.taskId);
4837 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004838 }
4839
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004840 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4841 final TaskRecord task;
4842 final int callingUid;
4843 final String callingPackage;
4844 final Intent intent;
4845 final int userId;
4846 final ActivityOptions activityOptions = (bOptions != null)
4847 ? new ActivityOptions(bOptions) : null;
4848 final int launchStackId = (activityOptions != null)
4849 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004850 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004851 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004852 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004853 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004854
4855 if (launchStackId == DOCKED_STACK_ID) {
4856 mWindowManager.setDockedStackCreateState(
4857 activityOptions.getDockCreateMode(), null /* initialBounds */);
4858
4859 // Defer updating the stack in which recents is until the app transition is done, to
4860 // not run into issues where we still need to draw the task in recents but the
4861 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004862 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004863 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4864 }
4865
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004866 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4867 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004868 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004869 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004870 throw new IllegalArgumentException(
4871 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4872 }
4873
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004874 // Since we don't have an actual source record here, we assume that the currently focused
4875 // activity was the source.
4876 final ActivityStack focusedStack = getFocusedStack();
4877 final ActivityRecord sourceRecord =
4878 focusedStack != null ? focusedStack.topActivity() : null;
4879
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004880 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004881 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004882 moveTaskToStackLocked(
4883 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4884 ANIMATE);
4885 }
4886 }
4887
4888 // If the user must confirm credentials (e.g. when first launching a work app and the
4889 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4890 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4891 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004892 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004893 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004894 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004895 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4896 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004897
4898 // If we are launching the task in the docked stack, put it into resizing mode so
4899 // the window renders full-screen with the background filling the void. Also only
4900 // call this at the end to make sure that tasks exists on the window manager side.
4901 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004902 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004903 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004904
4905 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4906 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004907 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4908 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004909 return ActivityManager.START_TASK_TO_FRONT;
4910 }
4911 callingUid = task.mCallingUid;
4912 callingPackage = task.mCallingPackage;
4913 intent = task.intent;
4914 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4915 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004916 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4917 null, null, 0, 0, bOptions, userId, null, task);
4918 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004919 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004920 }
4921 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004922 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004923
4924 /**
4925 * @return a list of activities which are the top ones in each visible stack. The first
4926 * entry will be the focused activity.
4927 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004928 List<IBinder> getTopVisibleActivities() {
4929 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4930 // Traverse all displays.
4931 for (int i = mActivityDisplays.size(); i >= 0; i--) {
4932 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4933 // Traverse all stacks on a display.
4934 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4935 final ActivityStack stack = display.mStacks.get(j);
4936 // Get top activity from a visible stack and add it to the list.
4937 if (stack.getStackVisibilityLocked(null /* starting */)
4938 == ActivityStack.STACK_VISIBLE) {
4939 final ActivityRecord top = stack.topActivity();
4940 if (top != null) {
4941 if (stack == mFocusedStack) {
4942 topActivityTokens.add(0, top.appToken);
4943 } else {
4944 topActivityTokens.add(top.appToken);
4945 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004946 }
4947 }
4948 }
4949 }
4950 return topActivityTokens;
4951 }
Craig Mautner27084302013-03-25 08:05:25 -07004952}