blob: 83a786f626bf6fdc366d4db4a549db2e98330e4a [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;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080092import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020093
Svetoslav7008b512015-06-24 18:47:07 -070094import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -070095import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +010096import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080099import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700100import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.app.ActivityOptions;
103import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700106import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400108import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700109import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400110import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700112import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700114import android.content.Intent;
115import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700117import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.pm.PackageManager;
119import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100120import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700122import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800123import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124import android.hardware.display.DisplayManager;
125import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800126import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800127import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800128import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800129import android.hardware.input.InputManager;
130import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100132import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700133import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700134import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700136import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700137import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700139import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700140import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700141import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400142import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700143import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700144import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700145import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700146import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100147import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700148import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700149import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400150import android.provider.Settings;
151import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700152import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700153import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700154import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700155import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700156import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800157import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700158import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800159import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800161import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800162import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800163import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800164
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800165import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700166import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400167import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700168import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400169import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800170import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700171import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700173
Craig Mautner8d341ef2013-03-26 09:03:27 -0700174import java.io.FileDescriptor;
175import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700176import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700177import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700178import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700179import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700180import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700181import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700182
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800183public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700186 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700188 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800195 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800197
Craig Mautnerf3333272013-04-22 10:55:53 -0700198 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700200
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700203
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206
Craig Mautner05d29032013-05-03 13:40:13 -0700207 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
208 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
209 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
213 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
214 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700215 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400216 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
217 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700218 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700219 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700220 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800221 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
222 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700225
Jason Monk62515be2014-05-21 16:06:19 -0400226 private static final String LOCK_TASK_TAG = "Lock-to-App";
227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 // Used to indicate if an object (e.g. stack) that we are trying to get
229 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800230 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700232 // Used to indicate that windows of activities should be preserved during the resize.
233 static final boolean PRESERVE_WINDOWS = true;
234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. task) should be moved/created
236 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700237 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
239 // Used to indicate that an objects (e.g. task) removal from its container
240 // (e.g. stack) is due to it moving to another container.
241 static final boolean MOVING = true;
242
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700243 // Force the focus to change to the stack we are moving a task to..
244 static final boolean FORCE_FOCUS = true;
245
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700246 // Restore task from the saved recents if it can't be found in any live stack.
247 static final boolean RESTORE_FROM_RECENTS = true;
248
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700249 // Don't execute any calls to resume.
250 static final boolean DEFER_RESUME = true;
251
Winson Chung010927a2016-12-15 16:12:35 -0800252 // Used to indicate that a task is removed it should also be removed from recents.
253 static final boolean REMOVE_FROM_RECENTS = true;
254
Svetoslav7008b512015-06-24 18:47:07 -0700255 // Activity actions an app cannot start if it uses a permission which is not granted.
256 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
257 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700258
Svetoslav7008b512015-06-24 18:47:07 -0700259 static {
260 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
261 Manifest.permission.CAMERA);
262 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
263 Manifest.permission.CAMERA);
264 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
265 Manifest.permission.CALL_PHONE);
266 }
267
Svet Ganov99b60432015-06-27 13:15:22 -0700268 /** Action restriction: launching the activity is not restricted. */
269 private static final int ACTIVITY_RESTRICTION_NONE = 0;
270 /** Action restriction: launching the activity is restricted by a permission. */
271 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
272 /** Action restriction: launching the activity is restricted by an app op. */
273 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700274
justinzhang5286d3f2014-05-12 17:06:01 -0400275 /** Status Bar Service **/
276 private IBinder mToken = new Binder();
277 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400278 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400279
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700280 // For debugging to make sure the caller when acquiring/releasing our
281 // wake lock is the system process.
282 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /** The number of distinct task ids that can be assigned to the tasks of a single user */
284 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Craig Mautner27084302013-03-25 08:05:25 -0700286 final ActivityManagerService mService;
287
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290 final ActivityStackSupervisorHandler mHandler;
291
292 /** Short cut */
293 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700296 /** Counter for next free stack ID to use for dynamic activity stacks. */
297 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800299 /**
300 * Maps the task identifier that activities are currently being started in to the userId of the
301 * task. Each time a new task is created, the entry for the userId of the task is incremented
302 */
303 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700304
Craig Mautner2420ead2013-04-01 17:13:20 -0700305 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700307
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 /** The stack containing the launcher app. Assumed to always be attached to
309 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800313 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700314
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
316 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800318 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
320 /** List of activities that are waiting for a new activity to become visible before completing
321 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800322 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700325 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326
327 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700328 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330 /** List of activities that are ready to be stopped, but waiting for the next activity to
331 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700333
Craig Mautnerf3333272013-04-22 10:55:53 -0700334 /** List of activities that are ready to be finished, but waiting for the previous activity to
335 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700337
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800339 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340
Wale Ogunwale22e25262016-02-01 10:32:02 -0800341 /** List of activities whose multi-window mode changed that we need to report to the
342 * application */
343 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
344
345 /** List of activities whose picture-in-picture mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
348
Craig Mautnerf3333272013-04-22 10:55:53 -0700349 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700350 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700351
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
353 * is being brought in front of us. */
354 boolean mUserLeaving = false;
355
Craig Mautner0eea92c2013-05-16 13:35:39 -0700356 /** Set when we have taken too long waiting to go to sleep. */
357 boolean mSleepTimeout = false;
358
359 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360 * We don't want to allow the device to go to sleep while in the process
361 * of launching an activity. This is primarily to allow alarm intent
362 * receivers to launch an activity and get that to run before the device
363 * goes back to sleep.
364 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700365 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700366
367 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368 * Set when the system is going to sleep, until we have
369 * successfully paused the current activity and released our wake lock.
370 * At that point the system is allowed to actually sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700374 /** Stack id of the front stack when user switched, indexed by userId. */
375 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700376
Craig Mautner4504de52013-12-20 09:06:56 -0800377 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800379 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380
381 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800384 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
385
386 private DisplayManagerInternal mDisplayManagerInternal;
387 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800388
Craig Mautner15df08a2015-04-01 12:17:18 -0700389 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
390 * may be finished until there is only one entry left. If this is empty the system is not
391 * in lockTask mode. */
392 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000393 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700394 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
395 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000396 */
397 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400398 /**
399 * Notifies the user when entering/exiting lock-task.
400 */
401 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800402
Wale Ogunwaled046a012015-12-24 13:05:59 -0800403 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800404 boolean inResumeTopActivity;
405
Andrii Kulian1e32e022016-09-16 15:29:34 -0700406 /**
407 * Temporary rect used during docked stack resize calculation so we don't need to create a new
408 * object each time.
409 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700410 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700411
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700412 // The default minimal size that will be used if the activity doesn't specify its minimal size.
413 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700414 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700415
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700416 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
417 private boolean mTaskLayersChanged = true;
418
Jorim Jaggi275561a2016-02-23 10:11:02 -0500419 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800420
Andrii Kulian1779e612016-10-12 21:58:25 -0700421 @Override
422 protected int getChildCount() {
423 return mActivityDisplays.size();
424 }
425
426 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700427 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700428 return mActivityDisplays.valueAt(index);
429 }
430
431 @Override
432 protected ConfigurationContainer getParent() {
433 return null;
434 }
435
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700436 Configuration getDisplayOverrideConfiguration(int displayId) {
437 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
438 if (activityDisplay == null) {
439 throw new IllegalArgumentException("No display found with id: " + displayId);
440 }
441
442 return activityDisplay.getOverrideConfiguration();
443 }
444
445 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
446 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
447 if (activityDisplay == null) {
448 throw new IllegalArgumentException("No display found with id: " + displayId);
449 }
450
451 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
452 }
453
Wale Ogunwale39381972015-12-17 17:15:29 -0800454 static class FindTaskResult {
455 ActivityRecord r;
456 boolean matchedByRootAffinity;
457 }
458 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
459
Craig Mautneree36c772014-07-16 14:56:05 -0700460 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800461 * Temp storage for display ids sorted in focus order.
462 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
463 * it's more efficient, as the number of displays is usually small.
464 */
465 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
466
467 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100468 * Used to keep track whether app visibilities got changed since the last pause. Useful to
469 * determine whether to invoke the task stack change listener after pausing.
470 */
471 boolean mAppVisibilitiesChangedSinceLastPause;
472
473 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100474 * Set of tasks that are in resizing mode during an app transition to fill the "void".
475 */
476 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
477
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700478
479 /**
480 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
481 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
482 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
483 * like the docked stack going empty.
484 */
485 private boolean mAllowDockedStackResize = true;
486
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100487 /**
Tony Mak853304c2016-04-18 15:17:41 +0100488 * Is dock currently minimized.
489 */
490 boolean mIsDockMinimized;
491
Jorim Jaggife762342016-10-13 14:33:27 +0200492 final KeyguardController mKeyguardController;
493
Tony Mak853304c2016-04-18 15:17:41 +0100494 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700495 * Description of a request to start a new activity, which has been held
496 * due to app switches being disabled.
497 */
498 static class PendingActivityLaunch {
499 final ActivityRecord r;
500 final ActivityRecord sourceRecord;
501 final int startFlags;
502 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700503 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700504
505 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700506 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700507 r = _r;
508 sourceRecord = _sourceRecord;
509 startFlags = _startFlags;
510 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700511 callerApp = _callerApp;
512 }
513
514 void sendErrorResult(String message) {
515 try {
516 if (callerApp.thread != null) {
517 callerApp.thread.scheduleCrash(message);
518 }
519 } catch (RemoteException e) {
520 Slog.e(TAG, "Exception scheduling crash of failed "
521 + "activity launcher sourceRecord=" + sourceRecord, e);
522 }
Craig Mautneree36c772014-07-16 14:56:05 -0700523 }
524 }
525
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800526 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700527 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700528 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800529 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200530 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700531 }
532
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800533 void setRecentTasks(RecentTasks recentTasks) {
534 mRecentTasks = recentTasks;
535 }
536
Jeff Brown2c43c332014-06-12 22:38:59 -0700537 /**
538 * At the time when the constructor runs, the power manager has not yet been
539 * initialized. So we initialize our wakelocks afterwards.
540 */
541 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700543 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700544 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700545 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700546 }
547
justinzhang5286d3f2014-05-12 17:06:01 -0400548 // This function returns a IStatusBarService. The value is from ServiceManager.
549 // getService and is cached.
550 private IStatusBarService getStatusBarService() {
551 synchronized (mService) {
552 if (mStatusBarService == null) {
553 mStatusBarService = IStatusBarService.Stub.asInterface(
554 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
555 if (mStatusBarService == null) {
556 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
557 }
558 }
559 return mStatusBarService;
560 }
561 }
562
Jason Monk35c62a42014-06-17 10:24:47 -0400563 private IDevicePolicyManager getDevicePolicyManager() {
564 synchronized (mService) {
565 if (mDevicePolicyManager == null) {
566 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
567 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
568 if (mDevicePolicyManager == null) {
569 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
570 }
571 }
572 return mDevicePolicyManager;
573 }
574 }
575
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700576 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800577 synchronized (mService) {
578 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200579 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800580
581 mDisplayManager =
582 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
583 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800584 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800585
586 Display[] displays = mDisplayManager.getDisplays();
587 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
588 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800589 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700590 if (activityDisplay.mDisplay == null) {
591 throw new IllegalStateException("Default Display does not exist");
592 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700594 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 }
596
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800597 mHomeStack = mFocusedStack = mLastFocusedStack =
598 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800599
600 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 }
Craig Mautner27084302013-03-25 08:05:25 -0700602 }
603
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700604 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700606 }
607
Craig Mautnerde4ef022013-04-07 19:01:33 -0700608 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700610 }
611
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700612 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700613 if (stack == null) {
614 return false;
615 }
616
Craig Mautnerdf88d732014-01-27 09:21:32 -0800617 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
618 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700619 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800620 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800621 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700622 }
623
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700624 /** The top most stack. */
625 boolean isFrontStack(ActivityStack stack) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800626 return isFrontOfStackList(stack, mHomeStack.mStacks);
627 }
628
629 /** The top most stack on its display. */
630 boolean isFrontStackOnDisplay(ActivityStack stack) {
631 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
632 }
633
634 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700635 if (stack == null) {
636 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700638
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700639 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
640 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700641 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800642 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800643 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700644 }
645
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800647 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
648 if (!focusCandidate.isFocusable()) {
649 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800650 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 }
652
653 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800655 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800656
Wale Ogunwaled046a012015-12-24 13:05:59 -0800657 EventLogTags.writeAmFocusedStack(
658 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
659 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
660 }
661
662 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800663 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800664 if (r != null && r.idle) {
665 checkFinishBootingLocked();
666 }
667 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700668 }
669
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700670 void moveHomeStackToFront(String reason) {
671 mHomeStack.moveToFront(reason);
672 }
673
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700674 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800675 boolean moveHomeStackTaskToTop(String reason) {
676 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700677
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700678 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700679 if (top == null) {
680 return false;
681 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700682 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700683 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700684 }
685
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800686 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700687 if (!mService.mBooting && !mService.mBooted) {
688 // Not ready yet!
689 return false;
690 }
691
Craig Mautner84984fa2014-06-19 11:19:20 -0700692 if (prev != null) {
693 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
694 }
695
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800696 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700697 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800698 final String myReason = reason + " resumeHomeStackTask";
699
Mark Lua56ea122015-10-08 13:31:01 +0800700 // Only resume home activity if isn't finishing.
701 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700702 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800703 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700704 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700706 }
707
Craig Mautner8d341ef2013-03-26 09:03:27 -0700708 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700709 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700710 }
711
712 /**
713 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
714 * @param id Id of the task we would like returned.
715 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
716 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700717 * @param stackId The stack to restore the task to (default launch stack will be used if
718 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700719 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700720 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800721 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 ActivityStack stack = stacks.get(stackNdx);
726 TaskRecord task = stack.taskForIdLocked(id);
727 if (task != null) {
728 return task;
729 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700730 }
731 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800732
733 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700734 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800735 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800736 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700737 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800738 return null;
739 }
740
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700741 if (!restoreFromRecents) {
742 return task;
743 }
744
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700745 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700746 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
747 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800748 return null;
749 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700750 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800751 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700752 }
753
Craig Mautner6170f732013-04-02 13:05:23 -0700754 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800755 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800757 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
759 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
760 if (r != null) {
761 return r;
762 }
Craig Mautner6170f732013-04-02 13:05:23 -0700763 }
764 }
765 return null;
766 }
767
Tony Mak853304c2016-04-18 15:17:41 +0100768 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000769 * Detects whether we should show a lock screen in front of this task for a locked user.
770 * <p>
771 * We'll do this if either of the following holds:
772 * <ul>
773 * <li>The top activity explicitly belongs to {@param userId}.</li>
774 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
775 * </ul>
776 *
777 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100778 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000779 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
780 // To handle the case that work app is in the task but just is not the top one.
781 final ActivityRecord activityRecord = task.getTopActivity();
782 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
783
784 return (activityRecord != null && activityRecord.userId == userId)
785 || (resultTo != null && resultTo.userId == userId);
786 }
787
788 /**
789 * Find all visible task stacks containing {@param userId} and intercept them with an activity
790 * to block out the contents and possibly start a credential-confirming intent.
791 *
792 * @param userId user handle for the locked managed profile.
793 */
794 void lockAllProfileTasks(@UserIdInt int userId) {
795 mWindowManager.deferSurfaceLayout();
796 try {
797 final List<ActivityStack> stacks = getStacks();
798 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
799 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
800 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
801 final TaskRecord task = tasks.get(taskNdx);
802
803 // Check the task for a top activity belonging to userId, or returning a result
804 // to an activity belonging to userId. Example case: a document picker for
805 // personal files, opened by a work app, should still get locked.
806 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000807 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
808 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000809 }
Tony Mak853304c2016-04-18 15:17:41 +0100810 }
811 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000812 } finally {
813 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100814 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000815 }
816
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800817 void setNextTaskIdForUserLocked(int taskId, int userId) {
818 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
819 if (taskId > currentTaskId) {
820 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700821 }
822 }
823
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700824 static int nextTaskIdForUser(int taskId, int userId) {
825 int nextTaskId = taskId + 1;
826 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
827 // Wrap around as there will be smaller task ids that are available now.
828 nextTaskId -= MAX_TASK_IDS_PER_USER;
829 }
830 return nextTaskId;
831 }
832
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800833 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800834 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
835 // for a userId u, a taskId can only be in the range
836 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
837 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700838 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800839 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
840 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
841 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700842 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800843 if (candidateTaskId == currentTaskId) {
844 // Something wrong!
845 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
846 throw new IllegalStateException("Cannot get an available task id."
847 + " Reached limit of " + MAX_TASK_IDS_PER_USER
848 + " running tasks per user.");
849 }
850 }
851 mCurTaskIdForUser.put(userId, candidateTaskId);
852 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700853 }
854
Chong Zhang6cda19c2016-06-14 19:07:56 -0700855 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800856 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700857 if (stack == null) {
858 return null;
859 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700860 ActivityRecord resumedActivity = stack.mResumedActivity;
861 if (resumedActivity == null || resumedActivity.app == null) {
862 resumedActivity = stack.mPausingActivity;
863 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700864 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700865 }
866 }
867 return resumedActivity;
868 }
869
Dianne Hackbornff072722014-09-24 10:56:28 -0700870 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700871 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800873 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
874 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
876 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700877 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 continue;
879 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700880 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800881 if (hr != null) {
882 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
883 && processName.equals(hr.processName)) {
884 try {
George Mount2c92c972014-03-20 09:38:23 -0700885 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 didSomething = true;
887 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700888 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 Slog.w(TAG, "Exception in new application when starting activity "
890 + hr.intent.getComponent().flattenToShortString(), e);
891 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700892 }
Craig Mautner20e72272013-04-01 13:45:53 -0700893 }
Craig Mautner20e72272013-04-01 13:45:53 -0700894 }
895 }
896 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700897 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700898 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700899 }
Craig Mautner20e72272013-04-01 13:45:53 -0700900 return didSomething;
901 }
902
903 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800904 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
905 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800906 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
907 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700908 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 continue;
910 }
911 final ActivityRecord resumedActivity = stack.mResumedActivity;
912 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700913 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800914 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 return false;
916 }
Craig Mautner20e72272013-04-01 13:45:53 -0700917 }
918 }
Wei Wang65c7a152016-06-02 18:51:22 -0700919 // Send launch end powerhint when idle
920 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700921 return true;
922 }
923
Craig Mautnerde4ef022013-04-07 19:01:33 -0700924 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800925 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
926 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
928 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700929 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700931 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 return false;
933 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700934 }
935 }
936 }
937 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700938 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
940 mLastFocusedStack + " to=" + mFocusedStack);
941 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700942 return true;
943 }
944
945 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800946 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
948 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
950 final ActivityStack stack = stacks.get(stackNdx);
951 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800952 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700953 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800954 return false;
955 }
956 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 }
959 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800960 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 }
962
Craig Mautner2acc3892013-09-23 10:28:14 -0700963 /**
964 * Pause all activities in either all of the stacks or just the back stacks.
965 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700966 * @param resuming The resuming activity.
967 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
968 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700969 * @return true if any activity was paused as a result of this call.
970 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700971 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700972 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
974 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
976 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700977 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700978 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700980 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
981 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700983 }
984 }
985 return someActivityPaused;
986 }
987
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700989 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
991 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
994 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700995 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700997 Slog.d(TAG_STATES,
998 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 pausing = false;
1000 } else {
1001 return false;
1002 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001003 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 }
1005 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001006 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001007 }
1008
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001009 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001010 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001011 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1013 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1015 final ActivityStack stack = stacks.get(stackNdx);
1016 if (stack.mResumedActivity != null &&
1017 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001018 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001019 }
1020 }
1021 }
1022 }
1023
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001024 void cancelInitializingActivities() {
1025 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1026 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1027 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1028 stacks.get(stackNdx).cancelInitializingActivities();
1029 }
1030 }
1031 }
1032
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001033 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001034 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001035 }
1036
1037 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1038 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001039 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001041 if (w.who == null) {
1042 changed = true;
1043 w.timeout = false;
1044 if (r != null) {
1045 w.who = new ComponentName(r.info.packageName, r.info.name);
1046 }
1047 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1048 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001050 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001051 if (changed) {
1052 mService.notifyAll();
1053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001054 }
1055
Chong Zhang5022da32016-06-21 16:31:37 -07001056 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1057 boolean changed = false;
1058 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1059 WaitResult w = mWaitingActivityLaunched.remove(i);
1060 if (w.who == null) {
1061 changed = true;
1062 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1063 // the starting activity ends up moving another activity to front, and it should
1064 // wait for this new activity to become visible instead.
1065 // Do not modify other fields.
1066 w.result = START_TASK_TO_FRONT;
1067 }
1068 }
1069 if (changed) {
1070 mService.notifyAll();
1071 }
1072 }
1073
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1075 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001077 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001078 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001079 if (w.who == null) {
1080 changed = true;
1081 w.timeout = timeout;
1082 if (r != null) {
1083 w.who = new ComponentName(r.info.packageName, r.info.name);
1084 }
1085 w.thisTime = thisTime;
1086 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001087 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001088 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001089 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001090 if (changed) {
1091 mService.notifyAll();
1092 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 }
1094
Craig Mautner29219d92013-04-16 20:19:12 -07001095 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001096 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001097 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001098 if (r != null) {
1099 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001100 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001101
Andrii Kulian7318d632016-07-20 18:59:28 -07001102 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001103 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001104 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1105 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001106 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001107 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001108 if (r != null) {
1109 return r;
1110 }
1111 }
1112 }
1113 return null;
1114 }
1115
Dianne Hackborn09233282014-04-30 11:33:59 -07001116 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001117 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001118 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1119 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001120 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001121 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001122 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001123 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1124 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001125 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001126 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001127 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001128 }
1129 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001130
1131 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1132 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1133 while (maxNum > 0) {
1134 long mostRecentActiveTime = Long.MIN_VALUE;
1135 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001136 final int numTaskLists = runningTaskLists.size();
1137 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1138 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001139 if (!stackTaskList.isEmpty()) {
1140 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1141 if (lastActiveTime > mostRecentActiveTime) {
1142 mostRecentActiveTime = lastActiveTime;
1143 selectedStackList = stackTaskList;
1144 }
1145 }
1146 }
1147 if (selectedStackList != null) {
1148 list.add(selectedStackList.remove(0));
1149 --maxNum;
1150 } else {
1151 break;
1152 }
1153 }
Craig Mautner20e72272013-04-01 13:45:53 -07001154 }
1155
Todd Kennedy7440f172015-12-09 14:31:22 -08001156 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1157 ProfilerInfo profilerInfo) {
1158 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001159 if (aInfo != null) {
1160 // Store the found target back into the intent, because now that
1161 // we have it we never want to do this again. For example, if the
1162 // user navigates back to this point in the history, we should
1163 // always restart the exact same activity.
1164 intent.setComponent(new ComponentName(
1165 aInfo.applicationInfo.packageName, aInfo.name));
1166
1167 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001168 if (!aInfo.processName.equals("system")) {
1169 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001170 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001171 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001172
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001173 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1174 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1175 }
1176
Man Caocfa78b22015-06-11 20:14:34 -07001177 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1178 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1179 }
1180
1181 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001182 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001183 }
1184 }
1185 }
1186 return aInfo;
1187 }
1188
Todd Kennedy7440f172015-12-09 14:31:22 -08001189 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001190 return resolveIntent(intent, resolvedType, userId, 0);
1191 }
1192
1193 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001194 try {
1195 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001196 PackageManager.MATCH_DEFAULT_ONLY | flags
1197 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001198 } catch (RemoteException e) {
1199 }
1200 return null;
1201 }
1202
1203 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1204 ProfilerInfo profilerInfo, int userId) {
1205 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1206 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1207 }
1208
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001209 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1210 boolean andResume, boolean checkConfig) throws RemoteException {
1211
1212 if (!allPausedActivitiesComplete()) {
1213 // While there are activities pausing we skipping starting any new activities until
1214 // pauses are complete. NOTE: that we also do this for activities that are starting in
1215 // the paused state because they will first be resumed then paused on the client side.
1216 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1217 "realStartActivityLocked: Skipping start of r=" + r
1218 + " some activities pausing...");
1219 return false;
1220 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001221
Craig Mautner2568c3a2015-03-26 14:22:34 -07001222 if (andResume) {
1223 r.startFreezingScreenLocked(app, 0);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001224 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225
Craig Mautner2568c3a2015-03-26 14:22:34 -07001226 // schedule launch ticks to collect information about slow apps.
1227 r.startLaunchTickingLocked();
1228 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001229
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001230 // Have the window manager re-evaluate the orientation of the screen based on the new
1231 // activity order. Note that as a result of this, it can call back into the activity
1232 // manager with a new orientation. We don't care about that, because the activity is not
1233 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001235 final int displayId = r.getDisplayId();
1236 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1237 getDisplayOverrideConfiguration(displayId),
1238 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001239 // Deferring resume here because we're going to launch new activity shortly.
1240 // We don't want to perform a redundant launch of the same record while ensuring
1241 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001242 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1243 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 }
1245
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001246 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001247 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001248 }
1249
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 r.app = app;
1251 app.waitingToKill = null;
1252 r.launchCount++;
1253 r.lastLaunchTime = SystemClock.uptimeMillis();
1254
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001255 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001256
1257 int idx = app.activities.indexOf(r);
1258 if (idx < 0) {
1259 app.activities.add(r);
1260 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001261 mService.updateLruProcessLocked(app, true, null);
1262 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001263
Craig Mautner15df08a2015-04-01 12:17:18 -07001264 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001265 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1266 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001267 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001268 }
1269
Andrii Kulian02b7a832016-10-06 23:11:56 -07001270 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 try {
1272 if (app.thread == null) {
1273 throw new RemoteException();
1274 }
1275 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001276 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 if (andResume) {
1278 results = r.results;
1279 newIntents = r.newIntents;
1280 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001281 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1282 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1283 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 if (andResume) {
1285 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1286 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001287 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001289 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001290 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001291 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001293 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1294 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 r.sleeping = false;
1296 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001297 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 mService.showAskCompatModeDialogLocked(r);
1299 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001300 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1302 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1303 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 final String profileFile = mService.mProfileFile;
1305 if (profileFile != null) {
1306 ParcelFileDescriptor profileFd = mService.mProfileFd;
1307 if (profileFd != null) {
1308 try {
1309 profileFd = profileFd.dup();
1310 } catch (IOException e) {
1311 if (profileFd != null) {
1312 try {
1313 profileFd.close();
1314 } catch (IOException o) {
1315 }
1316 profileFd = null;
1317 }
1318 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001320
1321 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001322 mService.mSamplingInterval, mService.mAutoStopProfiler,
1323 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 }
1325 }
1326 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001327
Craig Mautner2568c3a2015-03-26 14:22:34 -07001328 if (andResume) {
1329 app.hasShownUi = true;
1330 app.pendingUiClean = true;
1331 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001332 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001333 // Because we could be starting an Activity in the system process this may not go across
1334 // a Binder interface which would create a new Configuration. Consequently we have to
1335 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001337 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001338 new Configuration(mService.getGlobalConfiguration()),
1339 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1340 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1341 r.persistentState, results, newIntents, !andResume,
1342 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001344 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001345 // This may be a heavy-weight process! Note that the package
1346 // manager will ensure that only activity can run in the main
1347 // process of the .apk, which is the only thing that will be
1348 // considered heavy-weight.
1349 if (app.processName.equals(app.info.packageName)) {
1350 if (mService.mHeavyWeightProcess != null
1351 && mService.mHeavyWeightProcess != app) {
1352 Slog.w(TAG, "Starting new heavy weight process " + app
1353 + " when already running "
1354 + mService.mHeavyWeightProcess);
1355 }
1356 mService.mHeavyWeightProcess = app;
1357 Message msg = mService.mHandler.obtainMessage(
1358 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1359 msg.obj = r;
1360 mService.mHandler.sendMessage(msg);
1361 }
1362 }
1363
1364 } catch (RemoteException e) {
1365 if (r.launchFailed) {
1366 // This is the second time we failed -- finish activity
1367 // and give up.
1368 Slog.e(TAG, "Second failure launching "
1369 + r.intent.getComponent().flattenToShortString()
1370 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001371 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1373 "2nd-crash", false);
1374 return false;
1375 }
1376
1377 // This is the first time we failed -- restart process and
1378 // retry.
1379 app.activities.remove(r);
1380 throw e;
1381 }
1382
1383 r.launchFailed = false;
1384 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001385 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001386 }
1387
1388 if (andResume) {
1389 // As part of the process of launching, ActivityThread also performs
1390 // a resume.
1391 stack.minimalResumeActivityLocked(r);
1392 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001393 // This activity is not starting in the resumed state... which should look like we asked
1394 // it to pause+stop (but remain visible), and it has done so and reported back the
1395 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001396 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001397 "Moving to PAUSED: " + r + " (starting in paused state)");
1398 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 }
1400
1401 // Launch the new version setup screen if needed. We do this -after-
1402 // launching the initial activity (that is, home), so that it can have
1403 // a chance to initialize itself while in the background, making the
1404 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001405 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 mService.startSetupActivityLocked();
1407 }
1408
Dianne Hackborn465fa392014-09-14 14:21:18 -07001409 // Update any services we are bound to that might care about whether
1410 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001411 if (r.app != null) {
1412 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1413 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001414
Craig Mautner2420ead2013-04-01 17:13:20 -07001415 return true;
1416 }
1417
Craig Mautnere79d42682013-04-01 19:01:53 -07001418 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001419 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001420 // Is this activity's application already running?
1421 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001422 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001423
Andrii Kulian02b7a832016-10-06 23:11:56 -07001424 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001425
1426 if (app != null && app.thread != null) {
1427 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001428 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1429 || !"android".equals(r.info.packageName)) {
1430 // Don't add this if it is a platform component that is marked
1431 // to run in multiple processes, because this is actually
1432 // part of the framework so doesn't make sense to track as a
1433 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001434 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1435 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001436 }
George Mount2c92c972014-03-20 09:38:23 -07001437 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001438 return;
1439 } catch (RemoteException e) {
1440 Slog.w(TAG, "Exception when starting activity "
1441 + r.intent.getComponent().flattenToShortString(), e);
1442 }
1443
1444 // If a dead object exception was thrown -- fall through to
1445 // restart the application.
1446 }
1447
1448 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001449 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001450 }
1451
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001452 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001453 String resultWho, int requestCode, int callingPid, int callingUid,
1454 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001455 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001456 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1457 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001458 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001459 return true;
1460 }
1461 final int componentRestriction = getComponentRestrictionForCallingPackage(
1462 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1463 final int actionRestriction = getActionRestrictionForCallingPackage(
1464 intent.getAction(), callingPackage, callingPid, callingUid);
1465 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1466 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1467 if (resultRecord != null) {
1468 resultStack.sendActivityResultLocked(-1,
1469 resultRecord, resultWho, requestCode,
1470 Activity.RESULT_CANCELED, null);
1471 }
1472 final String msg;
1473 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1474 msg = "Permission Denial: starting " + intent.toString()
1475 + " from " + callerApp + " (pid=" + callingPid
1476 + ", uid=" + callingUid + ")" + " with revoked permission "
1477 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1478 } else if (!aInfo.exported) {
1479 msg = "Permission Denial: starting " + intent.toString()
1480 + " from " + callerApp + " (pid=" + callingPid
1481 + ", uid=" + callingUid + ")"
1482 + " not exported from uid " + aInfo.applicationInfo.uid;
1483 } else {
1484 msg = "Permission Denial: starting " + intent.toString()
1485 + " from " + callerApp + " (pid=" + callingPid
1486 + ", uid=" + callingUid + ")"
1487 + " requires " + aInfo.permission;
1488 }
1489 Slog.w(TAG, msg);
1490 throw new SecurityException(msg);
1491 }
1492
1493 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1494 final String message = "Appop Denial: starting " + intent.toString()
1495 + " from " + callerApp + " (pid=" + callingPid
1496 + ", uid=" + callingUid + ")"
1497 + " requires " + AppOpsManager.permissionToOp(
1498 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1499 Slog.w(TAG, message);
1500 return false;
1501 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1502 final String message = "Appop Denial: starting " + intent.toString()
1503 + " from " + callerApp + " (pid=" + callingPid
1504 + ", uid=" + callingUid + ")"
1505 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1506 Slog.w(TAG, message);
1507 return false;
1508 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001509 if (options != null) {
1510 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1511 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1512 callingPid, callingUid);
1513 if (startInTaskPerm != PERMISSION_GRANTED) {
1514 final String msg = "Permission Denial: starting " + intent.toString()
1515 + " from " + callerApp + " (pid=" + callingPid
1516 + ", uid=" + callingUid + ") with launchTaskId="
1517 + options.getLaunchTaskId();
1518 Slog.w(TAG, msg);
1519 throw new SecurityException(msg);
1520 }
1521 }
1522 // Check if someone tries to launch an activity on a private display with a different
1523 // owner.
1524 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001525 if (launchDisplayId != INVALID_DISPLAY
1526 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1527 final String msg = "Permission Denial: starting " + intent.toString()
1528 + " from " + callerApp + " (pid=" + callingPid
1529 + ", uid=" + callingUid + ") with launchDisplayId="
1530 + launchDisplayId;
1531 Slog.w(TAG, msg);
1532 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001533 }
1534 }
1535
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001536 return true;
1537 }
1538
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001539 /** Check if caller is allowed to launch activities on specified display. */
1540 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1541 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1542 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1543
1544 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1545 if (activityDisplay == null) {
1546 Slog.w(TAG, "Launch on display check: display not found");
1547 return false;
1548 }
1549
1550 if (!activityDisplay.isPrivate()) {
1551 // Anyone can launch on a public display.
1552 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1553 + " allow launch on public display");
1554 return true;
1555 }
1556
1557 // Check if the caller is the owner of the display.
1558 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1559 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1560 + " allow launch for owner of the display");
1561 return true;
1562 }
1563
1564 // Check if caller is present on display
1565 if (activityDisplay.isUidPresent(callingUid)) {
1566 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1567 + " allow launch for caller present on the display");
1568 return true;
1569 }
1570
1571 // Check if the caller can launch anything.
1572 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1573 callingUid);
1574 if (startAnyPerm == PERMISSION_GRANTED) {
1575 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1576 + " allow launch any on display");
1577 return true;
1578 }
1579
1580 Slog.w(TAG, "Launch on display check: denied");
1581 return false;
1582 }
1583
1584 /** Update lists of UIDs that are present on displays and have access to them. */
1585 void updateUIDsPresentOnDisplay() {
1586 mDisplayAccessUIDs.clear();
1587 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1588 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1589 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1590 }
1591 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1592 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1593 }
1594
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001595 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001596 final long identity = Binder.clearCallingIdentity();
1597 try {
1598 return UserManager.get(mService.mContext).getUserInfo(userId);
1599 } finally {
1600 Binder.restoreCallingIdentity(identity);
1601 }
1602 }
1603
Svet Ganov99b60432015-06-27 13:15:22 -07001604 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001605 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001606 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1607 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001608 == PackageManager.PERMISSION_DENIED) {
1609 return ACTIVITY_RESTRICTION_PERMISSION;
1610 }
1611
Christopher Tateff7add02015-08-17 10:23:22 -07001612 if (activityInfo.permission == null) {
1613 return ACTIVITY_RESTRICTION_NONE;
1614 }
1615
Svet Ganov99b60432015-06-27 13:15:22 -07001616 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1617 if (opCode == AppOpsManager.OP_NONE) {
1618 return ACTIVITY_RESTRICTION_NONE;
1619 }
1620
1621 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1622 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001623 if (!ignoreTargetSecurity) {
1624 return ACTIVITY_RESTRICTION_APPOP;
1625 }
Svet Ganov99b60432015-06-27 13:15:22 -07001626 }
1627
1628 return ACTIVITY_RESTRICTION_NONE;
1629 }
1630
Svetoslav7008b512015-06-24 18:47:07 -07001631 private int getActionRestrictionForCallingPackage(String action,
1632 String callingPackage, int callingPid, int callingUid) {
1633 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001634 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001635 }
1636
1637 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1638 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001639 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001640 }
1641
1642 final PackageInfo packageInfo;
1643 try {
1644 packageInfo = mService.mContext.getPackageManager()
1645 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1646 } catch (PackageManager.NameNotFoundException e) {
1647 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001648 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001649 }
1650
1651 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001652 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001653 }
1654
1655 if (mService.checkPermission(permission, callingPid, callingUid) ==
1656 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001657 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001658 }
1659
1660 final int opCode = AppOpsManager.permissionToOpCode(permission);
1661 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001662 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001663 }
1664
1665 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1666 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001667 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001668 }
1669
Svet Ganov99b60432015-06-27 13:15:22 -07001670 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001671 }
1672
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001673 void setLaunchSource(int uid) {
1674 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1675 }
1676
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001677 void acquireLaunchWakelock() {
1678 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1679 throw new IllegalStateException("Calling must be system uid");
1680 }
1681 mLaunchingActivity.acquire();
1682 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1683 // To be safe, don't allow the wake lock to be held for too long.
1684 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1685 }
1686 }
1687
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001688 /**
1689 * Called when the frontmost task is idle.
1690 * @return the state of mService.mBooting before this was called.
1691 */
1692 private boolean checkFinishBootingLocked() {
1693 final boolean booting = mService.mBooting;
1694 boolean enableScreen = false;
1695 mService.mBooting = false;
1696 if (!mService.mBooted) {
1697 mService.mBooted = true;
1698 enableScreen = true;
1699 }
1700 if (booting || enableScreen) {
1701 mService.postFinishBooting(booting, enableScreen);
1702 }
1703 return booting;
1704 }
1705
Craig Mautnerf3333272013-04-22 10:55:53 -07001706 // Checked.
1707 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001708 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001709 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001710
Craig Mautnerf3333272013-04-22 10:55:53 -07001711 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001712 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001713 int NS = 0;
1714 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001715 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001716 boolean activityRemoved = false;
1717
Wale Ogunwale7d701172015-03-11 15:36:30 -07001718 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001719 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001720 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1721 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001722 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1723 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001724 if (fromTimeout) {
1725 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001726 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001727
1728 // This is a hack to semi-deal with a race condition
1729 // in the client where it can be constructed with a
1730 // newer configuration from when we asked it to launch.
1731 // We'll update with whatever configuration it now says
1732 // it used to launch.
1733 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001734 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001735 }
1736
1737 // We are now idle. If someone is waiting for a thumbnail from
1738 // us, we can now deliver.
1739 r.idle = true;
1740
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001741 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001742 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001743 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001744 }
1745 }
1746
1747 if (allResumedActivitiesIdle()) {
1748 if (r != null) {
1749 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001750 }
1751
1752 if (mLaunchingActivity.isHeld()) {
1753 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1754 if (VALIDATE_WAKE_LOCK_CALLER &&
1755 Binder.getCallingUid() != Process.myUid()) {
1756 throw new IllegalStateException("Calling must be system uid");
1757 }
1758 mLaunchingActivity.release();
1759 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001760 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001761 }
1762
1763 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001764 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1765 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001766 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001767 if ((NF = mFinishingActivities.size()) > 0) {
1768 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001769 mFinishingActivities.clear();
1770 }
1771
Craig Mautnerf3333272013-04-22 10:55:53 -07001772 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001773 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001774 mStartingUsers.clear();
1775 }
1776
Craig Mautnerf3333272013-04-22 10:55:53 -07001777 // Stop any activities that are scheduled to do so but have been
1778 // waiting for the next one to start.
1779 for (int i = 0; i < NS; i++) {
1780 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001781 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001782 if (stack != null) {
1783 if (r.finishing) {
1784 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1785 } else {
1786 stack.stopActivityLocked(r);
1787 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001788 }
1789 }
1790
1791 // Finish any activities that are scheduled to do so but have been
1792 // waiting for the next one to start.
1793 for (int i = 0; i < NF; i++) {
1794 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001795 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001796 if (stack != null) {
1797 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1798 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001799 }
1800
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001801 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001802 // Complete user switch
1803 if (startingUsers != null) {
1804 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001805 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001806 }
1807 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001808 }
1809
1810 mService.trimApplications();
1811 //dump();
1812 //mWindowManager.dump();
1813
Craig Mautnerf3333272013-04-22 10:55:53 -07001814 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001815 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 }
1817
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001818 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001819 }
1820
Craig Mautner8e569572013-10-11 17:36:59 -07001821 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001822 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001823 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1824 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001825 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1826 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1827 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001828 }
Craig Mautner19091252013-10-05 00:03:53 -07001829 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001830 }
1831
1832 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001833 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1834 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001835 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1836 stacks.get(stackNdx).closeSystemDialogsLocked();
1837 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001838 }
1839 }
1840
Craig Mautner93529a42013-10-04 15:03:13 -07001841 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001842 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001843 }
1844
Craig Mautner8d341ef2013-03-26 09:03:27 -07001845 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001846 * Update the last used stack id for non-current user (current user's last
1847 * used stack is the focused stack)
1848 */
1849 void updateUserStackLocked(int userId, ActivityStack stack) {
1850 if (userId != mCurrentUser) {
1851 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1852 }
1853 }
1854
1855 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001856 * @return true if some activity was finished (or would have finished if doit were true).
1857 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001858 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1859 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001860 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001861 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1862 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001863 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001864 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001865 if (stack.finishDisabledPackageActivitiesLocked(
1866 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001867 didSomething = true;
1868 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001869 }
1870 }
1871 return didSomething;
1872 }
1873
Dianne Hackborna413dc02013-07-12 12:02:55 -07001874 void updatePreviousProcessLocked(ActivityRecord r) {
1875 // Now that this process has stopped, we may want to consider
1876 // it to be the previous app to try to keep around in case
1877 // the user wants to return to it.
1878
1879 // First, found out what is currently the foreground app, so that
1880 // we don't blow away the previous app if this activity is being
1881 // hosted by the process that is actually still the foreground.
1882 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001883 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1884 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001885 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1886 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001887 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001888 if (stack.mResumedActivity != null) {
1889 fgApp = stack.mResumedActivity.app;
1890 } else if (stack.mPausingActivity != null) {
1891 fgApp = stack.mPausingActivity.app;
1892 }
1893 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001894 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001895 }
1896 }
1897
1898 // Now set this one as the previous process, only if that really
1899 // makes sense to.
1900 if (r.app != null && fgApp != null && r.app != fgApp
1901 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001902 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001903 mService.mPreviousProcess = r.app;
1904 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1905 }
1906 }
1907
Wale Ogunwaled046a012015-12-24 13:05:59 -08001908 boolean resumeFocusedStackTopActivityLocked() {
1909 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001910 }
1911
Wale Ogunwaled046a012015-12-24 13:05:59 -08001912 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001913 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001914 if (targetStack != null && isFocusedStack(targetStack)) {
1915 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001916 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001917 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1918 if (r == null || r.state != RESUMED) {
1919 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001920 } else if (r.state == RESUMED) {
1921 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1922 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001923 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001924 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001925 }
1926
Todd Kennedy39bfee52016-02-24 10:28:21 -08001927 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1928 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1929 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1930 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1931 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1932 }
1933 }
1934 }
1935
Adrian Roos20d7df32016-01-12 18:59:43 +01001936 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1937 TaskRecord finishedTask = null;
1938 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1940 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001941 final int numStacks = stacks.size();
1942 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1943 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001944 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1945 if (stack == focusedStack || finishedTask == null) {
1946 finishedTask = t;
1947 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001948 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001949 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001950 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 }
1952
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001953 void finishVoiceTask(IVoiceInteractionSession session) {
1954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1955 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1956 final int numStacks = stacks.size();
1957 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1958 final ActivityStack stack = stacks.get(stackNdx);
1959 stack.finishVoiceTask(session);
1960 }
1961 }
1962 }
1963
Andrii Kulianc27916642016-04-12 17:59:27 -07001964 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1965 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001966 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1967 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001968 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001969 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1970 // Caller wants the home activity moved with it. To accomplish this,
1971 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001972 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001973 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001974 ActivityStack currentStack = task.getStack();
1975 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001976 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1977 + task + " to front. Stack is null");
1978 return;
1979 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001980
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001981 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001982 int stackId = options.getLaunchStackId();
1983 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001984 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001985 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001986 if (stackId == INVALID_STACK_ID) {
1987 stackId = task.getLaunchStackId();
1988 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001989 if (stackId != currentStack.mStackId) {
1990 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08001991 !FORCE_FOCUS, reason);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001992 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001993 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1994 // still need moveTaskToFrontLocked() below for any transition settings.
1995 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001996 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1997 resizeStackLocked(stackId, bounds,
1998 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001999 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002000 } else {
2001 // WM resizeTask must be done after the task is moved to the correct stack,
2002 // because Task's setBounds() also updates dim layer's bounds, but that has
2003 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002004 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002005 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002006 }
2007 }
2008
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002009 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002010 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002011 r == null ? null : r.appTimeTracker, reason);
2012
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002013 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002014 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002015
Andrii Kulian02b7a832016-10-06 23:11:56 -07002016 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002017 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002018 }
2019
Wale Ogunwale854809c2015-12-27 16:18:19 -08002020 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002021 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002022 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002023 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002024 return false;
2025 }
2026 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2027 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002028 }
2029
Craig Mautner967212c2013-04-13 21:10:58 -07002030 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002031 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002032 }
2033
2034 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002035 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002036 if (activityContainer != null) {
2037 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002038 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002039 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002040 return null;
2041 }
Jorim Jaggife762342016-10-13 14:33:27 +02002042 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002043 }
2044
Andrii Kulian16802aa2016-11-02 12:21:33 -07002045 /**
2046 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2047 * If there is no such stack, new dynamic stack can be created.
2048 * @param displayId Target display.
2049 * @param r Activity that should be launched there.
2050 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2051 */
2052 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2053 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2054 if (activityDisplay == null) {
2055 throw new IllegalArgumentException(
2056 "Display with displayId=" + displayId + " not found.");
2057 }
2058
2059 // Return the topmost valid stack on the display.
2060 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2061 final ActivityStack stack = activityDisplay.mStacks.get(i);
2062 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2063 return stack;
2064 }
2065 }
2066
2067 // If there is no valid stack on the external display - check if new dynamic stack will do.
2068 if (displayId != Display.DEFAULT_DISPLAY) {
2069 final int newDynamicStackId = getNextStackId();
2070 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2071 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2072 }
2073 }
2074
2075 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2076 return null;
2077 }
2078
Craig Mautner967212c2013-04-13 21:10:58 -07002079 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002080 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002081 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2082 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002083 }
2084 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002085 }
2086
Jorim Jaggife762342016-10-13 14:33:27 +02002087 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2088 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2089 }
2090
Andrii Kulian7fc22812016-12-28 13:04:11 -08002091 /**
2092 * Get next focusable stack in the system. This will search across displays and stacks
2093 * in last-focused order for a focusable and visible stack, different from the target stack.
2094 *
2095 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2096 * searching for next candidate.
2097 * @return Next focusable {@link ActivityStack}, null if not found.
2098 */
2099 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2100 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2101
2102 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2103 final int displayId = mTmpOrderedDisplayIds.get(i);
2104 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2105 if (stacks == null) {
2106 continue;
2107 }
2108 for (int j = stacks.size() - 1; j >= 0; --j) {
2109 final ActivityStack stack = stacks.get(j);
2110 if (stack != currentFocus && stack.isFocusable()
2111 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2112 return stack;
2113 }
2114 }
2115 }
2116
2117 return null;
2118 }
2119
Craig Mautneree2e45a2014-06-27 12:10:03 -07002120 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002121 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002122 }
2123
2124 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002125 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2126 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2127 final TaskRecord task = tasks.get(taskNdx);
2128 if (task.isHomeTask()) {
2129 final ArrayList<ActivityRecord> activities = task.mActivities;
2130 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2131 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002132 if (r.isHomeActivity()
2133 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002134 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002135 }
2136 }
2137 }
2138 }
2139 return null;
2140 }
2141
Chong Zhangb15758a2015-11-17 12:12:03 -08002142 /**
2143 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2144 * the docked stack itself, or if it's side-by-side to the docked stack.
2145 */
2146 boolean isStackDockedInEffect(int stackId) {
2147 return stackId == DOCKED_STACK_ID ||
2148 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2149 }
2150
Todd Kennedyca4d8422015-01-15 15:19:22 -08002151 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002152 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002153 ActivityContainer activityContainer =
2154 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002155 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002156 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2157 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002158 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002159 return activityContainer;
2160 }
2161
Craig Mautner34b73df2014-01-12 21:11:08 -08002162 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002163 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2164 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2165 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002166 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2167 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002168 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002169 }
2170 }
2171
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002172 void deleteActivityContainerRecord(int stackId) {
2173 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2174 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2175 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002176 }
2177
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002179 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002180 if (stackId == DOCKED_STACK_ID) {
2181 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002182 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002183 return;
2184 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002185 final ActivityStack stack = getStack(stackId);
2186 if (stack == null) {
2187 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2188 return;
2189 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002190
Jorim Jaggidc249c42015-12-15 14:57:31 -08002191 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002192 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2193 // stack size changing so things don't get out of sync.
2194 return;
2195 }
2196
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002197 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002198 mWindowManager.deferSurfaceLayout();
2199 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002200 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002201 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002202 stack.ensureVisibleActivitiesConfigurationLocked(
2203 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002204 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002205 } finally {
2206 mWindowManager.continueSurfaceLayout();
2207 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002208 }
2209 }
2210
Jorim Jaggi192086e2016-03-11 17:17:03 +01002211 void deferUpdateBounds(int stackId) {
2212 final ActivityStack stack = getStack(stackId);
2213 if (stack != null) {
2214 stack.deferUpdateBounds();
2215 }
2216 }
2217
2218 void continueUpdateBounds(int stackId) {
2219 final ActivityStack stack = getStack(stackId);
2220 if (stack != null) {
2221 stack.continueUpdateBounds();
2222 }
2223 }
2224
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002225 void notifyAppTransitionDone() {
2226 continueUpdateBounds(HOME_STACK_ID);
2227 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2228 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002229 final TaskRecord task =
2230 anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2231 if (task != null) {
2232 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002233 }
2234 }
2235 mResizingTasksDuringAnimation.clear();
2236 }
2237
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002238 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2239 final ActivityStack stack = getStack(fromStackId);
2240 if (stack == null) {
2241 return;
2242 }
2243
2244 mWindowManager.deferSurfaceLayout();
2245 try {
2246 if (fromStackId == DOCKED_STACK_ID) {
2247
2248 // We are moving all tasks from the docked stack to the fullscreen stack,
2249 // which is dismissing the docked stack, so resize all other stacks to
2250 // fullscreen here already so we don't end up with resize trashing.
2251 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2252 if (StackId.isResizeableByDockedStack(i)) {
2253 ActivityStack otherStack = getStack(i);
2254 if (otherStack != null) {
2255 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2256 true /* allowResizeInDockedMode */, DEFER_RESUME);
2257 }
2258 }
2259 }
2260
2261 // Also disable docked stack resizing since we have manually adjusted the
2262 // size of other stacks above and we don't want to trigger a docked stack
2263 // resize when we remove task from it below and it is detached from the
2264 // display because it no longer contains any tasks.
2265 mAllowDockedStackResize = false;
2266 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002267 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002268 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2269 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002270 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2271 final int size = tasks.size();
2272 if (onTop) {
2273 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002274 final TaskRecord task = tasks.get(i);
2275 if (fromStackId == PINNED_STACK_ID) {
2276 // Update the return-to to reflect where the pinned stack task was moved
2277 // from so that we retain the stack that was previously visible if the
2278 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002279 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2280 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002281 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002282 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002283 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002284 "moveTasksToFullscreenStack - onTop", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002285 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002286
2287 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2288 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002289 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002290 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002291 final TaskRecord task = tasks.get(i);
2292 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, MAX_VALUE,
2293 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002294 }
2295 }
2296 } finally {
2297 mAllowDockedStackResize = true;
2298 mWindowManager.continueSurfaceLayout();
2299 }
2300 }
2301
Jorim Jaggidc249c42015-12-15 14:57:31 -08002302 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002303 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2304 boolean preserveWindows) {
2305 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2306 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2307 false /* deferResume */);
2308 }
2309
2310 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2311 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2312 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002313
2314 if (!mAllowDockedStackResize) {
2315 // Docked stack resize currently disabled.
2316 return;
2317 }
2318
Jorim Jaggidc249c42015-12-15 14:57:31 -08002319 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2320 if (stack == null) {
2321 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2322 return;
2323 }
2324
2325 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2326 mWindowManager.deferSurfaceLayout();
2327 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002328 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2329 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002330 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002331 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002332
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002333 // TODO: Checking for isAttached might not be needed as if the user passes in null
2334 // dockedBounds then they want the docked stack to be dismissed.
2335 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2336 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002337 // In this case we make all other static stacks fullscreen and move all
2338 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002339 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002340
2341 // stack shouldn't contain anymore activities, so nothing to resume.
2342 r = null;
2343 } else {
2344 // Docked stacks occupy a dedicated region on screen so the size of all other
2345 // static stacks need to be adjusted so they don't overlap with the docked stack.
2346 // We get the bounds to use from window manager which has been adjusted for any
2347 // screen controls and is also the same for all stacks.
Matthew Nge15352e2016-12-20 15:36:29 -08002348 final Rect tempOtherTaskRect = new Rect();
2349 final Rect tempOtherTaskInsetRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002350 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002351 final ActivityStack current = getStack(i);
2352 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Nge15352e2016-12-20 15:36:29 -08002353 current.getStackDockedModeBounds(tempRect, tempOtherTaskRect,
2354 tempOtherTaskInsetRect, true /* ignoreVisibility */);
2355 resizeStackLocked(i, tempRect,
2356 !tempOtherTaskRect.isEmpty() ? tempOtherTaskRect :
2357 tempOtherTaskBounds,
2358 !tempOtherTaskInsetRect.isEmpty() ? tempOtherTaskInsetRect :
2359 tempOtherTaskInsetBounds,
2360 preserveWindows, true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002361 }
2362 }
2363 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002364 if (!deferResume) {
2365 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2366 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002367 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002368 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002369 mWindowManager.continueSurfaceLayout();
2370 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2371 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002372 }
2373
Robert Carr0d00c2e2016-02-29 17:45:02 -08002374 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2375 final ActivityStack stack = getStack(PINNED_STACK_ID);
2376 if (stack == null) {
2377 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2378 return;
2379 }
2380 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2381 mWindowManager.deferSurfaceLayout();
2382 try {
2383 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002384 stack.resize(pinnedBounds, tempPinnedTaskBounds, null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002385 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002386 } finally {
2387 mWindowManager.continueSurfaceLayout();
2388 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2389 }
2390 }
2391
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002392 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002393 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002394 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002395 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002396 }
2397
Wale Ogunwale1666e312016-12-16 11:27:18 -08002398 final ActivityContainer activityContainer =
2399 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002400 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002401 }
2402
Winson Chung010927a2016-12-15 16:12:35 -08002403 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002404 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002405 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2406 * instead moved back onto the fullscreen stack.
2407 */
2408 void removeStackLocked(int stackId) {
2409 final ActivityStack stack = getStack(stackId);
2410 if (stack == null) {
2411 return;
2412 }
2413
2414 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2415 if (stack.getStackId() == PINNED_STACK_ID) {
2416 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2417 if (fullscreenStack != null) {
2418 final boolean isFullscreenStackVisible =
2419 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2420 for (int i = 0; i < tasks.size(); i++) {
2421 // Insert the task either at the top of the fullscreen stack if it is hidden,
2422 // or just under the top task if it is currently visible
2423 final int insertPosition = isFullscreenStackVisible
2424 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2425 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002426 final TaskRecord task = tasks.get(i);
2427 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002428 }
2429 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2430 resumeFocusedStackTopActivityLocked();
2431 } else {
2432 // If there is no fullscreen stack, then create the stack and move all the tasks
2433 // onto the stack
2434 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, false /* onTop */);
2435 }
2436 } else {
2437 for (int i = tasks.size() - 1; i >= 0; i--) {
2438 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2439 REMOVE_FROM_RECENTS);
2440 }
2441 }
2442 }
2443
2444 /**
2445 * Removes the task with the specified task id.
2446 *
2447 * @param taskId Identifier of the task to be removed.
2448 * @param killProcess Kill any process associated with the task if possible.
2449 * @param removeFromRecents Whether to also remove the task from recents.
2450 * @return Returns true if the given task was found and removed.
2451 */
2452 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2453 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2454 if (tr != null) {
2455 tr.removeTaskActivitiesLocked();
2456 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2457 if (tr.isPersistable) {
2458 mService.notifyTaskPersisterLocked(null, true);
2459 }
2460 return true;
2461 }
2462 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2463 return false;
2464 }
2465
2466 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2467 if (removeFromRecents) {
2468 mRecentTasks.remove(tr);
2469 tr.removedFromRecents();
2470 }
2471 ComponentName component = tr.getBaseIntent().getComponent();
2472 if (component == null) {
2473 Slog.w(TAG, "No component for base intent of task: " + tr);
2474 return;
2475 }
2476
2477 // Find any running services associated with this app and stop if needed.
2478 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2479
2480 if (!killProcess) {
2481 return;
2482 }
2483
2484 // Determine if the process(es) for this task should be killed.
2485 final String pkg = component.getPackageName();
2486 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2487 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2488 for (int i = 0; i < pmap.size(); i++) {
2489
2490 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2491 for (int j = 0; j < uids.size(); j++) {
2492 ProcessRecord proc = uids.valueAt(j);
2493 if (proc.userId != tr.userId) {
2494 // Don't kill process for a different user.
2495 continue;
2496 }
2497 if (proc == mService.mHomeProcess) {
2498 // Don't kill the home process along with tasks from the same package.
2499 continue;
2500 }
2501 if (!proc.pkgList.containsKey(pkg)) {
2502 // Don't kill process that is not associated with this task.
2503 continue;
2504 }
2505
2506 for (int k = 0; k < proc.activities.size(); k++) {
2507 TaskRecord otherTask = proc.activities.get(k).task;
2508 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2509 // Don't kill process(es) that has an activity in a different task that is
2510 // also in recents.
2511 return;
2512 }
2513 }
2514
2515 if (proc.foregroundServices) {
2516 // Don't kill process(es) with foreground service.
2517 return;
2518 }
2519
2520 // Add process to kill list.
2521 procsToKill.add(proc);
2522 }
2523 }
2524
2525 // Kill the running processes.
2526 for (int i = 0; i < procsToKill.size(); i++) {
2527 ProcessRecord pr = procsToKill.get(i);
2528 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2529 && pr.curReceivers.isEmpty()) {
2530 pr.kill("remove task", true);
2531 } else {
2532 // We delay killing processes that are not in the background or running a receiver.
2533 pr.waitingToKill = "remove task";
2534 }
2535 }
2536 }
2537
Craig Mautner4a1cb222013-12-04 16:14:06 -08002538 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002539 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002540 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2541 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002542 break;
2543 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002544 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002545 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002546 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002547 }
2548
Chong Zhang5dcb2752015-08-18 13:50:26 -07002549 /**
2550 * Restores a recent task to a stack
2551 * @param task The recent task to be restored.
2552 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002553 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2554 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002555 * @return true if the task has been restored successfully.
2556 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002557 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002558 if (!StackId.isStaticStack(stackId)) {
2559 // If stack is not static (or stack id is invalid) - use the default one.
2560 // This means that tasks that were on external displays will be restored on the
2561 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002562 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002563 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002564 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2565 // Put it in the fullscreen stack.
2566 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002567 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002568
Andrii Kulian02b7a832016-10-06 23:11:56 -07002569 final ActivityStack currentStack = task.getStack();
2570 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002571 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002572 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002573 // Nothing else to do since it is already restored in the right stack.
2574 return true;
2575 }
2576 // Remove current stack association, so we can re-associate the task with the
2577 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002578 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002579 }
2580
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002581 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002582 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002583
2584 if (stack == null) {
2585 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002586 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2587 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002588 return false;
2589 }
2590
Wale Ogunwale72919d22016-12-08 18:58:50 -08002591 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002592 // TODO: move call for creation here and other place into Stack.addTask()
2593 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002594 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2595 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002596 final ArrayList<ActivityRecord> activities = task.mActivities;
2597 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002598 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002599 }
2600 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002601 }
2602
Wale Ogunwale040b4702015-08-06 18:10:50 -07002603 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002604 * Move stack with all its existing content to specified display.
2605 * @param stackId Id of stack to move.
2606 * @param displayId Id of display to move stack to.
2607 */
2608 void moveStackToDisplayLocked(int stackId, int displayId) {
2609 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2610 if (activityDisplay == null) {
2611 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2612 + displayId);
2613 }
2614 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2615 if (activityContainer != null) {
2616 if (activityContainer.isAttachedLocked()) {
2617 if (activityContainer.getDisplayId() == displayId) {
2618 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2619 + " to its current displayId=" + displayId);
2620 }
2621
2622 activityContainer.moveToDisplayLocked(activityDisplay);
2623 } else {
2624 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2625 + stackId + " is not attached to any display.");
2626 }
2627 } else {
2628 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2629 + stackId);
2630 }
2631
2632 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2633 !PRESERVE_WINDOWS);
2634 // TODO(multi-display): resize stacks properly if moved from split-screen.
2635 }
2636
2637 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002638 * Moves the specified task record to the input stack id.
2639 * WARNING: This method performs an unchecked/raw move of the task and
2640 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002641 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002642 * @param task Task to move.
2643 * @param stackId Id of stack to move task to.
2644 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002645 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002646 * @param reason Reason the task is been moved.
2647 * @return The stack the task was moved to.
2648 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002649 ActivityStack moveTaskToStackUncheckedLocked(TaskRecord task, int stackId, boolean toTop,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002650 boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002651
2652 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2653 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2654 + "support multi-window task=" + task + " to stackId=" + stackId);
2655 }
2656
Wale Ogunwale06579d62016-04-30 15:29:06 -07002657 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002658 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002659 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002660 final boolean wasResumed = prevStack.mResumedActivity == r;
Winson Chungc2baac02017-01-11 13:34:47 -08002661 final boolean wasPaused = prevStack.mPausingActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002662 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2663 // Whenever we are moving the top activity from the front stack we want to make sure to move
2664 // the stack to the front.
2665 final boolean wasFront = isFrontStack(prevStack)
2666 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002667
Chong Zhangd545dce2016-02-29 18:09:17 -08002668 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002669 // We don't allow moving a unresizeable task to the docked stack since the docked
2670 // stack is used for split-screen mode and will cause things like the docked divider to
2671 // show up. We instead leave the task in its current stack or move it to the fullscreen
2672 // stack if it isn't currently in a stack.
2673 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002674 Slog.w(TAG, "Can not move unresizeable task=" + task
2675 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2676 }
2677
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002678 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2679 // if a docked stack is created below which will lead to the stack we are moving from and
2680 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002681 task.mTemporarilyUnresizable = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002682 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002683 task.mTemporarilyUnresizable = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002684 task.reparent(stack.mStackId, toTop ? MAX_VALUE : 0, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002685
Winson Chungc2baac02017-01-11 13:34:47 -08002686 // Reset the resumed activity on the previous stack
2687 if (wasResumed) {
2688 prevStack.mResumedActivity = null;
2689 }
2690 // Reset the paused activity on the previous stack
2691 if (wasPaused) {
2692 prevStack.mPausingActivity = null;
Winson Chung4dabf232017-01-25 13:25:22 -08002693 prevStack.removeTimeoutsForActivityLocked(r);
Winson Chungc2baac02017-01-11 13:34:47 -08002694 }
2695
Chong Zhang02898352015-08-21 17:27:14 -07002696 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002697 // move focus to the new stack by moving the stack to the front.
Winson Chungc2baac02017-01-11 13:34:47 -08002698 stack.moveToFrontAndResumeStateIfNeeded(r, forceFocus || wasFocused || wasFront, wasResumed,
2699 wasPaused, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002700
Wale Ogunwale040b4702015-08-06 18:10:50 -07002701 return stack;
2702 }
2703
Chong Zhange4fbd322016-03-01 14:44:03 -08002704 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002705 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002706 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002707 false /* deferResume */);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002708 }
2709
2710 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002711 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002712 final TaskRecord task = anyTaskForIdLocked(taskId);
2713 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002714 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002715 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002716 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002717
Andrii Kulian02b7a832016-10-06 23:11:56 -07002718 final ActivityStack currentStack = task.getStack();
2719 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002720 // You are already in the right stack silly...
2721 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002722 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002723 }
2724
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002725 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2726 throw new IllegalArgumentException("moveTaskToStack:"
2727 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2728 }
2729
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002730 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002731 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002732 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002733 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002734 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002735 // We are about to relaunch the activity because its configuration changed due to
2736 // being maximized, i.e. size change. The activity will first remove the old window
2737 // and then add a new one. This call will tell window manager about this, so it can
2738 // preserve the old window until the new one is drawn. This prevents having a gap
2739 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002740 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002741 // Note here we always set the replacing window first, as the flags might be needed
2742 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002743 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002744 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002745
2746 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002747 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002748 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002749 try {
2750 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002751 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002752 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002753
Wale Ogunwale961f4852016-02-01 20:25:54 -08002754 if (!animate) {
2755 stack.mNoAnimActivities.add(topActivity);
2756 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002757
Wale Ogunwale961f4852016-02-01 20:25:54 -08002758 // We might trigger a configuration change. Save the current task bounds for freezing.
Wale Ogunwale1666e312016-12-16 11:27:18 -08002759 // TODO: Should this call be moved inside the resize method in WM?
2760 stack.prepareFreezingTaskBounds();
Wale Ogunwale961f4852016-02-01 20:25:54 -08002761
2762 // Make sure the task has the appropriate bounds/size for the stack it is in.
2763 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002764 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2765 deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002766 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2767 Rect bounds = task.getLaunchBounds();
2768 if (bounds == null) {
2769 stack.layoutTaskInStack(task, null);
2770 bounds = task.mBounds;
2771 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002772 kept = task.resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002773 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002774 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2775 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002776 }
2777 } finally {
2778 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002779 }
2780
2781 if (mightReplaceWindow) {
2782 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2783 // window), we need to clear the replace window settings. Otherwise, we schedule a
2784 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002785 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002786 }
2787
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002788 if (!deferResume) {
2789
2790 // The task might have already been running and its visibility needs to be synchronized with
2791 // the visibility of the stack / windows.
2792 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2793 resumeFocusedStackTopActivityLocked();
2794 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002795
Jorim Jaggid53f0922016-04-06 22:16:23 -07002796 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002797
2798 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002799 }
2800
Wale Ogunwale079a0042015-10-24 11:44:07 -07002801 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2802 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2803 if (stack == null) {
2804 throw new IllegalArgumentException(
2805 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2806 }
2807
2808 final ActivityRecord r = stack.topRunningActivityLocked();
2809 if (r == null) {
2810 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2811 + " in stack=" + stack);
2812 return false;
2813 }
2814
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002815 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002816 Slog.w(TAG,
2817 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002818 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002819 return false;
2820 }
2821
Winson Chungb5c41b72016-12-07 15:00:47 -08002822 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2823 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002824 return true;
2825 }
2826
Winson Chungb5c41b72016-12-07 15:00:47 -08002827 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2828 boolean moveHomeStackToFront) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002829
Wale Ogunwale480dca02016-02-06 13:58:29 -08002830 mWindowManager.deferSurfaceLayout();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002831 // Need to make sure the pinned stack exist so we can resize it below...
2832 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2833
Wale Ogunwale480dca02016-02-06 13:58:29 -08002834 try {
2835 final TaskRecord task = r.task;
2836
Andrii Kulian02b7a832016-10-06 23:11:56 -07002837 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002838 // An activity can't be pinned and visible behind at the same time. Go ahead and
2839 // release it from been visible behind before pinning.
2840 requestVisibleBehindLocked(r, false);
2841 }
2842
Wale Ogunwale480dca02016-02-06 13:58:29 -08002843 // Resize the pinned stack to match the current size of the task the activity we are
2844 // going to be moving is currently contained in. We do this to have the right starting
2845 // animation bounds for the pinned stack to the desired bounds the caller wants.
2846 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2847 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002848 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002849
2850 if (task.mActivities.size() == 1) {
2851 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002852 // the stack without re-parenting the activity in a different task. We don't
2853 // move the home stack forward if we are currently entering picture-in-picture
2854 // while pausing because that changes the focused stack and may prevent the new
2855 // starting activity from resuming.
2856 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
2857 && !r.supportsPictureInPictureWhilePausing) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002858 // Move the home stack forward if the task we just moved to the pinned stack
2859 // was launched from home so home should be visible behind it.
2860 moveHomeStackToFront(reason);
2861 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002862 moveTaskToStackLocked(
2863 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2864 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002865 // There are multiple activities in the task and moving the top activity should
2866 // reveal/leave the other activities in their original task
Wale Ogunwale480dca02016-02-06 13:58:29 -08002867 stack.moveActivityToStack(r);
2868 }
Winson Chungc2baac02017-01-11 13:34:47 -08002869
2870 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2871 // to the pinned stack
2872 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002873 } finally {
2874 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002875 }
2876
Wale Ogunwale480dca02016-02-06 13:58:29 -08002877 // The task might have already been running and its visibility needs to be synchronized
2878 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002879 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002880 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002881
Wale Ogunwale1666e312016-12-16 11:27:18 -08002882 stack.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002883 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002884 }
2885
Chong Zhang6cda19c2016-06-14 19:07:56 -07002886 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2887 if (r == null || !r.isFocusable()) {
2888 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2889 "moveActivityStackToFront: unfocusable r=" + r);
2890 return false;
2891 }
2892
2893 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002894 final ActivityStack stack = r.getStack();
2895 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002896 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2897 + r + " task=" + task);
2898 return false;
2899 }
2900
Chong Zhang6cda19c2016-06-14 19:07:56 -07002901 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2902 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2903 "moveActivityStackToFront: already on top, r=" + r);
2904 return false;
2905 }
2906
2907 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2908 "moveActivityStackToFront: r=" + r);
2909
2910 stack.moveToFront(reason, task);
2911 return true;
2912 }
2913
Craig Mautnerac6f8432013-07-17 13:24:59 -07002914 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002915 mTmpFindTaskResult.r = null;
2916 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002917 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002918 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2919 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2921 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002922 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002923 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002924 continue;
2925 }
2926 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002927 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2928 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002929 continue;
2930 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002931 stack.findTaskLocked(r, mTmpFindTaskResult);
2932 // It is possible to have task in multiple stacks with the same root affinity.
2933 // If the match we found was based on root affinity we keep on looking to see if
2934 // there is a better match in another stack. We eventually return the match based
2935 // on root affinity if we don't find a better match.
2936 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2937 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002938 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002939 }
2940 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002941 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2942 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002943 }
2944
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002945 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2946 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2948 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002949 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002950 final ActivityRecord ar = stacks.get(stackNdx)
2951 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002952 if (ar != null) {
2953 return ar;
2954 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002955 }
2956 }
2957 return null;
2958 }
2959
Craig Mautner8d341ef2013-03-26 09:03:27 -07002960 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961 scheduleSleepTimeout();
2962 if (!mGoingToSleep.isHeld()) {
2963 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002964 if (mLaunchingActivity.isHeld()) {
2965 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2966 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002967 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002968 mLaunchingActivity.release();
2969 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002970 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002972 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 }
2974
2975 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002976 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002977
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002978 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002979 final long endTime = System.currentTimeMillis() + timeout;
2980 while (true) {
2981 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002982 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2983 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002984 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2985 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2986 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002987 }
2988 if (cantShutdown) {
2989 long timeRemaining = endTime - System.currentTimeMillis();
2990 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002991 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002992 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002993 } catch (InterruptedException e) {
2994 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 } else {
2996 Slog.w(TAG, "Activity manager shutdown timed out");
2997 timedout = true;
2998 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002999 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003000 } else {
3001 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003002 }
3003 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003004
3005 // Force checkReadyForSleep to complete.
3006 mSleepTimeout = true;
3007 checkReadyForSleepLocked();
3008
Craig Mautner8d341ef2013-03-26 09:03:27 -07003009 return timedout;
3010 }
3011
3012 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013 removeSleepTimeouts();
3014 if (mGoingToSleep.isHeld()) {
3015 mGoingToSleep.release();
3016 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003017 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3018 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003019 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3020 final ActivityStack stack = stacks.get(stackNdx);
3021 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003022 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003023 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003024 }
Craig Mautner5314a402013-09-26 12:40:16 -07003025 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003026 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003027 mGoingToSleepActivities.clear();
3028 }
3029
3030 void activitySleptLocked(ActivityRecord r) {
3031 mGoingToSleepActivities.remove(r);
3032 checkReadyForSleepLocked();
3033 }
3034
3035 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003036 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003037 // Do not care.
3038 return;
3039 }
3040
3041 if (!mSleepTimeout) {
3042 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3044 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003045 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3046 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3047 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003048 }
3049
3050 if (mStoppingActivities.size() > 0) {
3051 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003052 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003053 + mStoppingActivities.size() + " activities");
3054 scheduleIdleLocked();
3055 dontSleep = true;
3056 }
3057
3058 if (mGoingToSleepActivities.size() > 0) {
3059 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003060 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003061 + mGoingToSleepActivities.size() + " activities");
3062 dontSleep = true;
3063 }
3064
3065 if (dontSleep) {
3066 return;
3067 }
3068 }
3069
Wei Wang65c7a152016-06-02 18:51:22 -07003070 // Send launch end powerhint before going sleep
3071 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3072
Craig Mautnere0a38842013-12-16 16:14:02 -08003073 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3074 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003075 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3076 stacks.get(stackNdx).goToSleep();
3077 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078 }
3079
3080 removeSleepTimeouts();
3081
3082 if (mGoingToSleep.isHeld()) {
3083 mGoingToSleep.release();
3084 }
3085 if (mService.mShuttingDown) {
3086 mService.notifyAll();
3087 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003088 }
3089
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003090 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003091 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003092 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003093 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003094 }
3095 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003096 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003097 mWindowManager.executeAppTransition();
3098 return true;
3099 }
3100 return false;
3101 }
3102
Craig Mautner8d341ef2013-03-26 09:03:27 -07003103 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003104 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3105 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003106 int stackNdx = stacks.size() - 1;
3107 while (stackNdx >= 0) {
3108 stacks.get(stackNdx).handleAppCrashLocked(app);
3109 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003110 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003111 }
3112 }
3113
Jose Lima4b6c6692014-08-12 17:41:12 -07003114 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003115 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003116 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003117 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3118 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003119 return false;
3120 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003121
3122 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3123 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3124 + " visible=" + visible + " stackId=" + stack.mStackId
3125 + " can't contain visible behind activities");
3126 return false;
3127 }
3128
Jose Lima4b6c6692014-08-12 17:41:12 -07003129 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003130 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3131 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003132
3133 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003134 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003135 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003136 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003137 return true;
3138 }
3139
3140 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003141 if (visible && top.fullscreen) {
3142 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003143 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3144 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3145 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3146 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003147 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003148 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3149 // Only the activity set as currently visible behind should actively reset its
3150 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003151 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3152 "requestVisibleBehind: returning visible=" + visible
3153 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3154 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003155 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003156 }
3157
Jose Lima4b6c6692014-08-12 17:41:12 -07003158 stack.setVisibleBehindActivity(visible ? r : null);
3159 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003160 // If there is a translucent home activity, we need to force it stop being translucent,
3161 // because we can't depend on the application to necessarily perform that operation.
3162 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003163 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003164 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003165 mService.convertFromTranslucent(next.appToken);
3166 }
3167 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003168 if (top.app != null && top.app.thread != null) {
3169 // Notify the top app of the change.
3170 try {
3171 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3172 } catch (RemoteException e) {
3173 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003174 }
3175 return true;
3176 }
3177
Craig Mautnerbb742462014-07-07 15:28:55 -07003178 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003179 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003180 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003181 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003182
3183 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003184 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003185 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003186 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003187 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003188
3189 // When launching tasks behind, update the last active time of the top task after the new
3190 // task has been shown briefly
3191 final ActivityRecord top = stack.topActivity();
3192 if (top != null) {
3193 top.task.touchActiveTime();
3194 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003195 }
3196
3197 void scheduleLaunchTaskBehindComplete(IBinder token) {
3198 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3199 }
3200
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003201 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3202 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003203 mKeyguardController.beginActivityVisibilityUpdate();
3204 try {
3205 // First the front stacks. In case any are not fullscreen and are in front of home.
3206 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3207 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3208 final int topStackNdx = stacks.size() - 1;
3209 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3210 final ActivityStack stack = stacks.get(stackNdx);
3211 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3212 }
Craig Mautner580ea812013-04-25 12:58:38 -07003213 }
Jorim Jaggife762342016-10-13 14:33:27 +02003214 } finally {
3215 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003216 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217 }
3218
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003219 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3220 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3221 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3222 final int topStackNdx = stacks.size() - 1;
3223 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3224 final ActivityStack stack = stacks.get(stackNdx);
3225 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3226 }
3227 }
3228 }
3229
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003230 void invalidateTaskLayers() {
3231 mTaskLayersChanged = true;
3232 }
3233
3234 void rankTaskLayersIfNeeded() {
3235 if (!mTaskLayersChanged) {
3236 return;
3237 }
3238 mTaskLayersChanged = false;
3239 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3240 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3241 int baseLayer = 0;
3242 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3243 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3244 }
3245 }
3246 }
3247
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003248 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3249 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3250 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3251 final int topStackNdx = stacks.size() - 1;
3252 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3253 final ActivityStack stack = stacks.get(stackNdx);
3254 stack.clearOtherAppTimeTrackers(except);
3255 }
3256 }
3257 }
3258
Craig Mautner8d341ef2013-03-26 09:03:27 -07003259 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003260 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3261 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003262 final int numStacks = stacks.size();
3263 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3264 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003265 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003266 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003267 }
3268 }
3269
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003270 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3271 // Examine all activities currently running in the process.
3272 TaskRecord firstTask = null;
3273 // Tasks is non-null only if two or more tasks are found.
3274 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003275 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3276 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003277 ActivityRecord r = app.activities.get(i);
3278 // First, if we find an activity that is in the process of being destroyed,
3279 // then we just aren't going to do anything for now; we want things to settle
3280 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003281 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003282 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003283 return;
3284 }
3285 // Don't consider any activies that are currently not in a state where they
3286 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003287 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3288 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003289 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003290 continue;
3291 }
3292 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003293 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003294 + " from " + r);
3295 if (firstTask == null) {
3296 firstTask = r.task;
3297 } else if (firstTask != r.task) {
3298 if (tasks == null) {
3299 tasks = new ArraySet<>();
3300 tasks.add(firstTask);
3301 }
3302 tasks.add(r.task);
3303 }
3304 }
3305 }
3306 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003307 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003308 return;
3309 }
3310 // If we have activities in multiple tasks that are in a position to be destroyed,
3311 // let's iterate through the tasks and release the oldest one.
3312 final int numDisplays = mActivityDisplays.size();
3313 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3314 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3315 // Step through all stacks starting from behind, to hit the oldest things first.
3316 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3317 final ActivityStack stack = stacks.get(stackNdx);
3318 // Try to release activities in this stack; if we manage to, we are done.
3319 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3320 return;
3321 }
3322 }
3323 }
3324 }
3325
Amith Yamasani37a40c22015-06-17 13:25:42 -07003326 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003327 final int focusStackId = mFocusedStack.getStackId();
3328 // We dismiss the docked stack whenever we switch users.
3329 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003330 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3331 // also cause all tasks to be moved to the fullscreen stack at a position that is
3332 // appropriate.
3333 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003334
3335 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003336 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003337 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003338
Craig Mautner858d8a62013-04-23 17:08:34 -07003339 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003340 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3341 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003342 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003343 final ActivityStack stack = stacks.get(stackNdx);
3344 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003345 TaskRecord task = stack.topTask();
3346 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003347 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003348 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003350 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003351
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003352 ActivityStack stack = getStack(restoreStackId);
3353 if (stack == null) {
3354 stack = mHomeStack;
3355 }
3356 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003357 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003358 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003359 } else {
3360 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003361 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003362 }
Craig Mautner93529a42013-10-04 15:03:13 -07003363 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003364 }
3365
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003366 /** Checks whether the userid is a profile of the current user. */
3367 boolean isCurrentProfileLocked(int userId) {
3368 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003369 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003370 }
3371
Winson Chung4dabf232017-01-25 13:25:22 -08003372 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3373 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003374 ArrayList<ActivityRecord> stops = null;
3375
3376 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003377 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3378 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003379 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3380 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003381 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003382 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003383 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3384 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003385 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003386 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003387 if (s.finishing) {
3388 // If this activity is finishing, it is sitting on top of
3389 // everyone else but we now know it is no longer needed...
3390 // so get rid of it. Otherwise, we need to go through the
3391 // normal flow and hide it once we determine that it is
3392 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003393 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003394 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003395 }
3396 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003397 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003398 if (!processPausingActivities && s.state == PAUSING) {
3399 // Defer processing pausing activities in this iteration and reschedule
3400 // a delayed idle to reprocess it again
3401 removeTimeoutsForActivityLocked(idleActivity);
3402 scheduleIdleTimeoutLocked(idleActivity);
3403 continue;
3404 }
3405
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003406 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003407 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003408 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003409 }
3410 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003411 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003412 }
3413 }
3414
3415 return stops;
3416 }
3417
Craig Mautnercf910b02013-04-23 11:23:27 -07003418 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003419 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3420 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3421 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3422 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003423 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003424 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003425 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003426 if (r == null) Slog.e(TAG,
3427 "validateTop...: null top activity, stack=" + stack);
3428 else {
3429 final ActivityRecord pausing = stack.mPausingActivity;
3430 if (pausing != null && pausing == r) Slog.e(TAG,
3431 "validateTop...: top stack has pausing activity r=" + r
3432 + " state=" + state);
3433 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3434 "validateTop...: activity in front not resumed r=" + r
3435 + " state=" + state);
3436 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003437 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003438 final ActivityRecord resumed = stack.mResumedActivity;
3439 if (resumed != null && resumed == r) Slog.e(TAG,
3440 "validateTop...: back stack has resumed activity r=" + r
3441 + " state=" + state);
3442 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3443 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003444 }
3445 }
3446 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003447 }
3448
Craig Mautnere0570202015-05-13 13:06:11 -07003449 private String lockTaskModeToString() {
3450 switch (mLockTaskModeState) {
3451 case LOCK_TASK_MODE_LOCKED:
3452 return "LOCKED";
3453 case LOCK_TASK_MODE_PINNED:
3454 return "PINNED";
3455 case LOCK_TASK_MODE_NONE:
3456 return "NONE";
3457 default: return "unknown=" + mLockTaskModeState;
3458 }
3459 }
3460
Craig Mautner27084302013-03-25 08:05:25 -07003461 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003462 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003463 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003464 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003465 pw.print(prefix);
3466 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003467 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003468 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003469 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003470 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3471 if (packages.size() > 0) {
3472 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3473 for (int i = 0; i < packages.size(); ++i) {
3474 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3475 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3476 }
3477 }
3478 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3479 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003480 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003481
Winson43d1f262016-06-14 16:05:55 -07003482 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003483 * Dump all connected displays' configurations.
3484 * @param prefix Prefix to apply to each line of the dump.
3485 */
3486 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3487 pw.print(prefix); pw.println("Display override configurations:");
3488 final int displayCount = mActivityDisplays.size();
3489 for (int i = 0; i < displayCount; i++) {
3490 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3491 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3492 pw.println(activityDisplay.getOverrideConfiguration());
3493 }
3494 }
3495
3496 /**
Winson43d1f262016-06-14 16:05:55 -07003497 * Dumps the activities matching the given {@param name} in the either the focused stack
3498 * or all visible stacks if {@param dumpVisibleStacks} is true.
3499 */
3500 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3501 if (dumpVisibleStacks) {
3502 ArrayList<ActivityRecord> activities = new ArrayList<>();
3503 int numDisplays = mActivityDisplays.size();
3504 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3505 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3506 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3507 ActivityStack stack = stacks.get(stackNdx);
3508 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3509 activities.addAll(stack.getDumpActivitiesLocked(name));
3510 }
3511 }
3512 }
3513 return activities;
3514 } else {
3515 return mFocusedStack.getDumpActivitiesLocked(name);
3516 }
Craig Mautner20e72272013-04-01 13:45:53 -07003517 }
3518
Dianne Hackborn390517b2013-05-30 15:03:32 -07003519 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3520 boolean needSep, String prefix) {
3521 if (activity != null) {
3522 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3523 if (needSep) {
3524 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003525 }
3526 pw.print(prefix);
3527 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003528 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003529 }
3530 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003531 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003532 }
3533
Craig Mautner8d341ef2013-03-26 09:03:27 -07003534 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3535 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003536 boolean printed = false;
3537 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003538 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3539 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003540 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003541 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003542 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003543 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003544 final ActivityStack stack = stacks.get(stackNdx);
3545 StringBuilder stackHeader = new StringBuilder(128);
3546 stackHeader.append(" Stack #");
3547 stackHeader.append(stack.mStackId);
3548 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003549 stackHeader.append("\n");
3550 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3551 stackHeader.append("\n");
3552 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003553 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3554 needSep, stackHeader.toString());
3555 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3556 !dumpAll, false, dumpPackage, true,
3557 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003558
Craig Mautner4a1cb222013-12-04 16:14:06 -08003559 needSep = printed;
3560 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3561 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003562 if (pr) {
3563 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003564 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003565 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003566 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3567 " mResumedActivity: ");
3568 if (pr) {
3569 printed = true;
3570 needSep = false;
3571 }
3572 if (dumpAll) {
3573 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3574 " mLastPausedActivity: ");
3575 if (pr) {
3576 printed = true;
3577 needSep = true;
3578 }
3579 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3580 needSep, " mLastNoHistoryActivity: ");
3581 }
3582 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003583 }
3584 }
3585
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003586 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3587 false, dumpPackage, true, " Activities waiting to finish:", null);
3588 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3589 false, dumpPackage, true, " Activities waiting to stop:", null);
3590 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3591 false, dumpPackage, true, " Activities waiting for another to become visible:",
3592 null);
3593 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3594 false, dumpPackage, true, " Activities waiting to sleep:", null);
3595 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3596 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003597
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003598 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003599 }
3600
Dianne Hackborn390517b2013-05-30 15:03:32 -07003601 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003602 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003603 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003604 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003605 String innerPrefix = null;
3606 String[] args = null;
3607 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003608 for (int i=list.size()-1; i>=0; i--) {
3609 final ActivityRecord r = list.get(i);
3610 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3611 continue;
3612 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003613 if (innerPrefix == null) {
3614 innerPrefix = prefix + " ";
3615 args = new String[0];
3616 }
3617 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003618 final boolean full = !brief && (complete || !r.isInHistory());
3619 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003620 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003621 needNL = false;
3622 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003623 if (header1 != null) {
3624 pw.println(header1);
3625 header1 = null;
3626 }
3627 if (header2 != null) {
3628 pw.println(header2);
3629 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003630 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003631 if (lastTask != r.task) {
3632 lastTask = r.task;
3633 pw.print(prefix);
3634 pw.print(full ? "* " : " ");
3635 pw.println(lastTask);
3636 if (full) {
3637 lastTask.dump(pw, prefix + " ");
3638 } else if (complete) {
3639 // Complete + brief == give a summary. Isn't that obvious?!?
3640 if (lastTask.intent != null) {
3641 pw.print(prefix); pw.print(" ");
3642 pw.println(lastTask.intent.toInsecureStringWithClip());
3643 }
3644 }
3645 }
3646 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3647 pw.print(" #"); pw.print(i); pw.print(": ");
3648 pw.println(r);
3649 if (full) {
3650 r.dump(pw, innerPrefix);
3651 } else if (complete) {
3652 // Complete + brief == give a summary. Isn't that obvious?!?
3653 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3654 if (r.app != null) {
3655 pw.print(innerPrefix); pw.println(r.app);
3656 }
3657 }
3658 if (client && r.app != null && r.app.thread != null) {
3659 // flush anything that is already in the PrintWriter since the thread is going
3660 // to write to the file descriptor directly
3661 pw.flush();
3662 try {
3663 TransferPipe tp = new TransferPipe();
3664 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003665 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003666 // Short timeout, since blocking here can
3667 // deadlock with the application.
3668 tp.go(fd, 2000);
3669 } finally {
3670 tp.kill();
3671 }
3672 } catch (IOException e) {
3673 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3674 } catch (RemoteException e) {
3675 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3676 }
3677 needNL = true;
3678 }
3679 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003680 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003681 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003682
Craig Mautnerf3333272013-04-22 10:55:53 -07003683 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003684 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3685 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003686 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3687 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003688 }
3689
3690 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003691 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003692 }
3693
3694 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003695 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3696 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003697 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3698 }
3699
Craig Mautner05d29032013-05-03 13:40:13 -07003700 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003701 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3702 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3703 }
Craig Mautner05d29032013-05-03 13:40:13 -07003704 }
3705
Craig Mautner0eea92c2013-05-16 13:35:39 -07003706 void removeSleepTimeouts() {
3707 mSleepTimeout = false;
3708 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3709 }
3710
3711 final void scheduleSleepTimeout() {
3712 removeSleepTimeouts();
3713 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3714 }
3715
Craig Mautner4a1cb222013-12-04 16:14:06 -08003716 @Override
3717 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003718 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3720 }
3721
3722 @Override
3723 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003724 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003725 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3726 }
3727
3728 @Override
3729 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003730 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003731 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3732 }
3733
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003734 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003735 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003736 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003737 newDisplay = mActivityDisplays.get(displayId) == null;
3738 if (newDisplay) {
3739 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003740 if (activityDisplay.mDisplay == null) {
3741 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3742 return;
3743 }
Craig Mautner4504de52013-12-20 09:06:56 -08003744 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003745 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003746 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003747 }
Craig Mautner4504de52013-12-20 09:06:56 -08003748 if (newDisplay) {
3749 mWindowManager.onDisplayAdded(displayId);
3750 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 }
3752
Andrii Kulianca007a62016-11-22 16:33:28 -08003753 /** Check if display with specified id is added to the list. */
3754 boolean isDisplayAdded(int displayId) {
3755 return mActivityDisplays.get(displayId) != null;
3756 }
3757
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003758 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003759 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003760 mService.mContext.getResources().getDimensionPixelSize(
3761 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003762 }
3763
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003764 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003765 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003766 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3767 if (activityDisplay != null) {
3768 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian03c403d2016-11-11 11:14:12 -08003770 final ActivityStack stack = stacks.get(stackNdx);
3771 // TODO: Implement proper stack removal and ability to choose the behavior -
3772 // remove stack completely or move it to other display.
3773 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003774 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003775 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 }
3777 }
3778 mWindowManager.onDisplayRemoved(displayId);
3779 }
3780
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003781 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003783 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3784 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 // TODO: Update the bounds.
3786 }
3787 }
3788 mWindowManager.onDisplayChanged(displayId);
3789 }
3790
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003791 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003792 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003794 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003795 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003797 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003798 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003799 info.position = display != null
3800 ? display.mStacks.indexOf(stack)
3801 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802
3803 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3804 final int numTasks = tasks.size();
3805 int[] taskIds = new int[numTasks];
3806 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003807 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003808 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 for (int i = 0; i < numTasks; ++i) {
3810 final TaskRecord task = tasks.get(i);
3811 taskIds[i] = task.taskId;
3812 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3813 : task.realActivity != null ? task.realActivity.flattenToString()
3814 : task.getTopActivity() != null ? task.getTopActivity().packageName
3815 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003816 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003817 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003818 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 }
3820 info.taskIds = taskIds;
3821 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003822 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003823 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003824
3825 final ActivityRecord top = stack.topRunningActivityLocked();
3826 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003827 return info;
3828 }
3829
3830 StackInfo getStackInfoLocked(int stackId) {
3831 ActivityStack stack = getStack(stackId);
3832 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003833 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 }
3835 return null;
3836 }
3837
3838 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003839 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3841 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003843 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844 }
3845 }
3846 return list;
3847 }
3848
Craig Mautner15df08a2015-04-01 12:17:18 -07003849 TaskRecord getLockedTaskLocked() {
3850 final int top = mLockTaskModeTasks.size() - 1;
3851 if (top >= 0) {
3852 return mLockTaskModeTasks.get(top);
3853 }
3854 return null;
3855 }
3856
3857 boolean isLockedTask(TaskRecord task) {
3858 return mLockTaskModeTasks.contains(task);
3859 }
3860
3861 boolean isLastLockedTask(TaskRecord task) {
3862 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3863 }
3864
3865 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003866 if (!mLockTaskModeTasks.remove(task)) {
3867 return;
3868 }
3869 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3870 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003871 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003872 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3873 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003874 final Message lockTaskMsg = Message.obtain();
3875 lockTaskMsg.arg1 = task.userId;
3876 lockTaskMsg.what = LOCK_TASK_END_MSG;
3877 mHandler.sendMessage(lockTaskMsg);
3878 }
3879 }
3880
Andrii Kulianc27916642016-04-12 17:59:27 -07003881 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3882 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3883 false /* forceNonResizable */);
3884 }
3885
Jorim Jaggid53f0922016-04-06 22:16:23 -07003886 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003887 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003888 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3889 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003890 return;
3891 }
3892
Jorim Jaggicd13d332016-04-27 15:40:20 -07003893 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003894 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003895 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003896 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003897
Andrii Kulianc27916642016-04-12 17:59:27 -07003898 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3899 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003900 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003901 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003902 && !topActivity.noDisplay) {
3903 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003904 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3905 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003906 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003907 }
3908
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003909 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003910 if (mLockTaskNotify != null) {
3911 mLockTaskNotify.showToast(mLockTaskModeState);
3912 }
Jason Monka8f569c2014-07-07 12:15:21 -04003913 }
3914
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003915 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3916 if (mLockTaskModeTasks.contains(task)) {
3917 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3918 }
3919 }
3920
Craig Mautner432f64e2015-05-20 14:59:57 -07003921 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3922 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003923 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003924 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003925 final TaskRecord lockedTask = getLockedTaskLocked();
3926 if (lockedTask != null) {
3927 removeLockedTaskLocked(lockedTask);
3928 if (!mLockTaskModeTasks.isEmpty()) {
3929 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003930 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3931 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003932 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003933 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003934 return;
3935 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003936 }
Craig Mautnere0570202015-05-13 13:06:11 -07003937 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3938 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003939 return;
3940 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003941
3942 // Should have already been checked, but do it again.
3943 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003944 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3945 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003946 return;
3947 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003948 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003949 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003950 return;
3951 }
3952
3953 if (mLockTaskModeTasks.isEmpty()) {
3954 // First locktask.
3955 final Message lockTaskMsg = Message.obtain();
3956 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3957 lockTaskMsg.arg1 = task.userId;
3958 lockTaskMsg.what = LOCK_TASK_START_MSG;
3959 lockTaskMsg.arg2 = lockTaskModeState;
3960 mHandler.sendMessage(lockTaskMsg);
3961 }
3962 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003963 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3964 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003965 mLockTaskModeTasks.remove(task);
3966 mLockTaskModeTasks.add(task);
3967
3968 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003969 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003971
3972 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003973 findTaskToMoveToFrontLocked(task, 0, null, reason,
3974 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003975 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003976 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003977 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003978 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003979 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003980 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003981 }
3982
3983 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003984 return isLockTaskModeViolation(task, false);
3985 }
3986
3987 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3988 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003989 return false;
3990 }
3991 final int lockTaskAuth = task.mLockTaskAuth;
3992 switch (lockTaskAuth) {
3993 case LOCK_TASK_AUTH_DONT_LOCK:
3994 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003995 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003996 case LOCK_TASK_AUTH_LAUNCHABLE:
3997 case LOCK_TASK_AUTH_WHITELISTED:
3998 return false;
3999 case LOCK_TASK_AUTH_PINNABLE:
4000 // Pinnable tasks can't be launched on top of locktask tasks.
4001 return !mLockTaskModeTasks.isEmpty();
4002 default:
4003 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4004 return true;
4005 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004006 }
4007
Craig Mautner15df08a2015-04-01 12:17:18 -07004008 void onLockTaskPackagesUpdatedLocked() {
4009 boolean didSomething = false;
4010 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4011 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004012 final boolean wasWhitelisted =
4013 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4014 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004015 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004016 final boolean isWhitelisted =
4017 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4018 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4019 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004020 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004021 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4022 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004023 removeLockedTaskLocked(lockedTask);
4024 lockedTask.performClearTaskLocked();
4025 didSomething = true;
4026 }
4027 }
4028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4029 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4031 final ActivityStack stack = stacks.get(stackNdx);
4032 stack.onLockTaskPackagesUpdatedLocked();
4033 }
4034 }
Craig Mautnere0570202015-05-13 13:06:11 -07004035 final ActivityRecord r = topRunningActivityLocked();
4036 final TaskRecord task = r != null ? r.task : null;
4037 if (mLockTaskModeTasks.isEmpty() && task != null
4038 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4039 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004040 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4041 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4042 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4043 false);
4044 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004045 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004046 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004047 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004048 }
4049 }
4050
Benjamin Franz43261142015-02-11 15:59:44 +00004051 int getLockTaskModeState() {
4052 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004053 }
4054
Jorim Jaggife89d122015-12-22 16:28:44 +01004055 void activityRelaunchedLocked(IBinder token) {
4056 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004057 if (mService.isSleepingOrShuttingDownLocked()) {
4058 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4059 if (r != null) {
4060 r.setSleeping(true, true);
4061 }
4062 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004063 }
4064
4065 void activityRelaunchingLocked(ActivityRecord r) {
4066 mWindowManager.notifyAppRelaunching(r.appToken);
4067 }
4068
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004069 void logStackState() {
4070 mActivityMetricsLogger.logWindowState();
4071 }
4072
Andrii Kulian933076d2016-03-29 17:04:42 -07004073 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004074 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4075 final ActivityRecord r = task.mActivities.get(i);
4076 if (r.app != null && r.app.thread != null) {
4077 mMultiWindowModeChangedActivities.add(r);
4078 }
4079 }
4080
4081 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4082 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4083 }
4084 }
4085
Andrii Kulian933076d2016-03-29 17:04:42 -07004086 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004087 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004088 if (prevStack == null || prevStack == stack
4089 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4090 return;
4091 }
4092
4093 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4094 final ActivityRecord r = task.mActivities.get(i);
4095 if (r.app != null && r.app.thread != null) {
4096 mPipModeChangedActivities.add(r);
4097 }
4098 }
4099
4100 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4101 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4102 }
4103 }
4104
Tony Mak853304c2016-04-18 15:17:41 +01004105 void setDockedStackMinimized(boolean minimized) {
4106 mIsDockMinimized = minimized;
4107 if (minimized) {
4108 // Docked stack is not visible, no need to confirm credentials for its top activity.
4109 return;
4110 }
4111 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
4112 if (dockedStack == null) {
4113 return;
4114 }
4115 final ActivityRecord top = dockedStack.topRunningActivityLocked();
4116 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
4117 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
4118 }
4119 }
4120
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004121 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004122
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004123 public ActivityStackSupervisorHandler(Looper looper) {
4124 super(looper);
4125 }
4126
Winson Chung4dabf232017-01-25 13:25:22 -08004127 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004128 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004129 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4130 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004131 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004132 }
4133
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004134 @Override
4135 public void handleMessage(Message msg) {
4136 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004137 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4138 synchronized (mService) {
4139 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4140 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004141 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004142 }
4143 }
4144 } break;
4145 case REPORT_PIP_MODE_CHANGED_MSG: {
4146 synchronized (mService) {
4147 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4148 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004149 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004150 }
4151 }
4152 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004153 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004154 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4155 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004156 if (mService.mDidDexOpt) {
4157 mService.mDidDexOpt = false;
4158 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4159 nmsg.obj = msg.obj;
4160 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4161 return;
4162 }
4163 // We don't at this point know if the activity is fullscreen,
4164 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004165 activityIdleInternal((ActivityRecord) msg.obj,
4166 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004167 } break;
4168 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004169 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004170 activityIdleInternal((ActivityRecord) msg.obj,
4171 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004172 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004173 case RESUME_TOP_ACTIVITY_MSG: {
4174 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004175 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004176 }
4177 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004178 case SLEEP_TIMEOUT_MSG: {
4179 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004180 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004181 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4182 mSleepTimeout = true;
4183 checkReadyForSleepLocked();
4184 }
4185 }
4186 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004187 case LAUNCH_TIMEOUT_MSG: {
4188 if (mService.mDidDexOpt) {
4189 mService.mDidDexOpt = false;
4190 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4191 return;
4192 }
4193 synchronized (mService) {
4194 if (mLaunchingActivity.isHeld()) {
4195 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4196 if (VALIDATE_WAKE_LOCK_CALLER
4197 && Binder.getCallingUid() != Process.myUid()) {
4198 throw new IllegalStateException("Calling must be system uid");
4199 }
4200 mLaunchingActivity.release();
4201 }
4202 }
4203 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004204 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004205 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 } break;
4207 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004208 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 } break;
4210 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004211 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004212 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004213 case CONTAINER_CALLBACK_VISIBILITY: {
4214 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004215 final IActivityContainerCallback callback = container.mCallback;
4216 if (callback != null) {
4217 try {
4218 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4219 } catch (RemoteException e) {
4220 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004221 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004222 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004223 case LOCK_TASK_START_MSG: {
4224 // When lock task starts, we disable the status bars.
4225 try {
Jason Monk62515be2014-05-21 16:06:19 -04004226 if (mLockTaskNotify == null) {
4227 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004228 }
Jason Monk62515be2014-05-21 16:06:19 -04004229 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004230 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004231 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004232 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004233 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004234 flags = StatusBarManager.DISABLE_MASK
4235 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004236 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004237 flags = StatusBarManager.DISABLE_MASK
4238 & (~StatusBarManager.DISABLE_BACK)
4239 & (~StatusBarManager.DISABLE_HOME)
4240 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004241 }
4242 getStatusBarService().disable(flags, mToken,
4243 mService.mContext.getPackageName());
4244 }
4245 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004246 if (getDevicePolicyManager() != null) {
4247 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004248 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004249 }
justinzhang5286d3f2014-05-12 17:06:01 -04004250 } catch (RemoteException ex) {
4251 throw new RuntimeException(ex);
4252 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004253 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004254 case LOCK_TASK_END_MSG: {
4255 // When lock task ends, we enable the status bars.
4256 try {
Jason Monk62515be2014-05-21 16:06:19 -04004257 if (getStatusBarService() != null) {
4258 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4259 mService.mContext.getPackageName());
4260 }
4261 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004262 if (getDevicePolicyManager() != null) {
4263 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4264 msg.arg1);
4265 }
Jason Monk62515be2014-05-21 16:06:19 -04004266 if (mLockTaskNotify == null) {
4267 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4268 }
4269 mLockTaskNotify.show(false);
4270 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004271 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004272 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004273 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004275 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004276 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004277 new LockPatternUtils(mService.mContext)
4278 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004279 }
4280 } catch (SettingNotFoundException e) {
4281 // No setting, don't lock.
4282 }
justinzhang5286d3f2014-05-12 17:06:01 -04004283 } catch (RemoteException ex) {
4284 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004285 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004286 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004287 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004288 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004289 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4290 if (mLockTaskNotify == null) {
4291 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4292 }
4293 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4294 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004295 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4296 final ActivityContainer container = (ActivityContainer) msg.obj;
4297 final IActivityContainerCallback callback = container.mCallback;
4298 if (callback != null) {
4299 try {
4300 callback.onAllActivitiesComplete(container.asBinder());
4301 } catch (RemoteException e) {
4302 }
4303 }
4304 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004305 case LAUNCH_TASK_BEHIND_COMPLETE: {
4306 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004307 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004308 if (r != null) {
4309 handleLaunchTaskBehindCompleteLocked(r);
4310 }
4311 }
4312 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004313
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004314 }
4315 }
4316 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004317
Ying Wangb081a592014-04-22 15:20:16 -07004318 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004319 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4320 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004321 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004322 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004323 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004324 ActivityRecord mParentActivity = null;
4325 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004326
Craig Mautnere3a00d72014-04-16 08:31:19 -07004327 boolean mVisible = true;
4328
Craig Mautner4a1cb222013-12-04 16:14:06 -08004329 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004330 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004331
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004332 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4333 final static int CONTAINER_STATE_NO_SURFACE = 1;
4334 final static int CONTAINER_STATE_FINISHING = 2;
4335 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4336
Wale Ogunwale1666e312016-12-16 11:27:18 -08004337 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004338 synchronized (mService) {
4339 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004340 mActivityDisplay = activityDisplay;
4341 new ActivityStack(this, mRecentTasks, onTop);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004342 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004343 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004344 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004345 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004346
Andrii Kulian839def92016-11-02 10:58:58 -07004347 /**
4348 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004349 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004350 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004351 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004352 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004353 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004354 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004355 if (mActivityDisplay != null) {
4356 throw new IllegalStateException("ActivityContainer is already attached, " +
4357 "displayId=" + mActivityDisplay.mDisplayId);
4358 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004359 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004360 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004361 }
4362
4363 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004364 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004365 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004366 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4367 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004368 return;
4369 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004370 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 }
4372 }
4373
4374 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004375 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004376 synchronized (mService) {
4377 if (mActivityDisplay != null) {
4378 return mActivityDisplay.mDisplayId;
4379 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004380 }
4381 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004382 }
4383
Jeff Brownca9bc702014-02-11 14:32:56 -08004384 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004385 public int getStackId() {
4386 synchronized (mService) {
4387 return mStackId;
4388 }
4389 }
4390
4391 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004392 public boolean injectEvent(InputEvent event) {
4393 final long origId = Binder.clearCallingIdentity();
4394 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004395 synchronized (mService) {
4396 if (mActivityDisplay != null) {
4397 return mInputManagerInternal.injectInputEvent(event,
4398 mActivityDisplay.mDisplayId,
4399 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4400 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004401 }
4402 return false;
4403 } finally {
4404 Binder.restoreCallingIdentity(origId);
4405 }
4406 }
4407
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004408 @Override
4409 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004410 synchronized (mService) {
4411 if (mContainerState == CONTAINER_STATE_FINISHING) {
4412 return;
4413 }
4414 mContainerState = CONTAINER_STATE_FINISHING;
4415
Craig Mautnerd163e752014-06-13 17:18:47 -07004416 long origId = Binder.clearCallingIdentity();
4417 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004418 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004419 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004420 } finally {
4421 Binder.restoreCallingIdentity(origId);
4422 }
4423 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004424 }
4425
Andrii Kulian03c403d2016-11-11 11:14:12 -08004426 /**
4427 * Remove the stack completely. Must be called only when there are no tasks left in it,
4428 * as this method does not finish running activities.
4429 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004430 void removeLocked() {
4431 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004432 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004433 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004434 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004435 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004436 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004437 }
4438
Andrii Kulian839def92016-11-02 10:58:58 -07004439 /**
4440 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4441 * completely or re-parented.
4442 */
4443 private void removeFromDisplayLocked() {
4444 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4445 + " current displayId=" + mActivityDisplay.mDisplayId);
4446
Wale Ogunwale1666e312016-12-16 11:27:18 -08004447 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004448 mActivityDisplay = null;
4449 }
4450
4451 /**
4452 * Move the stack to specified display.
4453 * @param activityDisplay Target display to move the stack to.
4454 */
4455 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4456 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4457 + mActivityDisplay + " to display=" + activityDisplay
4458 + " Callers=" + Debug.getCallers(2));
4459
4460 removeFromDisplayLocked();
4461
4462 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004463 mStack.reparent(activityDisplay, ON_TOP);
Andrii Kulian839def92016-11-02 10:58:58 -07004464 }
4465
Craig Mautner4a1cb222013-12-04 16:14:06 -08004466 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004467 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004468 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004469 }
4470
4471 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004472 public final int startActivityIntentSender(IIntentSender intentSender)
4473 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004474 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4475
4476 if (!(intentSender instanceof PendingIntentRecord)) {
4477 throw new IllegalArgumentException("Bad PendingIntent object");
4478 }
4479
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004480 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004481 Binder.getCallingUid(), mCurrentUser, false,
4482 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004483
4484 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4485 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4486 pendingIntent.key.requestResolvedType);
4487
4488 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4489 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4490 }
4491
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004492 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004493 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004494 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004495 throw new SecurityException(
4496 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4497 }
4498 }
4499
Craig Mautnerdf88d732014-01-27 09:21:32 -08004500 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004501 public IBinder asBinder() {
4502 return this;
4503 }
4504
Craig Mautner4504de52013-12-20 09:06:56 -08004505 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004506 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004507 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004508 }
4509
Craig Mautner4a1cb222013-12-04 16:14:06 -08004510 ActivityStackSupervisor getOuter() {
4511 return ActivityStackSupervisor.this;
4512 }
4513
Craig Mautnerd163e752014-06-13 17:18:47 -07004514 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004515 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004516 }
4517
Craig Mautner6985bad2014-04-21 15:22:06 -07004518 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004519 void setVisible(boolean visible) {
4520 if (mVisible != visible) {
4521 mVisible = visible;
4522 if (mCallback != null) {
4523 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4524 0 /* unused */, this).sendToTarget();
4525 }
4526 }
4527 }
4528
Craig Mautner6985bad2014-04-21 15:22:06 -07004529 void setDrawn() {
4530 }
4531
Craig Mautner1b4bf852014-05-26 15:06:32 -07004532 // You can always start a new task on a regular ActivityStack.
4533 boolean isEligibleForNewTasks() {
4534 return true;
4535 }
4536
Craig Mautnerd163e752014-06-13 17:18:47 -07004537 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004538 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004539 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004540 }
4541
Craig Mautner34b73df2014-01-12 21:11:08 -08004542 @Override
4543 public String toString() {
4544 return mIdString + (mActivityDisplay == null ? "N" : "A");
4545 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004546 }
4547
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004548 private class VirtualActivityContainer extends ActivityContainer {
4549 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004550 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004551
4552 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004553 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4554 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004555 mParentActivity = parent;
4556 mCallback = callback;
4557 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004558 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004559 }
4560
4561 @Override
4562 public void setSurface(Surface surface, int width, int height, int density) {
4563 super.setSurface(surface, width, height, density);
4564
4565 synchronized (mService) {
4566 final long origId = Binder.clearCallingIdentity();
4567 try {
4568 setSurfaceLocked(surface, width, height, density);
4569 } finally {
4570 Binder.restoreCallingIdentity(origId);
4571 }
4572 }
4573 }
4574
4575 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4576 if (mContainerState == CONTAINER_STATE_FINISHING) {
4577 return;
4578 }
4579 VirtualActivityDisplay virtualActivityDisplay =
4580 (VirtualActivityDisplay) mActivityDisplay;
4581 if (virtualActivityDisplay == null) {
4582 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004583 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004584 mActivityDisplay = virtualActivityDisplay;
4585 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004586 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004587 }
4588
4589 if (mSurface != null) {
4590 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004591 }
4592
Craig Mautner6985bad2014-04-21 15:22:06 -07004593 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004594 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004595 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004596 } else {
4597 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004598 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004599 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004600 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004601 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004602 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004603
Craig Mautnerd163e752014-06-13 17:18:47 -07004604 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004605
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004606 if (DEBUG_STACK) Slog.d(TAG_STACK,
4607 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004608 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004609
Craig Mautner6985bad2014-04-21 15:22:06 -07004610 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004611 boolean isAttachedLocked() {
4612 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004613 }
4614
4615 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004616 void setDrawn() {
4617 synchronized (mService) {
4618 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004619 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004620 }
4621 }
4622
Craig Mautner1b4bf852014-05-26 15:06:32 -07004623 // Never start a new task on an ActivityView if it isn't explicitly specified.
4624 @Override
4625 boolean isEligibleForNewTasks() {
4626 return false;
4627 }
4628
Craig Mautnerd163e752014-06-13 17:18:47 -07004629 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004630 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004631 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4632 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4633 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4634 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4635 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 }
4637 }
4638
Craig Mautner4a1cb222013-12-04 16:14:06 -08004639 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4640 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004641 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004642 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004643 int mDisplayId;
4644 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004645 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4646 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004647
Craig Mautner4a1cb222013-12-04 16:14:06 -08004648 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4649 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004650 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004651
Jose Lima4b6c6692014-08-12 17:41:12 -07004652 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004653
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004654 /** Array of all UIDs that are present on the display. */
4655 private IntArray mDisplayAccessUIDs = new IntArray();
4656
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004657 ActivityDisplay() {
4658 }
Craig Mautner4504de52013-12-20 09:06:56 -08004659
Craig Mautner1a70a162014-09-13 12:09:31 -07004660 // After instantiation, check that mDisplay is not null before using this. The alternative
4661 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004662 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004663 final Display display = mDisplayManager.getDisplay(displayId);
4664 if (display == null) {
4665 return;
4666 }
4667 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004668 }
4669
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004670 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004671 mDisplay = display;
4672 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004673 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004674
Wale Ogunwale1666e312016-12-16 11:27:18 -08004675 void attachStack(ActivityStack stack, boolean onTop) {
4676 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
4677 + " to displayId=" + mDisplayId + " onTop=" + onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004678 if (onTop) {
4679 mStacks.add(stack);
4680 } else {
4681 mStacks.add(0, stack);
4682 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004683 }
4684
Wale Ogunwale1666e312016-12-16 11:27:18 -08004685 void detachStack(ActivityStack stack) {
4686 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004687 + " from displayId=" + mDisplayId);
4688 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004689 }
4690
Jose Lima4b6c6692014-08-12 17:41:12 -07004691 void setVisibleBehindActivity(ActivityRecord r) {
4692 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004693 }
4694
Jose Lima4b6c6692014-08-12 17:41:12 -07004695 boolean hasVisibleBehindActivity() {
4696 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004697 }
4698
Craig Mautner34b73df2014-01-12 21:11:08 -08004699 @Override
4700 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004701 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4702 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004703
4704 @Override
4705 protected int getChildCount() {
4706 return mStacks.size();
4707 }
4708
4709 @Override
4710 protected ConfigurationContainer getChildAt(int index) {
4711 return mStacks.get(index);
4712 }
4713
4714 @Override
4715 protected ConfigurationContainer getParent() {
4716 return ActivityStackSupervisor.this;
4717 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004718
4719 boolean isPrivate() {
4720 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4721 }
4722
4723 boolean isUidPresent(int uid) {
4724 for (ActivityStack stack : mStacks) {
4725 if (stack.isUidPresent(uid)) {
4726 return true;
4727 }
4728 }
4729 return false;
4730 }
4731
4732 /** Update and get all UIDs that are present on the display and have access to it. */
4733 private IntArray getPresentUIDs() {
4734 mDisplayAccessUIDs.clear();
4735 for (ActivityStack stack : mStacks) {
4736 stack.getPresentUIDs(mDisplayAccessUIDs);
4737 }
4738 return mDisplayAccessUIDs;
4739 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004740 }
4741
4742 class VirtualActivityDisplay extends ActivityDisplay {
4743 VirtualDisplay mVirtualDisplay;
4744
Craig Mautner6985bad2014-04-21 15:22:06 -07004745 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004746 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004747 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4748 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4749 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4750 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004751
4752 init(mVirtualDisplay.getDisplay());
4753
4754 mWindowManager.handleDisplayAdded(mDisplayId);
4755 }
4756
4757 void setSurface(Surface surface) {
4758 if (mVirtualDisplay != null) {
4759 mVirtualDisplay.setSurface(surface);
4760 }
4761 }
4762
4763 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004764 void detachStack(ActivityStack stack) {
4765 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004766 if (mVirtualDisplay != null) {
4767 mVirtualDisplay.release();
4768 mVirtualDisplay = null;
4769 }
4770 }
4771
4772 @Override
4773 public String toString() {
4774 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004775 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004776 }
Jose Lima58e66d62014-05-27 20:00:27 -07004777
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004778 ActivityStack findStackBehind(ActivityStack stack) {
4779 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004780 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004781 if (display == null) {
4782 return null;
4783 }
4784 final ArrayList<ActivityStack> stacks = display.mStacks;
4785 for (int i = stacks.size() - 1; i >= 0; i--) {
4786 if (stacks.get(i) == stack && i > 0) {
4787 return stacks.get(i - 1);
4788 }
4789 }
4790 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4791 + " in=" + stacks);
4792 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004793
Jorim Jaggic69bd222016-03-15 14:38:37 +01004794 /**
4795 * Puts a task into resizing mode during the next app transition.
4796 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004797 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004798 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004799 private void setResizingDuringAnimation(TaskRecord task) {
4800 mResizingTasksDuringAnimation.add(task.taskId);
4801 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004802 }
4803
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004804 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4805 final TaskRecord task;
4806 final int callingUid;
4807 final String callingPackage;
4808 final Intent intent;
4809 final int userId;
4810 final ActivityOptions activityOptions = (bOptions != null)
4811 ? new ActivityOptions(bOptions) : null;
4812 final int launchStackId = (activityOptions != null)
4813 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004814 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004815 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004816 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004817 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004818
4819 if (launchStackId == DOCKED_STACK_ID) {
4820 mWindowManager.setDockedStackCreateState(
4821 activityOptions.getDockCreateMode(), null /* initialBounds */);
4822
4823 // Defer updating the stack in which recents is until the app transition is done, to
4824 // not run into issues where we still need to draw the task in recents but the
4825 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004826 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004827 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4828 }
4829
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004830 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4831 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004832 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004833 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004834 throw new IllegalArgumentException(
4835 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4836 }
4837
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004838 // Since we don't have an actual source record here, we assume that the currently focused
4839 // activity was the source.
4840 final ActivityStack focusedStack = getFocusedStack();
4841 final ActivityRecord sourceRecord =
4842 focusedStack != null ? focusedStack.topActivity() : null;
4843
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004844 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004845 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004846 moveTaskToStackLocked(
4847 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4848 ANIMATE);
4849 }
4850 }
4851
4852 // If the user must confirm credentials (e.g. when first launching a work app and the
4853 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4854 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4855 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004856 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004857 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004858 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004859 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4860 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004861
4862 // If we are launching the task in the docked stack, put it into resizing mode so
4863 // the window renders full-screen with the background filling the void. Also only
4864 // call this at the end to make sure that tasks exists on the window manager side.
4865 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004866 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004867 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004868
4869 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4870 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004871 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4872 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004873 return ActivityManager.START_TASK_TO_FRONT;
4874 }
4875 callingUid = task.mCallingUid;
4876 callingPackage = task.mCallingPackage;
4877 intent = task.intent;
4878 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4879 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004880 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4881 null, null, 0, 0, bOptions, userId, null, task);
4882 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004883 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004884 }
4885 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004886 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004887
4888 /**
4889 * @return a list of activities which are the top ones in each visible stack. The first
4890 * entry will be the focused activity.
4891 */
4892 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004893 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4894 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004895 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004896 if (display == null) {
4897 return Collections.EMPTY_LIST;
4898 }
4899 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4900 final ArrayList<ActivityStack> stacks = display.mStacks;
4901 for (int i = stacks.size() - 1; i >= 0; i--) {
4902 ActivityStack stack = stacks.get(i);
4903 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4904 ActivityRecord top = stack.topActivity();
4905 if (top != null) {
4906 if (stack == mFocusedStack) {
4907 topActivityTokens.add(0, top.appToken);
4908 } else {
4909 topActivityTokens.add(top.appToken);
4910 }
4911 }
4912 }
4913 }
4914 return topActivityTokens;
4915 }
Felipe Lemebd00fef2017-01-24 15:10:26 -08004916
4917 public IBinder getTopVisibleActivity(int uid) {
4918 // TODO(b/33197203): get rid of DEFAULT_DISPLAY here?. Used in
4919 // VoiceInteractionManagerServiceImpl#showSessionLocked.
4920 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
4921 if (display == null) {
4922 return null;
4923 }
4924 final ArrayList<ActivityStack> stacks = display.mStacks;
4925 for (int i = stacks.size() - 1; i >= 0; i--) {
4926 ActivityStack stack = stacks.get(i);
4927 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4928 ActivityRecord top = stack.topActivity();
4929 if (top != null && stack == mFocusedStack && top.app.uid == uid) {
4930 return top.appToken;
4931 }
4932 }
4933 }
4934 return null;
4935 }
Craig Mautner27084302013-03-25 08:05:25 -07004936}