blob: a93524903b29c1ca69127cf1a335fc01da393909 [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;
Jorim Jaggife762342016-10-13 14:33:27 +020043
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
69import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
71import static com.android.server.am.ActivityManagerService.ANIMATE;
72import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
73import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
74import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
76import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
77import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
78import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
80import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
81import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
83import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
84import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
85import static com.android.server.am.ActivityStack.STACK_VISIBLE;
86import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
91import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
92
Svetoslav7008b512015-06-24 18:47:07 -070093import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -070094import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +010095import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070097import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080098import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070099import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800100import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700101import android.app.ActivityOptions;
102import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700103import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700105import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700106import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400107import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700108import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400109import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700111import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700113import android.content.Intent;
114import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700116import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.pm.PackageManager;
118import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100119import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700121import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800122import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800123import android.hardware.display.DisplayManager;
124import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800125import android.hardware.display.DisplayManagerGlobal;
126import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800127import android.hardware.input.InputManager;
128import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100130import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700131import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700132import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700134import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700135import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700136import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700137import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700138import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700139import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400140import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700142import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700143import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700144import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100145import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700146import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700147import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400148import android.provider.Settings;
149import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700150import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700151import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700152import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700153import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700154import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700155import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800156import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800157import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800158import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800159import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800160import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800161
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800162import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700163import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400164import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700165import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400166import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800167import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700168import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700169import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700170
Craig Mautner8d341ef2013-03-26 09:03:27 -0700171import java.io.FileDescriptor;
172import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700173import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700174import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700175import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700176import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800178import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700179import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700180
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800181public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700184 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700186 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800193 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800195
Craig Mautnerf3333272013-04-22 10:55:53 -0700196 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700198
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204
Craig Mautner05d29032013-05-03 13:40:13 -0700205 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
206 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
207 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
211 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
212 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700213 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400214 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
215 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700216 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700217 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700218 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800219 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
220 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800221
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223
Jason Monk62515be2014-05-21 16:06:19 -0400224 private static final String LOCK_TASK_TAG = "Lock-to-App";
225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 // Used to indicate if an object (e.g. stack) that we are trying to get
227 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800228 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700230 // Used to indicate that windows of activities should be preserved during the resize.
231 static final boolean PRESERVE_WINDOWS = true;
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. task) should be moved/created
234 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700235 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
237 // Used to indicate that an objects (e.g. task) removal from its container
238 // (e.g. stack) is due to it moving to another container.
239 static final boolean MOVING = true;
240
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700241 // Force the focus to change to the stack we are moving a task to..
242 static final boolean FORCE_FOCUS = true;
243
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700244 // Restore task from the saved recents if it can't be found in any live stack.
245 static final boolean RESTORE_FROM_RECENTS = true;
246
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700247 // Don't execute any calls to resume.
248 static final boolean DEFER_RESUME = true;
249
Winson Chung010927a2016-12-15 16:12:35 -0800250 // Used to indicate that a task is removed it should also be removed from recents.
251 static final boolean REMOVE_FROM_RECENTS = true;
252
Svetoslav7008b512015-06-24 18:47:07 -0700253 // Activity actions an app cannot start if it uses a permission which is not granted.
254 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
255 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700256
Svetoslav7008b512015-06-24 18:47:07 -0700257 static {
258 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
259 Manifest.permission.CAMERA);
260 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
261 Manifest.permission.CAMERA);
262 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
263 Manifest.permission.CALL_PHONE);
264 }
265
Svet Ganov99b60432015-06-27 13:15:22 -0700266 /** Action restriction: launching the activity is not restricted. */
267 private static final int ACTIVITY_RESTRICTION_NONE = 0;
268 /** Action restriction: launching the activity is restricted by a permission. */
269 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
270 /** Action restriction: launching the activity is restricted by an app op. */
271 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700272
justinzhang5286d3f2014-05-12 17:06:01 -0400273 /** Status Bar Service **/
274 private IBinder mToken = new Binder();
275 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400276 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400277
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700278 // For debugging to make sure the caller when acquiring/releasing our
279 // wake lock is the system process.
280 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800281 /** The number of distinct task ids that can be assigned to the tasks of a single user */
282 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700283
Craig Mautner27084302013-03-25 08:05:25 -0700284 final ActivityManagerService mService;
285
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800286 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800287
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700288 final ActivityStackSupervisorHandler mHandler;
289
290 /** Short cut */
291 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800292 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700293
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700294 /** Counter for next free stack ID to use for dynamic activity stacks. */
295 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700296
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800297 /**
298 * Maps the task identifier that activities are currently being started in to the userId of the
299 * task. Each time a new task is created, the entry for the userId of the task is incremented
300 */
301 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Craig Mautner2420ead2013-04-01 17:13:20 -0700303 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800304 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700305
Craig Mautnere0a38842013-12-16 16:14:02 -0800306 /** The stack containing the launcher app. Assumed to always be attached to
307 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700309
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700312
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
314 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800315 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700317
318 /** List of activities that are waiting for a new activity to become visible before completing
319 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700323 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324
325 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700326 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328 /** List of activities that are ready to be stopped, but waiting for the next activity to
329 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800330 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331
Craig Mautnerf3333272013-04-22 10:55:53 -0700332 /** List of activities that are ready to be finished, but waiting for the previous activity to
333 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800334 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700335
Craig Mautner0eea92c2013-05-16 13:35:39 -0700336 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338
Wale Ogunwale22e25262016-02-01 10:32:02 -0800339 /** List of activities whose multi-window mode changed that we need to report to the
340 * application */
341 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
342
343 /** List of activities whose picture-in-picture mode changed that we need to report to the
344 * application */
345 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
346
Craig Mautnerf3333272013-04-22 10:55:53 -0700347 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700348 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700349
Craig Mautnerde4ef022013-04-07 19:01:33 -0700350 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
351 * is being brought in front of us. */
352 boolean mUserLeaving = false;
353
Craig Mautner0eea92c2013-05-16 13:35:39 -0700354 /** Set when we have taken too long waiting to go to sleep. */
355 boolean mSleepTimeout = false;
356
357 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700358 * We don't want to allow the device to go to sleep while in the process
359 * of launching an activity. This is primarily to allow alarm intent
360 * receivers to launch an activity and get that to run before the device
361 * goes back to sleep.
362 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700363 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700364
365 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 * Set when the system is going to sleep, until we have
367 * successfully paused the current activity and released our wake lock.
368 * At that point the system is allowed to actually sleep.
369 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700370 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700371
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700372 /** Stack id of the front stack when user switched, indexed by userId. */
373 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700374
Craig Mautner4504de52013-12-20 09:06:56 -0800375 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800376 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378
379 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381
Jeff Brownca9bc702014-02-11 14:32:56 -0800382 InputManagerInternal mInputManagerInternal;
383
Craig Mautner15df08a2015-04-01 12:17:18 -0700384 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
385 * may be finished until there is only one entry left. If this is empty the system is not
386 * in lockTask mode. */
387 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000388 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700389 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
390 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000391 */
392 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400393 /**
394 * Notifies the user when entering/exiting lock-task.
395 */
396 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800397
Wale Ogunwaled046a012015-12-24 13:05:59 -0800398 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800399 boolean inResumeTopActivity;
400
Andrii Kulian1e32e022016-09-16 15:29:34 -0700401 /**
402 * Temporary rect used during docked stack resize calculation so we don't need to create a new
403 * object each time.
404 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700405 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700406
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700407 // The default minimal size that will be used if the activity doesn't specify its minimal size.
408 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700409 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700410
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700411 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
412 private boolean mTaskLayersChanged = true;
413
Jorim Jaggi275561a2016-02-23 10:11:02 -0500414 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800415
Jorim Jaggidc249c42015-12-15 14:57:31 -0800416 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
417
Andrii Kulian1779e612016-10-12 21:58:25 -0700418 @Override
419 protected int getChildCount() {
420 return mActivityDisplays.size();
421 }
422
423 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700424 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700425 return mActivityDisplays.valueAt(index);
426 }
427
428 @Override
429 protected ConfigurationContainer getParent() {
430 return null;
431 }
432
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700433 Configuration getDisplayOverrideConfiguration(int displayId) {
434 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
435 if (activityDisplay == null) {
436 throw new IllegalArgumentException("No display found with id: " + displayId);
437 }
438
439 return activityDisplay.getOverrideConfiguration();
440 }
441
442 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
443 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
444 if (activityDisplay == null) {
445 throw new IllegalArgumentException("No display found with id: " + displayId);
446 }
447
448 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
449 }
450
Wale Ogunwale39381972015-12-17 17:15:29 -0800451 static class FindTaskResult {
452 ActivityRecord r;
453 boolean matchedByRootAffinity;
454 }
455 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
456
Craig Mautneree36c772014-07-16 14:56:05 -0700457 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800458 * Temp storage for display ids sorted in focus order.
459 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
460 * it's more efficient, as the number of displays is usually small.
461 */
462 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
463
464 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100465 * Used to keep track whether app visibilities got changed since the last pause. Useful to
466 * determine whether to invoke the task stack change listener after pausing.
467 */
468 boolean mAppVisibilitiesChangedSinceLastPause;
469
470 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100471 * Set of tasks that are in resizing mode during an app transition to fill the "void".
472 */
473 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
474
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700475
476 /**
477 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
478 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
479 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
480 * like the docked stack going empty.
481 */
482 private boolean mAllowDockedStackResize = true;
483
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100484 /**
Tony Mak853304c2016-04-18 15:17:41 +0100485 * Is dock currently minimized.
486 */
487 boolean mIsDockMinimized;
488
Jorim Jaggife762342016-10-13 14:33:27 +0200489 final KeyguardController mKeyguardController;
490
Tony Mak853304c2016-04-18 15:17:41 +0100491 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700492 * Description of a request to start a new activity, which has been held
493 * due to app switches being disabled.
494 */
495 static class PendingActivityLaunch {
496 final ActivityRecord r;
497 final ActivityRecord sourceRecord;
498 final int startFlags;
499 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700500 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700501
502 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700503 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700504 r = _r;
505 sourceRecord = _sourceRecord;
506 startFlags = _startFlags;
507 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700508 callerApp = _callerApp;
509 }
510
511 void sendErrorResult(String message) {
512 try {
513 if (callerApp.thread != null) {
514 callerApp.thread.scheduleCrash(message);
515 }
516 } catch (RemoteException e) {
517 Slog.e(TAG, "Exception scheduling crash of failed "
518 + "activity launcher sourceRecord=" + sourceRecord, e);
519 }
Craig Mautneree36c772014-07-16 14:56:05 -0700520 }
521 }
522
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800523 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700524 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700525 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800526 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800527 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jorim Jaggife762342016-10-13 14:33:27 +0200528 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700529 }
530
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800531 void setRecentTasks(RecentTasks recentTasks) {
532 mRecentTasks = recentTasks;
533 }
534
Jeff Brown2c43c332014-06-12 22:38:59 -0700535 /**
536 * At the time when the constructor runs, the power manager has not yet been
537 * initialized. So we initialize our wakelocks afterwards.
538 */
539 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800540 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700541 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700542 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700543 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700544 }
545
justinzhang5286d3f2014-05-12 17:06:01 -0400546 // This function returns a IStatusBarService. The value is from ServiceManager.
547 // getService and is cached.
548 private IStatusBarService getStatusBarService() {
549 synchronized (mService) {
550 if (mStatusBarService == null) {
551 mStatusBarService = IStatusBarService.Stub.asInterface(
552 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
553 if (mStatusBarService == null) {
554 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
555 }
556 }
557 return mStatusBarService;
558 }
559 }
560
Jason Monk35c62a42014-06-17 10:24:47 -0400561 private IDevicePolicyManager getDevicePolicyManager() {
562 synchronized (mService) {
563 if (mDevicePolicyManager == null) {
564 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
565 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
566 if (mDevicePolicyManager == null) {
567 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
568 }
569 }
570 return mDevicePolicyManager;
571 }
572 }
573
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700574 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800575 synchronized (mService) {
576 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200577 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800578
579 mDisplayManager =
580 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
581 mDisplayManager.registerDisplayListener(this, null);
582
583 Display[] displays = mDisplayManager.getDisplays();
584 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
585 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800586 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700587 if (activityDisplay.mDisplay == null) {
588 throw new IllegalStateException("Default Display does not exist");
589 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800590 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700591 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 }
593
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800594 mHomeStack = mFocusedStack = mLastFocusedStack =
595 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800596
597 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 }
Craig Mautner27084302013-03-25 08:05:25 -0700599 }
600
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700601 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700603 }
604
Craig Mautnerde4ef022013-04-07 19:01:33 -0700605 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700607 }
608
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700609 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700610 if (stack == null) {
611 return false;
612 }
613
Craig Mautnerdf88d732014-01-27 09:21:32 -0800614 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
615 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700616 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800617 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800618 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700619 }
620
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700621 /** The top most stack. */
622 boolean isFrontStack(ActivityStack stack) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800623 return isFrontOfStackList(stack, mHomeStack.mStacks);
624 }
625
626 /** 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.
Craig Mautnere0a38842013-12-16 16:14:02 -08001100 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001101 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1102 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001103 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001104 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001105 if (r != null) {
1106 return r;
1107 }
1108 }
1109 }
1110 return null;
1111 }
1112
Dianne Hackborn09233282014-04-30 11:33:59 -07001113 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001114 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001115 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1116 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001117 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001118 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001119 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001120 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1121 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001122 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001123 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001124 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001125 }
1126 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001127
1128 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1129 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1130 while (maxNum > 0) {
1131 long mostRecentActiveTime = Long.MIN_VALUE;
1132 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001133 final int numTaskLists = runningTaskLists.size();
1134 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1135 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001136 if (!stackTaskList.isEmpty()) {
1137 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1138 if (lastActiveTime > mostRecentActiveTime) {
1139 mostRecentActiveTime = lastActiveTime;
1140 selectedStackList = stackTaskList;
1141 }
1142 }
1143 }
1144 if (selectedStackList != null) {
1145 list.add(selectedStackList.remove(0));
1146 --maxNum;
1147 } else {
1148 break;
1149 }
1150 }
Craig Mautner20e72272013-04-01 13:45:53 -07001151 }
1152
Todd Kennedy7440f172015-12-09 14:31:22 -08001153 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1154 ProfilerInfo profilerInfo) {
1155 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001156 if (aInfo != null) {
1157 // Store the found target back into the intent, because now that
1158 // we have it we never want to do this again. For example, if the
1159 // user navigates back to this point in the history, we should
1160 // always restart the exact same activity.
1161 intent.setComponent(new ComponentName(
1162 aInfo.applicationInfo.packageName, aInfo.name));
1163
1164 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001165 if (!aInfo.processName.equals("system")) {
1166 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001167 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001168 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001169
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001170 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1171 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1172 }
1173
Man Caocfa78b22015-06-11 20:14:34 -07001174 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1175 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1176 }
1177
1178 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001179 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 }
1181 }
1182 }
1183 return aInfo;
1184 }
1185
Todd Kennedy7440f172015-12-09 14:31:22 -08001186 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001187 return resolveIntent(intent, resolvedType, userId, 0);
1188 }
1189
1190 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001191 try {
1192 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001193 PackageManager.MATCH_DEFAULT_ONLY | flags
1194 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001195 } catch (RemoteException e) {
1196 }
1197 return null;
1198 }
1199
1200 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1201 ProfilerInfo profilerInfo, int userId) {
1202 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1203 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1204 }
1205
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001206 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1207 boolean andResume, boolean checkConfig) throws RemoteException {
1208
1209 if (!allPausedActivitiesComplete()) {
1210 // While there are activities pausing we skipping starting any new activities until
1211 // pauses are complete. NOTE: that we also do this for activities that are starting in
1212 // the paused state because they will first be resumed then paused on the client side.
1213 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1214 "realStartActivityLocked: Skipping start of r=" + r
1215 + " some activities pausing...");
1216 return false;
1217 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001218
Craig Mautner2568c3a2015-03-26 14:22:34 -07001219 if (andResume) {
1220 r.startFreezingScreenLocked(app, 0);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001221 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001222
Craig Mautner2568c3a2015-03-26 14:22:34 -07001223 // schedule launch ticks to collect information about slow apps.
1224 r.startLaunchTickingLocked();
1225 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001226
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001227 // Have the window manager re-evaluate the orientation of the screen based on the new
1228 // activity order. Note that as a result of this, it can call back into the activity
1229 // manager with a new orientation. We don't care about that, because the activity is not
1230 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001232 final int displayId = r.getDisplayId();
1233 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1234 getDisplayOverrideConfiguration(displayId),
1235 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001236 // Deferring resume here because we're going to launch new activity shortly.
1237 // We don't want to perform a redundant launch of the same record while ensuring
1238 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001239 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1240 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001241 }
1242
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001243 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001244 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001245 }
1246
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 r.app = app;
1248 app.waitingToKill = null;
1249 r.launchCount++;
1250 r.lastLaunchTime = SystemClock.uptimeMillis();
1251
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001252 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001253
1254 int idx = app.activities.indexOf(r);
1255 if (idx < 0) {
1256 app.activities.add(r);
1257 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001258 mService.updateLruProcessLocked(app, true, null);
1259 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001260
Craig Mautner15df08a2015-04-01 12:17:18 -07001261 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001262 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1263 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001264 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001265 }
1266
Andrii Kulian02b7a832016-10-06 23:11:56 -07001267 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 try {
1269 if (app.thread == null) {
1270 throw new RemoteException();
1271 }
1272 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001273 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001274 if (andResume) {
1275 results = r.results;
1276 newIntents = r.newIntents;
1277 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001278 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1279 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1280 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 if (andResume) {
1282 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1283 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001284 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001286 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001287 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001288 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001289 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001290 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1291 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 r.sleeping = false;
1293 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001294 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 mService.showAskCompatModeDialogLocked(r);
1296 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001297 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1299 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1300 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001301 final String profileFile = mService.mProfileFile;
1302 if (profileFile != null) {
1303 ParcelFileDescriptor profileFd = mService.mProfileFd;
1304 if (profileFd != null) {
1305 try {
1306 profileFd = profileFd.dup();
1307 } catch (IOException e) {
1308 if (profileFd != null) {
1309 try {
1310 profileFd.close();
1311 } catch (IOException o) {
1312 }
1313 profileFd = null;
1314 }
1315 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001317
1318 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1319 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001320 }
1321 }
1322 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001323
Craig Mautner2568c3a2015-03-26 14:22:34 -07001324 if (andResume) {
1325 app.hasShownUi = true;
1326 app.pendingUiClean = true;
1327 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001328 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001329 // Because we could be starting an Activity in the system process this may not go across
1330 // a Binder interface which would create a new Configuration. Consequently we have to
1331 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001332 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001333 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001334 new Configuration(mService.getGlobalConfiguration()),
1335 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1336 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1337 r.persistentState, results, newIntents, !andResume,
1338 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001339
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001340 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001341 // This may be a heavy-weight process! Note that the package
1342 // manager will ensure that only activity can run in the main
1343 // process of the .apk, which is the only thing that will be
1344 // considered heavy-weight.
1345 if (app.processName.equals(app.info.packageName)) {
1346 if (mService.mHeavyWeightProcess != null
1347 && mService.mHeavyWeightProcess != app) {
1348 Slog.w(TAG, "Starting new heavy weight process " + app
1349 + " when already running "
1350 + mService.mHeavyWeightProcess);
1351 }
1352 mService.mHeavyWeightProcess = app;
1353 Message msg = mService.mHandler.obtainMessage(
1354 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1355 msg.obj = r;
1356 mService.mHandler.sendMessage(msg);
1357 }
1358 }
1359
1360 } catch (RemoteException e) {
1361 if (r.launchFailed) {
1362 // This is the second time we failed -- finish activity
1363 // and give up.
1364 Slog.e(TAG, "Second failure launching "
1365 + r.intent.getComponent().flattenToShortString()
1366 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001367 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001368 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1369 "2nd-crash", false);
1370 return false;
1371 }
1372
1373 // This is the first time we failed -- restart process and
1374 // retry.
1375 app.activities.remove(r);
1376 throw e;
1377 }
1378
1379 r.launchFailed = false;
1380 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001381 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 }
1383
1384 if (andResume) {
1385 // As part of the process of launching, ActivityThread also performs
1386 // a resume.
1387 stack.minimalResumeActivityLocked(r);
1388 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001389 // This activity is not starting in the resumed state... which should look like we asked
1390 // it to pause+stop (but remain visible), and it has done so and reported back the
1391 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001392 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001393 "Moving to PAUSED: " + r + " (starting in paused state)");
1394 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 }
1396
1397 // Launch the new version setup screen if needed. We do this -after-
1398 // launching the initial activity (that is, home), so that it can have
1399 // a chance to initialize itself while in the background, making the
1400 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001401 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001402 mService.startSetupActivityLocked();
1403 }
1404
Dianne Hackborn465fa392014-09-14 14:21:18 -07001405 // Update any services we are bound to that might care about whether
1406 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001407 if (r.app != null) {
1408 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1409 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001410
Craig Mautner2420ead2013-04-01 17:13:20 -07001411 return true;
1412 }
1413
Craig Mautnere79d42682013-04-01 19:01:53 -07001414 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001415 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001416 // Is this activity's application already running?
1417 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001418 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001419
Andrii Kulian02b7a832016-10-06 23:11:56 -07001420 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001421
1422 if (app != null && app.thread != null) {
1423 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001424 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1425 || !"android".equals(r.info.packageName)) {
1426 // Don't add this if it is a platform component that is marked
1427 // to run in multiple processes, because this is actually
1428 // part of the framework so doesn't make sense to track as a
1429 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001430 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1431 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001432 }
George Mount2c92c972014-03-20 09:38:23 -07001433 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001434 return;
1435 } catch (RemoteException e) {
1436 Slog.w(TAG, "Exception when starting activity "
1437 + r.intent.getComponent().flattenToShortString(), e);
1438 }
1439
1440 // If a dead object exception was thrown -- fall through to
1441 // restart the application.
1442 }
1443
1444 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001445 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001446 }
1447
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001448 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001449 String resultWho, int requestCode, int callingPid, int callingUid,
1450 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001451 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001452 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1453 callingUid);
1454 if (startAnyPerm == PERMISSION_GRANTED) {
1455 return true;
1456 }
1457 final int componentRestriction = getComponentRestrictionForCallingPackage(
1458 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1459 final int actionRestriction = getActionRestrictionForCallingPackage(
1460 intent.getAction(), callingPackage, callingPid, callingUid);
1461 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1462 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1463 if (resultRecord != null) {
1464 resultStack.sendActivityResultLocked(-1,
1465 resultRecord, resultWho, requestCode,
1466 Activity.RESULT_CANCELED, null);
1467 }
1468 final String msg;
1469 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1470 msg = "Permission Denial: starting " + intent.toString()
1471 + " from " + callerApp + " (pid=" + callingPid
1472 + ", uid=" + callingUid + ")" + " with revoked permission "
1473 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1474 } else if (!aInfo.exported) {
1475 msg = "Permission Denial: starting " + intent.toString()
1476 + " from " + callerApp + " (pid=" + callingPid
1477 + ", uid=" + callingUid + ")"
1478 + " not exported from uid " + aInfo.applicationInfo.uid;
1479 } else {
1480 msg = "Permission Denial: starting " + intent.toString()
1481 + " from " + callerApp + " (pid=" + callingPid
1482 + ", uid=" + callingUid + ")"
1483 + " requires " + aInfo.permission;
1484 }
1485 Slog.w(TAG, msg);
1486 throw new SecurityException(msg);
1487 }
1488
1489 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1490 final String message = "Appop Denial: starting " + intent.toString()
1491 + " from " + callerApp + " (pid=" + callingPid
1492 + ", uid=" + callingUid + ")"
1493 + " requires " + AppOpsManager.permissionToOp(
1494 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1495 Slog.w(TAG, message);
1496 return false;
1497 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1498 final String message = "Appop Denial: starting " + intent.toString()
1499 + " from " + callerApp + " (pid=" + callingPid
1500 + ", uid=" + callingUid + ")"
1501 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1502 Slog.w(TAG, message);
1503 return false;
1504 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001505 if (options != null) {
1506 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1507 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1508 callingPid, callingUid);
1509 if (startInTaskPerm != PERMISSION_GRANTED) {
1510 final String msg = "Permission Denial: starting " + intent.toString()
1511 + " from " + callerApp + " (pid=" + callingPid
1512 + ", uid=" + callingUid + ") with launchTaskId="
1513 + options.getLaunchTaskId();
1514 Slog.w(TAG, msg);
1515 throw new SecurityException(msg);
1516 }
1517 }
1518 // Check if someone tries to launch an activity on a private display with a different
1519 // owner.
1520 final int launchDisplayId = options.getLaunchDisplayId();
1521 if (launchDisplayId != INVALID_DISPLAY) {
1522 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1523 if (activityDisplay != null
1524 && (activityDisplay.mDisplay.getFlags() & FLAG_PRIVATE) != 0) {
1525 if (activityDisplay.mDisplay.getOwnerUid() != callingUid) {
1526 final String msg = "Permission Denial: starting " + intent.toString()
1527 + " from " + callerApp + " (pid=" + callingPid
1528 + ", uid=" + callingUid + ") with launchDisplayId="
1529 + launchDisplayId;
1530 Slog.w(TAG, msg);
1531 throw new SecurityException(msg);
1532 }
1533 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001534 }
1535 }
1536
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001537 return true;
1538 }
1539
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001540 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001541 final long identity = Binder.clearCallingIdentity();
1542 try {
1543 return UserManager.get(mService.mContext).getUserInfo(userId);
1544 } finally {
1545 Binder.restoreCallingIdentity(identity);
1546 }
1547 }
1548
Svet Ganov99b60432015-06-27 13:15:22 -07001549 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001550 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001551 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1552 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001553 == PackageManager.PERMISSION_DENIED) {
1554 return ACTIVITY_RESTRICTION_PERMISSION;
1555 }
1556
Christopher Tateff7add02015-08-17 10:23:22 -07001557 if (activityInfo.permission == null) {
1558 return ACTIVITY_RESTRICTION_NONE;
1559 }
1560
Svet Ganov99b60432015-06-27 13:15:22 -07001561 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1562 if (opCode == AppOpsManager.OP_NONE) {
1563 return ACTIVITY_RESTRICTION_NONE;
1564 }
1565
1566 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1567 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001568 if (!ignoreTargetSecurity) {
1569 return ACTIVITY_RESTRICTION_APPOP;
1570 }
Svet Ganov99b60432015-06-27 13:15:22 -07001571 }
1572
1573 return ACTIVITY_RESTRICTION_NONE;
1574 }
1575
Svetoslav7008b512015-06-24 18:47:07 -07001576 private int getActionRestrictionForCallingPackage(String action,
1577 String callingPackage, int callingPid, int callingUid) {
1578 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001579 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001580 }
1581
1582 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1583 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001584 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001585 }
1586
1587 final PackageInfo packageInfo;
1588 try {
1589 packageInfo = mService.mContext.getPackageManager()
1590 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1591 } catch (PackageManager.NameNotFoundException e) {
1592 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001593 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001594 }
1595
1596 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001597 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001598 }
1599
1600 if (mService.checkPermission(permission, callingPid, callingUid) ==
1601 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001602 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001603 }
1604
1605 final int opCode = AppOpsManager.permissionToOpCode(permission);
1606 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001607 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001608 }
1609
1610 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1611 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001612 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001613 }
1614
Svet Ganov99b60432015-06-27 13:15:22 -07001615 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001616 }
1617
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001618 void setLaunchSource(int uid) {
1619 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1620 }
1621
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001622 void acquireLaunchWakelock() {
1623 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1624 throw new IllegalStateException("Calling must be system uid");
1625 }
1626 mLaunchingActivity.acquire();
1627 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1628 // To be safe, don't allow the wake lock to be held for too long.
1629 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1630 }
1631 }
1632
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001633 /**
1634 * Called when the frontmost task is idle.
1635 * @return the state of mService.mBooting before this was called.
1636 */
1637 private boolean checkFinishBootingLocked() {
1638 final boolean booting = mService.mBooting;
1639 boolean enableScreen = false;
1640 mService.mBooting = false;
1641 if (!mService.mBooted) {
1642 mService.mBooted = true;
1643 enableScreen = true;
1644 }
1645 if (booting || enableScreen) {
1646 mService.postFinishBooting(booting, enableScreen);
1647 }
1648 return booting;
1649 }
1650
Craig Mautnerf3333272013-04-22 10:55:53 -07001651 // Checked.
1652 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1653 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001654 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001655
Craig Mautnerf3333272013-04-22 10:55:53 -07001656 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001657 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001658 int NS = 0;
1659 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001660 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001661 boolean activityRemoved = false;
1662
Wale Ogunwale7d701172015-03-11 15:36:30 -07001663 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001664 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001665 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1666 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001667 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1668 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001669 if (fromTimeout) {
1670 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001671 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001672
1673 // This is a hack to semi-deal with a race condition
1674 // in the client where it can be constructed with a
1675 // newer configuration from when we asked it to launch.
1676 // We'll update with whatever configuration it now says
1677 // it used to launch.
1678 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001679 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001680 }
1681
1682 // We are now idle. If someone is waiting for a thumbnail from
1683 // us, we can now deliver.
1684 r.idle = true;
1685
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001686 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001687 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001688 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001689 }
1690 }
1691
1692 if (allResumedActivitiesIdle()) {
1693 if (r != null) {
1694 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001695 }
1696
1697 if (mLaunchingActivity.isHeld()) {
1698 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1699 if (VALIDATE_WAKE_LOCK_CALLER &&
1700 Binder.getCallingUid() != Process.myUid()) {
1701 throw new IllegalStateException("Calling must be system uid");
1702 }
1703 mLaunchingActivity.release();
1704 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001705 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001706 }
1707
1708 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001709 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001710 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001711 if ((NF = mFinishingActivities.size()) > 0) {
1712 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001713 mFinishingActivities.clear();
1714 }
1715
Craig Mautnerf3333272013-04-22 10:55:53 -07001716 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001717 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001718 mStartingUsers.clear();
1719 }
1720
Craig Mautnerf3333272013-04-22 10:55:53 -07001721 // Stop any activities that are scheduled to do so but have been
1722 // waiting for the next one to start.
1723 for (int i = 0; i < NS; i++) {
1724 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001725 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001726 if (stack != null) {
1727 if (r.finishing) {
1728 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1729 } else {
1730 stack.stopActivityLocked(r);
1731 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001732 }
1733 }
1734
1735 // Finish any activities that are scheduled to do so but have been
1736 // waiting for the next one to start.
1737 for (int i = 0; i < NF; i++) {
1738 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001739 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001740 if (stack != null) {
1741 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1742 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001743 }
1744
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001745 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001746 // Complete user switch
1747 if (startingUsers != null) {
1748 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001749 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001750 }
1751 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001752 }
1753
1754 mService.trimApplications();
1755 //dump();
1756 //mWindowManager.dump();
1757
Craig Mautnerf3333272013-04-22 10:55:53 -07001758 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001759 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001760 }
1761
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001762 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001763 }
1764
Craig Mautner8e569572013-10-11 17:36:59 -07001765 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001766 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1770 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1771 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001772 }
Craig Mautner19091252013-10-05 00:03:53 -07001773 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001774 }
1775
1776 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001777 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1778 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001779 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1780 stacks.get(stackNdx).closeSystemDialogsLocked();
1781 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001782 }
1783 }
1784
Craig Mautner93529a42013-10-04 15:03:13 -07001785 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001786 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001787 }
1788
Craig Mautner8d341ef2013-03-26 09:03:27 -07001789 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001790 * Update the last used stack id for non-current user (current user's last
1791 * used stack is the focused stack)
1792 */
1793 void updateUserStackLocked(int userId, ActivityStack stack) {
1794 if (userId != mCurrentUser) {
1795 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1796 }
1797 }
1798
1799 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001800 * @return true if some activity was finished (or would have finished if doit were true).
1801 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001802 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1803 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001804 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001805 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1806 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001807 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001808 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001809 if (stack.finishDisabledPackageActivitiesLocked(
1810 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001811 didSomething = true;
1812 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001813 }
1814 }
1815 return didSomething;
1816 }
1817
Dianne Hackborna413dc02013-07-12 12:02:55 -07001818 void updatePreviousProcessLocked(ActivityRecord r) {
1819 // Now that this process has stopped, we may want to consider
1820 // it to be the previous app to try to keep around in case
1821 // the user wants to return to it.
1822
1823 // First, found out what is currently the foreground app, so that
1824 // we don't blow away the previous app if this activity is being
1825 // hosted by the process that is actually still the foreground.
1826 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1828 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001829 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1830 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001831 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001832 if (stack.mResumedActivity != null) {
1833 fgApp = stack.mResumedActivity.app;
1834 } else if (stack.mPausingActivity != null) {
1835 fgApp = stack.mPausingActivity.app;
1836 }
1837 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001838 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001839 }
1840 }
1841
1842 // Now set this one as the previous process, only if that really
1843 // makes sense to.
1844 if (r.app != null && fgApp != null && r.app != fgApp
1845 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001846 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001847 mService.mPreviousProcess = r.app;
1848 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1849 }
1850 }
1851
Wale Ogunwaled046a012015-12-24 13:05:59 -08001852 boolean resumeFocusedStackTopActivityLocked() {
1853 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001854 }
1855
Wale Ogunwaled046a012015-12-24 13:05:59 -08001856 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001857 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001858 if (targetStack != null && isFocusedStack(targetStack)) {
1859 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001860 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001861 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1862 if (r == null || r.state != RESUMED) {
1863 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001864 } else if (r.state == RESUMED) {
1865 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1866 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001867 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001868 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001869 }
1870
Todd Kennedy39bfee52016-02-24 10:28:21 -08001871 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1872 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1873 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1874 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1875 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1876 }
1877 }
1878 }
1879
Adrian Roos20d7df32016-01-12 18:59:43 +01001880 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1881 TaskRecord finishedTask = null;
1882 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001883 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1884 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001885 final int numStacks = stacks.size();
1886 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1887 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001888 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1889 if (stack == focusedStack || finishedTask == null) {
1890 finishedTask = t;
1891 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001892 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001893 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001894 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001895 }
1896
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001897 void finishVoiceTask(IVoiceInteractionSession session) {
1898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1899 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1900 final int numStacks = stacks.size();
1901 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1902 final ActivityStack stack = stacks.get(stackNdx);
1903 stack.finishVoiceTask(session);
1904 }
1905 }
1906 }
1907
Andrii Kulianc27916642016-04-12 17:59:27 -07001908 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1909 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001910 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1911 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001912 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001913 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1914 // Caller wants the home activity moved with it. To accomplish this,
1915 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001916 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001917 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001918 ActivityStack currentStack = task.getStack();
1919 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001920 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1921 + task + " to front. Stack is null");
1922 return;
1923 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001924
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001925 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001926 int stackId = options.getLaunchStackId();
1927 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001928 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001929 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001930 if (stackId == INVALID_STACK_ID) {
1931 stackId = task.getLaunchStackId();
1932 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001933 if (stackId != currentStack.mStackId) {
1934 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1935 !FORCE_FOCUS, reason);
1936 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001937 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1938 // still need moveTaskToFrontLocked() below for any transition settings.
1939 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001940 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1941 resizeStackLocked(stackId, bounds,
1942 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001943 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001944 } else {
1945 // WM resizeTask must be done after the task is moved to the correct stack,
1946 // because Task's setBounds() also updates dim layer's bounds, but that has
1947 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001948 mWindowManager.resizeTask(task.taskId, task.mBounds,
1949 task.getOverrideConfiguration(), false /* relayout */,
1950 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001951 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001952 }
1953 }
1954
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001955 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001956 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001957 r == null ? null : r.appTimeTracker, reason);
1958
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001959 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001960 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001961
Andrii Kulian02b7a832016-10-06 23:11:56 -07001962 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001963 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001964 }
1965
Wale Ogunwale854809c2015-12-27 16:18:19 -08001966 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001967 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001968 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001969 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001970 return false;
1971 }
1972 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1973 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001974 }
1975
Craig Mautner967212c2013-04-13 21:10:58 -07001976 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001977 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001978 }
1979
1980 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001981 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001982 if (activityContainer != null) {
1983 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001984 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001985 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001986 return null;
1987 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001988 // TODO(multi-display): Allow creating stacks on secondary displays.
Jorim Jaggife762342016-10-13 14:33:27 +02001989 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001990 }
1991
Andrii Kulian16802aa2016-11-02 12:21:33 -07001992 /**
1993 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
1994 * If there is no such stack, new dynamic stack can be created.
1995 * @param displayId Target display.
1996 * @param r Activity that should be launched there.
1997 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
1998 */
1999 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2000 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2001 if (activityDisplay == null) {
2002 throw new IllegalArgumentException(
2003 "Display with displayId=" + displayId + " not found.");
2004 }
2005
2006 // Return the topmost valid stack on the display.
2007 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2008 final ActivityStack stack = activityDisplay.mStacks.get(i);
2009 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2010 return stack;
2011 }
2012 }
2013
2014 // If there is no valid stack on the external display - check if new dynamic stack will do.
2015 if (displayId != Display.DEFAULT_DISPLAY) {
2016 final int newDynamicStackId = getNextStackId();
2017 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2018 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2019 }
2020 }
2021
2022 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2023 return null;
2024 }
2025
Craig Mautner967212c2013-04-13 21:10:58 -07002026 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002027 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2029 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002030 }
2031 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002032 }
2033
Jorim Jaggife762342016-10-13 14:33:27 +02002034 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2035 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2036 }
2037
Andrii Kulian7fc22812016-12-28 13:04:11 -08002038 /**
2039 * Get next focusable stack in the system. This will search across displays and stacks
2040 * in last-focused order for a focusable and visible stack, different from the target stack.
2041 *
2042 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2043 * searching for next candidate.
2044 * @return Next focusable {@link ActivityStack}, null if not found.
2045 */
2046 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2047 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2048
2049 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2050 final int displayId = mTmpOrderedDisplayIds.get(i);
2051 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2052 if (stacks == null) {
2053 continue;
2054 }
2055 for (int j = stacks.size() - 1; j >= 0; --j) {
2056 final ActivityStack stack = stacks.get(j);
2057 if (stack != currentFocus && stack.isFocusable()
2058 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2059 return stack;
2060 }
2061 }
2062 }
2063
2064 return null;
2065 }
2066
Craig Mautneree2e45a2014-06-27 12:10:03 -07002067 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002068 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002069 }
2070
2071 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002072 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2073 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2074 final TaskRecord task = tasks.get(taskNdx);
2075 if (task.isHomeTask()) {
2076 final ArrayList<ActivityRecord> activities = task.mActivities;
2077 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2078 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002079 if (r.isHomeActivity()
2080 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002081 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002082 }
2083 }
2084 }
2085 }
2086 return null;
2087 }
2088
Chong Zhangb15758a2015-11-17 12:12:03 -08002089 /**
2090 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2091 * the docked stack itself, or if it's side-by-side to the docked stack.
2092 */
2093 boolean isStackDockedInEffect(int stackId) {
2094 return stackId == DOCKED_STACK_ID ||
2095 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2096 }
2097
Todd Kennedyca4d8422015-01-15 15:19:22 -08002098 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002099 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002100 ActivityContainer activityContainer =
2101 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002102 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002103 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2104 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002105 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002106 return activityContainer;
2107 }
2108
Craig Mautner34b73df2014-01-12 21:11:08 -08002109 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002110 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2111 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2112 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002113 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2114 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002115 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002116 }
2117 }
2118
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002119 void deleteActivityContainerRecord(int stackId) {
2120 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2121 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2122 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002123 }
2124
Jorim Jaggidc249c42015-12-15 14:57:31 -08002125 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002126 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002127 if (stackId == DOCKED_STACK_ID) {
2128 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002129 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002130 return;
2131 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002132 final ActivityStack stack = getStack(stackId);
2133 if (stack == null) {
2134 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2135 return;
2136 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002137
Jorim Jaggidc249c42015-12-15 14:57:31 -08002138 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002139 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2140 // stack size changing so things don't get out of sync.
2141 return;
2142 }
2143
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002144 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002145 mWindowManager.deferSurfaceLayout();
2146 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002147 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002148 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002149 stack.ensureVisibleActivitiesConfigurationLocked(
2150 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002151 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002152 } finally {
2153 mWindowManager.continueSurfaceLayout();
2154 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002155 }
2156 }
2157
Jorim Jaggi192086e2016-03-11 17:17:03 +01002158 void deferUpdateBounds(int stackId) {
2159 final ActivityStack stack = getStack(stackId);
2160 if (stack != null) {
2161 stack.deferUpdateBounds();
2162 }
2163 }
2164
2165 void continueUpdateBounds(int stackId) {
2166 final ActivityStack stack = getStack(stackId);
2167 if (stack != null) {
2168 stack.continueUpdateBounds();
2169 }
2170 }
2171
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002172 void notifyAppTransitionDone() {
2173 continueUpdateBounds(HOME_STACK_ID);
2174 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2175 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002176 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002177 mWindowManager.setTaskDockedResizing(taskId, false);
2178 }
2179 }
2180 mResizingTasksDuringAnimation.clear();
2181 }
2182
Jorim Jaggi192086e2016-03-11 17:17:03 +01002183 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002184 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002185 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002186
Jorim Jaggi192086e2016-03-11 17:17:03 +01002187 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2188 return;
2189 }
2190
Andrii Kulian1e32e022016-09-16 15:29:34 -07002191 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002192 }
2193
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002194 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2195 final ActivityStack stack = getStack(fromStackId);
2196 if (stack == null) {
2197 return;
2198 }
2199
2200 mWindowManager.deferSurfaceLayout();
2201 try {
2202 if (fromStackId == DOCKED_STACK_ID) {
2203
2204 // We are moving all tasks from the docked stack to the fullscreen stack,
2205 // which is dismissing the docked stack, so resize all other stacks to
2206 // fullscreen here already so we don't end up with resize trashing.
2207 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2208 if (StackId.isResizeableByDockedStack(i)) {
2209 ActivityStack otherStack = getStack(i);
2210 if (otherStack != null) {
2211 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2212 true /* allowResizeInDockedMode */, DEFER_RESUME);
2213 }
2214 }
2215 }
2216
2217 // Also disable docked stack resizing since we have manually adjusted the
2218 // size of other stacks above and we don't want to trigger a docked stack
2219 // resize when we remove task from it below and it is detached from the
2220 // display because it no longer contains any tasks.
2221 mAllowDockedStackResize = false;
2222 }
Winson Chungc85d9ce2017-01-03 11:59:52 -08002223 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2224 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2225 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002226 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2227 final int size = tasks.size();
2228 if (onTop) {
2229 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002230 final TaskRecord task = tasks.get(i);
2231 if (fromStackId == PINNED_STACK_ID) {
2232 // Update the return-to to reflect where the pinned stack task was moved
2233 // from so that we retain the stack that was previously visible if the
2234 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002235 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2236 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002237 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002238 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002239 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002240 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002241 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002242
2243 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2244 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002245 } else {
2246 for (int i = size - 1; i >= 0; i--) {
2247 positionTaskInStackLocked(tasks.get(i).taskId,
Andrii Kuliand2765632016-12-12 22:26:34 -08002248 FULLSCREEN_WORKSPACE_STACK_ID, 0 /* position */);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002249 }
2250 }
2251 } finally {
2252 mAllowDockedStackResize = true;
2253 mWindowManager.continueSurfaceLayout();
2254 }
2255 }
2256
Jorim Jaggidc249c42015-12-15 14:57:31 -08002257 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002258 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2259 boolean preserveWindows) {
2260 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2261 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2262 false /* deferResume */);
2263 }
2264
2265 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2266 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2267 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002268
2269 if (!mAllowDockedStackResize) {
2270 // Docked stack resize currently disabled.
2271 return;
2272 }
2273
Jorim Jaggidc249c42015-12-15 14:57:31 -08002274 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2275 if (stack == null) {
2276 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2277 return;
2278 }
2279
2280 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2281 mWindowManager.deferSurfaceLayout();
2282 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002283 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2284 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002285 ActivityRecord r = stack.topRunningActivityLocked();
2286 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2287 tempDockedTaskInsetBounds);
2288
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002289 // TODO: Checking for isAttached might not be needed as if the user passes in null
2290 // dockedBounds then they want the docked stack to be dismissed.
2291 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2292 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002293 // In this case we make all other static stacks fullscreen and move all
2294 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002295 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002296
2297 // stack shouldn't contain anymore activities, so nothing to resume.
2298 r = null;
2299 } else {
2300 // Docked stacks occupy a dedicated region on screen so the size of all other
2301 // static stacks need to be adjusted so they don't overlap with the docked stack.
2302 // We get the bounds to use from window manager which has been adjusted for any
2303 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002304 mWindowManager.getStackDockedModeBounds(
2305 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002306 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002307 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2308 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2309 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002310 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002311 }
2312 }
2313 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002314 if (!deferResume) {
2315 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2316 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002317 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002318 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002319 mWindowManager.continueSurfaceLayout();
2320 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2321 }
2322
2323 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2324 tempDockedTaskBounds != null
2325 || tempDockedTaskInsetBounds != null
2326 || tempOtherTaskBounds != null
2327 || tempOtherTaskInsetBounds != null);
2328 }
2329
Robert Carr0d00c2e2016-02-29 17:45:02 -08002330 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2331 final ActivityStack stack = getStack(PINNED_STACK_ID);
2332 if (stack == null) {
2333 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2334 return;
2335 }
2336 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2337 mWindowManager.deferSurfaceLayout();
2338 try {
2339 ActivityRecord r = stack.topRunningActivityLocked();
2340 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2341 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002342 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002343 } finally {
2344 mWindowManager.continueSurfaceLayout();
2345 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2346 }
2347 }
2348
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002349 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2350 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002351 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002352 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002353 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002354 }
2355
Chong Zhang87b21722015-09-21 15:39:51 -07002356 // If this is a forced resize, let it go through even if the bounds is not changing,
2357 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002358 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002359 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002360 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002361 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002362 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002363 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002364
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002365 if (!mWindowManager.isValidTaskId(task.taskId)) {
2366 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002367 // All we can do for now is update the bounds so it can be used when the task is
2368 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002369 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002370 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002371 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002372 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002373 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002374 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002375 }
2376
skuhne@google.com322347b2016-12-02 12:54:03 -08002377 if (!task.canResizeToBounds(bounds)) {
2378 throw new IllegalArgumentException("resizeTaskLocked: Can not resize task=" + task
2379 + " to bounds=" + bounds + " resizeMode=" + task.mResizeMode);
2380 }
2381
Chong Zhang6de2ae82015-09-30 18:25:21 -07002382 // Do not move the task to another stack here.
2383 // This method assumes that the task is already placed in the right stack.
2384 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002385
Chong Zhang6de2ae82015-09-30 18:25:21 -07002386 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002387
Andrii Kulian8072d112016-09-16 11:11:01 -07002388 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002389 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002390 // way and the activity was kept the way it was. If it's false, it means the activity had
2391 // to be relaunched due to configuration change.
2392 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002393 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002394 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002395 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002396 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002397
2398 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002399 // All other activities must be made visible with their correct configuration.
2400 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2401 if (!kept) {
2402 resumeFocusedStackTopActivityLocked();
2403 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002404 }
2405 }
2406 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002407 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2408 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002409
2410 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002411 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002412 }
2413
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002414 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002415 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2416 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002417 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002418 }
2419
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002420 ActivityContainer activityContainer = new ActivityContainer(stackId);
2421 mActivityContainers.put(stackId, activityContainer);
Andrii Kulian839def92016-11-02 10:58:58 -07002422 activityContainer.addToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002423 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002424 }
2425
Winson Chung010927a2016-12-15 16:12:35 -08002426 /**
2427 * Removes the stack associed with the given {@param stackId}. If the {@param stackId} is the
2428 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2429 * instead moved back onto the fullscreen stack.
2430 */
2431 void removeStackLocked(int stackId) {
2432 final ActivityStack stack = getStack(stackId);
2433 if (stack == null) {
2434 return;
2435 }
2436
2437 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2438 if (stack.getStackId() == PINNED_STACK_ID) {
2439 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2440 if (fullscreenStack != null) {
2441 final boolean isFullscreenStackVisible =
2442 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2443 for (int i = 0; i < tasks.size(); i++) {
2444 // Insert the task either at the top of the fullscreen stack if it is hidden,
2445 // or just under the top task if it is currently visible
2446 final int insertPosition = isFullscreenStackVisible
2447 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2448 : fullscreenStack.getChildCount();
2449 positionTaskInStackLocked(tasks.get(i).taskId, FULLSCREEN_WORKSPACE_STACK_ID,
2450 insertPosition);
2451 }
2452 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2453 resumeFocusedStackTopActivityLocked();
2454 } else {
2455 // If there is no fullscreen stack, then create the stack and move all the tasks
2456 // onto the stack
2457 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, false /* onTop */);
2458 }
2459 } else {
2460 for (int i = tasks.size() - 1; i >= 0; i--) {
2461 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2462 REMOVE_FROM_RECENTS);
2463 }
2464 }
2465 }
2466
2467 /**
2468 * Removes the task with the specified task id.
2469 *
2470 * @param taskId Identifier of the task to be removed.
2471 * @param killProcess Kill any process associated with the task if possible.
2472 * @param removeFromRecents Whether to also remove the task from recents.
2473 * @return Returns true if the given task was found and removed.
2474 */
2475 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2476 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2477 if (tr != null) {
2478 tr.removeTaskActivitiesLocked();
2479 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2480 if (tr.isPersistable) {
2481 mService.notifyTaskPersisterLocked(null, true);
2482 }
2483 return true;
2484 }
2485 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2486 return false;
2487 }
2488
2489 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2490 if (removeFromRecents) {
2491 mRecentTasks.remove(tr);
2492 tr.removedFromRecents();
2493 }
2494 ComponentName component = tr.getBaseIntent().getComponent();
2495 if (component == null) {
2496 Slog.w(TAG, "No component for base intent of task: " + tr);
2497 return;
2498 }
2499
2500 // Find any running services associated with this app and stop if needed.
2501 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2502
2503 if (!killProcess) {
2504 return;
2505 }
2506
2507 // Determine if the process(es) for this task should be killed.
2508 final String pkg = component.getPackageName();
2509 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2510 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2511 for (int i = 0; i < pmap.size(); i++) {
2512
2513 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2514 for (int j = 0; j < uids.size(); j++) {
2515 ProcessRecord proc = uids.valueAt(j);
2516 if (proc.userId != tr.userId) {
2517 // Don't kill process for a different user.
2518 continue;
2519 }
2520 if (proc == mService.mHomeProcess) {
2521 // Don't kill the home process along with tasks from the same package.
2522 continue;
2523 }
2524 if (!proc.pkgList.containsKey(pkg)) {
2525 // Don't kill process that is not associated with this task.
2526 continue;
2527 }
2528
2529 for (int k = 0; k < proc.activities.size(); k++) {
2530 TaskRecord otherTask = proc.activities.get(k).task;
2531 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2532 // Don't kill process(es) that has an activity in a different task that is
2533 // also in recents.
2534 return;
2535 }
2536 }
2537
2538 if (proc.foregroundServices) {
2539 // Don't kill process(es) with foreground service.
2540 return;
2541 }
2542
2543 // Add process to kill list.
2544 procsToKill.add(proc);
2545 }
2546 }
2547
2548 // Kill the running processes.
2549 for (int i = 0; i < procsToKill.size(); i++) {
2550 ProcessRecord pr = procsToKill.get(i);
2551 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2552 && pr.curReceivers.isEmpty()) {
2553 pr.kill("remove task", true);
2554 } else {
2555 // We delay killing processes that are not in the background or running a receiver.
2556 pr.waitingToKill = "remove task";
2557 }
2558 }
2559 }
2560
Craig Mautner4a1cb222013-12-04 16:14:06 -08002561 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002562 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002563 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2564 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002565 break;
2566 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002567 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002568 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002569 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002570 }
2571
Chong Zhang5dcb2752015-08-18 13:50:26 -07002572 /**
2573 * Restores a recent task to a stack
2574 * @param task The recent task to be restored.
2575 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002576 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2577 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002578 * @return true if the task has been restored successfully.
2579 */
2580 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002581 if (!StackId.isStaticStack(stackId)) {
2582 // If stack is not static (or stack id is invalid) - use the default one.
2583 // This means that tasks that were on external displays will be restored on the
2584 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002585 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002586 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2587 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2588 // Put it in the fullscreen stack.
2589 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002590 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002591
Andrii Kulian02b7a832016-10-06 23:11:56 -07002592 final ActivityStack currentStack = task.getStack();
2593 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002594 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002595 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002596 // Nothing else to do since it is already restored in the right stack.
2597 return true;
2598 }
2599 // Remove current stack association, so we can re-associate the task with the
2600 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002601 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002602 }
2603
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002604 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002605 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002606
2607 if (stack == null) {
2608 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002609 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2610 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002611 return false;
2612 }
2613
Wale Ogunwale72919d22016-12-08 18:58:50 -08002614 stack.addTask(task, false /* toTop */, "restoreRecentTask");
2615 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
2616 mWindowManager.addTask(task.taskId, stack.mStackId, task.userId, bounds,
2617 task.getOverrideConfiguration(), task.mResizeMode, task.isHomeTask(),
2618 task.isOnTopLauncher(), false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002619 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2620 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002621 final ArrayList<ActivityRecord> activities = task.mActivities;
2622 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002623 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002624 }
2625 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002626 }
2627
Wale Ogunwale040b4702015-08-06 18:10:50 -07002628 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002629 * Move stack with all its existing content to specified display.
2630 * @param stackId Id of stack to move.
2631 * @param displayId Id of display to move stack to.
2632 */
2633 void moveStackToDisplayLocked(int stackId, int displayId) {
2634 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2635 if (activityDisplay == null) {
2636 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2637 + displayId);
2638 }
2639 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2640 if (activityContainer != null) {
2641 if (activityContainer.isAttachedLocked()) {
2642 if (activityContainer.getDisplayId() == displayId) {
2643 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2644 + " to its current displayId=" + displayId);
2645 }
2646
2647 activityContainer.moveToDisplayLocked(activityDisplay);
2648 } else {
2649 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2650 + stackId + " is not attached to any display.");
2651 }
2652 } else {
2653 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2654 + stackId);
2655 }
2656
2657 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2658 !PRESERVE_WINDOWS);
2659 // TODO(multi-display): resize stacks properly if moved from split-screen.
2660 }
2661
2662 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002663 * Moves the specified task record to the input stack id.
2664 * WARNING: This method performs an unchecked/raw move of the task and
2665 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002666 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002667 * @param task Task to move.
2668 * @param stackId Id of stack to move task to.
2669 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002670 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002671 * @param reason Reason the task is been moved.
2672 * @return The stack the task was moved to.
2673 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002674 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002675 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002676
2677 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2678 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2679 + "support multi-window task=" + task + " to stackId=" + stackId);
2680 }
2681
Wale Ogunwale06579d62016-04-30 15:29:06 -07002682 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002683 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002684 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002685 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002686 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2687 // Whenever we are moving the top activity from the front stack we want to make sure to move
2688 // the stack to the front.
2689 final boolean wasFront = isFrontStack(prevStack)
2690 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002691
Chong Zhangd545dce2016-02-29 18:09:17 -08002692 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002693 // We don't allow moving a unresizeable task to the docked stack since the docked
2694 // stack is used for split-screen mode and will cause things like the docked divider to
2695 // show up. We instead leave the task in its current stack or move it to the fullscreen
2696 // stack if it isn't currently in a stack.
2697 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002698 Slog.w(TAG, "Can not move unresizeable task=" + task
2699 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2700 }
2701
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002702 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2703 // if a docked stack is created below which will lead to the stack we are moving from and
2704 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002705 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002706 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002707 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002708 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002709 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002710
2711 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002712 // move focus to the new stack by moving the stack to the front.
2713 stack.moveToFrontAndResumeStateIfNeeded(
2714 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002715
Wale Ogunwale040b4702015-08-06 18:10:50 -07002716 return stack;
2717 }
2718
Chong Zhange4fbd322016-03-01 14:44:03 -08002719 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002720 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002721 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2722 false /* deferResume */);
2723 }
2724
2725 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2726 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002727 final TaskRecord task = anyTaskForIdLocked(taskId);
2728 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002729 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002730 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002731 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002732
Andrii Kulian02b7a832016-10-06 23:11:56 -07002733 final ActivityStack currentStack = task.getStack();
2734 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002735 // You are already in the right stack silly...
2736 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002737 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002738 }
2739
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002740 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2741 throw new IllegalArgumentException("moveTaskToStack:"
2742 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2743 }
2744
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002745 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002746 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002747 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002748 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002749 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002750 // We are about to relaunch the activity because its configuration changed due to
2751 // being maximized, i.e. size change. The activity will first remove the old window
2752 // and then add a new one. This call will tell window manager about this, so it can
2753 // preserve the old window until the new one is drawn. This prevents having a gap
2754 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002755 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002756 // Note here we always set the replacing window first, as the flags might be needed
2757 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002758 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002759 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002760
2761 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002762 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002763 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002764 try {
2765 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002766 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002767 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002768
Wale Ogunwale961f4852016-02-01 20:25:54 -08002769 if (!animate) {
2770 stack.mNoAnimActivities.add(topActivity);
2771 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002772
Wale Ogunwale961f4852016-02-01 20:25:54 -08002773 // We might trigger a configuration change. Save the current task bounds for freezing.
2774 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2775
2776 // Make sure the task has the appropriate bounds/size for the stack it is in.
2777 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002778 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2779 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002780 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2781 Rect bounds = task.getLaunchBounds();
2782 if (bounds == null) {
2783 stack.layoutTaskInStack(task, null);
2784 bounds = task.mBounds;
2785 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002786 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2787 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002788 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002789 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2790 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002791 }
2792 } finally {
2793 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002794 }
2795
2796 if (mightReplaceWindow) {
2797 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2798 // window), we need to clear the replace window settings. Otherwise, we schedule a
2799 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002800 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002801 }
2802
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002803 if (!deferResume) {
2804
2805 // The task might have already been running and its visibility needs to be synchronized with
2806 // the visibility of the stack / windows.
2807 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2808 resumeFocusedStackTopActivityLocked();
2809 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002810
Jorim Jaggid53f0922016-04-06 22:16:23 -07002811 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002812
2813 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
2815
Wale Ogunwale079a0042015-10-24 11:44:07 -07002816 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2817 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2818 if (stack == null) {
2819 throw new IllegalArgumentException(
2820 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2821 }
2822
2823 final ActivityRecord r = stack.topRunningActivityLocked();
2824 if (r == null) {
2825 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2826 + " in stack=" + stack);
2827 return false;
2828 }
2829
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002830 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002831 Slog.w(TAG,
2832 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002833 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002834 return false;
2835 }
2836
Winson Chungb5c41b72016-12-07 15:00:47 -08002837 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2838 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002839 return true;
2840 }
2841
Winson Chungb5c41b72016-12-07 15:00:47 -08002842 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2843 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002844 mWindowManager.deferSurfaceLayout();
2845 try {
2846 final TaskRecord task = r.task;
2847
Andrii Kulian02b7a832016-10-06 23:11:56 -07002848 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002849 // An activity can't be pinned and visible behind at the same time. Go ahead and
2850 // release it from been visible behind before pinning.
2851 requestVisibleBehindLocked(r, false);
2852 }
2853
Wale Ogunwale480dca02016-02-06 13:58:29 -08002854 // Need to make sure the pinned stack exist so we can resize it below...
2855 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2856
2857 // Resize the pinned stack to match the current size of the task the activity we are
2858 // going to be moving is currently contained in. We do this to have the right starting
2859 // animation bounds for the pinned stack to the desired bounds the caller wants.
2860 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2861 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002862 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002863
2864 if (task.mActivities.size() == 1) {
2865 // There is only one activity in the task. So, we can just move the task over to
2866 // the stack without re-parenting the activity in a different task.
Winson Chungb5c41b72016-12-07 15:00:47 -08002867 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002868 // Move the home stack forward if the task we just moved to the pinned stack
2869 // was launched from home so home should be visible behind it.
2870 moveHomeStackToFront(reason);
2871 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002872 moveTaskToStackLocked(
2873 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2874 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002875 // There are multiple activities in the task and moving the top activity should
2876 // reveal/leave the other activities in their original task
Wale Ogunwale480dca02016-02-06 13:58:29 -08002877 stack.moveActivityToStack(r);
2878 }
2879 } finally {
2880 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002881 }
2882
Wale Ogunwale480dca02016-02-06 13:58:29 -08002883 // The task might have already been running and its visibility needs to be synchronized
2884 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002885 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002886 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002887
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002888 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002889 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002890 }
2891
Chong Zhang6cda19c2016-06-14 19:07:56 -07002892 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2893 if (r == null || !r.isFocusable()) {
2894 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2895 "moveActivityStackToFront: unfocusable r=" + r);
2896 return false;
2897 }
2898
2899 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002900 final ActivityStack stack = r.getStack();
2901 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002902 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2903 + r + " task=" + task);
2904 return false;
2905 }
2906
Chong Zhang6cda19c2016-06-14 19:07:56 -07002907 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2908 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2909 "moveActivityStackToFront: already on top, r=" + r);
2910 return false;
2911 }
2912
2913 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2914 "moveActivityStackToFront: r=" + r);
2915
2916 stack.moveToFront(reason, task);
2917 return true;
2918 }
2919
Andrii Kuliand2765632016-12-12 22:26:34 -08002920 /** @see ActivityManagerService#positionTaskInStack(int, int, int). */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002921 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2922 final TaskRecord task = anyTaskForIdLocked(taskId);
2923 if (task == null) {
2924 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2925 return;
2926 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002927 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2928
2929 task.updateOverrideConfigurationForStack(stack);
2930
Andrii Kuliand2765632016-12-12 22:26:34 -08002931 // TODO: Return final position from WM for AM to use instead of duplicating computations in
2932 // ActivityStack#insertTaskAtPosition.
Wale Ogunwale935e5022015-11-10 12:36:10 -08002933 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002934 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002935 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002936 // The task might have already been running and its visibility needs to be synchronized with
2937 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002938 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002939 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002940 }
2941
Craig Mautnerac6f8432013-07-17 13:24:59 -07002942 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002943 mTmpFindTaskResult.r = null;
2944 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002945 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2947 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2949 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002950 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002951 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002952 continue;
2953 }
2954 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002955 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2956 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002957 continue;
2958 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002959 stack.findTaskLocked(r, mTmpFindTaskResult);
2960 // It is possible to have task in multiple stacks with the same root affinity.
2961 // If the match we found was based on root affinity we keep on looking to see if
2962 // there is a better match in another stack. We eventually return the match based
2963 // on root affinity if we don't find a better match.
2964 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2965 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002966 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002967 }
2968 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002969 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2970 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002971 }
2972
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002973 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2974 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2976 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002978 final ActivityRecord ar = stacks.get(stackNdx)
2979 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002980 if (ar != null) {
2981 return ar;
2982 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002983 }
2984 }
2985 return null;
2986 }
2987
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002989 scheduleSleepTimeout();
2990 if (!mGoingToSleep.isHeld()) {
2991 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002992 if (mLaunchingActivity.isHeld()) {
2993 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2994 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002996 mLaunchingActivity.release();
2997 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002998 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002999 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003000 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003001 }
3002
3003 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003004 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003006 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003007 final long endTime = System.currentTimeMillis() + timeout;
3008 while (true) {
3009 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3011 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003012 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3013 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3014 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003015 }
3016 if (cantShutdown) {
3017 long timeRemaining = endTime - System.currentTimeMillis();
3018 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003020 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003021 } catch (InterruptedException e) {
3022 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003023 } else {
3024 Slog.w(TAG, "Activity manager shutdown timed out");
3025 timedout = true;
3026 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003028 } else {
3029 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003030 }
3031 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003032
3033 // Force checkReadyForSleep to complete.
3034 mSleepTimeout = true;
3035 checkReadyForSleepLocked();
3036
Craig Mautner8d341ef2013-03-26 09:03:27 -07003037 return timedout;
3038 }
3039
3040 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003041 removeSleepTimeouts();
3042 if (mGoingToSleep.isHeld()) {
3043 mGoingToSleep.release();
3044 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003045 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3046 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003047 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3048 final ActivityStack stack = stacks.get(stackNdx);
3049 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003050 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003051 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003052 }
Craig Mautner5314a402013-09-26 12:40:16 -07003053 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003054 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003055 mGoingToSleepActivities.clear();
3056 }
3057
3058 void activitySleptLocked(ActivityRecord r) {
3059 mGoingToSleepActivities.remove(r);
3060 checkReadyForSleepLocked();
3061 }
3062
3063 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003064 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003065 // Do not care.
3066 return;
3067 }
3068
3069 if (!mSleepTimeout) {
3070 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003071 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3072 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003073 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3074 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3075 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003076 }
3077
3078 if (mStoppingActivities.size() > 0) {
3079 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003080 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003081 + mStoppingActivities.size() + " activities");
3082 scheduleIdleLocked();
3083 dontSleep = true;
3084 }
3085
3086 if (mGoingToSleepActivities.size() > 0) {
3087 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003088 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003089 + mGoingToSleepActivities.size() + " activities");
3090 dontSleep = true;
3091 }
3092
3093 if (dontSleep) {
3094 return;
3095 }
3096 }
3097
Wei Wang65c7a152016-06-02 18:51:22 -07003098 // Send launch end powerhint before going sleep
3099 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3100
Craig Mautnere0a38842013-12-16 16:14:02 -08003101 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3102 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3104 stacks.get(stackNdx).goToSleep();
3105 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003106 }
3107
3108 removeSleepTimeouts();
3109
3110 if (mGoingToSleep.isHeld()) {
3111 mGoingToSleep.release();
3112 }
3113 if (mService.mShuttingDown) {
3114 mService.notifyAll();
3115 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003116 }
3117
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003118 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003119 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003120 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003121 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122 }
3123 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003124 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003125 mWindowManager.executeAppTransition();
3126 return true;
3127 }
3128 return false;
3129 }
3130
Craig Mautner8d341ef2013-03-26 09:03:27 -07003131 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003132 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3133 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003134 int stackNdx = stacks.size() - 1;
3135 while (stackNdx >= 0) {
3136 stacks.get(stackNdx).handleAppCrashLocked(app);
3137 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003138 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003139 }
3140 }
3141
Jose Lima4b6c6692014-08-12 17:41:12 -07003142 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003143 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003144 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003145 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3146 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003147 return false;
3148 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003149
3150 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3151 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3152 + " visible=" + visible + " stackId=" + stack.mStackId
3153 + " can't contain visible behind activities");
3154 return false;
3155 }
3156
Jose Lima4b6c6692014-08-12 17:41:12 -07003157 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003158 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3159 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003160
3161 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003162 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003163 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003164 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003165 return true;
3166 }
3167
3168 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003169 if (visible && top.fullscreen) {
3170 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003171 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3172 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3173 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3174 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003175 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003176 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3177 // Only the activity set as currently visible behind should actively reset its
3178 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003179 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3180 "requestVisibleBehind: returning visible=" + visible
3181 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3182 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003183 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003184 }
3185
Jose Lima4b6c6692014-08-12 17:41:12 -07003186 stack.setVisibleBehindActivity(visible ? r : null);
3187 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003188 // If there is a translucent home activity, we need to force it stop being translucent,
3189 // because we can't depend on the application to necessarily perform that operation.
3190 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003191 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003192 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003193 mService.convertFromTranslucent(next.appToken);
3194 }
3195 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003196 if (top.app != null && top.app.thread != null) {
3197 // Notify the top app of the change.
3198 try {
3199 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3200 } catch (RemoteException e) {
3201 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003202 }
3203 return true;
3204 }
3205
Craig Mautnerbb742462014-07-07 15:28:55 -07003206 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003207 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003208 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003209 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003210
3211 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003212 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003213 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003214 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003215 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003216
3217 // When launching tasks behind, update the last active time of the top task after the new
3218 // task has been shown briefly
3219 final ActivityRecord top = stack.topActivity();
3220 if (top != null) {
3221 top.task.touchActiveTime();
3222 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003223 }
3224
3225 void scheduleLaunchTaskBehindComplete(IBinder token) {
3226 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3227 }
3228
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003229 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3230 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003231 mKeyguardController.beginActivityVisibilityUpdate();
3232 try {
3233 // First the front stacks. In case any are not fullscreen and are in front of home.
3234 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3235 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3236 final int topStackNdx = stacks.size() - 1;
3237 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3238 final ActivityStack stack = stacks.get(stackNdx);
3239 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3240 }
Craig Mautner580ea812013-04-25 12:58:38 -07003241 }
Jorim Jaggife762342016-10-13 14:33:27 +02003242 } finally {
3243 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003244 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003245 }
3246
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003247 void invalidateTaskLayers() {
3248 mTaskLayersChanged = true;
3249 }
3250
3251 void rankTaskLayersIfNeeded() {
3252 if (!mTaskLayersChanged) {
3253 return;
3254 }
3255 mTaskLayersChanged = false;
3256 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3257 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3258 int baseLayer = 0;
3259 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3260 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3261 }
3262 }
3263 }
3264
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003265 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3266 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3267 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3268 final int topStackNdx = stacks.size() - 1;
3269 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3270 final ActivityStack stack = stacks.get(stackNdx);
3271 stack.clearOtherAppTimeTrackers(except);
3272 }
3273 }
3274 }
3275
Craig Mautner8d341ef2013-03-26 09:03:27 -07003276 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003277 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3278 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003279 final int numStacks = stacks.size();
3280 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3281 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003282 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 }
3285 }
3286
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003287 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3288 // Examine all activities currently running in the process.
3289 TaskRecord firstTask = null;
3290 // Tasks is non-null only if two or more tasks are found.
3291 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003292 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3293 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003294 ActivityRecord r = app.activities.get(i);
3295 // First, if we find an activity that is in the process of being destroyed,
3296 // then we just aren't going to do anything for now; we want things to settle
3297 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003298 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003299 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003300 return;
3301 }
3302 // Don't consider any activies that are currently not in a state where they
3303 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003304 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3305 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003306 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003307 continue;
3308 }
3309 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003310 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003311 + " from " + r);
3312 if (firstTask == null) {
3313 firstTask = r.task;
3314 } else if (firstTask != r.task) {
3315 if (tasks == null) {
3316 tasks = new ArraySet<>();
3317 tasks.add(firstTask);
3318 }
3319 tasks.add(r.task);
3320 }
3321 }
3322 }
3323 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003324 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003325 return;
3326 }
3327 // If we have activities in multiple tasks that are in a position to be destroyed,
3328 // let's iterate through the tasks and release the oldest one.
3329 final int numDisplays = mActivityDisplays.size();
3330 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3331 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3332 // Step through all stacks starting from behind, to hit the oldest things first.
3333 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3334 final ActivityStack stack = stacks.get(stackNdx);
3335 // Try to release activities in this stack; if we manage to, we are done.
3336 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3337 return;
3338 }
3339 }
3340 }
3341 }
3342
Amith Yamasani37a40c22015-06-17 13:25:42 -07003343 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003344 final int focusStackId = mFocusedStack.getStackId();
3345 // We dismiss the docked stack whenever we switch users.
3346 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003347 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3348 // also cause all tasks to be moved to the fullscreen stack at a position that is
3349 // appropriate.
3350 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003351
3352 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003353 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003354 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003355
Craig Mautner858d8a62013-04-23 17:08:34 -07003356 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003357 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3358 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003359 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003360 final ActivityStack stack = stacks.get(stackNdx);
3361 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003362 TaskRecord task = stack.topTask();
3363 if (task != null) {
3364 mWindowManager.moveTaskToTop(task.taskId);
3365 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003367 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003368
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003369 ActivityStack stack = getStack(restoreStackId);
3370 if (stack == null) {
3371 stack = mHomeStack;
3372 }
3373 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003374 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003375 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003376 } else {
3377 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003378 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003379 }
Craig Mautner93529a42013-10-04 15:03:13 -07003380 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003381 }
3382
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003383 /** Checks whether the userid is a profile of the current user. */
3384 boolean isCurrentProfileLocked(int userId) {
3385 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003386 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003387 }
3388
Craig Mautnerde4ef022013-04-07 19:01:33 -07003389 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003390 ArrayList<ActivityRecord> stops = null;
3391
3392 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003393 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3394 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003395 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3396 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003397 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003398 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003399 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3400 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003401 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003402 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003403 if (s.finishing) {
3404 // If this activity is finishing, it is sitting on top of
3405 // everyone else but we now know it is no longer needed...
3406 // so get rid of it. Otherwise, we need to go through the
3407 // normal flow and hide it once we determine that it is
3408 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003409 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003410 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003411 }
3412 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003413 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003414 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003415 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003416 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003417 }
3418 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003419 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003420 }
3421 }
3422
3423 return stops;
3424 }
3425
Craig Mautnercf910b02013-04-23 11:23:27 -07003426 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003427 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3428 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3429 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3430 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003431 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003432 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003433 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003434 if (r == null) Slog.e(TAG,
3435 "validateTop...: null top activity, stack=" + stack);
3436 else {
3437 final ActivityRecord pausing = stack.mPausingActivity;
3438 if (pausing != null && pausing == r) Slog.e(TAG,
3439 "validateTop...: top stack has pausing activity r=" + r
3440 + " state=" + state);
3441 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3442 "validateTop...: activity in front not resumed r=" + r
3443 + " state=" + state);
3444 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003445 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003446 final ActivityRecord resumed = stack.mResumedActivity;
3447 if (resumed != null && resumed == r) Slog.e(TAG,
3448 "validateTop...: back stack has resumed activity r=" + r
3449 + " state=" + state);
3450 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3451 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003452 }
3453 }
3454 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003455 }
3456
Craig Mautnere0570202015-05-13 13:06:11 -07003457 private String lockTaskModeToString() {
3458 switch (mLockTaskModeState) {
3459 case LOCK_TASK_MODE_LOCKED:
3460 return "LOCKED";
3461 case LOCK_TASK_MODE_PINNED:
3462 return "PINNED";
3463 case LOCK_TASK_MODE_NONE:
3464 return "NONE";
3465 default: return "unknown=" + mLockTaskModeState;
3466 }
3467 }
3468
Craig Mautner27084302013-03-25 08:05:25 -07003469 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003470 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003472 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003473 pw.print(prefix);
3474 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003475 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003476 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003477 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003478 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3479 if (packages.size() > 0) {
3480 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3481 for (int i = 0; i < packages.size(); ++i) {
3482 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3483 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3484 }
3485 }
3486 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3487 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003488 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003489
Winson43d1f262016-06-14 16:05:55 -07003490 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003491 * Dump all connected displays' configurations.
3492 * @param prefix Prefix to apply to each line of the dump.
3493 */
3494 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3495 pw.print(prefix); pw.println("Display override configurations:");
3496 final int displayCount = mActivityDisplays.size();
3497 for (int i = 0; i < displayCount; i++) {
3498 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3499 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3500 pw.println(activityDisplay.getOverrideConfiguration());
3501 }
3502 }
3503
3504 /**
Winson43d1f262016-06-14 16:05:55 -07003505 * Dumps the activities matching the given {@param name} in the either the focused stack
3506 * or all visible stacks if {@param dumpVisibleStacks} is true.
3507 */
3508 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3509 if (dumpVisibleStacks) {
3510 ArrayList<ActivityRecord> activities = new ArrayList<>();
3511 int numDisplays = mActivityDisplays.size();
3512 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3513 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3514 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3515 ActivityStack stack = stacks.get(stackNdx);
3516 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3517 activities.addAll(stack.getDumpActivitiesLocked(name));
3518 }
3519 }
3520 }
3521 return activities;
3522 } else {
3523 return mFocusedStack.getDumpActivitiesLocked(name);
3524 }
Craig Mautner20e72272013-04-01 13:45:53 -07003525 }
3526
Dianne Hackborn390517b2013-05-30 15:03:32 -07003527 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3528 boolean needSep, String prefix) {
3529 if (activity != null) {
3530 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3531 if (needSep) {
3532 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003533 }
3534 pw.print(prefix);
3535 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003536 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003537 }
3538 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003539 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003540 }
3541
Craig Mautner8d341ef2013-03-26 09:03:27 -07003542 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3543 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003544 boolean printed = false;
3545 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3547 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003548 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003549 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003550 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003551 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003552 final ActivityStack stack = stacks.get(stackNdx);
3553 StringBuilder stackHeader = new StringBuilder(128);
3554 stackHeader.append(" Stack #");
3555 stackHeader.append(stack.mStackId);
3556 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003557 stackHeader.append("\n");
3558 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3559 stackHeader.append("\n");
3560 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003561 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3562 needSep, stackHeader.toString());
3563 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3564 !dumpAll, false, dumpPackage, true,
3565 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003566
Craig Mautner4a1cb222013-12-04 16:14:06 -08003567 needSep = printed;
3568 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3569 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003570 if (pr) {
3571 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003572 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003573 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003574 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3575 " mResumedActivity: ");
3576 if (pr) {
3577 printed = true;
3578 needSep = false;
3579 }
3580 if (dumpAll) {
3581 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3582 " mLastPausedActivity: ");
3583 if (pr) {
3584 printed = true;
3585 needSep = true;
3586 }
3587 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3588 needSep, " mLastNoHistoryActivity: ");
3589 }
3590 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003591 }
3592 }
3593
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003594 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3595 false, dumpPackage, true, " Activities waiting to finish:", null);
3596 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3597 false, dumpPackage, true, " Activities waiting to stop:", null);
3598 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3599 false, dumpPackage, true, " Activities waiting for another to become visible:",
3600 null);
3601 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3602 false, dumpPackage, true, " Activities waiting to sleep:", null);
3603 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3604 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003605
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003606 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003607 }
3608
Dianne Hackborn390517b2013-05-30 15:03:32 -07003609 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003610 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003611 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003612 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003613 String innerPrefix = null;
3614 String[] args = null;
3615 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003616 for (int i=list.size()-1; i>=0; i--) {
3617 final ActivityRecord r = list.get(i);
3618 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3619 continue;
3620 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003621 if (innerPrefix == null) {
3622 innerPrefix = prefix + " ";
3623 args = new String[0];
3624 }
3625 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003626 final boolean full = !brief && (complete || !r.isInHistory());
3627 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003628 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629 needNL = false;
3630 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003631 if (header1 != null) {
3632 pw.println(header1);
3633 header1 = null;
3634 }
3635 if (header2 != null) {
3636 pw.println(header2);
3637 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003638 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003639 if (lastTask != r.task) {
3640 lastTask = r.task;
3641 pw.print(prefix);
3642 pw.print(full ? "* " : " ");
3643 pw.println(lastTask);
3644 if (full) {
3645 lastTask.dump(pw, prefix + " ");
3646 } else if (complete) {
3647 // Complete + brief == give a summary. Isn't that obvious?!?
3648 if (lastTask.intent != null) {
3649 pw.print(prefix); pw.print(" ");
3650 pw.println(lastTask.intent.toInsecureStringWithClip());
3651 }
3652 }
3653 }
3654 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3655 pw.print(" #"); pw.print(i); pw.print(": ");
3656 pw.println(r);
3657 if (full) {
3658 r.dump(pw, innerPrefix);
3659 } else if (complete) {
3660 // Complete + brief == give a summary. Isn't that obvious?!?
3661 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3662 if (r.app != null) {
3663 pw.print(innerPrefix); pw.println(r.app);
3664 }
3665 }
3666 if (client && r.app != null && r.app.thread != null) {
3667 // flush anything that is already in the PrintWriter since the thread is going
3668 // to write to the file descriptor directly
3669 pw.flush();
3670 try {
3671 TransferPipe tp = new TransferPipe();
3672 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003673 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003674 // Short timeout, since blocking here can
3675 // deadlock with the application.
3676 tp.go(fd, 2000);
3677 } finally {
3678 tp.kill();
3679 }
3680 } catch (IOException e) {
3681 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3682 } catch (RemoteException e) {
3683 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3684 }
3685 needNL = true;
3686 }
3687 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003688 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003689 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003690
Craig Mautnerf3333272013-04-22 10:55:53 -07003691 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003692 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3693 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003694 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3695 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003696 }
3697
3698 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003699 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003700 }
3701
3702 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003703 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3704 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003705 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3706 }
3707
Craig Mautner05d29032013-05-03 13:40:13 -07003708 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003709 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3710 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3711 }
Craig Mautner05d29032013-05-03 13:40:13 -07003712 }
3713
Craig Mautner0eea92c2013-05-16 13:35:39 -07003714 void removeSleepTimeouts() {
3715 mSleepTimeout = false;
3716 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3717 }
3718
3719 final void scheduleSleepTimeout() {
3720 removeSleepTimeouts();
3721 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3722 }
3723
Craig Mautner4a1cb222013-12-04 16:14:06 -08003724 @Override
3725 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003726 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003727 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3728 }
3729
3730 @Override
3731 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003732 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3734 }
3735
3736 @Override
3737 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003738 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003739 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3740 }
3741
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003742 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003743 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003744 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003745 newDisplay = mActivityDisplays.get(displayId) == null;
3746 if (newDisplay) {
3747 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003748 if (activityDisplay.mDisplay == null) {
3749 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3750 return;
3751 }
Craig Mautner4504de52013-12-20 09:06:56 -08003752 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003753 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003754 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 }
Craig Mautner4504de52013-12-20 09:06:56 -08003756 if (newDisplay) {
3757 mWindowManager.onDisplayAdded(displayId);
3758 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003759 }
3760
Andrii Kulianca007a62016-11-22 16:33:28 -08003761 /** Check if display with specified id is added to the list. */
3762 boolean isDisplayAdded(int displayId) {
3763 return mActivityDisplays.get(displayId) != null;
3764 }
3765
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003766 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003767 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003768 mService.mContext.getResources().getDimensionPixelSize(
3769 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003770 }
3771
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003772 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003774 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3775 if (activityDisplay != null) {
3776 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian03c403d2016-11-11 11:14:12 -08003778 final ActivityStack stack = stacks.get(stackNdx);
3779 // TODO: Implement proper stack removal and ability to choose the behavior -
3780 // remove stack completely or move it to other display.
3781 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003783 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 }
3785 }
3786 mWindowManager.onDisplayRemoved(displayId);
3787 }
3788
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003789 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003791 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3792 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 // TODO: Update the bounds.
3794 }
3795 }
3796 mWindowManager.onDisplayChanged(displayId);
3797 }
3798
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003799 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003800 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 StackInfo info = new StackInfo();
3802 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003803 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003805 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003806 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003807 info.position = display != null
3808 ? display.mStacks.indexOf(stack)
3809 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003810
3811 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3812 final int numTasks = tasks.size();
3813 int[] taskIds = new int[numTasks];
3814 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003815 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003816 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 for (int i = 0; i < numTasks; ++i) {
3818 final TaskRecord task = tasks.get(i);
3819 taskIds[i] = task.taskId;
3820 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3821 : task.realActivity != null ? task.realActivity.flattenToString()
3822 : task.getTopActivity() != null ? task.getTopActivity().packageName
3823 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003824 taskBounds[i] = new Rect();
3825 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003826 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003827 }
3828 info.taskIds = taskIds;
3829 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003830 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003831 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003832
3833 final ActivityRecord top = stack.topRunningActivityLocked();
3834 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 return info;
3836 }
3837
3838 StackInfo getStackInfoLocked(int stackId) {
3839 ActivityStack stack = getStack(stackId);
3840 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003841 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 }
3843 return null;
3844 }
3845
3846 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003847 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003848 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3849 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003851 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003852 }
3853 }
3854 return list;
3855 }
3856
Craig Mautner15df08a2015-04-01 12:17:18 -07003857 TaskRecord getLockedTaskLocked() {
3858 final int top = mLockTaskModeTasks.size() - 1;
3859 if (top >= 0) {
3860 return mLockTaskModeTasks.get(top);
3861 }
3862 return null;
3863 }
3864
3865 boolean isLockedTask(TaskRecord task) {
3866 return mLockTaskModeTasks.contains(task);
3867 }
3868
3869 boolean isLastLockedTask(TaskRecord task) {
3870 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3871 }
3872
3873 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003874 if (!mLockTaskModeTasks.remove(task)) {
3875 return;
3876 }
3877 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3878 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003879 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003880 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3881 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003882 final Message lockTaskMsg = Message.obtain();
3883 lockTaskMsg.arg1 = task.userId;
3884 lockTaskMsg.what = LOCK_TASK_END_MSG;
3885 mHandler.sendMessage(lockTaskMsg);
3886 }
3887 }
3888
Andrii Kulianc27916642016-04-12 17:59:27 -07003889 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3890 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3891 false /* forceNonResizable */);
3892 }
3893
Jorim Jaggid53f0922016-04-06 22:16:23 -07003894 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003895 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003896 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3897 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003898 return;
3899 }
3900
Jorim Jaggicd13d332016-04-27 15:40:20 -07003901 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003902 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003903 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003904 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003905
Andrii Kulianc27916642016-04-12 17:59:27 -07003906 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3907 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003908 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003909 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3910 && !topActivity.noDisplay) {
3911 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003912 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3913 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003914 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003915 }
3916
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003917 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003918 if (mLockTaskNotify != null) {
3919 mLockTaskNotify.showToast(mLockTaskModeState);
3920 }
Jason Monka8f569c2014-07-07 12:15:21 -04003921 }
3922
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003923 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3924 if (mLockTaskModeTasks.contains(task)) {
3925 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3926 }
3927 }
3928
Craig Mautner432f64e2015-05-20 14:59:57 -07003929 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3930 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003931 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003932 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003933 final TaskRecord lockedTask = getLockedTaskLocked();
3934 if (lockedTask != null) {
3935 removeLockedTaskLocked(lockedTask);
3936 if (!mLockTaskModeTasks.isEmpty()) {
3937 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003938 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3939 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003940 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003941 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003942 return;
3943 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003944 }
Craig Mautnere0570202015-05-13 13:06:11 -07003945 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3946 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003947 return;
3948 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003949
3950 // Should have already been checked, but do it again.
3951 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003952 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3953 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003954 return;
3955 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003956 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003957 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003958 return;
3959 }
3960
3961 if (mLockTaskModeTasks.isEmpty()) {
3962 // First locktask.
3963 final Message lockTaskMsg = Message.obtain();
3964 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3965 lockTaskMsg.arg1 = task.userId;
3966 lockTaskMsg.what = LOCK_TASK_START_MSG;
3967 lockTaskMsg.arg2 = lockTaskModeState;
3968 mHandler.sendMessage(lockTaskMsg);
3969 }
3970 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003971 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3972 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003973 mLockTaskModeTasks.remove(task);
3974 mLockTaskModeTasks.add(task);
3975
3976 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003977 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003978 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003979
3980 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003981 findTaskToMoveToFrontLocked(task, 0, null, reason,
3982 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003983 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003984 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003985 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003986 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003987 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003988 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003989 }
3990
3991 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003992 return isLockTaskModeViolation(task, false);
3993 }
3994
3995 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3996 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003997 return false;
3998 }
3999 final int lockTaskAuth = task.mLockTaskAuth;
4000 switch (lockTaskAuth) {
4001 case LOCK_TASK_AUTH_DONT_LOCK:
4002 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004003 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004004 case LOCK_TASK_AUTH_LAUNCHABLE:
4005 case LOCK_TASK_AUTH_WHITELISTED:
4006 return false;
4007 case LOCK_TASK_AUTH_PINNABLE:
4008 // Pinnable tasks can't be launched on top of locktask tasks.
4009 return !mLockTaskModeTasks.isEmpty();
4010 default:
4011 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4012 return true;
4013 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004014 }
4015
Craig Mautner15df08a2015-04-01 12:17:18 -07004016 void onLockTaskPackagesUpdatedLocked() {
4017 boolean didSomething = false;
4018 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4019 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004020 final boolean wasWhitelisted =
4021 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4022 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004023 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004024 final boolean isWhitelisted =
4025 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4026 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4027 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004028 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004029 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4030 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004031 removeLockedTaskLocked(lockedTask);
4032 lockedTask.performClearTaskLocked();
4033 didSomething = true;
4034 }
4035 }
4036 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4037 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4038 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4039 final ActivityStack stack = stacks.get(stackNdx);
4040 stack.onLockTaskPackagesUpdatedLocked();
4041 }
4042 }
Craig Mautnere0570202015-05-13 13:06:11 -07004043 final ActivityRecord r = topRunningActivityLocked();
4044 final TaskRecord task = r != null ? r.task : null;
4045 if (mLockTaskModeTasks.isEmpty() && task != null
4046 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4047 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004048 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4049 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4050 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4051 false);
4052 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004053 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004054 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004055 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004056 }
4057 }
4058
Benjamin Franz43261142015-02-11 15:59:44 +00004059 int getLockTaskModeState() {
4060 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004061 }
4062
Jorim Jaggife89d122015-12-22 16:28:44 +01004063 void activityRelaunchedLocked(IBinder token) {
4064 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004065 if (mService.isSleepingOrShuttingDownLocked()) {
4066 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4067 if (r != null) {
4068 r.setSleeping(true, true);
4069 }
4070 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004071 }
4072
4073 void activityRelaunchingLocked(ActivityRecord r) {
4074 mWindowManager.notifyAppRelaunching(r.appToken);
4075 }
4076
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004077 void logStackState() {
4078 mActivityMetricsLogger.logWindowState();
4079 }
4080
Andrii Kulian933076d2016-03-29 17:04:42 -07004081 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004082 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4083 final ActivityRecord r = task.mActivities.get(i);
4084 if (r.app != null && r.app.thread != null) {
4085 mMultiWindowModeChangedActivities.add(r);
4086 }
4087 }
4088
4089 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4090 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4091 }
4092 }
4093
Andrii Kulian933076d2016-03-29 17:04:42 -07004094 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004095 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004096 if (prevStack == null || prevStack == stack
4097 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4098 return;
4099 }
4100
4101 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4102 final ActivityRecord r = task.mActivities.get(i);
4103 if (r.app != null && r.app.thread != null) {
4104 mPipModeChangedActivities.add(r);
4105 }
4106 }
4107
4108 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4109 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4110 }
4111 }
4112
Tony Mak853304c2016-04-18 15:17:41 +01004113 void setDockedStackMinimized(boolean minimized) {
4114 mIsDockMinimized = minimized;
4115 if (minimized) {
4116 // Docked stack is not visible, no need to confirm credentials for its top activity.
4117 return;
4118 }
4119 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
4120 if (dockedStack == null) {
4121 return;
4122 }
4123 final ActivityRecord top = dockedStack.topRunningActivityLocked();
4124 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
4125 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
4126 }
4127 }
4128
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004129 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004130
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004131 public ActivityStackSupervisorHandler(Looper looper) {
4132 super(looper);
4133 }
4134
Craig Mautnerf3333272013-04-22 10:55:53 -07004135 void activityIdleInternal(ActivityRecord r) {
4136 synchronized (mService) {
4137 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4138 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004139 }
4140
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004141 @Override
4142 public void handleMessage(Message msg) {
4143 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004144 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4145 synchronized (mService) {
4146 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4147 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004148 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004149 }
4150 }
4151 } break;
4152 case REPORT_PIP_MODE_CHANGED_MSG: {
4153 synchronized (mService) {
4154 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4155 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004156 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004157 }
4158 }
4159 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004160 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004161 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4162 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004163 if (mService.mDidDexOpt) {
4164 mService.mDidDexOpt = false;
4165 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4166 nmsg.obj = msg.obj;
4167 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4168 return;
4169 }
4170 // We don't at this point know if the activity is fullscreen,
4171 // so we need to be conservative and assume it isn't.
4172 activityIdleInternal((ActivityRecord)msg.obj);
4173 } break;
4174 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004175 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004176 activityIdleInternal((ActivityRecord)msg.obj);
4177 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004178 case RESUME_TOP_ACTIVITY_MSG: {
4179 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004180 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004181 }
4182 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004183 case SLEEP_TIMEOUT_MSG: {
4184 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004185 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004186 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4187 mSleepTimeout = true;
4188 checkReadyForSleepLocked();
4189 }
4190 }
4191 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004192 case LAUNCH_TIMEOUT_MSG: {
4193 if (mService.mDidDexOpt) {
4194 mService.mDidDexOpt = false;
4195 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4196 return;
4197 }
4198 synchronized (mService) {
4199 if (mLaunchingActivity.isHeld()) {
4200 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4201 if (VALIDATE_WAKE_LOCK_CALLER
4202 && Binder.getCallingUid() != Process.myUid()) {
4203 throw new IllegalStateException("Calling must be system uid");
4204 }
4205 mLaunchingActivity.release();
4206 }
4207 }
4208 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004210 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004211 } break;
4212 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004213 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 } break;
4215 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004216 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004217 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004218 case CONTAINER_CALLBACK_VISIBILITY: {
4219 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004220 final IActivityContainerCallback callback = container.mCallback;
4221 if (callback != null) {
4222 try {
4223 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4224 } catch (RemoteException e) {
4225 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004226 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004227 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004228 case LOCK_TASK_START_MSG: {
4229 // When lock task starts, we disable the status bars.
4230 try {
Jason Monk62515be2014-05-21 16:06:19 -04004231 if (mLockTaskNotify == null) {
4232 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004233 }
Jason Monk62515be2014-05-21 16:06:19 -04004234 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004235 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004236 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004237 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004238 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004239 flags = StatusBarManager.DISABLE_MASK
4240 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004241 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004242 flags = StatusBarManager.DISABLE_MASK
4243 & (~StatusBarManager.DISABLE_BACK)
4244 & (~StatusBarManager.DISABLE_HOME)
4245 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004246 }
4247 getStatusBarService().disable(flags, mToken,
4248 mService.mContext.getPackageName());
4249 }
4250 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004251 if (getDevicePolicyManager() != null) {
4252 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004253 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004254 }
justinzhang5286d3f2014-05-12 17:06:01 -04004255 } catch (RemoteException ex) {
4256 throw new RuntimeException(ex);
4257 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004258 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004259 case LOCK_TASK_END_MSG: {
4260 // When lock task ends, we enable the status bars.
4261 try {
Jason Monk62515be2014-05-21 16:06:19 -04004262 if (getStatusBarService() != null) {
4263 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4264 mService.mContext.getPackageName());
4265 }
4266 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004267 if (getDevicePolicyManager() != null) {
4268 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4269 msg.arg1);
4270 }
Jason Monk62515be2014-05-21 16:06:19 -04004271 if (mLockTaskNotify == null) {
4272 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4273 }
4274 mLockTaskNotify.show(false);
4275 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004276 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004277 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004278 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004279 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004280 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004281 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004282 new LockPatternUtils(mService.mContext)
4283 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004284 }
4285 } catch (SettingNotFoundException e) {
4286 // No setting, don't lock.
4287 }
justinzhang5286d3f2014-05-12 17:06:01 -04004288 } catch (RemoteException ex) {
4289 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004290 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004291 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004292 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004293 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004294 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4295 if (mLockTaskNotify == null) {
4296 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4297 }
4298 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4299 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004300 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4301 final ActivityContainer container = (ActivityContainer) msg.obj;
4302 final IActivityContainerCallback callback = container.mCallback;
4303 if (callback != null) {
4304 try {
4305 callback.onAllActivitiesComplete(container.asBinder());
4306 } catch (RemoteException e) {
4307 }
4308 }
4309 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004310 case LAUNCH_TASK_BEHIND_COMPLETE: {
4311 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004312 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004313 if (r != null) {
4314 handleLaunchTaskBehindCompleteLocked(r);
4315 }
4316 }
4317 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004318
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004319 }
4320 }
4321 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004322
Ying Wangb081a592014-04-22 15:20:16 -07004323 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004324 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4325 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004326 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004327 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004328 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004329 ActivityRecord mParentActivity = null;
4330 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004331
Craig Mautnere3a00d72014-04-16 08:31:19 -07004332 boolean mVisible = true;
4333
Craig Mautner4a1cb222013-12-04 16:14:06 -08004334 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004335 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004336
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004337 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4338 final static int CONTAINER_STATE_NO_SURFACE = 1;
4339 final static int CONTAINER_STATE_FINISHING = 2;
4340 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4341
4342 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004343 synchronized (mService) {
4344 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004345 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004346 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004347 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004349 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004350
Andrii Kulian839def92016-11-02 10:58:58 -07004351 /**
4352 * Adds the stack to specified display. Also calls WindowManager to do the same from
4353 * {@link ActivityStack#addToDisplay(ActivityDisplay, boolean)}.
4354 * @param activityDisplay The display to add the stack to.
4355 * @param onTop If true the stack will be place at the top of the display, else at the
4356 * bottom.
4357 */
4358 void addToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
4359 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004360 + " to display=" + activityDisplay + " onTop=" + onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004361 if (mActivityDisplay != null) {
4362 throw new IllegalStateException("ActivityContainer is already attached, " +
4363 "displayId=" + mActivityDisplay.mDisplayId);
4364 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004365 mActivityDisplay = activityDisplay;
Andrii Kulian839def92016-11-02 10:58:58 -07004366 mStack.addToDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004367 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004368 }
4369
4370 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004371 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004372 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004373 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4374 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004375 return;
4376 }
Andrii Kuliand2765632016-12-12 22:26:34 -08004377 addToDisplayLocked(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004378 }
4379 }
4380
4381 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004382 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004383 synchronized (mService) {
4384 if (mActivityDisplay != null) {
4385 return mActivityDisplay.mDisplayId;
4386 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004387 }
4388 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004389 }
4390
Jeff Brownca9bc702014-02-11 14:32:56 -08004391 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004392 public int getStackId() {
4393 synchronized (mService) {
4394 return mStackId;
4395 }
4396 }
4397
4398 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004399 public boolean injectEvent(InputEvent event) {
4400 final long origId = Binder.clearCallingIdentity();
4401 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004402 synchronized (mService) {
4403 if (mActivityDisplay != null) {
4404 return mInputManagerInternal.injectInputEvent(event,
4405 mActivityDisplay.mDisplayId,
4406 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4407 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004408 }
4409 return false;
4410 } finally {
4411 Binder.restoreCallingIdentity(origId);
4412 }
4413 }
4414
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004415 @Override
4416 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004417 synchronized (mService) {
4418 if (mContainerState == CONTAINER_STATE_FINISHING) {
4419 return;
4420 }
4421 mContainerState = CONTAINER_STATE_FINISHING;
4422
Craig Mautnerd163e752014-06-13 17:18:47 -07004423 long origId = Binder.clearCallingIdentity();
4424 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004425 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004426 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004427 } finally {
4428 Binder.restoreCallingIdentity(origId);
4429 }
4430 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004431 }
4432
Andrii Kulian03c403d2016-11-11 11:14:12 -08004433 /**
4434 * Remove the stack completely. Must be called only when there are no tasks left in it,
4435 * as this method does not finish running activities.
4436 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004437 void removeLocked() {
4438 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004439 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004440 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004441 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004443 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444 }
4445
Andrii Kulian839def92016-11-02 10:58:58 -07004446 /**
4447 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4448 * completely or re-parented.
4449 */
4450 private void removeFromDisplayLocked() {
4451 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4452 + " current displayId=" + mActivityDisplay.mDisplayId);
4453
4454 mActivityDisplay.detachActivitiesLocked(mStack);
4455 mActivityDisplay = null;
4456 }
4457
4458 /**
4459 * Move the stack to specified display.
4460 * @param activityDisplay Target display to move the stack to.
4461 */
4462 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4463 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4464 + mActivityDisplay + " to display=" + activityDisplay
4465 + " Callers=" + Debug.getCallers(2));
4466
4467 removeFromDisplayLocked();
4468
4469 mActivityDisplay = activityDisplay;
4470 mStack.moveToDisplay(activityDisplay);
4471 activityDisplay.attachActivities(mStack, ON_TOP);
4472 }
4473
Craig Mautner4a1cb222013-12-04 16:14:06 -08004474 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004475 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004476 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 }
4478
4479 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004480 public final int startActivityIntentSender(IIntentSender intentSender)
4481 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004482 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4483
4484 if (!(intentSender instanceof PendingIntentRecord)) {
4485 throw new IllegalArgumentException("Bad PendingIntent object");
4486 }
4487
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004488 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004489 Binder.getCallingUid(), mCurrentUser, false,
4490 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004491
4492 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4493 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4494 pendingIntent.key.requestResolvedType);
4495
4496 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4497 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4498 }
4499
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004500 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004501 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004502 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004503 throw new SecurityException(
4504 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4505 }
4506 }
4507
Craig Mautnerdf88d732014-01-27 09:21:32 -08004508 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004509 public IBinder asBinder() {
4510 return this;
4511 }
4512
Craig Mautner4504de52013-12-20 09:06:56 -08004513 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004514 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004515 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004516 }
4517
Craig Mautner4a1cb222013-12-04 16:14:06 -08004518 ActivityStackSupervisor getOuter() {
4519 return ActivityStackSupervisor.this;
4520 }
4521
Craig Mautnerd163e752014-06-13 17:18:47 -07004522 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004523 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004524 }
4525
Craig Mautner6985bad2014-04-21 15:22:06 -07004526 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004527 void setVisible(boolean visible) {
4528 if (mVisible != visible) {
4529 mVisible = visible;
4530 if (mCallback != null) {
4531 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4532 0 /* unused */, this).sendToTarget();
4533 }
4534 }
4535 }
4536
Craig Mautner6985bad2014-04-21 15:22:06 -07004537 void setDrawn() {
4538 }
4539
Craig Mautner1b4bf852014-05-26 15:06:32 -07004540 // You can always start a new task on a regular ActivityStack.
4541 boolean isEligibleForNewTasks() {
4542 return true;
4543 }
4544
Craig Mautnerd163e752014-06-13 17:18:47 -07004545 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004546 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004547 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004548 }
4549
Craig Mautner34b73df2014-01-12 21:11:08 -08004550 @Override
4551 public String toString() {
4552 return mIdString + (mActivityDisplay == null ? "N" : "A");
4553 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004554 }
4555
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004556 private class VirtualActivityContainer extends ActivityContainer {
4557 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004558 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004559
4560 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4561 super(getNextStackId());
4562 mParentActivity = parent;
4563 mCallback = callback;
4564 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004565 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004566 }
4567
4568 @Override
4569 public void setSurface(Surface surface, int width, int height, int density) {
4570 super.setSurface(surface, width, height, density);
4571
4572 synchronized (mService) {
4573 final long origId = Binder.clearCallingIdentity();
4574 try {
4575 setSurfaceLocked(surface, width, height, density);
4576 } finally {
4577 Binder.restoreCallingIdentity(origId);
4578 }
4579 }
4580 }
4581
4582 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4583 if (mContainerState == CONTAINER_STATE_FINISHING) {
4584 return;
4585 }
4586 VirtualActivityDisplay virtualActivityDisplay =
4587 (VirtualActivityDisplay) mActivityDisplay;
4588 if (virtualActivityDisplay == null) {
4589 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004590 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004591 mActivityDisplay = virtualActivityDisplay;
4592 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Andrii Kuliand2765632016-12-12 22:26:34 -08004593 addToDisplayLocked(virtualActivityDisplay, true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004594 }
4595
4596 if (mSurface != null) {
4597 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004598 }
4599
Craig Mautner6985bad2014-04-21 15:22:06 -07004600 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004601 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004602 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004603 } else {
4604 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004605 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004606 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004607 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004608 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004609 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004610
Craig Mautnerd163e752014-06-13 17:18:47 -07004611 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004612
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004613 if (DEBUG_STACK) Slog.d(TAG_STACK,
4614 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004615 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004616
Craig Mautner6985bad2014-04-21 15:22:06 -07004617 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004618 boolean isAttachedLocked() {
4619 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004620 }
4621
4622 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004623 void setDrawn() {
4624 synchronized (mService) {
4625 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004626 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004627 }
4628 }
4629
Craig Mautner1b4bf852014-05-26 15:06:32 -07004630 // Never start a new task on an ActivityView if it isn't explicitly specified.
4631 @Override
4632 boolean isEligibleForNewTasks() {
4633 return false;
4634 }
4635
Craig Mautnerd163e752014-06-13 17:18:47 -07004636 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004637 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004638 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4639 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4640 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4641 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4642 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004643 }
4644 }
4645
Craig Mautner4a1cb222013-12-04 16:14:06 -08004646 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4647 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004648 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004649 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004650 int mDisplayId;
4651 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004652 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4653 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004654
Craig Mautner4a1cb222013-12-04 16:14:06 -08004655 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4656 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004657 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004658
Jose Lima4b6c6692014-08-12 17:41:12 -07004659 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004660
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004661 ActivityDisplay() {
4662 }
Craig Mautner4504de52013-12-20 09:06:56 -08004663
Craig Mautner1a70a162014-09-13 12:09:31 -07004664 // After instantiation, check that mDisplay is not null before using this. The alternative
4665 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004666 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004667 final Display display = mDisplayManager.getDisplay(displayId);
4668 if (display == null) {
4669 return;
4670 }
4671 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004672 }
4673
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004674 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004675 mDisplay = display;
4676 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004677 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004678
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004679 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004680 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004681 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4682 + " onTop=" + onTop);
4683 if (onTop) {
4684 mStacks.add(stack);
4685 } else {
4686 mStacks.add(0, stack);
4687 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004688 }
4689
Craig Mautnere0a38842013-12-16 16:14:02 -08004690 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004691 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004692 + " from displayId=" + mDisplayId);
4693 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004694 }
4695
Jose Lima4b6c6692014-08-12 17:41:12 -07004696 void setVisibleBehindActivity(ActivityRecord r) {
4697 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004698 }
4699
Jose Lima4b6c6692014-08-12 17:41:12 -07004700 boolean hasVisibleBehindActivity() {
4701 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004702 }
4703
Craig Mautner34b73df2014-01-12 21:11:08 -08004704 @Override
4705 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004706 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4707 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004708
4709 @Override
4710 protected int getChildCount() {
4711 return mStacks.size();
4712 }
4713
4714 @Override
4715 protected ConfigurationContainer getChildAt(int index) {
4716 return mStacks.get(index);
4717 }
4718
4719 @Override
4720 protected ConfigurationContainer getParent() {
4721 return ActivityStackSupervisor.this;
4722 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004723 }
4724
4725 class VirtualActivityDisplay extends ActivityDisplay {
4726 VirtualDisplay mVirtualDisplay;
4727
Craig Mautner6985bad2014-04-21 15:22:06 -07004728 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004729 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004730 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4731 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4732 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4733 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004734
4735 init(mVirtualDisplay.getDisplay());
4736
4737 mWindowManager.handleDisplayAdded(mDisplayId);
4738 }
4739
4740 void setSurface(Surface surface) {
4741 if (mVirtualDisplay != null) {
4742 mVirtualDisplay.setSurface(surface);
4743 }
4744 }
4745
4746 @Override
4747 void detachActivitiesLocked(ActivityStack stack) {
4748 super.detachActivitiesLocked(stack);
4749 if (mVirtualDisplay != null) {
4750 mVirtualDisplay.release();
4751 mVirtualDisplay = null;
4752 }
4753 }
4754
4755 @Override
4756 public String toString() {
4757 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004758 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004759 }
Jose Lima58e66d62014-05-27 20:00:27 -07004760
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004761 ActivityStack findStackBehind(ActivityStack stack) {
4762 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004763 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004764 if (display == null) {
4765 return null;
4766 }
4767 final ArrayList<ActivityStack> stacks = display.mStacks;
4768 for (int i = stacks.size() - 1; i >= 0; i--) {
4769 if (stacks.get(i) == stack && i > 0) {
4770 return stacks.get(i - 1);
4771 }
4772 }
4773 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4774 + " in=" + stacks);
4775 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004776
Jorim Jaggic69bd222016-03-15 14:38:37 +01004777 /**
4778 * Puts a task into resizing mode during the next app transition.
4779 *
4780 * @param taskId the id of the task to put into resizing mode
4781 */
4782 private void setResizingDuringAnimation(int taskId) {
4783 mResizingTasksDuringAnimation.add(taskId);
4784 mWindowManager.setTaskDockedResizing(taskId, true);
4785 }
4786
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004787 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4788 final TaskRecord task;
4789 final int callingUid;
4790 final String callingPackage;
4791 final Intent intent;
4792 final int userId;
4793 final ActivityOptions activityOptions = (bOptions != null)
4794 ? new ActivityOptions(bOptions) : null;
4795 final int launchStackId = (activityOptions != null)
4796 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004797 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004798 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004799 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004800 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004801
4802 if (launchStackId == DOCKED_STACK_ID) {
4803 mWindowManager.setDockedStackCreateState(
4804 activityOptions.getDockCreateMode(), null /* initialBounds */);
4805
4806 // Defer updating the stack in which recents is until the app transition is done, to
4807 // not run into issues where we still need to draw the task in recents but the
4808 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004809 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004810 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4811 }
4812
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004813 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4814 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004815 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004816 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004817 throw new IllegalArgumentException(
4818 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4819 }
4820
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004821 // Since we don't have an actual source record here, we assume that the currently focused
4822 // activity was the source.
4823 final ActivityStack focusedStack = getFocusedStack();
4824 final ActivityRecord sourceRecord =
4825 focusedStack != null ? focusedStack.topActivity() : null;
4826
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004827 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004828 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004829 moveTaskToStackLocked(
4830 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4831 ANIMATE);
4832 }
4833 }
4834
4835 // If the user must confirm credentials (e.g. when first launching a work app and the
4836 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4837 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4838 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004839 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004840 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004841 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004842 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4843 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004844
4845 // If we are launching the task in the docked stack, put it into resizing mode so
4846 // the window renders full-screen with the background filling the void. Also only
4847 // call this at the end to make sure that tasks exists on the window manager side.
4848 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004849 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004850 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004851
4852 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4853 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004854 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4855 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004856 return ActivityManager.START_TASK_TO_FRONT;
4857 }
4858 callingUid = task.mCallingUid;
4859 callingPackage = task.mCallingPackage;
4860 intent = task.intent;
4861 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4862 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004863 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4864 null, null, 0, 0, bOptions, userId, null, task);
4865 if (launchStackId == DOCKED_STACK_ID) {
4866 setResizingDuringAnimation(task.taskId);
4867 }
4868 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004869 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004870
4871 /**
4872 * @return a list of activities which are the top ones in each visible stack. The first
4873 * entry will be the focused activity.
4874 */
4875 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004876 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4877 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004878 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004879 if (display == null) {
4880 return Collections.EMPTY_LIST;
4881 }
4882 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4883 final ArrayList<ActivityStack> stacks = display.mStacks;
4884 for (int i = stacks.size() - 1; i >= 0; i--) {
4885 ActivityStack stack = stacks.get(i);
4886 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4887 ActivityRecord top = stack.topActivity();
4888 if (top != null) {
4889 if (stack == mFocusedStack) {
4890 topActivityTokens.add(0, top.appToken);
4891 } else {
4892 topActivityTokens.add(top.appToken);
4893 }
4894 }
4895 }
4896 }
4897 return topActivityTokens;
4898 }
Craig Mautner27084302013-03-25 08:05:25 -07004899}