blob: 2c1c3a191d11bd31f2b3caadfeb1e7a4e046e742 [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;
93import static java.lang.Integer.MIN_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020094
Svetoslav7008b512015-06-24 18:47:07 -070095import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -070096import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +010097import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070098import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800100import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700101import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800102import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityOptions;
104import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700105import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700107import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400109import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700110import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400111import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700113import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import android.content.Intent;
116import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700118import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.pm.PackageManager;
120import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100121import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700123import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800124import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800125import android.hardware.display.DisplayManager;
126import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800127import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800128import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800129import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800130import android.hardware.input.InputManager;
131import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100133import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700134import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700135import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700136import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700137import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700138import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700140import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700141import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700142import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400143import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700145import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700146import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700147import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100148import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700149import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700150import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400151import android.provider.Settings;
152import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700153import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700154import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700155import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700156import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700157import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800158import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700159import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800162import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800163import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800164import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800165
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800166import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700167import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400168import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700169import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400170import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800171import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700172import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700173import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700174
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175import java.io.FileDescriptor;
176import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700177import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700178import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700179import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700180import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700181import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700182import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700183
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800184public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800185 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700187 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700189 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700193 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800196 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800198
Craig Mautnerf3333272013-04-22 10:55:53 -0700199 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700201
Craig Mautner0eea92c2013-05-16 13:35:39 -0700202 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700204
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700205 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700207
Craig Mautner05d29032013-05-03 13:40:13 -0700208 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
209 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
210 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700211 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800213 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
214 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
215 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700216 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400217 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
218 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700219 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700220 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700221 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800222 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
223 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700226
Jason Monk62515be2014-05-21 16:06:19 -0400227 private static final String LOCK_TASK_TAG = "Lock-to-App";
228
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229 // Used to indicate if an object (e.g. stack) that we are trying to get
230 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800231 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700233 // Used to indicate that windows of activities should be preserved during the resize.
234 static final boolean PRESERVE_WINDOWS = true;
235
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236 // Used to indicate if an object (e.g. task) should be moved/created
237 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700238 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700239
240 // Used to indicate that an objects (e.g. task) removal from its container
241 // (e.g. stack) is due to it moving to another container.
242 static final boolean MOVING = true;
243
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700244 // Force the focus to change to the stack we are moving a task to..
245 static final boolean FORCE_FOCUS = true;
246
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700247 // Restore task from the saved recents if it can't be found in any live stack.
248 static final boolean RESTORE_FROM_RECENTS = true;
249
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700250 // Don't execute any calls to resume.
251 static final boolean DEFER_RESUME = true;
252
Winson Chung010927a2016-12-15 16:12:35 -0800253 // Used to indicate that a task is removed it should also be removed from recents.
254 static final boolean REMOVE_FROM_RECENTS = true;
255
Svetoslav7008b512015-06-24 18:47:07 -0700256 // Activity actions an app cannot start if it uses a permission which is not granted.
257 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
258 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700259
Svetoslav7008b512015-06-24 18:47:07 -0700260 static {
261 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
262 Manifest.permission.CAMERA);
263 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
264 Manifest.permission.CAMERA);
265 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
266 Manifest.permission.CALL_PHONE);
267 }
268
Svet Ganov99b60432015-06-27 13:15:22 -0700269 /** Action restriction: launching the activity is not restricted. */
270 private static final int ACTIVITY_RESTRICTION_NONE = 0;
271 /** Action restriction: launching the activity is restricted by a permission. */
272 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
273 /** Action restriction: launching the activity is restricted by an app op. */
274 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700275
justinzhang5286d3f2014-05-12 17:06:01 -0400276 /** Status Bar Service **/
277 private IBinder mToken = new Binder();
278 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400279 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400280
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700281 // For debugging to make sure the caller when acquiring/releasing our
282 // wake lock is the system process.
283 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800284 /** The number of distinct task ids that can be assigned to the tasks of a single user */
285 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700286
Craig Mautner27084302013-03-25 08:05:25 -0700287 final ActivityManagerService mService;
288
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800289 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800290
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700291 final ActivityStackSupervisorHandler mHandler;
292
293 /** Short cut */
294 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700296
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700297 /** Counter for next free stack ID to use for dynamic activity stacks. */
298 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700299
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800300 /**
301 * Maps the task identifier that activities are currently being started in to the userId of the
302 * task. Each time a new task is created, the entry for the userId of the task is incremented
303 */
304 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700305
Craig Mautner2420ead2013-04-01 17:13:20 -0700306 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800307 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700308
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 /** The stack containing the launcher app. Assumed to always be attached to
310 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700315
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
317 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800318 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320
321 /** List of activities that are waiting for a new activity to become visible before completing
322 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700326 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
328 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700329 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331 /** List of activities that are ready to be stopped, but waiting for the next activity to
332 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800333 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334
Craig Mautnerf3333272013-04-22 10:55:53 -0700335 /** List of activities that are ready to be finished, but waiting for the previous activity to
336 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700338
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800340 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341
Wale Ogunwale22e25262016-02-01 10:32:02 -0800342 /** List of activities whose multi-window mode changed that we need to report to the
343 * application */
344 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
345
346 /** List of activities whose picture-in-picture mode changed that we need to report to the
347 * application */
348 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
349
Craig Mautnerf3333272013-04-22 10:55:53 -0700350 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700351 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700352
Craig Mautnerde4ef022013-04-07 19:01:33 -0700353 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
354 * is being brought in front of us. */
355 boolean mUserLeaving = false;
356
Craig Mautner0eea92c2013-05-16 13:35:39 -0700357 /** Set when we have taken too long waiting to go to sleep. */
358 boolean mSleepTimeout = false;
359
360 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361 * We don't want to allow the device to go to sleep while in the process
362 * of launching an activity. This is primarily to allow alarm intent
363 * receivers to launch an activity and get that to run before the device
364 * goes back to sleep.
365 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700366 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700367
368 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369 * Set when the system is going to sleep, until we have
370 * successfully paused the current activity and released our wake lock.
371 * At that point the system is allowed to actually sleep.
372 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700373 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700374
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700375 /** Stack id of the front stack when user switched, indexed by userId. */
376 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700377
Craig Mautner4504de52013-12-20 09:06:56 -0800378 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381
382 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700383 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800385 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
386
387 private DisplayManagerInternal mDisplayManagerInternal;
388 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800389
Craig Mautner15df08a2015-04-01 12:17:18 -0700390 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
391 * may be finished until there is only one entry left. If this is empty the system is not
392 * in lockTask mode. */
393 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000394 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700395 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
396 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000397 */
398 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400399 /**
400 * Notifies the user when entering/exiting lock-task.
401 */
402 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800403
Wale Ogunwaled046a012015-12-24 13:05:59 -0800404 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800405 boolean inResumeTopActivity;
406
Andrii Kulian1e32e022016-09-16 15:29:34 -0700407 /**
408 * Temporary rect used during docked stack resize calculation so we don't need to create a new
409 * object each time.
410 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700411 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700412
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700413 // The default minimal size that will be used if the activity doesn't specify its minimal size.
414 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700415 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700416
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700417 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
418 private boolean mTaskLayersChanged = true;
419
Jorim Jaggi275561a2016-02-23 10:11:02 -0500420 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800421
Jorim Jaggidc249c42015-12-15 14:57:31 -0800422 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
423
Andrii Kulian1779e612016-10-12 21:58:25 -0700424 @Override
425 protected int getChildCount() {
426 return mActivityDisplays.size();
427 }
428
429 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700430 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700431 return mActivityDisplays.valueAt(index);
432 }
433
434 @Override
435 protected ConfigurationContainer getParent() {
436 return null;
437 }
438
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700439 Configuration getDisplayOverrideConfiguration(int displayId) {
440 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
441 if (activityDisplay == null) {
442 throw new IllegalArgumentException("No display found with id: " + displayId);
443 }
444
445 return activityDisplay.getOverrideConfiguration();
446 }
447
448 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
449 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
450 if (activityDisplay == null) {
451 throw new IllegalArgumentException("No display found with id: " + displayId);
452 }
453
454 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
455 }
456
Wale Ogunwale39381972015-12-17 17:15:29 -0800457 static class FindTaskResult {
458 ActivityRecord r;
459 boolean matchedByRootAffinity;
460 }
461 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
462
Craig Mautneree36c772014-07-16 14:56:05 -0700463 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800464 * Temp storage for display ids sorted in focus order.
465 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
466 * it's more efficient, as the number of displays is usually small.
467 */
468 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
469
470 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100471 * Used to keep track whether app visibilities got changed since the last pause. Useful to
472 * determine whether to invoke the task stack change listener after pausing.
473 */
474 boolean mAppVisibilitiesChangedSinceLastPause;
475
476 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100477 * Set of tasks that are in resizing mode during an app transition to fill the "void".
478 */
479 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
480
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700481
482 /**
483 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
484 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
485 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
486 * like the docked stack going empty.
487 */
488 private boolean mAllowDockedStackResize = true;
489
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100490 /**
Tony Mak853304c2016-04-18 15:17:41 +0100491 * Is dock currently minimized.
492 */
493 boolean mIsDockMinimized;
494
Jorim Jaggife762342016-10-13 14:33:27 +0200495 final KeyguardController mKeyguardController;
496
Tony Mak853304c2016-04-18 15:17:41 +0100497 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700498 * Description of a request to start a new activity, which has been held
499 * due to app switches being disabled.
500 */
501 static class PendingActivityLaunch {
502 final ActivityRecord r;
503 final ActivityRecord sourceRecord;
504 final int startFlags;
505 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700506 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700507
508 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700509 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700510 r = _r;
511 sourceRecord = _sourceRecord;
512 startFlags = _startFlags;
513 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700514 callerApp = _callerApp;
515 }
516
517 void sendErrorResult(String message) {
518 try {
519 if (callerApp.thread != null) {
520 callerApp.thread.scheduleCrash(message);
521 }
522 } catch (RemoteException e) {
523 Slog.e(TAG, "Exception scheduling crash of failed "
524 + "activity launcher sourceRecord=" + sourceRecord, e);
525 }
Craig Mautneree36c772014-07-16 14:56:05 -0700526 }
527 }
528
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800529 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700530 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700531 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800532 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800533 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jorim Jaggife762342016-10-13 14:33:27 +0200534 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700535 }
536
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800537 void setRecentTasks(RecentTasks recentTasks) {
538 mRecentTasks = recentTasks;
539 }
540
Jeff Brown2c43c332014-06-12 22:38:59 -0700541 /**
542 * At the time when the constructor runs, the power manager has not yet been
543 * initialized. So we initialize our wakelocks afterwards.
544 */
545 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800546 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700547 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700548 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700549 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700550 }
551
justinzhang5286d3f2014-05-12 17:06:01 -0400552 // This function returns a IStatusBarService. The value is from ServiceManager.
553 // getService and is cached.
554 private IStatusBarService getStatusBarService() {
555 synchronized (mService) {
556 if (mStatusBarService == null) {
557 mStatusBarService = IStatusBarService.Stub.asInterface(
558 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
559 if (mStatusBarService == null) {
560 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
561 }
562 }
563 return mStatusBarService;
564 }
565 }
566
Jason Monk35c62a42014-06-17 10:24:47 -0400567 private IDevicePolicyManager getDevicePolicyManager() {
568 synchronized (mService) {
569 if (mDevicePolicyManager == null) {
570 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
571 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
572 if (mDevicePolicyManager == null) {
573 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
574 }
575 }
576 return mDevicePolicyManager;
577 }
578 }
579
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700580 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 synchronized (mService) {
582 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200583 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584
585 mDisplayManager =
586 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
587 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800588 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800589
590 Display[] displays = mDisplayManager.getDisplays();
591 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
592 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700594 if (activityDisplay.mDisplay == null) {
595 throw new IllegalStateException("Default Display does not exist");
596 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800597 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700598 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 }
600
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800601 mHomeStack = mFocusedStack = mLastFocusedStack =
602 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800603
604 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 }
Craig Mautner27084302013-03-25 08:05:25 -0700606 }
607
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700608 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700610 }
611
Craig Mautnerde4ef022013-04-07 19:01:33 -0700612 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700614 }
615
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700616 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700617 if (stack == null) {
618 return false;
619 }
620
Craig Mautnerdf88d732014-01-27 09:21:32 -0800621 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
622 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700623 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800624 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800625 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700626 }
627
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700628 /** The top most stack. */
629 boolean isFrontStack(ActivityStack stack) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800630 return isFrontOfStackList(stack, mHomeStack.mStacks);
631 }
632
633 /** The top most stack on its display. */
634 boolean isFrontStackOnDisplay(ActivityStack stack) {
635 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
636 }
637
638 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700639 if (stack == null) {
640 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700642
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700643 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
644 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700645 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800646 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800647 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700648 }
649
Wale Ogunwaled046a012015-12-24 13:05:59 -0800650 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
652 if (!focusCandidate.isFocusable()) {
653 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800654 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800655 }
656
657 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800658 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800659 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800660
Wale Ogunwaled046a012015-12-24 13:05:59 -0800661 EventLogTags.writeAmFocusedStack(
662 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
663 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
664 }
665
666 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800667 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800668 if (r != null && r.idle) {
669 checkFinishBootingLocked();
670 }
671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 }
673
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700674 void moveHomeStackToFront(String reason) {
675 mHomeStack.moveToFront(reason);
676 }
677
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800679 boolean moveHomeStackTaskToTop(String reason) {
680 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700682 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700683 if (top == null) {
684 return false;
685 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700686 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700687 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700688 }
689
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800690 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700691 if (!mService.mBooting && !mService.mBooted) {
692 // Not ready yet!
693 return false;
694 }
695
Craig Mautner84984fa2014-06-19 11:19:20 -0700696 if (prev != null) {
697 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
698 }
699
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800700 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700701 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 final String myReason = reason + " resumeHomeStackTask";
703
Mark Lua56ea122015-10-08 13:31:01 +0800704 // Only resume home activity if isn't finishing.
705 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700706 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800707 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700708 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800709 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700710 }
711
Craig Mautner8d341ef2013-03-26 09:03:27 -0700712 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700713 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700714 }
715
716 /**
717 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
718 * @param id Id of the task we would like returned.
719 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
720 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700721 * @param stackId The stack to restore the task to (default launch stack will be used if
722 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700723 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700724 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
729 ActivityStack stack = stacks.get(stackNdx);
730 TaskRecord task = stack.taskForIdLocked(id);
731 if (task != null) {
732 return task;
733 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700734 }
735 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800736
737 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700738 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800739 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800740 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700741 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800742 return null;
743 }
744
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700745 if (!restoreFromRecents) {
746 return task;
747 }
748
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700749 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700750 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
751 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800752 return null;
753 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700754 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800755 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700756 }
757
Craig Mautner6170f732013-04-02 13:05:23 -0700758 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800759 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800761 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
763 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
764 if (r != null) {
765 return r;
766 }
Craig Mautner6170f732013-04-02 13:05:23 -0700767 }
768 }
769 return null;
770 }
771
Tony Mak853304c2016-04-18 15:17:41 +0100772 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000773 * Detects whether we should show a lock screen in front of this task for a locked user.
774 * <p>
775 * We'll do this if either of the following holds:
776 * <ul>
777 * <li>The top activity explicitly belongs to {@param userId}.</li>
778 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
779 * </ul>
780 *
781 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100782 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000783 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
784 // To handle the case that work app is in the task but just is not the top one.
785 final ActivityRecord activityRecord = task.getTopActivity();
786 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
787
788 return (activityRecord != null && activityRecord.userId == userId)
789 || (resultTo != null && resultTo.userId == userId);
790 }
791
792 /**
793 * Find all visible task stacks containing {@param userId} and intercept them with an activity
794 * to block out the contents and possibly start a credential-confirming intent.
795 *
796 * @param userId user handle for the locked managed profile.
797 */
798 void lockAllProfileTasks(@UserIdInt int userId) {
799 mWindowManager.deferSurfaceLayout();
800 try {
801 final List<ActivityStack> stacks = getStacks();
802 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
803 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
804 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
805 final TaskRecord task = tasks.get(taskNdx);
806
807 // Check the task for a top activity belonging to userId, or returning a result
808 // to an activity belonging to userId. Example case: a document picker for
809 // personal files, opened by a work app, should still get locked.
810 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000811 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
812 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000813 }
Tony Mak853304c2016-04-18 15:17:41 +0100814 }
815 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000816 } finally {
817 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100818 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000819 }
820
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800821 void setNextTaskIdForUserLocked(int taskId, int userId) {
822 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
823 if (taskId > currentTaskId) {
824 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700825 }
826 }
827
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700828 static int nextTaskIdForUser(int taskId, int userId) {
829 int nextTaskId = taskId + 1;
830 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
831 // Wrap around as there will be smaller task ids that are available now.
832 nextTaskId -= MAX_TASK_IDS_PER_USER;
833 }
834 return nextTaskId;
835 }
836
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800837 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800838 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
839 // for a userId u, a taskId can only be in the range
840 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
841 // 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 -0700842 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800843 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
844 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
845 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700846 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800847 if (candidateTaskId == currentTaskId) {
848 // Something wrong!
849 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
850 throw new IllegalStateException("Cannot get an available task id."
851 + " Reached limit of " + MAX_TASK_IDS_PER_USER
852 + " running tasks per user.");
853 }
854 }
855 mCurTaskIdForUser.put(userId, candidateTaskId);
856 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700857 }
858
Chong Zhang6cda19c2016-06-14 19:07:56 -0700859 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800860 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700861 if (stack == null) {
862 return null;
863 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 ActivityRecord resumedActivity = stack.mResumedActivity;
865 if (resumedActivity == null || resumedActivity.app == null) {
866 resumedActivity = stack.mPausingActivity;
867 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700868 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700869 }
870 }
871 return resumedActivity;
872 }
873
Dianne Hackbornff072722014-09-24 10:56:28 -0700874 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700875 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800876 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800877 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
878 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
880 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700881 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 continue;
883 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700884 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 if (hr != null) {
886 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
887 && processName.equals(hr.processName)) {
888 try {
George Mount2c92c972014-03-20 09:38:23 -0700889 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 didSomething = true;
891 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700892 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 Slog.w(TAG, "Exception in new application when starting activity "
894 + hr.intent.getComponent().flattenToShortString(), e);
895 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700896 }
Craig Mautner20e72272013-04-01 13:45:53 -0700897 }
Craig Mautner20e72272013-04-01 13:45:53 -0700898 }
899 }
900 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700901 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700902 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700903 }
Craig Mautner20e72272013-04-01 13:45:53 -0700904 return didSomething;
905 }
906
907 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800908 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
909 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
911 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700912 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 continue;
914 }
915 final ActivityRecord resumedActivity = stack.mResumedActivity;
916 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700917 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800918 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800919 return false;
920 }
Craig Mautner20e72272013-04-01 13:45:53 -0700921 }
922 }
Wei Wang65c7a152016-06-02 18:51:22 -0700923 // Send launch end powerhint when idle
924 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700925 return true;
926 }
927
Craig Mautnerde4ef022013-04-07 19:01:33 -0700928 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
930 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
932 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700933 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700935 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 return false;
937 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 }
939 }
940 }
941 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700942 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
944 mLastFocusedStack + " to=" + mFocusedStack);
945 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700946 return true;
947 }
948
949 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800950 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
952 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
954 final ActivityStack stack = stacks.get(stackNdx);
955 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800956 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700957 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800958 return false;
959 }
960 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800961 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700962 }
963 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800964 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700965 }
966
Craig Mautner2acc3892013-09-23 10:28:14 -0700967 /**
968 * Pause all activities in either all of the stacks or just the back stacks.
969 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700970 * @param resuming The resuming activity.
971 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
972 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700973 * @return true if any activity was paused as a result of this call.
974 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700975 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700976 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800977 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
978 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
980 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700981 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700982 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800983 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700984 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
985 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700987 }
988 }
989 return someActivityPaused;
990 }
991
Craig Mautnerde4ef022013-04-07 19:01:33 -0700992 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700993 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800994 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
995 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
997 final ActivityStack stack = stacks.get(stackNdx);
998 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700999 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001001 Slog.d(TAG_STATES,
1002 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001003 pausing = false;
1004 } else {
1005 return false;
1006 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001007 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001008 }
1009 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001010 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 }
1012
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001013 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001014 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001015 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1017 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1019 final ActivityStack stack = stacks.get(stackNdx);
1020 if (stack.mResumedActivity != null &&
1021 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001022 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001023 }
1024 }
1025 }
1026 }
1027
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001028 void cancelInitializingActivities() {
1029 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1030 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1031 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1032 stacks.get(stackNdx).cancelInitializingActivities();
1033 }
1034 }
1035 }
1036
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001038 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001039 }
1040
1041 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1042 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001043 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001044 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001045 if (w.who == null) {
1046 changed = true;
1047 w.timeout = false;
1048 if (r != null) {
1049 w.who = new ComponentName(r.info.packageName, r.info.name);
1050 }
1051 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1052 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001054 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001055 if (changed) {
1056 mService.notifyAll();
1057 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001058 }
1059
Chong Zhang5022da32016-06-21 16:31:37 -07001060 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1061 boolean changed = false;
1062 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1063 WaitResult w = mWaitingActivityLaunched.remove(i);
1064 if (w.who == null) {
1065 changed = true;
1066 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1067 // the starting activity ends up moving another activity to front, and it should
1068 // wait for this new activity to become visible instead.
1069 // Do not modify other fields.
1070 w.result = START_TASK_TO_FRONT;
1071 }
1072 }
1073 if (changed) {
1074 mService.notifyAll();
1075 }
1076 }
1077
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1079 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001080 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001081 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001082 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001083 if (w.who == null) {
1084 changed = true;
1085 w.timeout = timeout;
1086 if (r != null) {
1087 w.who = new ComponentName(r.info.packageName, r.info.name);
1088 }
1089 w.thisTime = thisTime;
1090 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001091 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001092 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001094 if (changed) {
1095 mService.notifyAll();
1096 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001097 }
1098
Craig Mautner29219d92013-04-16 20:19:12 -07001099 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001100 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001101 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001102 if (r != null) {
1103 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001104 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001105
Andrii Kulian7318d632016-07-20 18:59:28 -07001106 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001107 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001108 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1109 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001110 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001111 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001112 if (r != null) {
1113 return r;
1114 }
1115 }
1116 }
1117 return null;
1118 }
1119
Dianne Hackborn09233282014-04-30 11:33:59 -07001120 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001121 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001122 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1123 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001124 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001125 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001126 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001127 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1128 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001129 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001130 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001131 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001132 }
1133 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001134
1135 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1136 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1137 while (maxNum > 0) {
1138 long mostRecentActiveTime = Long.MIN_VALUE;
1139 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001140 final int numTaskLists = runningTaskLists.size();
1141 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1142 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001143 if (!stackTaskList.isEmpty()) {
1144 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1145 if (lastActiveTime > mostRecentActiveTime) {
1146 mostRecentActiveTime = lastActiveTime;
1147 selectedStackList = stackTaskList;
1148 }
1149 }
1150 }
1151 if (selectedStackList != null) {
1152 list.add(selectedStackList.remove(0));
1153 --maxNum;
1154 } else {
1155 break;
1156 }
1157 }
Craig Mautner20e72272013-04-01 13:45:53 -07001158 }
1159
Todd Kennedy7440f172015-12-09 14:31:22 -08001160 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1161 ProfilerInfo profilerInfo) {
1162 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001163 if (aInfo != null) {
1164 // Store the found target back into the intent, because now that
1165 // we have it we never want to do this again. For example, if the
1166 // user navigates back to this point in the history, we should
1167 // always restart the exact same activity.
1168 intent.setComponent(new ComponentName(
1169 aInfo.applicationInfo.packageName, aInfo.name));
1170
1171 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001172 if (!aInfo.processName.equals("system")) {
1173 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001174 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001175 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001176
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001177 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1178 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1179 }
1180
Man Caocfa78b22015-06-11 20:14:34 -07001181 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1182 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1183 }
1184
1185 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001186 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001187 }
1188 }
1189 }
1190 return aInfo;
1191 }
1192
Todd Kennedy7440f172015-12-09 14:31:22 -08001193 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001194 return resolveIntent(intent, resolvedType, userId, 0);
1195 }
1196
1197 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001198 try {
1199 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001200 PackageManager.MATCH_DEFAULT_ONLY | flags
1201 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001202 } catch (RemoteException e) {
1203 }
1204 return null;
1205 }
1206
1207 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1208 ProfilerInfo profilerInfo, int userId) {
1209 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1210 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1211 }
1212
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001213 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1214 boolean andResume, boolean checkConfig) throws RemoteException {
1215
1216 if (!allPausedActivitiesComplete()) {
1217 // While there are activities pausing we skipping starting any new activities until
1218 // pauses are complete. NOTE: that we also do this for activities that are starting in
1219 // the paused state because they will first be resumed then paused on the client side.
1220 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1221 "realStartActivityLocked: Skipping start of r=" + r
1222 + " some activities pausing...");
1223 return false;
1224 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001225
Craig Mautner2568c3a2015-03-26 14:22:34 -07001226 if (andResume) {
1227 r.startFreezingScreenLocked(app, 0);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001228 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001229
Craig Mautner2568c3a2015-03-26 14:22:34 -07001230 // schedule launch ticks to collect information about slow apps.
1231 r.startLaunchTickingLocked();
1232 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001233
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001234 // Have the window manager re-evaluate the orientation of the screen based on the new
1235 // activity order. Note that as a result of this, it can call back into the activity
1236 // manager with a new orientation. We don't care about that, because the activity is not
1237 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001238 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001239 final int displayId = r.getDisplayId();
1240 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1241 getDisplayOverrideConfiguration(displayId),
1242 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001243 // Deferring resume here because we're going to launch new activity shortly.
1244 // We don't want to perform a redundant launch of the same record while ensuring
1245 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001246 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1247 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 }
1249
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001250 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001251 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001252 }
1253
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 r.app = app;
1255 app.waitingToKill = null;
1256 r.launchCount++;
1257 r.lastLaunchTime = SystemClock.uptimeMillis();
1258
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001259 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001260
1261 int idx = app.activities.indexOf(r);
1262 if (idx < 0) {
1263 app.activities.add(r);
1264 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001265 mService.updateLruProcessLocked(app, true, null);
1266 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001267
Craig Mautner15df08a2015-04-01 12:17:18 -07001268 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001269 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1270 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001271 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001272 }
1273
Andrii Kulian02b7a832016-10-06 23:11:56 -07001274 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 try {
1276 if (app.thread == null) {
1277 throw new RemoteException();
1278 }
1279 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001280 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 if (andResume) {
1282 results = r.results;
1283 newIntents = r.newIntents;
1284 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001285 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1286 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1287 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 if (andResume) {
1289 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1290 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001291 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001293 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001294 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001295 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001297 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1298 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 r.sleeping = false;
1300 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001301 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 mService.showAskCompatModeDialogLocked(r);
1303 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1306 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1307 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001308 final String profileFile = mService.mProfileFile;
1309 if (profileFile != null) {
1310 ParcelFileDescriptor profileFd = mService.mProfileFd;
1311 if (profileFd != null) {
1312 try {
1313 profileFd = profileFd.dup();
1314 } catch (IOException e) {
1315 if (profileFd != null) {
1316 try {
1317 profileFd.close();
1318 } catch (IOException o) {
1319 }
1320 profileFd = null;
1321 }
1322 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001324
1325 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1326 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 }
1328 }
1329 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001330
Craig Mautner2568c3a2015-03-26 14:22:34 -07001331 if (andResume) {
1332 app.hasShownUi = true;
1333 app.pendingUiClean = true;
1334 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001335 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001336 // Because we could be starting an Activity in the system process this may not go across
1337 // a Binder interface which would create a new Configuration. Consequently we have to
1338 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001339 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001340 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001341 new Configuration(mService.getGlobalConfiguration()),
1342 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1343 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1344 r.persistentState, results, newIntents, !andResume,
1345 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001346
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001347 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001348 // This may be a heavy-weight process! Note that the package
1349 // manager will ensure that only activity can run in the main
1350 // process of the .apk, which is the only thing that will be
1351 // considered heavy-weight.
1352 if (app.processName.equals(app.info.packageName)) {
1353 if (mService.mHeavyWeightProcess != null
1354 && mService.mHeavyWeightProcess != app) {
1355 Slog.w(TAG, "Starting new heavy weight process " + app
1356 + " when already running "
1357 + mService.mHeavyWeightProcess);
1358 }
1359 mService.mHeavyWeightProcess = app;
1360 Message msg = mService.mHandler.obtainMessage(
1361 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1362 msg.obj = r;
1363 mService.mHandler.sendMessage(msg);
1364 }
1365 }
1366
1367 } catch (RemoteException e) {
1368 if (r.launchFailed) {
1369 // This is the second time we failed -- finish activity
1370 // and give up.
1371 Slog.e(TAG, "Second failure launching "
1372 + r.intent.getComponent().flattenToShortString()
1373 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001374 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001375 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1376 "2nd-crash", false);
1377 return false;
1378 }
1379
1380 // This is the first time we failed -- restart process and
1381 // retry.
1382 app.activities.remove(r);
1383 throw e;
1384 }
1385
1386 r.launchFailed = false;
1387 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001388 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001389 }
1390
1391 if (andResume) {
1392 // As part of the process of launching, ActivityThread also performs
1393 // a resume.
1394 stack.minimalResumeActivityLocked(r);
1395 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001396 // This activity is not starting in the resumed state... which should look like we asked
1397 // it to pause+stop (but remain visible), and it has done so and reported back the
1398 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001399 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001400 "Moving to PAUSED: " + r + " (starting in paused state)");
1401 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001402 }
1403
1404 // Launch the new version setup screen if needed. We do this -after-
1405 // launching the initial activity (that is, home), so that it can have
1406 // a chance to initialize itself while in the background, making the
1407 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001408 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001409 mService.startSetupActivityLocked();
1410 }
1411
Dianne Hackborn465fa392014-09-14 14:21:18 -07001412 // Update any services we are bound to that might care about whether
1413 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001414 if (r.app != null) {
1415 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1416 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001417
Craig Mautner2420ead2013-04-01 17:13:20 -07001418 return true;
1419 }
1420
Craig Mautnere79d42682013-04-01 19:01:53 -07001421 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001422 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001423 // Is this activity's application already running?
1424 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001425 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001426
Andrii Kulian02b7a832016-10-06 23:11:56 -07001427 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001428
1429 if (app != null && app.thread != null) {
1430 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001431 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1432 || !"android".equals(r.info.packageName)) {
1433 // Don't add this if it is a platform component that is marked
1434 // to run in multiple processes, because this is actually
1435 // part of the framework so doesn't make sense to track as a
1436 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001437 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1438 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001439 }
George Mount2c92c972014-03-20 09:38:23 -07001440 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001441 return;
1442 } catch (RemoteException e) {
1443 Slog.w(TAG, "Exception when starting activity "
1444 + r.intent.getComponent().flattenToShortString(), e);
1445 }
1446
1447 // If a dead object exception was thrown -- fall through to
1448 // restart the application.
1449 }
1450
1451 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001452 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001453 }
1454
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001455 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001456 String resultWho, int requestCode, int callingPid, int callingUid,
1457 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001458 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001459 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1460 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001461 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001462 return true;
1463 }
1464 final int componentRestriction = getComponentRestrictionForCallingPackage(
1465 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1466 final int actionRestriction = getActionRestrictionForCallingPackage(
1467 intent.getAction(), callingPackage, callingPid, callingUid);
1468 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1469 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1470 if (resultRecord != null) {
1471 resultStack.sendActivityResultLocked(-1,
1472 resultRecord, resultWho, requestCode,
1473 Activity.RESULT_CANCELED, null);
1474 }
1475 final String msg;
1476 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1477 msg = "Permission Denial: starting " + intent.toString()
1478 + " from " + callerApp + " (pid=" + callingPid
1479 + ", uid=" + callingUid + ")" + " with revoked permission "
1480 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1481 } else if (!aInfo.exported) {
1482 msg = "Permission Denial: starting " + intent.toString()
1483 + " from " + callerApp + " (pid=" + callingPid
1484 + ", uid=" + callingUid + ")"
1485 + " not exported from uid " + aInfo.applicationInfo.uid;
1486 } else {
1487 msg = "Permission Denial: starting " + intent.toString()
1488 + " from " + callerApp + " (pid=" + callingPid
1489 + ", uid=" + callingUid + ")"
1490 + " requires " + aInfo.permission;
1491 }
1492 Slog.w(TAG, msg);
1493 throw new SecurityException(msg);
1494 }
1495
1496 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1497 final String message = "Appop Denial: starting " + intent.toString()
1498 + " from " + callerApp + " (pid=" + callingPid
1499 + ", uid=" + callingUid + ")"
1500 + " requires " + AppOpsManager.permissionToOp(
1501 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1502 Slog.w(TAG, message);
1503 return false;
1504 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1505 final String message = "Appop Denial: starting " + intent.toString()
1506 + " from " + callerApp + " (pid=" + callingPid
1507 + ", uid=" + callingUid + ")"
1508 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1509 Slog.w(TAG, message);
1510 return false;
1511 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001512 if (options != null) {
1513 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1514 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1515 callingPid, callingUid);
1516 if (startInTaskPerm != PERMISSION_GRANTED) {
1517 final String msg = "Permission Denial: starting " + intent.toString()
1518 + " from " + callerApp + " (pid=" + callingPid
1519 + ", uid=" + callingUid + ") with launchTaskId="
1520 + options.getLaunchTaskId();
1521 Slog.w(TAG, msg);
1522 throw new SecurityException(msg);
1523 }
1524 }
1525 // Check if someone tries to launch an activity on a private display with a different
1526 // owner.
1527 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001528 if (launchDisplayId != INVALID_DISPLAY
1529 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1530 final String msg = "Permission Denial: starting " + intent.toString()
1531 + " from " + callerApp + " (pid=" + callingPid
1532 + ", uid=" + callingUid + ") with launchDisplayId="
1533 + launchDisplayId;
1534 Slog.w(TAG, msg);
1535 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001536 }
1537 }
1538
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001539 return true;
1540 }
1541
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001542 /** Check if caller is allowed to launch activities on specified display. */
1543 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1544 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1545 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1546
1547 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1548 if (activityDisplay == null) {
1549 Slog.w(TAG, "Launch on display check: display not found");
1550 return false;
1551 }
1552
1553 if (!activityDisplay.isPrivate()) {
1554 // Anyone can launch on a public display.
1555 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1556 + " allow launch on public display");
1557 return true;
1558 }
1559
1560 // Check if the caller is the owner of the display.
1561 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1562 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1563 + " allow launch for owner of the display");
1564 return true;
1565 }
1566
1567 // Check if caller is present on display
1568 if (activityDisplay.isUidPresent(callingUid)) {
1569 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1570 + " allow launch for caller present on the display");
1571 return true;
1572 }
1573
1574 // Check if the caller can launch anything.
1575 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1576 callingUid);
1577 if (startAnyPerm == PERMISSION_GRANTED) {
1578 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1579 + " allow launch any on display");
1580 return true;
1581 }
1582
1583 Slog.w(TAG, "Launch on display check: denied");
1584 return false;
1585 }
1586
1587 /** Update lists of UIDs that are present on displays and have access to them. */
1588 void updateUIDsPresentOnDisplay() {
1589 mDisplayAccessUIDs.clear();
1590 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1591 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1592 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1593 }
1594 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1595 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1596 }
1597
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001598 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001599 final long identity = Binder.clearCallingIdentity();
1600 try {
1601 return UserManager.get(mService.mContext).getUserInfo(userId);
1602 } finally {
1603 Binder.restoreCallingIdentity(identity);
1604 }
1605 }
1606
Svet Ganov99b60432015-06-27 13:15:22 -07001607 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001608 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001609 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1610 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001611 == PackageManager.PERMISSION_DENIED) {
1612 return ACTIVITY_RESTRICTION_PERMISSION;
1613 }
1614
Christopher Tateff7add02015-08-17 10:23:22 -07001615 if (activityInfo.permission == null) {
1616 return ACTIVITY_RESTRICTION_NONE;
1617 }
1618
Svet Ganov99b60432015-06-27 13:15:22 -07001619 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1620 if (opCode == AppOpsManager.OP_NONE) {
1621 return ACTIVITY_RESTRICTION_NONE;
1622 }
1623
1624 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1625 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001626 if (!ignoreTargetSecurity) {
1627 return ACTIVITY_RESTRICTION_APPOP;
1628 }
Svet Ganov99b60432015-06-27 13:15:22 -07001629 }
1630
1631 return ACTIVITY_RESTRICTION_NONE;
1632 }
1633
Svetoslav7008b512015-06-24 18:47:07 -07001634 private int getActionRestrictionForCallingPackage(String action,
1635 String callingPackage, int callingPid, int callingUid) {
1636 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001637 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001638 }
1639
1640 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1641 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001642 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001643 }
1644
1645 final PackageInfo packageInfo;
1646 try {
1647 packageInfo = mService.mContext.getPackageManager()
1648 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1649 } catch (PackageManager.NameNotFoundException e) {
1650 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001651 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001652 }
1653
1654 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001655 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001656 }
1657
1658 if (mService.checkPermission(permission, callingPid, callingUid) ==
1659 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001660 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001661 }
1662
1663 final int opCode = AppOpsManager.permissionToOpCode(permission);
1664 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001665 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001666 }
1667
1668 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1669 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001670 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001671 }
1672
Svet Ganov99b60432015-06-27 13:15:22 -07001673 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001674 }
1675
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001676 void setLaunchSource(int uid) {
1677 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1678 }
1679
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001680 void acquireLaunchWakelock() {
1681 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1682 throw new IllegalStateException("Calling must be system uid");
1683 }
1684 mLaunchingActivity.acquire();
1685 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1686 // To be safe, don't allow the wake lock to be held for too long.
1687 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1688 }
1689 }
1690
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001691 /**
1692 * Called when the frontmost task is idle.
1693 * @return the state of mService.mBooting before this was called.
1694 */
1695 private boolean checkFinishBootingLocked() {
1696 final boolean booting = mService.mBooting;
1697 boolean enableScreen = false;
1698 mService.mBooting = false;
1699 if (!mService.mBooted) {
1700 mService.mBooted = true;
1701 enableScreen = true;
1702 }
1703 if (booting || enableScreen) {
1704 mService.postFinishBooting(booting, enableScreen);
1705 }
1706 return booting;
1707 }
1708
Craig Mautnerf3333272013-04-22 10:55:53 -07001709 // Checked.
1710 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1711 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001712 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001713
Craig Mautnerf3333272013-04-22 10:55:53 -07001714 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001715 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001716 int NS = 0;
1717 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001718 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001719 boolean activityRemoved = false;
1720
Wale Ogunwale7d701172015-03-11 15:36:30 -07001721 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001722 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001723 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1724 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001725 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1726 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001727 if (fromTimeout) {
1728 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001729 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001730
1731 // This is a hack to semi-deal with a race condition
1732 // in the client where it can be constructed with a
1733 // newer configuration from when we asked it to launch.
1734 // We'll update with whatever configuration it now says
1735 // it used to launch.
1736 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001737 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001738 }
1739
1740 // We are now idle. If someone is waiting for a thumbnail from
1741 // us, we can now deliver.
1742 r.idle = true;
1743
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001744 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001745 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001746 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001747 }
1748 }
1749
1750 if (allResumedActivitiesIdle()) {
1751 if (r != null) {
1752 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001753 }
1754
1755 if (mLaunchingActivity.isHeld()) {
1756 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1757 if (VALIDATE_WAKE_LOCK_CALLER &&
1758 Binder.getCallingUid() != Process.myUid()) {
1759 throw new IllegalStateException("Calling must be system uid");
1760 }
1761 mLaunchingActivity.release();
1762 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001763 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001764 }
1765
1766 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001767 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001768 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001769 if ((NF = mFinishingActivities.size()) > 0) {
1770 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001771 mFinishingActivities.clear();
1772 }
1773
Craig Mautnerf3333272013-04-22 10:55:53 -07001774 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001775 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001776 mStartingUsers.clear();
1777 }
1778
Craig Mautnerf3333272013-04-22 10:55:53 -07001779 // Stop any activities that are scheduled to do so but have been
1780 // waiting for the next one to start.
1781 for (int i = 0; i < NS; i++) {
1782 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001783 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001784 if (stack != null) {
1785 if (r.finishing) {
1786 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1787 } else {
1788 stack.stopActivityLocked(r);
1789 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001790 }
1791 }
1792
1793 // Finish any activities that are scheduled to do so but have been
1794 // waiting for the next one to start.
1795 for (int i = 0; i < NF; i++) {
1796 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001797 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001798 if (stack != null) {
1799 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1800 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001801 }
1802
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001803 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001804 // Complete user switch
1805 if (startingUsers != null) {
1806 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001807 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001808 }
1809 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001810 }
1811
1812 mService.trimApplications();
1813 //dump();
1814 //mWindowManager.dump();
1815
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001817 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 }
1819
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001820 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001821 }
1822
Craig Mautner8e569572013-10-11 17:36:59 -07001823 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001824 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1826 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001827 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1828 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1829 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001830 }
Craig Mautner19091252013-10-05 00:03:53 -07001831 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001832 }
1833
1834 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001835 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1836 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001837 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1838 stacks.get(stackNdx).closeSystemDialogsLocked();
1839 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001840 }
1841 }
1842
Craig Mautner93529a42013-10-04 15:03:13 -07001843 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001844 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001845 }
1846
Craig Mautner8d341ef2013-03-26 09:03:27 -07001847 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001848 * Update the last used stack id for non-current user (current user's last
1849 * used stack is the focused stack)
1850 */
1851 void updateUserStackLocked(int userId, ActivityStack stack) {
1852 if (userId != mCurrentUser) {
1853 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1854 }
1855 }
1856
1857 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001858 * @return true if some activity was finished (or would have finished if doit were true).
1859 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001860 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1861 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001862 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001863 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1864 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001865 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001866 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001867 if (stack.finishDisabledPackageActivitiesLocked(
1868 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001869 didSomething = true;
1870 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001871 }
1872 }
1873 return didSomething;
1874 }
1875
Dianne Hackborna413dc02013-07-12 12:02:55 -07001876 void updatePreviousProcessLocked(ActivityRecord r) {
1877 // Now that this process has stopped, we may want to consider
1878 // it to be the previous app to try to keep around in case
1879 // the user wants to return to it.
1880
1881 // First, found out what is currently the foreground app, so that
1882 // we don't blow away the previous app if this activity is being
1883 // hosted by the process that is actually still the foreground.
1884 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001885 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1886 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1888 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001889 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001890 if (stack.mResumedActivity != null) {
1891 fgApp = stack.mResumedActivity.app;
1892 } else if (stack.mPausingActivity != null) {
1893 fgApp = stack.mPausingActivity.app;
1894 }
1895 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001896 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001897 }
1898 }
1899
1900 // Now set this one as the previous process, only if that really
1901 // makes sense to.
1902 if (r.app != null && fgApp != null && r.app != fgApp
1903 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001904 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001905 mService.mPreviousProcess = r.app;
1906 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1907 }
1908 }
1909
Wale Ogunwaled046a012015-12-24 13:05:59 -08001910 boolean resumeFocusedStackTopActivityLocked() {
1911 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001912 }
1913
Wale Ogunwaled046a012015-12-24 13:05:59 -08001914 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001915 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001916 if (targetStack != null && isFocusedStack(targetStack)) {
1917 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001918 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001919 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1920 if (r == null || r.state != RESUMED) {
1921 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001922 } else if (r.state == RESUMED) {
1923 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1924 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001925 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001926 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001927 }
1928
Todd Kennedy39bfee52016-02-24 10:28:21 -08001929 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1931 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1932 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1933 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1934 }
1935 }
1936 }
1937
Adrian Roos20d7df32016-01-12 18:59:43 +01001938 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1939 TaskRecord finishedTask = null;
1940 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1942 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 final int numStacks = stacks.size();
1944 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1945 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001946 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1947 if (stack == focusedStack || finishedTask == null) {
1948 finishedTask = t;
1949 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001950 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001952 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001953 }
1954
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001955 void finishVoiceTask(IVoiceInteractionSession session) {
1956 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1957 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1958 final int numStacks = stacks.size();
1959 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1960 final ActivityStack stack = stacks.get(stackNdx);
1961 stack.finishVoiceTask(session);
1962 }
1963 }
1964 }
1965
Andrii Kulianc27916642016-04-12 17:59:27 -07001966 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1967 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001968 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1969 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001970 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001971 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1972 // Caller wants the home activity moved with it. To accomplish this,
1973 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001974 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001975 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001976 ActivityStack currentStack = task.getStack();
1977 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001978 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1979 + task + " to front. Stack is null");
1980 return;
1981 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001982
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001983 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001984 int stackId = options.getLaunchStackId();
1985 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001986 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001987 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001988 if (stackId == INVALID_STACK_ID) {
1989 stackId = task.getLaunchStackId();
1990 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001991 if (stackId != currentStack.mStackId) {
1992 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08001993 !FORCE_FOCUS, reason);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001994 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001995 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1996 // still need moveTaskToFrontLocked() below for any transition settings.
1997 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001998 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1999 resizeStackLocked(stackId, bounds,
2000 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002001 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002002 } else {
2003 // WM resizeTask must be done after the task is moved to the correct stack,
2004 // because Task's setBounds() also updates dim layer's bounds, but that has
2005 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002006 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002007 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002008 }
2009 }
2010
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002011 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002012 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002013 r == null ? null : r.appTimeTracker, reason);
2014
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002015 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002016 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002017
Andrii Kulian02b7a832016-10-06 23:11:56 -07002018 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002019 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002020 }
2021
Wale Ogunwale854809c2015-12-27 16:18:19 -08002022 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002023 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002024 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002025 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002026 return false;
2027 }
2028 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2029 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002030 }
2031
Craig Mautner967212c2013-04-13 21:10:58 -07002032 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002033 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002034 }
2035
2036 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002037 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002038 if (activityContainer != null) {
2039 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002040 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002041 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002042 return null;
2043 }
Jorim Jaggife762342016-10-13 14:33:27 +02002044 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002045 }
2046
Andrii Kulian16802aa2016-11-02 12:21:33 -07002047 /**
2048 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2049 * If there is no such stack, new dynamic stack can be created.
2050 * @param displayId Target display.
2051 * @param r Activity that should be launched there.
2052 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2053 */
2054 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2055 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2056 if (activityDisplay == null) {
2057 throw new IllegalArgumentException(
2058 "Display with displayId=" + displayId + " not found.");
2059 }
2060
2061 // Return the topmost valid stack on the display.
2062 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2063 final ActivityStack stack = activityDisplay.mStacks.get(i);
2064 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2065 return stack;
2066 }
2067 }
2068
2069 // If there is no valid stack on the external display - check if new dynamic stack will do.
2070 if (displayId != Display.DEFAULT_DISPLAY) {
2071 final int newDynamicStackId = getNextStackId();
2072 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2073 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2074 }
2075 }
2076
2077 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2078 return null;
2079 }
2080
Craig Mautner967212c2013-04-13 21:10:58 -07002081 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002082 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002083 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2084 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002085 }
2086 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002087 }
2088
Jorim Jaggife762342016-10-13 14:33:27 +02002089 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2090 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2091 }
2092
Andrii Kulian7fc22812016-12-28 13:04:11 -08002093 /**
2094 * Get next focusable stack in the system. This will search across displays and stacks
2095 * in last-focused order for a focusable and visible stack, different from the target stack.
2096 *
2097 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2098 * searching for next candidate.
2099 * @return Next focusable {@link ActivityStack}, null if not found.
2100 */
2101 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2102 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2103
2104 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2105 final int displayId = mTmpOrderedDisplayIds.get(i);
2106 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2107 if (stacks == null) {
2108 continue;
2109 }
2110 for (int j = stacks.size() - 1; j >= 0; --j) {
2111 final ActivityStack stack = stacks.get(j);
2112 if (stack != currentFocus && stack.isFocusable()
2113 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2114 return stack;
2115 }
2116 }
2117 }
2118
2119 return null;
2120 }
2121
Craig Mautneree2e45a2014-06-27 12:10:03 -07002122 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002123 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002124 }
2125
2126 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002127 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2128 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2129 final TaskRecord task = tasks.get(taskNdx);
2130 if (task.isHomeTask()) {
2131 final ArrayList<ActivityRecord> activities = task.mActivities;
2132 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2133 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002134 if (r.isHomeActivity()
2135 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002136 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002137 }
2138 }
2139 }
2140 }
2141 return null;
2142 }
2143
Chong Zhangb15758a2015-11-17 12:12:03 -08002144 /**
2145 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2146 * the docked stack itself, or if it's side-by-side to the docked stack.
2147 */
2148 boolean isStackDockedInEffect(int stackId) {
2149 return stackId == DOCKED_STACK_ID ||
2150 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2151 }
2152
Todd Kennedyca4d8422015-01-15 15:19:22 -08002153 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002154 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002155 ActivityContainer activityContainer =
2156 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002157 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002158 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2159 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002160 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002161 return activityContainer;
2162 }
2163
Craig Mautner34b73df2014-01-12 21:11:08 -08002164 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002165 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2166 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2167 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002168 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2169 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002170 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002171 }
2172 }
2173
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002174 void deleteActivityContainerRecord(int stackId) {
2175 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2176 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2177 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002178 }
2179
Jorim Jaggidc249c42015-12-15 14:57:31 -08002180 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002181 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002182 if (stackId == DOCKED_STACK_ID) {
2183 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002184 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002185 return;
2186 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002187 final ActivityStack stack = getStack(stackId);
2188 if (stack == null) {
2189 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2190 return;
2191 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002192
Jorim Jaggidc249c42015-12-15 14:57:31 -08002193 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002194 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2195 // stack size changing so things don't get out of sync.
2196 return;
2197 }
2198
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002199 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002200 mWindowManager.deferSurfaceLayout();
2201 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002202 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002203 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002204 stack.ensureVisibleActivitiesConfigurationLocked(
2205 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002206 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002207 } finally {
2208 mWindowManager.continueSurfaceLayout();
2209 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002210 }
2211 }
2212
Jorim Jaggi192086e2016-03-11 17:17:03 +01002213 void deferUpdateBounds(int stackId) {
2214 final ActivityStack stack = getStack(stackId);
2215 if (stack != null) {
2216 stack.deferUpdateBounds();
2217 }
2218 }
2219
2220 void continueUpdateBounds(int stackId) {
2221 final ActivityStack stack = getStack(stackId);
2222 if (stack != null) {
2223 stack.continueUpdateBounds();
2224 }
2225 }
2226
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002227 void notifyAppTransitionDone() {
2228 continueUpdateBounds(HOME_STACK_ID);
2229 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2230 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002231 final TaskRecord task =
2232 anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2233 if (task != null) {
2234 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002235 }
2236 }
2237 mResizingTasksDuringAnimation.clear();
2238 }
2239
Jorim Jaggi192086e2016-03-11 17:17:03 +01002240 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002241 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002242 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002243
Jorim Jaggi192086e2016-03-11 17:17:03 +01002244 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2245 return;
2246 }
2247
Andrii Kulian1e32e022016-09-16 15:29:34 -07002248 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002249 }
2250
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002251 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2252 final ActivityStack stack = getStack(fromStackId);
2253 if (stack == null) {
2254 return;
2255 }
2256
2257 mWindowManager.deferSurfaceLayout();
2258 try {
2259 if (fromStackId == DOCKED_STACK_ID) {
2260
2261 // We are moving all tasks from the docked stack to the fullscreen stack,
2262 // which is dismissing the docked stack, so resize all other stacks to
2263 // fullscreen here already so we don't end up with resize trashing.
2264 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2265 if (StackId.isResizeableByDockedStack(i)) {
2266 ActivityStack otherStack = getStack(i);
2267 if (otherStack != null) {
2268 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2269 true /* allowResizeInDockedMode */, DEFER_RESUME);
2270 }
2271 }
2272 }
2273
2274 // Also disable docked stack resizing since we have manually adjusted the
2275 // size of other stacks above and we don't want to trigger a docked stack
2276 // resize when we remove task from it below and it is detached from the
2277 // display because it no longer contains any tasks.
2278 mAllowDockedStackResize = false;
2279 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002280 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002281 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2282 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002283 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2284 final int size = tasks.size();
2285 if (onTop) {
2286 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002287 final TaskRecord task = tasks.get(i);
2288 if (fromStackId == PINNED_STACK_ID) {
2289 // Update the return-to to reflect where the pinned stack task was moved
2290 // from so that we retain the stack that was previously visible if the
2291 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002292 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2293 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002294 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002295 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002296 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002297 "moveTasksToFullscreenStack - onTop", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002298 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002299
2300 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2301 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002302 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002303 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002304 final TaskRecord task = tasks.get(i);
2305 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, MAX_VALUE,
2306 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002307 }
2308 }
2309 } finally {
2310 mAllowDockedStackResize = true;
2311 mWindowManager.continueSurfaceLayout();
2312 }
2313 }
2314
Jorim Jaggidc249c42015-12-15 14:57:31 -08002315 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002316 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2317 boolean preserveWindows) {
2318 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2319 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2320 false /* deferResume */);
2321 }
2322
2323 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2324 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2325 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002326
2327 if (!mAllowDockedStackResize) {
2328 // Docked stack resize currently disabled.
2329 return;
2330 }
2331
Jorim Jaggidc249c42015-12-15 14:57:31 -08002332 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2333 if (stack == null) {
2334 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2335 return;
2336 }
2337
2338 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2339 mWindowManager.deferSurfaceLayout();
2340 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002341 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2342 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002343 ActivityRecord r = stack.topRunningActivityLocked();
2344 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2345 tempDockedTaskInsetBounds);
2346
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002347 // TODO: Checking for isAttached might not be needed as if the user passes in null
2348 // dockedBounds then they want the docked stack to be dismissed.
2349 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2350 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002351 // In this case we make all other static stacks fullscreen and move all
2352 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002353 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002354
2355 // stack shouldn't contain anymore activities, so nothing to resume.
2356 r = null;
2357 } else {
2358 // Docked stacks occupy a dedicated region on screen so the size of all other
2359 // static stacks need to be adjusted so they don't overlap with the docked stack.
2360 // We get the bounds to use from window manager which has been adjusted for any
2361 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002362 mWindowManager.getStackDockedModeBounds(
2363 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002364 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002365 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2366 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2367 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002368 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002369 }
2370 }
2371 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002372 if (!deferResume) {
2373 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2374 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002375 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002376 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002377 mWindowManager.continueSurfaceLayout();
2378 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2379 }
2380
2381 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2382 tempDockedTaskBounds != null
2383 || tempDockedTaskInsetBounds != null
2384 || tempOtherTaskBounds != null
2385 || tempOtherTaskInsetBounds != null);
2386 }
2387
Robert Carr0d00c2e2016-02-29 17:45:02 -08002388 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2389 final ActivityStack stack = getStack(PINNED_STACK_ID);
2390 if (stack == null) {
2391 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2392 return;
2393 }
2394 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2395 mWindowManager.deferSurfaceLayout();
2396 try {
2397 ActivityRecord r = stack.topRunningActivityLocked();
2398 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2399 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002400 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002401 } finally {
2402 mWindowManager.continueSurfaceLayout();
2403 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2404 }
2405 }
2406
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002407 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002408 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2409 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002410 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002411 }
2412
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002413 ActivityContainer activityContainer = new ActivityContainer(stackId);
2414 mActivityContainers.put(stackId, activityContainer);
Andrii Kulian839def92016-11-02 10:58:58 -07002415 activityContainer.addToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002416 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002417 }
2418
Winson Chung010927a2016-12-15 16:12:35 -08002419 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002420 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002421 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2422 * instead moved back onto the fullscreen stack.
2423 */
2424 void removeStackLocked(int stackId) {
2425 final ActivityStack stack = getStack(stackId);
2426 if (stack == null) {
2427 return;
2428 }
2429
2430 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2431 if (stack.getStackId() == PINNED_STACK_ID) {
2432 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2433 if (fullscreenStack != null) {
2434 final boolean isFullscreenStackVisible =
2435 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2436 for (int i = 0; i < tasks.size(); i++) {
2437 // Insert the task either at the top of the fullscreen stack if it is hidden,
2438 // or just under the top task if it is currently visible
2439 final int insertPosition = isFullscreenStackVisible
2440 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2441 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002442 final TaskRecord task = tasks.get(i);
2443 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002444 }
2445 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2446 resumeFocusedStackTopActivityLocked();
2447 } else {
2448 // If there is no fullscreen stack, then create the stack and move all the tasks
2449 // onto the stack
2450 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, false /* onTop */);
2451 }
2452 } else {
2453 for (int i = tasks.size() - 1; i >= 0; i--) {
2454 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2455 REMOVE_FROM_RECENTS);
2456 }
2457 }
2458 }
2459
2460 /**
2461 * Removes the task with the specified task id.
2462 *
2463 * @param taskId Identifier of the task to be removed.
2464 * @param killProcess Kill any process associated with the task if possible.
2465 * @param removeFromRecents Whether to also remove the task from recents.
2466 * @return Returns true if the given task was found and removed.
2467 */
2468 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2469 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2470 if (tr != null) {
2471 tr.removeTaskActivitiesLocked();
2472 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2473 if (tr.isPersistable) {
2474 mService.notifyTaskPersisterLocked(null, true);
2475 }
2476 return true;
2477 }
2478 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2479 return false;
2480 }
2481
2482 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2483 if (removeFromRecents) {
2484 mRecentTasks.remove(tr);
2485 tr.removedFromRecents();
2486 }
2487 ComponentName component = tr.getBaseIntent().getComponent();
2488 if (component == null) {
2489 Slog.w(TAG, "No component for base intent of task: " + tr);
2490 return;
2491 }
2492
2493 // Find any running services associated with this app and stop if needed.
2494 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2495
2496 if (!killProcess) {
2497 return;
2498 }
2499
2500 // Determine if the process(es) for this task should be killed.
2501 final String pkg = component.getPackageName();
2502 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2503 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2504 for (int i = 0; i < pmap.size(); i++) {
2505
2506 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2507 for (int j = 0; j < uids.size(); j++) {
2508 ProcessRecord proc = uids.valueAt(j);
2509 if (proc.userId != tr.userId) {
2510 // Don't kill process for a different user.
2511 continue;
2512 }
2513 if (proc == mService.mHomeProcess) {
2514 // Don't kill the home process along with tasks from the same package.
2515 continue;
2516 }
2517 if (!proc.pkgList.containsKey(pkg)) {
2518 // Don't kill process that is not associated with this task.
2519 continue;
2520 }
2521
2522 for (int k = 0; k < proc.activities.size(); k++) {
2523 TaskRecord otherTask = proc.activities.get(k).task;
2524 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2525 // Don't kill process(es) that has an activity in a different task that is
2526 // also in recents.
2527 return;
2528 }
2529 }
2530
2531 if (proc.foregroundServices) {
2532 // Don't kill process(es) with foreground service.
2533 return;
2534 }
2535
2536 // Add process to kill list.
2537 procsToKill.add(proc);
2538 }
2539 }
2540
2541 // Kill the running processes.
2542 for (int i = 0; i < procsToKill.size(); i++) {
2543 ProcessRecord pr = procsToKill.get(i);
2544 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2545 && pr.curReceivers.isEmpty()) {
2546 pr.kill("remove task", true);
2547 } else {
2548 // We delay killing processes that are not in the background or running a receiver.
2549 pr.waitingToKill = "remove task";
2550 }
2551 }
2552 }
2553
Craig Mautner4a1cb222013-12-04 16:14:06 -08002554 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002555 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002556 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2557 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002558 break;
2559 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002560 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002561 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002562 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002563 }
2564
Chong Zhang5dcb2752015-08-18 13:50:26 -07002565 /**
2566 * Restores a recent task to a stack
2567 * @param task The recent task to be restored.
2568 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002569 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2570 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002571 * @return true if the task has been restored successfully.
2572 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002573 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002574 if (!StackId.isStaticStack(stackId)) {
2575 // If stack is not static (or stack id is invalid) - use the default one.
2576 // This means that tasks that were on external displays will be restored on the
2577 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002578 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002579 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2580 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2581 // Put it in the fullscreen stack.
2582 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002583 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002584
Andrii Kulian02b7a832016-10-06 23:11:56 -07002585 final ActivityStack currentStack = task.getStack();
2586 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002587 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002588 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002589 // Nothing else to do since it is already restored in the right stack.
2590 return true;
2591 }
2592 // Remove current stack association, so we can re-associate the task with the
2593 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002594 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002595 }
2596
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002597 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002598 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002599
2600 if (stack == null) {
2601 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002602 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2603 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002604 return false;
2605 }
2606
Wale Ogunwale72919d22016-12-08 18:58:50 -08002607 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002608 // TODO: move call for creation here and other place into Stack.addTask()
2609 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002610 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2611 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002612 final ArrayList<ActivityRecord> activities = task.mActivities;
2613 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002614 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002615 }
2616 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002617 }
2618
Wale Ogunwale040b4702015-08-06 18:10:50 -07002619 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002620 * Move stack with all its existing content to specified display.
2621 * @param stackId Id of stack to move.
2622 * @param displayId Id of display to move stack to.
2623 */
2624 void moveStackToDisplayLocked(int stackId, int displayId) {
2625 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2626 if (activityDisplay == null) {
2627 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2628 + displayId);
2629 }
2630 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2631 if (activityContainer != null) {
2632 if (activityContainer.isAttachedLocked()) {
2633 if (activityContainer.getDisplayId() == displayId) {
2634 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2635 + " to its current displayId=" + displayId);
2636 }
2637
2638 activityContainer.moveToDisplayLocked(activityDisplay);
2639 } else {
2640 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2641 + stackId + " is not attached to any display.");
2642 }
2643 } else {
2644 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2645 + stackId);
2646 }
2647
2648 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2649 !PRESERVE_WINDOWS);
2650 // TODO(multi-display): resize stacks properly if moved from split-screen.
2651 }
2652
2653 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002654 * Moves the specified task record to the input stack id.
2655 * WARNING: This method performs an unchecked/raw move of the task and
2656 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002657 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002658 * @param task Task to move.
2659 * @param stackId Id of stack to move task to.
2660 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002661 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002662 * @param reason Reason the task is been moved.
2663 * @return The stack the task was moved to.
2664 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002665 ActivityStack moveTaskToStackUncheckedLocked(TaskRecord task, int stackId, boolean toTop,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002666 boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002667
2668 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2669 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2670 + "support multi-window task=" + task + " to stackId=" + stackId);
2671 }
2672
Wale Ogunwale06579d62016-04-30 15:29:06 -07002673 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002674 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002675 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002676 final boolean wasResumed = prevStack.mResumedActivity == r;
Winson Chungc2baac02017-01-11 13:34:47 -08002677 final boolean wasPaused = prevStack.mPausingActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002678 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2679 // Whenever we are moving the top activity from the front stack we want to make sure to move
2680 // the stack to the front.
2681 final boolean wasFront = isFrontStack(prevStack)
2682 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002683
Chong Zhangd545dce2016-02-29 18:09:17 -08002684 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002685 // We don't allow moving a unresizeable task to the docked stack since the docked
2686 // stack is used for split-screen mode and will cause things like the docked divider to
2687 // show up. We instead leave the task in its current stack or move it to the fullscreen
2688 // stack if it isn't currently in a stack.
2689 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002690 Slog.w(TAG, "Can not move unresizeable task=" + task
2691 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2692 }
2693
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002694 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2695 // if a docked stack is created below which will lead to the stack we are moving from and
2696 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002697 task.mTemporarilyUnresizable = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002698 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002699 task.mTemporarilyUnresizable = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002700 task.reparent(stack.mStackId, toTop ? MAX_VALUE : 0, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002701
Winson Chungc2baac02017-01-11 13:34:47 -08002702 // Reset the resumed activity on the previous stack
2703 if (wasResumed) {
2704 prevStack.mResumedActivity = null;
2705 }
2706 // Reset the paused activity on the previous stack
2707 if (wasPaused) {
2708 prevStack.mPausingActivity = null;
2709 }
2710
Chong Zhang02898352015-08-21 17:27:14 -07002711 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002712 // move focus to the new stack by moving the stack to the front.
Winson Chungc2baac02017-01-11 13:34:47 -08002713 stack.moveToFrontAndResumeStateIfNeeded(r, forceFocus || wasFocused || wasFront, wasResumed,
2714 wasPaused, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002715
Wale Ogunwale040b4702015-08-06 18:10:50 -07002716 return stack;
2717 }
2718
Chong Zhange4fbd322016-03-01 14:44:03 -08002719 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002720 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002721 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002722 false /* deferResume */);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002723 }
2724
2725 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002726 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002727 final TaskRecord task = anyTaskForIdLocked(taskId);
2728 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002729 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002730 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002731 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002732
Andrii Kulian02b7a832016-10-06 23:11:56 -07002733 final ActivityStack currentStack = task.getStack();
2734 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002735 // You are already in the right stack silly...
2736 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002737 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002738 }
2739
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002740 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2741 throw new IllegalArgumentException("moveTaskToStack:"
2742 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2743 }
2744
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002745 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002746 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002747 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002748 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002749 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002750 // We are about to relaunch the activity because its configuration changed due to
2751 // being maximized, i.e. size change. The activity will first remove the old window
2752 // and then add a new one. This call will tell window manager about this, so it can
2753 // preserve the old window until the new one is drawn. This prevents having a gap
2754 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002755 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002756 // Note here we always set the replacing window first, as the flags might be needed
2757 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002758 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002759 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002760
2761 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002762 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002763 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002764 try {
2765 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002766 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002767 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002768
Wale Ogunwale961f4852016-02-01 20:25:54 -08002769 if (!animate) {
2770 stack.mNoAnimActivities.add(topActivity);
2771 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002772
Wale Ogunwale961f4852016-02-01 20:25:54 -08002773 // We might trigger a configuration change. Save the current task bounds for freezing.
2774 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2775
2776 // Make sure the task has the appropriate bounds/size for the stack it is in.
2777 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002778 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2779 deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002780 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2781 Rect bounds = task.getLaunchBounds();
2782 if (bounds == null) {
2783 stack.layoutTaskInStack(task, null);
2784 bounds = task.mBounds;
2785 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002786 kept = task.resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002787 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002788 kept = task.resize(stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
2789 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002790 }
2791 } finally {
2792 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002793 }
2794
2795 if (mightReplaceWindow) {
2796 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2797 // window), we need to clear the replace window settings. Otherwise, we schedule a
2798 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002799 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002800 }
2801
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002802 if (!deferResume) {
2803
2804 // The task might have already been running and its visibility needs to be synchronized with
2805 // the visibility of the stack / windows.
2806 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2807 resumeFocusedStackTopActivityLocked();
2808 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002809
Jorim Jaggid53f0922016-04-06 22:16:23 -07002810 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002811
2812 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002813 }
2814
Wale Ogunwale079a0042015-10-24 11:44:07 -07002815 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2816 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2817 if (stack == null) {
2818 throw new IllegalArgumentException(
2819 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2820 }
2821
2822 final ActivityRecord r = stack.topRunningActivityLocked();
2823 if (r == null) {
2824 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2825 + " in stack=" + stack);
2826 return false;
2827 }
2828
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002829 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002830 Slog.w(TAG,
2831 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002832 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002833 return false;
2834 }
2835
Winson Chungb5c41b72016-12-07 15:00:47 -08002836 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2837 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002838 return true;
2839 }
2840
Winson Chungb5c41b72016-12-07 15:00:47 -08002841 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2842 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002843 mWindowManager.deferSurfaceLayout();
2844 try {
2845 final TaskRecord task = r.task;
2846
Andrii Kulian02b7a832016-10-06 23:11:56 -07002847 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002848 // An activity can't be pinned and visible behind at the same time. Go ahead and
2849 // release it from been visible behind before pinning.
2850 requestVisibleBehindLocked(r, false);
2851 }
2852
Wale Ogunwale480dca02016-02-06 13:58:29 -08002853 // Need to make sure the pinned stack exist so we can resize it below...
2854 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2855
2856 // Resize the pinned stack to match the current size of the task the activity we are
2857 // going to be moving is currently contained in. We do this to have the right starting
2858 // animation bounds for the pinned stack to the desired bounds the caller wants.
2859 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2860 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002861 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002862
2863 if (task.mActivities.size() == 1) {
2864 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002865 // the stack without re-parenting the activity in a different task. We don't
2866 // move the home stack forward if we are currently entering picture-in-picture
2867 // while pausing because that changes the focused stack and may prevent the new
2868 // starting activity from resuming.
2869 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
2870 && !r.supportsPictureInPictureWhilePausing) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002871 // Move the home stack forward if the task we just moved to the pinned stack
2872 // was launched from home so home should be visible behind it.
2873 moveHomeStackToFront(reason);
2874 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002875 moveTaskToStackLocked(
2876 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2877 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002878 // There are multiple activities in the task and moving the top activity should
2879 // reveal/leave the other activities in their original task
Wale Ogunwale480dca02016-02-06 13:58:29 -08002880 stack.moveActivityToStack(r);
2881 }
Winson Chungc2baac02017-01-11 13:34:47 -08002882
2883 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2884 // to the pinned stack
2885 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002886 } finally {
2887 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002888 }
2889
Wale Ogunwale480dca02016-02-06 13:58:29 -08002890 // The task might have already been running and its visibility needs to be synchronized
2891 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002892 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002893 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002894
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002895 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002896 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002897 }
2898
Chong Zhang6cda19c2016-06-14 19:07:56 -07002899 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2900 if (r == null || !r.isFocusable()) {
2901 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2902 "moveActivityStackToFront: unfocusable r=" + r);
2903 return false;
2904 }
2905
2906 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002907 final ActivityStack stack = r.getStack();
2908 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002909 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2910 + r + " task=" + task);
2911 return false;
2912 }
2913
Chong Zhang6cda19c2016-06-14 19:07:56 -07002914 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2915 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2916 "moveActivityStackToFront: already on top, r=" + r);
2917 return false;
2918 }
2919
2920 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2921 "moveActivityStackToFront: r=" + r);
2922
2923 stack.moveToFront(reason, task);
2924 return true;
2925 }
2926
Craig Mautnerac6f8432013-07-17 13:24:59 -07002927 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002928 mTmpFindTaskResult.r = null;
2929 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002930 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002931 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2932 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2934 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002935 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002936 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002937 continue;
2938 }
2939 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002940 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2941 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 continue;
2943 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002944 stack.findTaskLocked(r, mTmpFindTaskResult);
2945 // It is possible to have task in multiple stacks with the same root affinity.
2946 // If the match we found was based on root affinity we keep on looking to see if
2947 // there is a better match in another stack. We eventually return the match based
2948 // on root affinity if we don't find a better match.
2949 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2950 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002952 }
2953 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002954 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2955 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002956 }
2957
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002958 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2959 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2961 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002962 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002963 final ActivityRecord ar = stacks.get(stackNdx)
2964 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002965 if (ar != null) {
2966 return ar;
2967 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002968 }
2969 }
2970 return null;
2971 }
2972
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002974 scheduleSleepTimeout();
2975 if (!mGoingToSleep.isHeld()) {
2976 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002977 if (mLaunchingActivity.isHeld()) {
2978 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2979 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002980 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002981 mLaunchingActivity.release();
2982 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002983 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002984 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002985 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002986 }
2987
2988 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002989 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002990
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002991 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002992 final long endTime = System.currentTimeMillis() + timeout;
2993 while (true) {
2994 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002995 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2996 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2998 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2999 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003000 }
3001 if (cantShutdown) {
3002 long timeRemaining = endTime - System.currentTimeMillis();
3003 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003004 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003006 } catch (InterruptedException e) {
3007 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003008 } else {
3009 Slog.w(TAG, "Activity manager shutdown timed out");
3010 timedout = true;
3011 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003012 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013 } else {
3014 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003015 }
3016 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017
3018 // Force checkReadyForSleep to complete.
3019 mSleepTimeout = true;
3020 checkReadyForSleepLocked();
3021
Craig Mautner8d341ef2013-03-26 09:03:27 -07003022 return timedout;
3023 }
3024
3025 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003026 removeSleepTimeouts();
3027 if (mGoingToSleep.isHeld()) {
3028 mGoingToSleep.release();
3029 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3031 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003032 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3033 final ActivityStack stack = stacks.get(stackNdx);
3034 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003035 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003036 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003037 }
Craig Mautner5314a402013-09-26 12:40:16 -07003038 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003039 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003040 mGoingToSleepActivities.clear();
3041 }
3042
3043 void activitySleptLocked(ActivityRecord r) {
3044 mGoingToSleepActivities.remove(r);
3045 checkReadyForSleepLocked();
3046 }
3047
3048 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003049 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003050 // Do not care.
3051 return;
3052 }
3053
3054 if (!mSleepTimeout) {
3055 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003056 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3057 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003058 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3059 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3060 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003061 }
3062
3063 if (mStoppingActivities.size() > 0) {
3064 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003065 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003066 + mStoppingActivities.size() + " activities");
3067 scheduleIdleLocked();
3068 dontSleep = true;
3069 }
3070
3071 if (mGoingToSleepActivities.size() > 0) {
3072 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003073 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003074 + mGoingToSleepActivities.size() + " activities");
3075 dontSleep = true;
3076 }
3077
3078 if (dontSleep) {
3079 return;
3080 }
3081 }
3082
Wei Wang65c7a152016-06-02 18:51:22 -07003083 // Send launch end powerhint before going sleep
3084 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3085
Craig Mautnere0a38842013-12-16 16:14:02 -08003086 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3087 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3089 stacks.get(stackNdx).goToSleep();
3090 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003091 }
3092
3093 removeSleepTimeouts();
3094
3095 if (mGoingToSleep.isHeld()) {
3096 mGoingToSleep.release();
3097 }
3098 if (mService.mShuttingDown) {
3099 mService.notifyAll();
3100 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003101 }
3102
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003103 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003104 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003105 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003106 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003107 }
3108 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003109 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003110 mWindowManager.executeAppTransition();
3111 return true;
3112 }
3113 return false;
3114 }
3115
Craig Mautner8d341ef2013-03-26 09:03:27 -07003116 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003117 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3118 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003119 int stackNdx = stacks.size() - 1;
3120 while (stackNdx >= 0) {
3121 stacks.get(stackNdx).handleAppCrashLocked(app);
3122 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003124 }
3125 }
3126
Jose Lima4b6c6692014-08-12 17:41:12 -07003127 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003128 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003129 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003130 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3131 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003132 return false;
3133 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003134
3135 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3136 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3137 + " visible=" + visible + " stackId=" + stack.mStackId
3138 + " can't contain visible behind activities");
3139 return false;
3140 }
3141
Jose Lima4b6c6692014-08-12 17:41:12 -07003142 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003143 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3144 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003145
3146 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003147 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003148 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003149 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003150 return true;
3151 }
3152
3153 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003154 if (visible && top.fullscreen) {
3155 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003156 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3157 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3158 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3159 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003160 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003161 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3162 // Only the activity set as currently visible behind should actively reset its
3163 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003164 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3165 "requestVisibleBehind: returning visible=" + visible
3166 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3167 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003168 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003169 }
3170
Jose Lima4b6c6692014-08-12 17:41:12 -07003171 stack.setVisibleBehindActivity(visible ? r : null);
3172 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003173 // If there is a translucent home activity, we need to force it stop being translucent,
3174 // because we can't depend on the application to necessarily perform that operation.
3175 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003176 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003177 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003178 mService.convertFromTranslucent(next.appToken);
3179 }
3180 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003181 if (top.app != null && top.app.thread != null) {
3182 // Notify the top app of the change.
3183 try {
3184 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3185 } catch (RemoteException e) {
3186 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003187 }
3188 return true;
3189 }
3190
Craig Mautnerbb742462014-07-07 15:28:55 -07003191 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003192 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003193 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003194 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003195
3196 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003197 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003198 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003199 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003200 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003201
3202 // When launching tasks behind, update the last active time of the top task after the new
3203 // task has been shown briefly
3204 final ActivityRecord top = stack.topActivity();
3205 if (top != null) {
3206 top.task.touchActiveTime();
3207 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003208 }
3209
3210 void scheduleLaunchTaskBehindComplete(IBinder token) {
3211 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3212 }
3213
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003214 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3215 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003216 mKeyguardController.beginActivityVisibilityUpdate();
3217 try {
3218 // First the front stacks. In case any are not fullscreen and are in front of home.
3219 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3220 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3221 final int topStackNdx = stacks.size() - 1;
3222 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3223 final ActivityStack stack = stacks.get(stackNdx);
3224 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3225 }
Craig Mautner580ea812013-04-25 12:58:38 -07003226 }
Jorim Jaggife762342016-10-13 14:33:27 +02003227 } finally {
3228 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003229 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003230 }
3231
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003232 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3233 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3234 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3235 final int topStackNdx = stacks.size() - 1;
3236 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3237 final ActivityStack stack = stacks.get(stackNdx);
3238 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3239 }
3240 }
3241 }
3242
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003243 void invalidateTaskLayers() {
3244 mTaskLayersChanged = true;
3245 }
3246
3247 void rankTaskLayersIfNeeded() {
3248 if (!mTaskLayersChanged) {
3249 return;
3250 }
3251 mTaskLayersChanged = false;
3252 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3253 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3254 int baseLayer = 0;
3255 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3256 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3257 }
3258 }
3259 }
3260
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003261 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3262 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3263 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3264 final int topStackNdx = stacks.size() - 1;
3265 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3266 final ActivityStack stack = stacks.get(stackNdx);
3267 stack.clearOtherAppTimeTrackers(except);
3268 }
3269 }
3270 }
3271
Craig Mautner8d341ef2013-03-26 09:03:27 -07003272 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003273 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3274 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003275 final int numStacks = stacks.size();
3276 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3277 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003278 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003279 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003280 }
3281 }
3282
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003283 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3284 // Examine all activities currently running in the process.
3285 TaskRecord firstTask = null;
3286 // Tasks is non-null only if two or more tasks are found.
3287 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003288 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3289 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003290 ActivityRecord r = app.activities.get(i);
3291 // First, if we find an activity that is in the process of being destroyed,
3292 // then we just aren't going to do anything for now; we want things to settle
3293 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003294 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003295 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003296 return;
3297 }
3298 // Don't consider any activies that are currently not in a state where they
3299 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003300 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3301 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003302 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003303 continue;
3304 }
3305 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003306 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003307 + " from " + r);
3308 if (firstTask == null) {
3309 firstTask = r.task;
3310 } else if (firstTask != r.task) {
3311 if (tasks == null) {
3312 tasks = new ArraySet<>();
3313 tasks.add(firstTask);
3314 }
3315 tasks.add(r.task);
3316 }
3317 }
3318 }
3319 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003320 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003321 return;
3322 }
3323 // If we have activities in multiple tasks that are in a position to be destroyed,
3324 // let's iterate through the tasks and release the oldest one.
3325 final int numDisplays = mActivityDisplays.size();
3326 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3327 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3328 // Step through all stacks starting from behind, to hit the oldest things first.
3329 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3330 final ActivityStack stack = stacks.get(stackNdx);
3331 // Try to release activities in this stack; if we manage to, we are done.
3332 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3333 return;
3334 }
3335 }
3336 }
3337 }
3338
Amith Yamasani37a40c22015-06-17 13:25:42 -07003339 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003340 final int focusStackId = mFocusedStack.getStackId();
3341 // We dismiss the docked stack whenever we switch users.
3342 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003343 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3344 // also cause all tasks to be moved to the fullscreen stack at a position that is
3345 // appropriate.
3346 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003347
3348 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003349 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003350 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003351
Craig Mautner858d8a62013-04-23 17:08:34 -07003352 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003353 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3354 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003355 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003356 final ActivityStack stack = stacks.get(stackNdx);
3357 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003358 TaskRecord task = stack.topTask();
3359 if (task != null) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003360 task.moveWindowContainerToTop(true /* includingParents */);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003361 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003362 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003363 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003364
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003365 ActivityStack stack = getStack(restoreStackId);
3366 if (stack == null) {
3367 stack = mHomeStack;
3368 }
3369 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003370 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003371 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003372 } else {
3373 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003374 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003375 }
Craig Mautner93529a42013-10-04 15:03:13 -07003376 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003377 }
3378
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003379 /** Checks whether the userid is a profile of the current user. */
3380 boolean isCurrentProfileLocked(int userId) {
3381 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003382 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003383 }
3384
Craig Mautnerde4ef022013-04-07 19:01:33 -07003385 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003386 ArrayList<ActivityRecord> stops = null;
3387
3388 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003389 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3390 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003391 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3392 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003393 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003394 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003395 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3396 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003397 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003398 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003399 if (s.finishing) {
3400 // If this activity is finishing, it is sitting on top of
3401 // everyone else but we now know it is no longer needed...
3402 // so get rid of it. Otherwise, we need to go through the
3403 // normal flow and hide it once we determine that it is
3404 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003405 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003406 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003407 }
3408 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003409 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003410 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003411 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003412 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003413 }
3414 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003415 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003416 }
3417 }
3418
3419 return stops;
3420 }
3421
Craig Mautnercf910b02013-04-23 11:23:27 -07003422 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003423 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3424 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3425 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3426 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003427 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003428 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003429 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003430 if (r == null) Slog.e(TAG,
3431 "validateTop...: null top activity, stack=" + stack);
3432 else {
3433 final ActivityRecord pausing = stack.mPausingActivity;
3434 if (pausing != null && pausing == r) Slog.e(TAG,
3435 "validateTop...: top stack has pausing activity r=" + r
3436 + " state=" + state);
3437 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3438 "validateTop...: activity in front not resumed r=" + r
3439 + " state=" + state);
3440 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003441 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003442 final ActivityRecord resumed = stack.mResumedActivity;
3443 if (resumed != null && resumed == r) Slog.e(TAG,
3444 "validateTop...: back stack has resumed activity r=" + r
3445 + " state=" + state);
3446 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3447 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003448 }
3449 }
3450 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003451 }
3452
Craig Mautnere0570202015-05-13 13:06:11 -07003453 private String lockTaskModeToString() {
3454 switch (mLockTaskModeState) {
3455 case LOCK_TASK_MODE_LOCKED:
3456 return "LOCKED";
3457 case LOCK_TASK_MODE_PINNED:
3458 return "PINNED";
3459 case LOCK_TASK_MODE_NONE:
3460 return "NONE";
3461 default: return "unknown=" + mLockTaskModeState;
3462 }
3463 }
3464
Craig Mautner27084302013-03-25 08:05:25 -07003465 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003466 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003468 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003469 pw.print(prefix);
3470 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003471 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003472 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003473 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003474 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3475 if (packages.size() > 0) {
3476 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3477 for (int i = 0; i < packages.size(); ++i) {
3478 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3479 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3480 }
3481 }
3482 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3483 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003484 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003485
Winson43d1f262016-06-14 16:05:55 -07003486 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003487 * Dump all connected displays' configurations.
3488 * @param prefix Prefix to apply to each line of the dump.
3489 */
3490 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3491 pw.print(prefix); pw.println("Display override configurations:");
3492 final int displayCount = mActivityDisplays.size();
3493 for (int i = 0; i < displayCount; i++) {
3494 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3495 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3496 pw.println(activityDisplay.getOverrideConfiguration());
3497 }
3498 }
3499
3500 /**
Winson43d1f262016-06-14 16:05:55 -07003501 * Dumps the activities matching the given {@param name} in the either the focused stack
3502 * or all visible stacks if {@param dumpVisibleStacks} is true.
3503 */
3504 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3505 if (dumpVisibleStacks) {
3506 ArrayList<ActivityRecord> activities = new ArrayList<>();
3507 int numDisplays = mActivityDisplays.size();
3508 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3509 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3510 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3511 ActivityStack stack = stacks.get(stackNdx);
3512 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3513 activities.addAll(stack.getDumpActivitiesLocked(name));
3514 }
3515 }
3516 }
3517 return activities;
3518 } else {
3519 return mFocusedStack.getDumpActivitiesLocked(name);
3520 }
Craig Mautner20e72272013-04-01 13:45:53 -07003521 }
3522
Dianne Hackborn390517b2013-05-30 15:03:32 -07003523 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3524 boolean needSep, String prefix) {
3525 if (activity != null) {
3526 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3527 if (needSep) {
3528 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003529 }
3530 pw.print(prefix);
3531 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003532 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003533 }
3534 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003535 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003536 }
3537
Craig Mautner8d341ef2013-03-26 09:03:27 -07003538 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3539 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003540 boolean printed = false;
3541 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003542 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3543 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003544 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003545 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 final ActivityStack stack = stacks.get(stackNdx);
3549 StringBuilder stackHeader = new StringBuilder(128);
3550 stackHeader.append(" Stack #");
3551 stackHeader.append(stack.mStackId);
3552 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003553 stackHeader.append("\n");
3554 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3555 stackHeader.append("\n");
3556 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3558 needSep, stackHeader.toString());
3559 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3560 !dumpAll, false, dumpPackage, true,
3561 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003562
Craig Mautner4a1cb222013-12-04 16:14:06 -08003563 needSep = printed;
3564 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3565 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003566 if (pr) {
3567 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003569 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3571 " mResumedActivity: ");
3572 if (pr) {
3573 printed = true;
3574 needSep = false;
3575 }
3576 if (dumpAll) {
3577 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3578 " mLastPausedActivity: ");
3579 if (pr) {
3580 printed = true;
3581 needSep = true;
3582 }
3583 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3584 needSep, " mLastNoHistoryActivity: ");
3585 }
3586 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003587 }
3588 }
3589
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003590 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3591 false, dumpPackage, true, " Activities waiting to finish:", null);
3592 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3593 false, dumpPackage, true, " Activities waiting to stop:", null);
3594 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3595 false, dumpPackage, true, " Activities waiting for another to become visible:",
3596 null);
3597 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3598 false, dumpPackage, true, " Activities waiting to sleep:", null);
3599 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3600 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003601
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003602 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003603 }
3604
Dianne Hackborn390517b2013-05-30 15:03:32 -07003605 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003606 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003607 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003608 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003609 String innerPrefix = null;
3610 String[] args = null;
3611 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003612 for (int i=list.size()-1; i>=0; i--) {
3613 final ActivityRecord r = list.get(i);
3614 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3615 continue;
3616 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003617 if (innerPrefix == null) {
3618 innerPrefix = prefix + " ";
3619 args = new String[0];
3620 }
3621 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003622 final boolean full = !brief && (complete || !r.isInHistory());
3623 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003624 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003625 needNL = false;
3626 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003627 if (header1 != null) {
3628 pw.println(header1);
3629 header1 = null;
3630 }
3631 if (header2 != null) {
3632 pw.println(header2);
3633 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003634 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003635 if (lastTask != r.task) {
3636 lastTask = r.task;
3637 pw.print(prefix);
3638 pw.print(full ? "* " : " ");
3639 pw.println(lastTask);
3640 if (full) {
3641 lastTask.dump(pw, prefix + " ");
3642 } else if (complete) {
3643 // Complete + brief == give a summary. Isn't that obvious?!?
3644 if (lastTask.intent != null) {
3645 pw.print(prefix); pw.print(" ");
3646 pw.println(lastTask.intent.toInsecureStringWithClip());
3647 }
3648 }
3649 }
3650 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3651 pw.print(" #"); pw.print(i); pw.print(": ");
3652 pw.println(r);
3653 if (full) {
3654 r.dump(pw, innerPrefix);
3655 } else if (complete) {
3656 // Complete + brief == give a summary. Isn't that obvious?!?
3657 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3658 if (r.app != null) {
3659 pw.print(innerPrefix); pw.println(r.app);
3660 }
3661 }
3662 if (client && r.app != null && r.app.thread != null) {
3663 // flush anything that is already in the PrintWriter since the thread is going
3664 // to write to the file descriptor directly
3665 pw.flush();
3666 try {
3667 TransferPipe tp = new TransferPipe();
3668 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003669 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003670 // Short timeout, since blocking here can
3671 // deadlock with the application.
3672 tp.go(fd, 2000);
3673 } finally {
3674 tp.kill();
3675 }
3676 } catch (IOException e) {
3677 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3678 } catch (RemoteException e) {
3679 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3680 }
3681 needNL = true;
3682 }
3683 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003684 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003685 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003686
Craig Mautnerf3333272013-04-22 10:55:53 -07003687 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003688 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3689 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003690 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3691 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003692 }
3693
3694 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003695 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003696 }
3697
3698 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003699 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3700 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003701 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3702 }
3703
Craig Mautner05d29032013-05-03 13:40:13 -07003704 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003705 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3706 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3707 }
Craig Mautner05d29032013-05-03 13:40:13 -07003708 }
3709
Craig Mautner0eea92c2013-05-16 13:35:39 -07003710 void removeSleepTimeouts() {
3711 mSleepTimeout = false;
3712 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3713 }
3714
3715 final void scheduleSleepTimeout() {
3716 removeSleepTimeouts();
3717 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3718 }
3719
Craig Mautner4a1cb222013-12-04 16:14:06 -08003720 @Override
3721 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003722 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3724 }
3725
3726 @Override
3727 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003728 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003729 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3730 }
3731
3732 @Override
3733 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003734 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3736 }
3737
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003738 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003739 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003741 newDisplay = mActivityDisplays.get(displayId) == null;
3742 if (newDisplay) {
3743 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003744 if (activityDisplay.mDisplay == null) {
3745 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3746 return;
3747 }
Craig Mautner4504de52013-12-20 09:06:56 -08003748 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003749 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003750 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 }
Craig Mautner4504de52013-12-20 09:06:56 -08003752 if (newDisplay) {
3753 mWindowManager.onDisplayAdded(displayId);
3754 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 }
3756
Andrii Kulianca007a62016-11-22 16:33:28 -08003757 /** Check if display with specified id is added to the list. */
3758 boolean isDisplayAdded(int displayId) {
3759 return mActivityDisplays.get(displayId) != null;
3760 }
3761
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003762 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003763 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003764 mService.mContext.getResources().getDimensionPixelSize(
3765 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003766 }
3767
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003768 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003770 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3771 if (activityDisplay != null) {
3772 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian03c403d2016-11-11 11:14:12 -08003774 final ActivityStack stack = stacks.get(stackNdx);
3775 // TODO: Implement proper stack removal and ability to choose the behavior -
3776 // remove stack completely or move it to other display.
3777 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003779 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 }
3781 }
3782 mWindowManager.onDisplayRemoved(displayId);
3783 }
3784
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003785 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003787 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3788 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003789 // TODO: Update the bounds.
3790 }
3791 }
3792 mWindowManager.onDisplayChanged(displayId);
3793 }
3794
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003795 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003796 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 StackInfo info = new StackInfo();
3798 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003799 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003801 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003802 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003803 info.position = display != null
3804 ? display.mStacks.indexOf(stack)
3805 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806
3807 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3808 final int numTasks = tasks.size();
3809 int[] taskIds = new int[numTasks];
3810 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003811 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003812 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 for (int i = 0; i < numTasks; ++i) {
3814 final TaskRecord task = tasks.get(i);
3815 taskIds[i] = task.taskId;
3816 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3817 : task.realActivity != null ? task.realActivity.flattenToString()
3818 : task.getTopActivity() != null ? task.getTopActivity().packageName
3819 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003820 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003821 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003822 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 }
3824 info.taskIds = taskIds;
3825 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003826 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003827 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003828
3829 final ActivityRecord top = stack.topRunningActivityLocked();
3830 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 return info;
3832 }
3833
3834 StackInfo getStackInfoLocked(int stackId) {
3835 ActivityStack stack = getStack(stackId);
3836 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003837 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 }
3839 return null;
3840 }
3841
3842 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003843 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003844 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3845 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003847 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 }
3849 }
3850 return list;
3851 }
3852
Craig Mautner15df08a2015-04-01 12:17:18 -07003853 TaskRecord getLockedTaskLocked() {
3854 final int top = mLockTaskModeTasks.size() - 1;
3855 if (top >= 0) {
3856 return mLockTaskModeTasks.get(top);
3857 }
3858 return null;
3859 }
3860
3861 boolean isLockedTask(TaskRecord task) {
3862 return mLockTaskModeTasks.contains(task);
3863 }
3864
3865 boolean isLastLockedTask(TaskRecord task) {
3866 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3867 }
3868
3869 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003870 if (!mLockTaskModeTasks.remove(task)) {
3871 return;
3872 }
3873 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3874 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003875 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003876 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3877 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003878 final Message lockTaskMsg = Message.obtain();
3879 lockTaskMsg.arg1 = task.userId;
3880 lockTaskMsg.what = LOCK_TASK_END_MSG;
3881 mHandler.sendMessage(lockTaskMsg);
3882 }
3883 }
3884
Andrii Kulianc27916642016-04-12 17:59:27 -07003885 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3886 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3887 false /* forceNonResizable */);
3888 }
3889
Jorim Jaggid53f0922016-04-06 22:16:23 -07003890 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003891 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003892 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3893 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003894 return;
3895 }
3896
Jorim Jaggicd13d332016-04-27 15:40:20 -07003897 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003898 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003899 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003900 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003901
Andrii Kulianc27916642016-04-12 17:59:27 -07003902 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3903 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003904 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003905 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3906 && !topActivity.noDisplay) {
3907 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003908 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3909 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003910 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003911 }
3912
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003913 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003914 if (mLockTaskNotify != null) {
3915 mLockTaskNotify.showToast(mLockTaskModeState);
3916 }
Jason Monka8f569c2014-07-07 12:15:21 -04003917 }
3918
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003919 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3920 if (mLockTaskModeTasks.contains(task)) {
3921 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3922 }
3923 }
3924
Craig Mautner432f64e2015-05-20 14:59:57 -07003925 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3926 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003927 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003928 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003929 final TaskRecord lockedTask = getLockedTaskLocked();
3930 if (lockedTask != null) {
3931 removeLockedTaskLocked(lockedTask);
3932 if (!mLockTaskModeTasks.isEmpty()) {
3933 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003934 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3935 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003936 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003937 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003938 return;
3939 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003940 }
Craig Mautnere0570202015-05-13 13:06:11 -07003941 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3942 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003943 return;
3944 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003945
3946 // Should have already been checked, but do it again.
3947 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003948 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3949 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003950 return;
3951 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003952 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003953 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003954 return;
3955 }
3956
3957 if (mLockTaskModeTasks.isEmpty()) {
3958 // First locktask.
3959 final Message lockTaskMsg = Message.obtain();
3960 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3961 lockTaskMsg.arg1 = task.userId;
3962 lockTaskMsg.what = LOCK_TASK_START_MSG;
3963 lockTaskMsg.arg2 = lockTaskModeState;
3964 mHandler.sendMessage(lockTaskMsg);
3965 }
3966 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003967 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3968 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003969 mLockTaskModeTasks.remove(task);
3970 mLockTaskModeTasks.add(task);
3971
3972 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003973 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003974 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003975
3976 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003977 findTaskToMoveToFrontLocked(task, 0, null, reason,
3978 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003979 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003980 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003981 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003982 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003983 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003984 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003985 }
3986
3987 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003988 return isLockTaskModeViolation(task, false);
3989 }
3990
3991 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3992 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003993 return false;
3994 }
3995 final int lockTaskAuth = task.mLockTaskAuth;
3996 switch (lockTaskAuth) {
3997 case LOCK_TASK_AUTH_DONT_LOCK:
3998 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003999 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004000 case LOCK_TASK_AUTH_LAUNCHABLE:
4001 case LOCK_TASK_AUTH_WHITELISTED:
4002 return false;
4003 case LOCK_TASK_AUTH_PINNABLE:
4004 // Pinnable tasks can't be launched on top of locktask tasks.
4005 return !mLockTaskModeTasks.isEmpty();
4006 default:
4007 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4008 return true;
4009 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004010 }
4011
Craig Mautner15df08a2015-04-01 12:17:18 -07004012 void onLockTaskPackagesUpdatedLocked() {
4013 boolean didSomething = false;
4014 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4015 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004016 final boolean wasWhitelisted =
4017 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4018 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004019 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004020 final boolean isWhitelisted =
4021 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4022 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4023 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004024 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004025 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4026 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004027 removeLockedTaskLocked(lockedTask);
4028 lockedTask.performClearTaskLocked();
4029 didSomething = true;
4030 }
4031 }
4032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4033 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4035 final ActivityStack stack = stacks.get(stackNdx);
4036 stack.onLockTaskPackagesUpdatedLocked();
4037 }
4038 }
Craig Mautnere0570202015-05-13 13:06:11 -07004039 final ActivityRecord r = topRunningActivityLocked();
4040 final TaskRecord task = r != null ? r.task : null;
4041 if (mLockTaskModeTasks.isEmpty() && task != null
4042 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4043 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004044 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4045 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4046 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4047 false);
4048 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004049 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004050 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004051 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004052 }
4053 }
4054
Benjamin Franz43261142015-02-11 15:59:44 +00004055 int getLockTaskModeState() {
4056 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004057 }
4058
Jorim Jaggife89d122015-12-22 16:28:44 +01004059 void activityRelaunchedLocked(IBinder token) {
4060 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004061 if (mService.isSleepingOrShuttingDownLocked()) {
4062 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4063 if (r != null) {
4064 r.setSleeping(true, true);
4065 }
4066 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004067 }
4068
4069 void activityRelaunchingLocked(ActivityRecord r) {
4070 mWindowManager.notifyAppRelaunching(r.appToken);
4071 }
4072
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004073 void logStackState() {
4074 mActivityMetricsLogger.logWindowState();
4075 }
4076
Andrii Kulian933076d2016-03-29 17:04:42 -07004077 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004078 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4079 final ActivityRecord r = task.mActivities.get(i);
4080 if (r.app != null && r.app.thread != null) {
4081 mMultiWindowModeChangedActivities.add(r);
4082 }
4083 }
4084
4085 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4086 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4087 }
4088 }
4089
Andrii Kulian933076d2016-03-29 17:04:42 -07004090 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004091 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004092 if (prevStack == null || prevStack == stack
4093 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4094 return;
4095 }
4096
4097 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4098 final ActivityRecord r = task.mActivities.get(i);
4099 if (r.app != null && r.app.thread != null) {
4100 mPipModeChangedActivities.add(r);
4101 }
4102 }
4103
4104 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4105 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4106 }
4107 }
4108
Tony Mak853304c2016-04-18 15:17:41 +01004109 void setDockedStackMinimized(boolean minimized) {
4110 mIsDockMinimized = minimized;
4111 if (minimized) {
4112 // Docked stack is not visible, no need to confirm credentials for its top activity.
4113 return;
4114 }
4115 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
4116 if (dockedStack == null) {
4117 return;
4118 }
4119 final ActivityRecord top = dockedStack.topRunningActivityLocked();
4120 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
4121 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
4122 }
4123 }
4124
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004125 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004126
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004127 public ActivityStackSupervisorHandler(Looper looper) {
4128 super(looper);
4129 }
4130
Craig Mautnerf3333272013-04-22 10:55:53 -07004131 void activityIdleInternal(ActivityRecord r) {
4132 synchronized (mService) {
4133 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4134 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004135 }
4136
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004137 @Override
4138 public void handleMessage(Message msg) {
4139 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004140 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4141 synchronized (mService) {
4142 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4143 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004144 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004145 }
4146 }
4147 } break;
4148 case REPORT_PIP_MODE_CHANGED_MSG: {
4149 synchronized (mService) {
4150 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4151 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004152 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004153 }
4154 }
4155 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004156 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004157 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4158 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004159 if (mService.mDidDexOpt) {
4160 mService.mDidDexOpt = false;
4161 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4162 nmsg.obj = msg.obj;
4163 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4164 return;
4165 }
4166 // We don't at this point know if the activity is fullscreen,
4167 // so we need to be conservative and assume it isn't.
4168 activityIdleInternal((ActivityRecord)msg.obj);
4169 } break;
4170 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004171 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004172 activityIdleInternal((ActivityRecord)msg.obj);
4173 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004174 case RESUME_TOP_ACTIVITY_MSG: {
4175 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004176 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004177 }
4178 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004179 case SLEEP_TIMEOUT_MSG: {
4180 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004181 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004182 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4183 mSleepTimeout = true;
4184 checkReadyForSleepLocked();
4185 }
4186 }
4187 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004188 case LAUNCH_TIMEOUT_MSG: {
4189 if (mService.mDidDexOpt) {
4190 mService.mDidDexOpt = false;
4191 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4192 return;
4193 }
4194 synchronized (mService) {
4195 if (mLaunchingActivity.isHeld()) {
4196 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4197 if (VALIDATE_WAKE_LOCK_CALLER
4198 && Binder.getCallingUid() != Process.myUid()) {
4199 throw new IllegalStateException("Calling must be system uid");
4200 }
4201 mLaunchingActivity.release();
4202 }
4203 }
4204 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004205 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004206 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004207 } break;
4208 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004209 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004210 } break;
4211 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004212 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004213 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004214 case CONTAINER_CALLBACK_VISIBILITY: {
4215 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004216 final IActivityContainerCallback callback = container.mCallback;
4217 if (callback != null) {
4218 try {
4219 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4220 } catch (RemoteException e) {
4221 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004222 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004223 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004224 case LOCK_TASK_START_MSG: {
4225 // When lock task starts, we disable the status bars.
4226 try {
Jason Monk62515be2014-05-21 16:06:19 -04004227 if (mLockTaskNotify == null) {
4228 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004229 }
Jason Monk62515be2014-05-21 16:06:19 -04004230 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004231 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004232 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004233 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004234 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004235 flags = StatusBarManager.DISABLE_MASK
4236 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004237 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004238 flags = StatusBarManager.DISABLE_MASK
4239 & (~StatusBarManager.DISABLE_BACK)
4240 & (~StatusBarManager.DISABLE_HOME)
4241 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004242 }
4243 getStatusBarService().disable(flags, mToken,
4244 mService.mContext.getPackageName());
4245 }
4246 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004247 if (getDevicePolicyManager() != null) {
4248 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004249 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004250 }
justinzhang5286d3f2014-05-12 17:06:01 -04004251 } catch (RemoteException ex) {
4252 throw new RuntimeException(ex);
4253 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004254 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004255 case LOCK_TASK_END_MSG: {
4256 // When lock task ends, we enable the status bars.
4257 try {
Jason Monk62515be2014-05-21 16:06:19 -04004258 if (getStatusBarService() != null) {
4259 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4260 mService.mContext.getPackageName());
4261 }
4262 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004263 if (getDevicePolicyManager() != null) {
4264 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4265 msg.arg1);
4266 }
Jason Monk62515be2014-05-21 16:06:19 -04004267 if (mLockTaskNotify == null) {
4268 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4269 }
4270 mLockTaskNotify.show(false);
4271 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004272 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004273 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004274 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004275 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004276 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004277 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004278 new LockPatternUtils(mService.mContext)
4279 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004280 }
4281 } catch (SettingNotFoundException e) {
4282 // No setting, don't lock.
4283 }
justinzhang5286d3f2014-05-12 17:06:01 -04004284 } catch (RemoteException ex) {
4285 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004286 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004287 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004288 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004289 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004290 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4291 if (mLockTaskNotify == null) {
4292 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4293 }
4294 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4295 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004296 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4297 final ActivityContainer container = (ActivityContainer) msg.obj;
4298 final IActivityContainerCallback callback = container.mCallback;
4299 if (callback != null) {
4300 try {
4301 callback.onAllActivitiesComplete(container.asBinder());
4302 } catch (RemoteException e) {
4303 }
4304 }
4305 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004306 case LAUNCH_TASK_BEHIND_COMPLETE: {
4307 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004308 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004309 if (r != null) {
4310 handleLaunchTaskBehindCompleteLocked(r);
4311 }
4312 }
4313 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004314
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004315 }
4316 }
4317 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004318
Ying Wangb081a592014-04-22 15:20:16 -07004319 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004320 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4321 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004322 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004323 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004324 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004325 ActivityRecord mParentActivity = null;
4326 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004327
Craig Mautnere3a00d72014-04-16 08:31:19 -07004328 boolean mVisible = true;
4329
Craig Mautner4a1cb222013-12-04 16:14:06 -08004330 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004331 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004332
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004333 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4334 final static int CONTAINER_STATE_NO_SURFACE = 1;
4335 final static int CONTAINER_STATE_FINISHING = 2;
4336 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4337
4338 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004339 synchronized (mService) {
4340 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004341 mStack = new ActivityStack(this, mRecentTasks);
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
4349 * {@link ActivityStack#addToDisplay(ActivityDisplay, boolean)}.
4350 * @param activityDisplay The display to add the stack to.
4351 * @param onTop If true the stack will be place at the top of the display, else at the
4352 * bottom.
4353 */
4354 void addToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
4355 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004356 + " to display=" + activityDisplay + " onTop=" + onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004357 if (mActivityDisplay != null) {
4358 throw new IllegalStateException("ActivityContainer is already attached, " +
4359 "displayId=" + mActivityDisplay.mDisplayId);
4360 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004361 mActivityDisplay = activityDisplay;
Andrii Kulian839def92016-11-02 10:58:58 -07004362 mStack.addToDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004363 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004364 }
4365
4366 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004367 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004368 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004369 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4370 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 return;
4372 }
Andrii Kuliand2765632016-12-12 22:26:34 -08004373 addToDisplayLocked(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004374 }
4375 }
4376
4377 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004378 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004379 synchronized (mService) {
4380 if (mActivityDisplay != null) {
4381 return mActivityDisplay.mDisplayId;
4382 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004383 }
4384 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004385 }
4386
Jeff Brownca9bc702014-02-11 14:32:56 -08004387 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004388 public int getStackId() {
4389 synchronized (mService) {
4390 return mStackId;
4391 }
4392 }
4393
4394 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004395 public boolean injectEvent(InputEvent event) {
4396 final long origId = Binder.clearCallingIdentity();
4397 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004398 synchronized (mService) {
4399 if (mActivityDisplay != null) {
4400 return mInputManagerInternal.injectInputEvent(event,
4401 mActivityDisplay.mDisplayId,
4402 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4403 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004404 }
4405 return false;
4406 } finally {
4407 Binder.restoreCallingIdentity(origId);
4408 }
4409 }
4410
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004411 @Override
4412 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004413 synchronized (mService) {
4414 if (mContainerState == CONTAINER_STATE_FINISHING) {
4415 return;
4416 }
4417 mContainerState = CONTAINER_STATE_FINISHING;
4418
Craig Mautnerd163e752014-06-13 17:18:47 -07004419 long origId = Binder.clearCallingIdentity();
4420 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004421 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004422 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004423 } finally {
4424 Binder.restoreCallingIdentity(origId);
4425 }
4426 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004427 }
4428
Andrii Kulian03c403d2016-11-11 11:14:12 -08004429 /**
4430 * Remove the stack completely. Must be called only when there are no tasks left in it,
4431 * as this method does not finish running activities.
4432 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004433 void removeLocked() {
4434 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004435 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004436 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004437 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004438 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004439 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004440 }
4441
Andrii Kulian839def92016-11-02 10:58:58 -07004442 /**
4443 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4444 * completely or re-parented.
4445 */
4446 private void removeFromDisplayLocked() {
4447 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4448 + " current displayId=" + mActivityDisplay.mDisplayId);
4449
4450 mActivityDisplay.detachActivitiesLocked(mStack);
4451 mActivityDisplay = null;
4452 }
4453
4454 /**
4455 * Move the stack to specified display.
4456 * @param activityDisplay Target display to move the stack to.
4457 */
4458 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4459 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4460 + mActivityDisplay + " to display=" + activityDisplay
4461 + " Callers=" + Debug.getCallers(2));
4462
4463 removeFromDisplayLocked();
4464
4465 mActivityDisplay = activityDisplay;
4466 mStack.moveToDisplay(activityDisplay);
4467 activityDisplay.attachActivities(mStack, ON_TOP);
4468 }
4469
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004471 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004472 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004473 }
4474
4475 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004476 public final int startActivityIntentSender(IIntentSender intentSender)
4477 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004478 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4479
4480 if (!(intentSender instanceof PendingIntentRecord)) {
4481 throw new IllegalArgumentException("Bad PendingIntent object");
4482 }
4483
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004484 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004485 Binder.getCallingUid(), mCurrentUser, false,
4486 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004487
4488 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4489 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4490 pendingIntent.key.requestResolvedType);
4491
4492 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4493 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4494 }
4495
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004496 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004497 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004498 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004499 throw new SecurityException(
4500 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4501 }
4502 }
4503
Craig Mautnerdf88d732014-01-27 09:21:32 -08004504 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004505 public IBinder asBinder() {
4506 return this;
4507 }
4508
Craig Mautner4504de52013-12-20 09:06:56 -08004509 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004510 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004511 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004512 }
4513
Craig Mautner4a1cb222013-12-04 16:14:06 -08004514 ActivityStackSupervisor getOuter() {
4515 return ActivityStackSupervisor.this;
4516 }
4517
Craig Mautnerd163e752014-06-13 17:18:47 -07004518 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004519 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004520 }
4521
Craig Mautner6985bad2014-04-21 15:22:06 -07004522 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004523 void setVisible(boolean visible) {
4524 if (mVisible != visible) {
4525 mVisible = visible;
4526 if (mCallback != null) {
4527 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4528 0 /* unused */, this).sendToTarget();
4529 }
4530 }
4531 }
4532
Craig Mautner6985bad2014-04-21 15:22:06 -07004533 void setDrawn() {
4534 }
4535
Craig Mautner1b4bf852014-05-26 15:06:32 -07004536 // You can always start a new task on a regular ActivityStack.
4537 boolean isEligibleForNewTasks() {
4538 return true;
4539 }
4540
Craig Mautnerd163e752014-06-13 17:18:47 -07004541 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004542 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004543 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004544 }
4545
Craig Mautner34b73df2014-01-12 21:11:08 -08004546 @Override
4547 public String toString() {
4548 return mIdString + (mActivityDisplay == null ? "N" : "A");
4549 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004550 }
4551
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004552 private class VirtualActivityContainer extends ActivityContainer {
4553 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004554 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004555
4556 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4557 super(getNextStackId());
4558 mParentActivity = parent;
4559 mCallback = callback;
4560 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004561 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004562 }
4563
4564 @Override
4565 public void setSurface(Surface surface, int width, int height, int density) {
4566 super.setSurface(surface, width, height, density);
4567
4568 synchronized (mService) {
4569 final long origId = Binder.clearCallingIdentity();
4570 try {
4571 setSurfaceLocked(surface, width, height, density);
4572 } finally {
4573 Binder.restoreCallingIdentity(origId);
4574 }
4575 }
4576 }
4577
4578 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4579 if (mContainerState == CONTAINER_STATE_FINISHING) {
4580 return;
4581 }
4582 VirtualActivityDisplay virtualActivityDisplay =
4583 (VirtualActivityDisplay) mActivityDisplay;
4584 if (virtualActivityDisplay == null) {
4585 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004586 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004587 mActivityDisplay = virtualActivityDisplay;
4588 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Andrii Kuliand2765632016-12-12 22:26:34 -08004589 addToDisplayLocked(virtualActivityDisplay, true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004590 }
4591
4592 if (mSurface != null) {
4593 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004594 }
4595
Craig Mautner6985bad2014-04-21 15:22:06 -07004596 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004597 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004598 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004599 } else {
4600 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004601 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004602 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004603 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004604 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004605 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004606
Craig Mautnerd163e752014-06-13 17:18:47 -07004607 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004608
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004609 if (DEBUG_STACK) Slog.d(TAG_STACK,
4610 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004611 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004612
Craig Mautner6985bad2014-04-21 15:22:06 -07004613 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004614 boolean isAttachedLocked() {
4615 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004616 }
4617
4618 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004619 void setDrawn() {
4620 synchronized (mService) {
4621 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004622 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004623 }
4624 }
4625
Craig Mautner1b4bf852014-05-26 15:06:32 -07004626 // Never start a new task on an ActivityView if it isn't explicitly specified.
4627 @Override
4628 boolean isEligibleForNewTasks() {
4629 return false;
4630 }
4631
Craig Mautnerd163e752014-06-13 17:18:47 -07004632 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004633 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004634 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4635 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4636 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4637 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4638 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004639 }
4640 }
4641
Craig Mautner4a1cb222013-12-04 16:14:06 -08004642 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4643 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004644 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004645 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004646 int mDisplayId;
4647 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004648 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4649 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004650
Craig Mautner4a1cb222013-12-04 16:14:06 -08004651 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4652 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004653 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004654
Jose Lima4b6c6692014-08-12 17:41:12 -07004655 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004656
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004657 /** Array of all UIDs that are present on the display. */
4658 private IntArray mDisplayAccessUIDs = new IntArray();
4659
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004660 ActivityDisplay() {
4661 }
Craig Mautner4504de52013-12-20 09:06:56 -08004662
Craig Mautner1a70a162014-09-13 12:09:31 -07004663 // After instantiation, check that mDisplay is not null before using this. The alternative
4664 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004665 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004666 final Display display = mDisplayManager.getDisplay(displayId);
4667 if (display == null) {
4668 return;
4669 }
4670 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004671 }
4672
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004673 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004674 mDisplay = display;
4675 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004676 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004677
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004678 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004679 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004680 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4681 + " onTop=" + onTop);
4682 if (onTop) {
4683 mStacks.add(stack);
4684 } else {
4685 mStacks.add(0, stack);
4686 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004687 }
4688
Craig Mautnere0a38842013-12-16 16:14:02 -08004689 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004690 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004691 + " from displayId=" + mDisplayId);
4692 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004693 }
4694
Jose Lima4b6c6692014-08-12 17:41:12 -07004695 void setVisibleBehindActivity(ActivityRecord r) {
4696 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004697 }
4698
Jose Lima4b6c6692014-08-12 17:41:12 -07004699 boolean hasVisibleBehindActivity() {
4700 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004701 }
4702
Craig Mautner34b73df2014-01-12 21:11:08 -08004703 @Override
4704 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004705 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4706 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004707
4708 @Override
4709 protected int getChildCount() {
4710 return mStacks.size();
4711 }
4712
4713 @Override
4714 protected ConfigurationContainer getChildAt(int index) {
4715 return mStacks.get(index);
4716 }
4717
4718 @Override
4719 protected ConfigurationContainer getParent() {
4720 return ActivityStackSupervisor.this;
4721 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004722
4723 boolean isPrivate() {
4724 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4725 }
4726
4727 boolean isUidPresent(int uid) {
4728 for (ActivityStack stack : mStacks) {
4729 if (stack.isUidPresent(uid)) {
4730 return true;
4731 }
4732 }
4733 return false;
4734 }
4735
4736 /** Update and get all UIDs that are present on the display and have access to it. */
4737 private IntArray getPresentUIDs() {
4738 mDisplayAccessUIDs.clear();
4739 for (ActivityStack stack : mStacks) {
4740 stack.getPresentUIDs(mDisplayAccessUIDs);
4741 }
4742 return mDisplayAccessUIDs;
4743 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004744 }
4745
4746 class VirtualActivityDisplay extends ActivityDisplay {
4747 VirtualDisplay mVirtualDisplay;
4748
Craig Mautner6985bad2014-04-21 15:22:06 -07004749 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004750 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004751 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4752 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4753 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4754 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004755
4756 init(mVirtualDisplay.getDisplay());
4757
4758 mWindowManager.handleDisplayAdded(mDisplayId);
4759 }
4760
4761 void setSurface(Surface surface) {
4762 if (mVirtualDisplay != null) {
4763 mVirtualDisplay.setSurface(surface);
4764 }
4765 }
4766
4767 @Override
4768 void detachActivitiesLocked(ActivityStack stack) {
4769 super.detachActivitiesLocked(stack);
4770 if (mVirtualDisplay != null) {
4771 mVirtualDisplay.release();
4772 mVirtualDisplay = null;
4773 }
4774 }
4775
4776 @Override
4777 public String toString() {
4778 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004779 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004780 }
Jose Lima58e66d62014-05-27 20:00:27 -07004781
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004782 ActivityStack findStackBehind(ActivityStack stack) {
4783 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004784 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004785 if (display == null) {
4786 return null;
4787 }
4788 final ArrayList<ActivityStack> stacks = display.mStacks;
4789 for (int i = stacks.size() - 1; i >= 0; i--) {
4790 if (stacks.get(i) == stack && i > 0) {
4791 return stacks.get(i - 1);
4792 }
4793 }
4794 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4795 + " in=" + stacks);
4796 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004797
Jorim Jaggic69bd222016-03-15 14:38:37 +01004798 /**
4799 * Puts a task into resizing mode during the next app transition.
4800 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004801 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004802 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004803 private void setResizingDuringAnimation(TaskRecord task) {
4804 mResizingTasksDuringAnimation.add(task.taskId);
4805 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004806 }
4807
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004808 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4809 final TaskRecord task;
4810 final int callingUid;
4811 final String callingPackage;
4812 final Intent intent;
4813 final int userId;
4814 final ActivityOptions activityOptions = (bOptions != null)
4815 ? new ActivityOptions(bOptions) : null;
4816 final int launchStackId = (activityOptions != null)
4817 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004818 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004819 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004820 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004821 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004822
4823 if (launchStackId == DOCKED_STACK_ID) {
4824 mWindowManager.setDockedStackCreateState(
4825 activityOptions.getDockCreateMode(), null /* initialBounds */);
4826
4827 // Defer updating the stack in which recents is until the app transition is done, to
4828 // not run into issues where we still need to draw the task in recents but the
4829 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004830 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004831 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4832 }
4833
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004834 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4835 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004836 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004837 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004838 throw new IllegalArgumentException(
4839 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4840 }
4841
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004842 // Since we don't have an actual source record here, we assume that the currently focused
4843 // activity was the source.
4844 final ActivityStack focusedStack = getFocusedStack();
4845 final ActivityRecord sourceRecord =
4846 focusedStack != null ? focusedStack.topActivity() : null;
4847
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004848 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004849 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004850 moveTaskToStackLocked(
4851 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4852 ANIMATE);
4853 }
4854 }
4855
4856 // If the user must confirm credentials (e.g. when first launching a work app and the
4857 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4858 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4859 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004860 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004861 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004862 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004863 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4864 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004865
4866 // If we are launching the task in the docked stack, put it into resizing mode so
4867 // the window renders full-screen with the background filling the void. Also only
4868 // call this at the end to make sure that tasks exists on the window manager side.
4869 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004870 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004871 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004872
4873 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4874 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004875 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4876 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004877 return ActivityManager.START_TASK_TO_FRONT;
4878 }
4879 callingUid = task.mCallingUid;
4880 callingPackage = task.mCallingPackage;
4881 intent = task.intent;
4882 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4883 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004884 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4885 null, null, 0, 0, bOptions, userId, null, task);
4886 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004887 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004888 }
4889 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004890 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004891
4892 /**
4893 * @return a list of activities which are the top ones in each visible stack. The first
4894 * entry will be the focused activity.
4895 */
4896 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004897 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4898 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004899 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004900 if (display == null) {
4901 return Collections.EMPTY_LIST;
4902 }
4903 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4904 final ArrayList<ActivityStack> stacks = display.mStacks;
4905 for (int i = stacks.size() - 1; i >= 0; i--) {
4906 ActivityStack stack = stacks.get(i);
4907 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4908 ActivityRecord top = stack.topActivity();
4909 if (top != null) {
4910 if (stack == mFocusedStack) {
4911 topActivityTokens.add(0, top.appToken);
4912 } else {
4913 topActivityTokens.add(top.appToken);
4914 }
4915 }
4916 }
4917 }
4918 return topActivityTokens;
4919 }
Craig Mautner27084302013-03-25 08:05:25 -07004920}