blob: 7c3280efcccc446e613957ce2a4c9d173e9e1c25 [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;
Jorim Jaggife762342016-10-13 14:33:27 +020024import static android.app.ActivityManager.START_TASK_TO_FRONT;
25import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
26import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
29import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.HOME_STACK_ID;
31import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
32import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
33import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080034import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020035import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
36import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
37import static android.content.pm.PackageManager.PERMISSION_GRANTED;
38import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
39import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070040import static android.view.Display.FLAG_PRIVATE;
41import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080042import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
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;
Winson Chung74666102017-02-22 17:49:24 -080091import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
92import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
93import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020094import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080095import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020096
Svetoslav7008b512015-06-24 18:47:07 -070097import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080098import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -070099import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100100import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700101import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800103import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700104import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700107import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800108import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700109import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700110import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400111import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700112import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400113import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117import android.content.Intent;
118import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700120import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.PackageManager;
122import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100123import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800125import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800126import android.hardware.display.DisplayManager;
127import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800128import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800129import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800130import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800131import android.hardware.input.InputManager;
132import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100134import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700135import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700136import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700138import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700139import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700141import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700142import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700143import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400144import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700145import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700146import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700147import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700148import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100149import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700150import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700151import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400152import android.provider.Settings;
153import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700154import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700155import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700156import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700157import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800158import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700159import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800162import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800163import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800164import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800165
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800166import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800167import com.android.internal.logging.MetricsLogger;
168import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700169import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400170import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700171import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400172import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800173import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700174import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700175import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700176
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177import java.io.FileDescriptor;
178import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700179import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800180import java.lang.annotation.Retention;
181import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700182import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700183import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700184import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700185import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700186
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800187public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800188 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700190 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700192 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700193 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800199 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800201
Craig Mautnerf3333272013-04-22 10:55:53 -0700202 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700204
Craig Mautner0eea92c2013-05-16 13:35:39 -0700205 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210
Craig Mautner05d29032013-05-03 13:40:13 -0700211 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
212 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
213 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700214 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700215 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800216 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
217 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
218 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700219 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400220 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
221 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700222 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700223 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700224 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800225 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
226 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700229
Jason Monk62515be2014-05-21 16:06:19 -0400230 private static final String LOCK_TASK_TAG = "Lock-to-App";
231
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232 // Used to indicate if an object (e.g. stack) that we are trying to get
233 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800234 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700236 // Used to indicate that windows of activities should be preserved during the resize.
237 static final boolean PRESERVE_WINDOWS = true;
238
Wale Ogunwale040b4702015-08-06 18:10:50 -0700239 // Used to indicate if an object (e.g. task) should be moved/created
240 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700241 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700242
243 // Used to indicate that an objects (e.g. task) removal from its container
244 // (e.g. stack) is due to it moving to another container.
245 static final boolean MOVING = true;
246
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700247 // Force the focus to change to the stack we are moving a task to..
248 static final boolean FORCE_FOCUS = true;
249
Wale 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
Winson Chung7900bee2017-03-10 08:59:25 -0800256 /**
257 * The modes which affect which tasks are returned when calling
258 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
259 */
260 @Retention(RetentionPolicy.SOURCE)
261 @IntDef({
262 MATCH_TASK_IN_STACKS_ONLY,
263 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
264 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
265 })
266 public @interface AnyTaskForIdMatchTaskMode {}
267 // Match only tasks in the current stacks
268 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
269 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
270 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
271 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
272 // provided stack id
273 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
274
Svetoslav7008b512015-06-24 18:47:07 -0700275 // Activity actions an app cannot start if it uses a permission which is not granted.
276 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
277 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700278
Svetoslav7008b512015-06-24 18:47:07 -0700279 static {
280 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
281 Manifest.permission.CAMERA);
282 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
283 Manifest.permission.CAMERA);
284 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
285 Manifest.permission.CALL_PHONE);
286 }
287
Svet Ganov99b60432015-06-27 13:15:22 -0700288 /** Action restriction: launching the activity is not restricted. */
289 private static final int ACTIVITY_RESTRICTION_NONE = 0;
290 /** Action restriction: launching the activity is restricted by a permission. */
291 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
292 /** Action restriction: launching the activity is restricted by an app op. */
293 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700294
justinzhang5286d3f2014-05-12 17:06:01 -0400295 /** Status Bar Service **/
296 private IBinder mToken = new Binder();
297 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400298 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400299
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700300 // For debugging to make sure the caller when acquiring/releasing our
301 // wake lock is the system process.
302 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800303 /** The number of distinct task ids that can be assigned to the tasks of a single user */
304 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700305
Craig Mautner27084302013-03-25 08:05:25 -0700306 final ActivityManagerService mService;
307
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800308 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800309
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700310 final ActivityStackSupervisorHandler mHandler;
311
312 /** Short cut */
313 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800314 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700315
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700316 /** Counter for next free stack ID to use for dynamic activity stacks. */
317 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700318
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800319 /**
320 * Maps the task identifier that activities are currently being started in to the userId of the
321 * task. Each time a new task is created, the entry for the userId of the task is incremented
322 */
323 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700324
Craig Mautner2420ead2013-04-01 17:13:20 -0700325 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800326 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700327
Craig Mautnere0a38842013-12-16 16:14:02 -0800328 /** The stack containing the launcher app. Assumed to always be attached to
329 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800330 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700331
Craig Mautnere0a38842013-12-16 16:14:02 -0800332 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800333 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700334
Craig Mautner4a1cb222013-12-04 16:14:06 -0800335 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
336 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800337 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800338 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700339
340 /** List of activities that are waiting for a new activity to become visible before completing
341 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800342 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700344 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700345 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700346
347 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700348 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700349
Craig Mautnerde4ef022013-04-07 19:01:33 -0700350 /** List of activities that are ready to be stopped, but waiting for the next activity to
351 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800352 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700353
Craig Mautnerf3333272013-04-22 10:55:53 -0700354 /** List of activities that are ready to be finished, but waiting for the previous activity to
355 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800356 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700357
Craig Mautner0eea92c2013-05-16 13:35:39 -0700358 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800359 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700360
Wale Ogunwale22e25262016-02-01 10:32:02 -0800361 /** List of activities whose multi-window mode changed that we need to report to the
362 * application */
363 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
364
365 /** List of activities whose picture-in-picture mode changed that we need to report to the
366 * application */
367 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
368
Craig Mautnerf3333272013-04-22 10:55:53 -0700369 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700370 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700371
Craig Mautnerde4ef022013-04-07 19:01:33 -0700372 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
373 * is being brought in front of us. */
374 boolean mUserLeaving = false;
375
Craig Mautner0eea92c2013-05-16 13:35:39 -0700376 /** Set when we have taken too long waiting to go to sleep. */
377 boolean mSleepTimeout = false;
378
379 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700380 * We don't want to allow the device to go to sleep while in the process
381 * of launching an activity. This is primarily to allow alarm intent
382 * receivers to launch an activity and get that to run before the device
383 * goes back to sleep.
384 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700385 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700386
387 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700388 * Set when the system is going to sleep, until we have
389 * successfully paused the current activity and released our wake lock.
390 * At that point the system is allowed to actually sleep.
391 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700392 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700393
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700394 /** Stack id of the front stack when user switched, indexed by userId. */
395 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700396
Craig Mautner4504de52013-12-20 09:06:56 -0800397 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800399 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800400
401 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700402 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800404 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
405
406 private DisplayManagerInternal mDisplayManagerInternal;
407 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800408
Craig Mautner15df08a2015-04-01 12:17:18 -0700409 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
410 * may be finished until there is only one entry left. If this is empty the system is not
411 * in lockTask mode. */
412 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000413 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700414 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
415 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000416 */
417 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400418 /**
419 * Notifies the user when entering/exiting lock-task.
420 */
421 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800422
Wale Ogunwaled046a012015-12-24 13:05:59 -0800423 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800424 boolean inResumeTopActivity;
425
Andrii Kulian1e32e022016-09-16 15:29:34 -0700426 /**
427 * Temporary rect used during docked stack resize calculation so we don't need to create a new
428 * object each time.
429 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700430 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700431
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700432 // The default minimal size that will be used if the activity doesn't specify its minimal size.
433 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700434 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700435
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700436 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
437 private boolean mTaskLayersChanged = true;
438
Jorim Jaggi275561a2016-02-23 10:11:02 -0500439 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800440
Andrii Kulian1779e612016-10-12 21:58:25 -0700441 @Override
442 protected int getChildCount() {
443 return mActivityDisplays.size();
444 }
445
446 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700447 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700448 return mActivityDisplays.valueAt(index);
449 }
450
451 @Override
452 protected ConfigurationContainer getParent() {
453 return null;
454 }
455
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700456 Configuration getDisplayOverrideConfiguration(int displayId) {
457 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
458 if (activityDisplay == null) {
459 throw new IllegalArgumentException("No display found with id: " + displayId);
460 }
461
462 return activityDisplay.getOverrideConfiguration();
463 }
464
465 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
466 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
467 if (activityDisplay == null) {
468 throw new IllegalArgumentException("No display found with id: " + displayId);
469 }
470
471 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
472 }
473
Wale Ogunwale39381972015-12-17 17:15:29 -0800474 static class FindTaskResult {
475 ActivityRecord r;
476 boolean matchedByRootAffinity;
477 }
478 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
479
Craig Mautneree36c772014-07-16 14:56:05 -0700480 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800481 * Temp storage for display ids sorted in focus order.
482 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
483 * it's more efficient, as the number of displays is usually small.
484 */
485 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
486
487 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100488 * Used to keep track whether app visibilities got changed since the last pause. Useful to
489 * determine whether to invoke the task stack change listener after pausing.
490 */
491 boolean mAppVisibilitiesChangedSinceLastPause;
492
493 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100494 * Set of tasks that are in resizing mode during an app transition to fill the "void".
495 */
496 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
497
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700498
499 /**
500 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
501 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
502 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
503 * like the docked stack going empty.
504 */
505 private boolean mAllowDockedStackResize = true;
506
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100507 /**
Tony Mak853304c2016-04-18 15:17:41 +0100508 * Is dock currently minimized.
509 */
510 boolean mIsDockMinimized;
511
Jorim Jaggife762342016-10-13 14:33:27 +0200512 final KeyguardController mKeyguardController;
513
Tony Mak853304c2016-04-18 15:17:41 +0100514 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700515 * Description of a request to start a new activity, which has been held
516 * due to app switches being disabled.
517 */
518 static class PendingActivityLaunch {
519 final ActivityRecord r;
520 final ActivityRecord sourceRecord;
521 final int startFlags;
522 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700523 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700524
525 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700526 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700527 r = _r;
528 sourceRecord = _sourceRecord;
529 startFlags = _startFlags;
530 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700531 callerApp = _callerApp;
532 }
533
534 void sendErrorResult(String message) {
535 try {
536 if (callerApp.thread != null) {
537 callerApp.thread.scheduleCrash(message);
538 }
539 } catch (RemoteException e) {
540 Slog.e(TAG, "Exception scheduling crash of failed "
541 + "activity launcher sourceRecord=" + sourceRecord, e);
542 }
Craig Mautneree36c772014-07-16 14:56:05 -0700543 }
544 }
545
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800546 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700547 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700548 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800549 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200550 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700551 }
552
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800553 void setRecentTasks(RecentTasks recentTasks) {
554 mRecentTasks = recentTasks;
555 }
556
Jeff Brown2c43c332014-06-12 22:38:59 -0700557 /**
558 * At the time when the constructor runs, the power manager has not yet been
559 * initialized. So we initialize our wakelocks afterwards.
560 */
561 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700563 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700564 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700565 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700566 }
567
justinzhang5286d3f2014-05-12 17:06:01 -0400568 // This function returns a IStatusBarService. The value is from ServiceManager.
569 // getService and is cached.
570 private IStatusBarService getStatusBarService() {
571 synchronized (mService) {
572 if (mStatusBarService == null) {
573 mStatusBarService = IStatusBarService.Stub.asInterface(
574 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
575 if (mStatusBarService == null) {
576 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
577 }
578 }
579 return mStatusBarService;
580 }
581 }
582
Jason Monk35c62a42014-06-17 10:24:47 -0400583 private IDevicePolicyManager getDevicePolicyManager() {
584 synchronized (mService) {
585 if (mDevicePolicyManager == null) {
586 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
587 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
588 if (mDevicePolicyManager == null) {
589 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
590 }
591 }
592 return mDevicePolicyManager;
593 }
594 }
595
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700596 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 synchronized (mService) {
598 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200599 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600
601 mDisplayManager =
602 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
603 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800604 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605
606 Display[] displays = mDisplayManager.getDisplays();
607 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
608 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800609 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700610 if (activityDisplay.mDisplay == null) {
611 throw new IllegalStateException("Default Display does not exist");
612 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800613 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700614 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 }
616
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800617 mHomeStack = mFocusedStack = mLastFocusedStack =
618 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800619
620 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 }
Craig Mautner27084302013-03-25 08:05:25 -0700622 }
623
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700624 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700626 }
627
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700630 }
631
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700632 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700633 if (stack == null) {
634 return false;
635 }
636
Craig Mautnerdf88d732014-01-27 09:21:32 -0800637 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
638 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700639 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800640 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800641 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700642 }
643
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800644 /** The top most stack on its display. */
645 boolean isFrontStackOnDisplay(ActivityStack stack) {
646 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
647 }
648
649 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700650 if (stack == null) {
651 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700653
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700654 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
655 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700656 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800657 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800658 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700659 }
660
Wale Ogunwaled046a012015-12-24 13:05:59 -0800661 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800662 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
663 if (!focusCandidate.isFocusable()) {
664 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800665 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800666 }
667
668 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800669 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800670 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800671
Wale Ogunwaled046a012015-12-24 13:05:59 -0800672 EventLogTags.writeAmFocusedStack(
673 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
674 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
675 }
676
677 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800678 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800679 if (r != null && r.idle) {
680 checkFinishBootingLocked();
681 }
682 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700683 }
684
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700685 void moveHomeStackToFront(String reason) {
686 mHomeStack.moveToFront(reason);
687 }
688
Matthew Ng330757d2017-02-28 14:19:17 -0800689 void moveRecentsStackToFront(String reason) {
690 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
691 if (recentsStack != null) {
692 recentsStack.moveToFront(reason);
693 }
694 }
695
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700696 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800697 boolean moveHomeStackTaskToTop(String reason) {
698 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700699
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700700 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700701 if (top == null) {
702 return false;
703 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700704 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700705 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700706 }
707
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800708 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700709 if (!mService.mBooting && !mService.mBooted) {
710 // Not ready yet!
711 return false;
712 }
713
Craig Mautner84984fa2014-06-19 11:19:20 -0700714 if (prev != null) {
715 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
716 }
717
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800718 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700719 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800720 final String myReason = reason + " resumeHomeStackTask";
721
Mark Lua56ea122015-10-08 13:31:01 +0800722 // Only resume home activity if isn't finishing.
723 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700724 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800725 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700726 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800727 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700728 }
729
Craig Mautner8d341ef2013-03-26 09:03:27 -0700730 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800731 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
732 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700733 }
734
735 /**
736 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
737 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800738 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700739 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800740 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
741 * valid if the matchMode is
742 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700743 */
Winson Chung7900bee2017-03-10 08:59:25 -0800744 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
745 // If there is a stack id set, ensure that we are attempting to actually restore a task
746 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
747 stackId != INVALID_STACK_ID) {
748 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
749 }
750
Craig Mautnere0a38842013-12-16 16:14:02 -0800751 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800753 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
755 ActivityStack stack = stacks.get(stackNdx);
756 TaskRecord task = stack.taskForIdLocked(id);
757 if (task != null) {
758 return task;
759 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700760 }
761 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800762
Winson Chung7900bee2017-03-10 08:59:25 -0800763 // If we are matching stack tasks only, return now
764 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800765 return null;
766 }
767
Winson Chung7900bee2017-03-10 08:59:25 -0800768 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
769 // the task from recents
770 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
771 TaskRecord task = mRecentTasks.taskForIdLocked(id);
772 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
773 if (DEBUG_RECENTS && task == null) {
774 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
775 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700776 return task;
777 }
778
Winson Chung7900bee2017-03-10 08:59:25 -0800779 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700780 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700781 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
782 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800783 return null;
784 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700785 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800786 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700787 }
788
Craig Mautner6170f732013-04-02 13:05:23 -0700789 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800792 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
794 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
795 if (r != null) {
796 return r;
797 }
Craig Mautner6170f732013-04-02 13:05:23 -0700798 }
799 }
800 return null;
801 }
802
Tony Mak853304c2016-04-18 15:17:41 +0100803 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000804 * Detects whether we should show a lock screen in front of this task for a locked user.
805 * <p>
806 * We'll do this if either of the following holds:
807 * <ul>
808 * <li>The top activity explicitly belongs to {@param userId}.</li>
809 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
810 * </ul>
811 *
812 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100813 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000814 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
815 // To handle the case that work app is in the task but just is not the top one.
816 final ActivityRecord activityRecord = task.getTopActivity();
817 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
818
819 return (activityRecord != null && activityRecord.userId == userId)
820 || (resultTo != null && resultTo.userId == userId);
821 }
822
823 /**
824 * Find all visible task stacks containing {@param userId} and intercept them with an activity
825 * to block out the contents and possibly start a credential-confirming intent.
826 *
827 * @param userId user handle for the locked managed profile.
828 */
829 void lockAllProfileTasks(@UserIdInt int userId) {
830 mWindowManager.deferSurfaceLayout();
831 try {
832 final List<ActivityStack> stacks = getStacks();
833 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
834 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
835 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
836 final TaskRecord task = tasks.get(taskNdx);
837
838 // Check the task for a top activity belonging to userId, or returning a result
839 // to an activity belonging to userId. Example case: a document picker for
840 // personal files, opened by a work app, should still get locked.
841 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000842 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
843 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000844 }
Tony Mak853304c2016-04-18 15:17:41 +0100845 }
846 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000847 } finally {
848 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100849 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000850 }
851
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800852 void setNextTaskIdForUserLocked(int taskId, int userId) {
853 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
854 if (taskId > currentTaskId) {
855 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700856 }
857 }
858
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700859 static int nextTaskIdForUser(int taskId, int userId) {
860 int nextTaskId = taskId + 1;
861 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
862 // Wrap around as there will be smaller task ids that are available now.
863 nextTaskId -= MAX_TASK_IDS_PER_USER;
864 }
865 return nextTaskId;
866 }
867
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800868 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800869 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
870 // for a userId u, a taskId can only be in the range
871 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
872 // 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 -0700873 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800874 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800875 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800876 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700877 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800878 if (candidateTaskId == currentTaskId) {
879 // Something wrong!
880 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
881 throw new IllegalStateException("Cannot get an available task id."
882 + " Reached limit of " + MAX_TASK_IDS_PER_USER
883 + " running tasks per user.");
884 }
885 }
886 mCurTaskIdForUser.put(userId, candidateTaskId);
887 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700888 }
889
Chong Zhang6cda19c2016-06-14 19:07:56 -0700890 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800891 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700892 if (stack == null) {
893 return null;
894 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 ActivityRecord resumedActivity = stack.mResumedActivity;
896 if (resumedActivity == null || resumedActivity.app == null) {
897 resumedActivity = stack.mPausingActivity;
898 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700899 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700900 }
901 }
902 return resumedActivity;
903 }
904
Dianne Hackbornff072722014-09-24 10:56:28 -0700905 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700906 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800907 boolean didSomething = false;
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)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 continue;
914 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700915 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800916 if (hr != null) {
917 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
918 && processName.equals(hr.processName)) {
919 try {
George Mount2c92c972014-03-20 09:38:23 -0700920 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800921 didSomething = true;
922 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700923 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 Slog.w(TAG, "Exception in new application when starting activity "
925 + hr.intent.getComponent().flattenToShortString(), e);
926 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700927 }
Craig Mautner20e72272013-04-01 13:45:53 -0700928 }
Craig Mautner20e72272013-04-01 13:45:53 -0700929 }
930 }
931 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700932 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700933 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700934 }
Craig Mautner20e72272013-04-01 13:45:53 -0700935 return didSomething;
936 }
937
938 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
940 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800941 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
942 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700943 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800944 continue;
945 }
946 final ActivityRecord resumedActivity = stack.mResumedActivity;
947 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700948 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800949 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 return false;
951 }
Craig Mautner20e72272013-04-01 13:45:53 -0700952 }
953 }
Wei Wang65c7a152016-06-02 18:51:22 -0700954 // Send launch end powerhint when idle
955 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700956 return true;
957 }
958
Craig Mautnerde4ef022013-04-07 19:01:33 -0700959 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
961 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
963 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700964 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 return false;
968 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 }
970 }
971 }
972 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
975 mLastFocusedStack + " to=" + mFocusedStack);
976 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700977 return true;
978 }
979
980 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800981 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800982 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
983 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
985 final ActivityStack stack = stacks.get(stackNdx);
986 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800987 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700988 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800989 return false;
990 }
991 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700993 }
994 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800995 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 }
997
Craig Mautner2acc3892013-09-23 10:28:14 -0700998 /**
999 * Pause all activities in either all of the stacks or just the back stacks.
1000 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001001 * @param resuming The resuming activity.
1002 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1003 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001004 * @return true if any activity was paused as a result of this call.
1005 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001006 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001007 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1009 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001010 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1011 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001012 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001013 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001015 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1016 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001018 }
1019 }
1020 return someActivityPaused;
1021 }
1022
Craig Mautnerde4ef022013-04-07 19:01:33 -07001023 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001024 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001025 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1026 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001027 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1028 final ActivityStack stack = stacks.get(stackNdx);
1029 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001030 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001031 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001032 Slog.d(TAG_STATES,
1033 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 pausing = false;
1035 } else {
1036 return false;
1037 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001038 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 }
1040 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001041 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 }
1043
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001044 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001045 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001046 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001047 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1048 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1049 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1050 final ActivityStack stack = stacks.get(stackNdx);
1051 if (stack.mResumedActivity != null &&
1052 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001053 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001054 }
1055 }
1056 }
1057 }
1058
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001059 void cancelInitializingActivities() {
1060 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1061 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1062 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1063 stacks.get(stackNdx).cancelInitializingActivities();
1064 }
1065 }
1066 }
1067
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001069 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001070 }
1071
1072 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1073 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001074 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001075 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 if (w.who == null) {
1077 changed = true;
1078 w.timeout = false;
1079 if (r != null) {
1080 w.who = new ComponentName(r.info.packageName, r.info.name);
1081 }
1082 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1083 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001086 if (changed) {
1087 mService.notifyAll();
1088 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001089 }
1090
Chong Zhang5022da32016-06-21 16:31:37 -07001091 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1092 boolean changed = false;
1093 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1094 WaitResult w = mWaitingActivityLaunched.remove(i);
1095 if (w.who == null) {
1096 changed = true;
1097 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1098 // the starting activity ends up moving another activity to front, and it should
1099 // wait for this new activity to become visible instead.
1100 // Do not modify other fields.
1101 w.result = START_TASK_TO_FRONT;
1102 }
1103 }
1104 if (changed) {
1105 mService.notifyAll();
1106 }
1107 }
1108
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001109 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1110 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001111 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001112 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001113 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001114 if (w.who == null) {
1115 changed = true;
1116 w.timeout = timeout;
1117 if (r != null) {
1118 w.who = new ComponentName(r.info.packageName, r.info.name);
1119 }
1120 w.thisTime = thisTime;
1121 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001122 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001124 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001125 if (changed) {
1126 mService.notifyAll();
1127 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001128 }
1129
Craig Mautner29219d92013-04-16 20:19:12 -07001130 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001131 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001132 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001133 if (r != null) {
1134 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001135 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001136
Andrii Kulian7318d632016-07-20 18:59:28 -07001137 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001138 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1139
1140 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1141 final int displayId = mTmpOrderedDisplayIds.get(i);
1142 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1143 if (stacks == null) {
1144 continue;
1145 }
1146 for (int j = stacks.size() - 1; j >= 0; --j) {
1147 final ActivityStack stack = stacks.get(j);
1148 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1149 r = stack.topRunningActivityLocked();
1150 if (r != null) {
1151 return r;
1152 }
Craig Mautner29219d92013-04-16 20:19:12 -07001153 }
1154 }
1155 }
1156 return null;
1157 }
1158
Dianne Hackborn09233282014-04-30 11:33:59 -07001159 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001160 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001161 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1162 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001163 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001164 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001165 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001166 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1167 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001168 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001169 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001170 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001171 }
1172 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001173
1174 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1175 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1176 while (maxNum > 0) {
1177 long mostRecentActiveTime = Long.MIN_VALUE;
1178 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001179 final int numTaskLists = runningTaskLists.size();
1180 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1181 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001182 if (!stackTaskList.isEmpty()) {
1183 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1184 if (lastActiveTime > mostRecentActiveTime) {
1185 mostRecentActiveTime = lastActiveTime;
1186 selectedStackList = stackTaskList;
1187 }
1188 }
1189 }
1190 if (selectedStackList != null) {
1191 list.add(selectedStackList.remove(0));
1192 --maxNum;
1193 } else {
1194 break;
1195 }
1196 }
Craig Mautner20e72272013-04-01 13:45:53 -07001197 }
1198
Todd Kennedy7440f172015-12-09 14:31:22 -08001199 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1200 ProfilerInfo profilerInfo) {
1201 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001202 if (aInfo != null) {
1203 // Store the found target back into the intent, because now that
1204 // we have it we never want to do this again. For example, if the
1205 // user navigates back to this point in the history, we should
1206 // always restart the exact same activity.
1207 intent.setComponent(new ComponentName(
1208 aInfo.applicationInfo.packageName, aInfo.name));
1209
1210 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001211 if (!aInfo.processName.equals("system")) {
1212 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001213 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001214 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001215
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001216 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1217 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1218 }
1219
Man Caocfa78b22015-06-11 20:14:34 -07001220 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1221 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1222 }
1223
1224 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001225 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001226 }
1227 }
1228 }
1229 return aInfo;
1230 }
1231
Todd Kennedy7440f172015-12-09 14:31:22 -08001232 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001233 return resolveIntent(intent, resolvedType, userId, 0);
1234 }
1235
1236 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001237 synchronized (mService) {
1238 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001239 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001240 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001241 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001242 }
1243
1244 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1245 ProfilerInfo profilerInfo, int userId) {
1246 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1247 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1248 }
1249
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001250 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1251 boolean andResume, boolean checkConfig) throws RemoteException {
1252
1253 if (!allPausedActivitiesComplete()) {
1254 // While there are activities pausing we skipping starting any new activities until
1255 // pauses are complete. NOTE: that we also do this for activities that are starting in
1256 // the paused state because they will first be resumed then paused on the client side.
1257 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1258 "realStartActivityLocked: Skipping start of r=" + r
1259 + " some activities pausing...");
1260 return false;
1261 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001262
Andrii Kulianada10292017-02-09 23:19:29 -08001263 r.startFreezingScreenLocked(app, 0);
1264 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001265
Andrii Kulianada10292017-02-09 23:19:29 -08001266 // schedule launch ticks to collect information about slow apps.
1267 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001268
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001269 // Have the window manager re-evaluate the orientation of the screen based on the new
1270 // activity order. Note that as a result of this, it can call back into the activity
1271 // manager with a new orientation. We don't care about that, because the activity is not
1272 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001274 final int displayId = r.getDisplayId();
1275 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1276 getDisplayOverrideConfiguration(displayId),
1277 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001278 // Deferring resume here because we're going to launch new activity shortly.
1279 // We don't want to perform a redundant launch of the same record while ensuring
1280 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001281 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1282 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 }
1284
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001285 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001286 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001287 }
1288
Craig Mautner2420ead2013-04-01 17:13:20 -07001289 r.app = app;
1290 app.waitingToKill = null;
1291 r.launchCount++;
1292 r.lastLaunchTime = SystemClock.uptimeMillis();
1293
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001294 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001295
1296 int idx = app.activities.indexOf(r);
1297 if (idx < 0) {
1298 app.activities.add(r);
1299 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001300 mService.updateLruProcessLocked(app, true, null);
1301 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001302
Craig Mautner15df08a2015-04-01 12:17:18 -07001303 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001304 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1305 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001306 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001307 }
1308
Andrii Kulian02b7a832016-10-06 23:11:56 -07001309 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001310 try {
1311 if (app.thread == null) {
1312 throw new RemoteException();
1313 }
1314 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001315 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001317 // We don't need to deliver new intents and/or set results if activity is going
1318 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 results = r.results;
1320 newIntents = r.newIntents;
1321 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001322 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1323 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1324 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001325 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1326 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001327 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001328 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001329 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001331 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1332 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 r.sleeping = false;
1334 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001335 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 mService.showAskCompatModeDialogLocked(r);
1337 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001338 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001339 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1340 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1341 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001342 final String profileFile = mService.mProfileFile;
1343 if (profileFile != null) {
1344 ParcelFileDescriptor profileFd = mService.mProfileFd;
1345 if (profileFd != null) {
1346 try {
1347 profileFd = profileFd.dup();
1348 } catch (IOException e) {
1349 if (profileFd != null) {
1350 try {
1351 profileFd.close();
1352 } catch (IOException o) {
1353 }
1354 profileFd = null;
1355 }
1356 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001357 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001358
1359 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001360 mService.mSamplingInterval, mService.mAutoStopProfiler,
1361 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 }
1363 }
1364 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001365
Andrii Kulianada10292017-02-09 23:19:29 -08001366 app.hasShownUi = true;
1367 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001368 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001369 // Because we could be starting an Activity in the system process this may not go across
1370 // a Binder interface which would create a new Configuration. Consequently we have to
1371 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001372
1373 final Configuration globalConfiguration =
1374 new Configuration(mService.getGlobalConfiguration());
1375 r.setLastReportedGlobalConfiguration(globalConfiguration);
1376 final Configuration mergedOverrideConfiguration =
1377 new Configuration(task.getMergedOverrideConfiguration());
1378 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1379
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001381 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001382 globalConfiguration,
1383 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001384 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1385 r.persistentState, results, newIntents, !andResume,
1386 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001387
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001388 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001389 // This may be a heavy-weight process! Note that the package
1390 // manager will ensure that only activity can run in the main
1391 // process of the .apk, which is the only thing that will be
1392 // considered heavy-weight.
1393 if (app.processName.equals(app.info.packageName)) {
1394 if (mService.mHeavyWeightProcess != null
1395 && mService.mHeavyWeightProcess != app) {
1396 Slog.w(TAG, "Starting new heavy weight process " + app
1397 + " when already running "
1398 + mService.mHeavyWeightProcess);
1399 }
1400 mService.mHeavyWeightProcess = app;
1401 Message msg = mService.mHandler.obtainMessage(
1402 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1403 msg.obj = r;
1404 mService.mHandler.sendMessage(msg);
1405 }
1406 }
1407
1408 } catch (RemoteException e) {
1409 if (r.launchFailed) {
1410 // This is the second time we failed -- finish activity
1411 // and give up.
1412 Slog.e(TAG, "Second failure launching "
1413 + r.intent.getComponent().flattenToShortString()
1414 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001415 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001416 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1417 "2nd-crash", false);
1418 return false;
1419 }
1420
1421 // This is the first time we failed -- restart process and
1422 // retry.
1423 app.activities.remove(r);
1424 throw e;
1425 }
1426
1427 r.launchFailed = false;
1428 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001429 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001430 }
1431
1432 if (andResume) {
1433 // As part of the process of launching, ActivityThread also performs
1434 // a resume.
1435 stack.minimalResumeActivityLocked(r);
1436 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001437 // This activity is not starting in the resumed state... which should look like we asked
1438 // it to pause+stop (but remain visible), and it has done so and reported back the
1439 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001440 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001441 "Moving to PAUSED: " + r + " (starting in paused state)");
1442 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001443 }
1444
1445 // Launch the new version setup screen if needed. We do this -after-
1446 // launching the initial activity (that is, home), so that it can have
1447 // a chance to initialize itself while in the background, making the
1448 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001449 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001450 mService.startSetupActivityLocked();
1451 }
1452
Dianne Hackborn465fa392014-09-14 14:21:18 -07001453 // Update any services we are bound to that might care about whether
1454 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001455 if (r.app != null) {
1456 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1457 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001458
Craig Mautner2420ead2013-04-01 17:13:20 -07001459 return true;
1460 }
1461
Craig Mautnere79d42682013-04-01 19:01:53 -07001462 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001463 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001464 // Is this activity's application already running?
1465 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001466 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001467
Andrii Kulian02b7a832016-10-06 23:11:56 -07001468 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001469
1470 if (app != null && app.thread != null) {
1471 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001472 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1473 || !"android".equals(r.info.packageName)) {
1474 // Don't add this if it is a platform component that is marked
1475 // to run in multiple processes, because this is actually
1476 // part of the framework so doesn't make sense to track as a
1477 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001478 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1479 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001480 }
George Mount2c92c972014-03-20 09:38:23 -07001481 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001482 return;
1483 } catch (RemoteException e) {
1484 Slog.w(TAG, "Exception when starting activity "
1485 + r.intent.getComponent().flattenToShortString(), e);
1486 }
1487
1488 // If a dead object exception was thrown -- fall through to
1489 // restart the application.
1490 }
1491
1492 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001493 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001494 }
1495
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001496 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001497 String resultWho, int requestCode, int callingPid, int callingUid,
1498 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001499 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001500 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1501 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001502 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001503 return true;
1504 }
1505 final int componentRestriction = getComponentRestrictionForCallingPackage(
1506 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1507 final int actionRestriction = getActionRestrictionForCallingPackage(
1508 intent.getAction(), callingPackage, callingPid, callingUid);
1509 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1510 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1511 if (resultRecord != null) {
1512 resultStack.sendActivityResultLocked(-1,
1513 resultRecord, resultWho, requestCode,
1514 Activity.RESULT_CANCELED, null);
1515 }
1516 final String msg;
1517 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1518 msg = "Permission Denial: starting " + intent.toString()
1519 + " from " + callerApp + " (pid=" + callingPid
1520 + ", uid=" + callingUid + ")" + " with revoked permission "
1521 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1522 } else if (!aInfo.exported) {
1523 msg = "Permission Denial: starting " + intent.toString()
1524 + " from " + callerApp + " (pid=" + callingPid
1525 + ", uid=" + callingUid + ")"
1526 + " not exported from uid " + aInfo.applicationInfo.uid;
1527 } else {
1528 msg = "Permission Denial: starting " + intent.toString()
1529 + " from " + callerApp + " (pid=" + callingPid
1530 + ", uid=" + callingUid + ")"
1531 + " requires " + aInfo.permission;
1532 }
1533 Slog.w(TAG, msg);
1534 throw new SecurityException(msg);
1535 }
1536
1537 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1538 final String message = "Appop Denial: starting " + intent.toString()
1539 + " from " + callerApp + " (pid=" + callingPid
1540 + ", uid=" + callingUid + ")"
1541 + " requires " + AppOpsManager.permissionToOp(
1542 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1543 Slog.w(TAG, message);
1544 return false;
1545 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1546 final String message = "Appop Denial: starting " + intent.toString()
1547 + " from " + callerApp + " (pid=" + callingPid
1548 + ", uid=" + callingUid + ")"
1549 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1550 Slog.w(TAG, message);
1551 return false;
1552 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001553 if (options != null) {
1554 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1555 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1556 callingPid, callingUid);
1557 if (startInTaskPerm != PERMISSION_GRANTED) {
1558 final String msg = "Permission Denial: starting " + intent.toString()
1559 + " from " + callerApp + " (pid=" + callingPid
1560 + ", uid=" + callingUid + ") with launchTaskId="
1561 + options.getLaunchTaskId();
1562 Slog.w(TAG, msg);
1563 throw new SecurityException(msg);
1564 }
1565 }
1566 // Check if someone tries to launch an activity on a private display with a different
1567 // owner.
1568 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001569 if (launchDisplayId != INVALID_DISPLAY
1570 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1571 final String msg = "Permission Denial: starting " + intent.toString()
1572 + " from " + callerApp + " (pid=" + callingPid
1573 + ", uid=" + callingUid + ") with launchDisplayId="
1574 + launchDisplayId;
1575 Slog.w(TAG, msg);
1576 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001577 }
1578 }
1579
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001580 return true;
1581 }
1582
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001583 /** Check if caller is allowed to launch activities on specified display. */
1584 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1585 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1586 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1587
1588 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1589 if (activityDisplay == null) {
1590 Slog.w(TAG, "Launch on display check: display not found");
1591 return false;
1592 }
1593
1594 if (!activityDisplay.isPrivate()) {
1595 // Anyone can launch on a public display.
1596 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1597 + " allow launch on public display");
1598 return true;
1599 }
1600
1601 // Check if the caller is the owner of the display.
1602 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1603 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1604 + " allow launch for owner of the display");
1605 return true;
1606 }
1607
1608 // Check if caller is present on display
1609 if (activityDisplay.isUidPresent(callingUid)) {
1610 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1611 + " allow launch for caller present on the display");
1612 return true;
1613 }
1614
1615 // Check if the caller can launch anything.
1616 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1617 callingUid);
1618 if (startAnyPerm == PERMISSION_GRANTED) {
1619 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1620 + " allow launch any on display");
1621 return true;
1622 }
1623
1624 Slog.w(TAG, "Launch on display check: denied");
1625 return false;
1626 }
1627
1628 /** Update lists of UIDs that are present on displays and have access to them. */
1629 void updateUIDsPresentOnDisplay() {
1630 mDisplayAccessUIDs.clear();
1631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1632 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1633 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1634 }
1635 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1636 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1637 }
1638
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001639 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001640 final long identity = Binder.clearCallingIdentity();
1641 try {
1642 return UserManager.get(mService.mContext).getUserInfo(userId);
1643 } finally {
1644 Binder.restoreCallingIdentity(identity);
1645 }
1646 }
1647
Svet Ganov99b60432015-06-27 13:15:22 -07001648 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001649 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001650 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1651 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001652 == PackageManager.PERMISSION_DENIED) {
1653 return ACTIVITY_RESTRICTION_PERMISSION;
1654 }
1655
Christopher Tateff7add02015-08-17 10:23:22 -07001656 if (activityInfo.permission == null) {
1657 return ACTIVITY_RESTRICTION_NONE;
1658 }
1659
Svet Ganov99b60432015-06-27 13:15:22 -07001660 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1661 if (opCode == AppOpsManager.OP_NONE) {
1662 return ACTIVITY_RESTRICTION_NONE;
1663 }
1664
1665 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1666 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001667 if (!ignoreTargetSecurity) {
1668 return ACTIVITY_RESTRICTION_APPOP;
1669 }
Svet Ganov99b60432015-06-27 13:15:22 -07001670 }
1671
1672 return ACTIVITY_RESTRICTION_NONE;
1673 }
1674
Svetoslav7008b512015-06-24 18:47:07 -07001675 private int getActionRestrictionForCallingPackage(String action,
1676 String callingPackage, int callingPid, int callingUid) {
1677 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001678 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001679 }
1680
1681 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1682 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001683 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001684 }
1685
1686 final PackageInfo packageInfo;
1687 try {
1688 packageInfo = mService.mContext.getPackageManager()
1689 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1690 } catch (PackageManager.NameNotFoundException e) {
1691 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001692 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001693 }
1694
1695 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001696 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001697 }
1698
1699 if (mService.checkPermission(permission, callingPid, callingUid) ==
1700 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001701 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001702 }
1703
1704 final int opCode = AppOpsManager.permissionToOpCode(permission);
1705 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001706 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001707 }
1708
1709 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1710 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001711 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001712 }
1713
Svet Ganov99b60432015-06-27 13:15:22 -07001714 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001715 }
1716
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001717 void setLaunchSource(int uid) {
1718 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1719 }
1720
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001721 void acquireLaunchWakelock() {
1722 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1723 throw new IllegalStateException("Calling must be system uid");
1724 }
1725 mLaunchingActivity.acquire();
1726 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1727 // To be safe, don't allow the wake lock to be held for too long.
1728 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1729 }
1730 }
1731
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001732 /**
1733 * Called when the frontmost task is idle.
1734 * @return the state of mService.mBooting before this was called.
1735 */
1736 private boolean checkFinishBootingLocked() {
1737 final boolean booting = mService.mBooting;
1738 boolean enableScreen = false;
1739 mService.mBooting = false;
1740 if (!mService.mBooted) {
1741 mService.mBooted = true;
1742 enableScreen = true;
1743 }
1744 if (booting || enableScreen) {
1745 mService.postFinishBooting(booting, enableScreen);
1746 }
1747 return booting;
1748 }
1749
Craig Mautnerf3333272013-04-22 10:55:53 -07001750 // Checked.
1751 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001752 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001753 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001754
Craig Mautnerf3333272013-04-22 10:55:53 -07001755 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001756 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001757 int NS = 0;
1758 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001759 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001760 boolean activityRemoved = false;
1761
Wale Ogunwale7d701172015-03-11 15:36:30 -07001762 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001763 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001764 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1765 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001766 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1767 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001768 if (fromTimeout) {
1769 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001770 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001771
1772 // This is a hack to semi-deal with a race condition
1773 // in the client where it can be constructed with a
1774 // newer configuration from when we asked it to launch.
1775 // We'll update with whatever configuration it now says
1776 // it used to launch.
1777 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001778 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001779 }
1780
1781 // We are now idle. If someone is waiting for a thumbnail from
1782 // us, we can now deliver.
1783 r.idle = true;
1784
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001785 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001786 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001787 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001788 }
1789 }
1790
1791 if (allResumedActivitiesIdle()) {
1792 if (r != null) {
1793 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001794 }
1795
1796 if (mLaunchingActivity.isHeld()) {
1797 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1798 if (VALIDATE_WAKE_LOCK_CALLER &&
1799 Binder.getCallingUid() != Process.myUid()) {
1800 throw new IllegalStateException("Calling must be system uid");
1801 }
1802 mLaunchingActivity.release();
1803 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001804 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001805 }
1806
1807 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001808 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1809 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001810 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001811 if ((NF = mFinishingActivities.size()) > 0) {
1812 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001813 mFinishingActivities.clear();
1814 }
1815
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001817 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 mStartingUsers.clear();
1819 }
1820
Craig Mautnerf3333272013-04-22 10:55:53 -07001821 // Stop any activities that are scheduled to do so but have been
1822 // waiting for the next one to start.
1823 for (int i = 0; i < NS; i++) {
1824 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001825 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001826 if (stack != null) {
1827 if (r.finishing) {
1828 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1829 } else {
1830 stack.stopActivityLocked(r);
1831 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 }
1833 }
1834
1835 // Finish any activities that are scheduled to do so but have been
1836 // waiting for the next one to start.
1837 for (int i = 0; i < NF; i++) {
1838 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001839 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001840 if (stack != null) {
1841 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1842 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001843 }
1844
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001845 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001846 // Complete user switch
1847 if (startingUsers != null) {
1848 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001849 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001850 }
1851 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001852 }
1853
1854 mService.trimApplications();
1855 //dump();
1856 //mWindowManager.dump();
1857
Craig Mautnerf3333272013-04-22 10:55:53 -07001858 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001859 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001860 }
1861
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001862 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001863 }
1864
Craig Mautner8e569572013-10-11 17:36:59 -07001865 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001866 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001867 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1868 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001869 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1870 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1871 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001872 }
Craig Mautner19091252013-10-05 00:03:53 -07001873 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001874 }
1875
1876 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001877 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1878 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001879 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1880 stacks.get(stackNdx).closeSystemDialogsLocked();
1881 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001882 }
1883 }
1884
Craig Mautner93529a42013-10-04 15:03:13 -07001885 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001886 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001887 }
1888
Craig Mautner8d341ef2013-03-26 09:03:27 -07001889 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001890 * Update the last used stack id for non-current user (current user's last
1891 * used stack is the focused stack)
1892 */
1893 void updateUserStackLocked(int userId, ActivityStack stack) {
1894 if (userId != mCurrentUser) {
1895 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1896 }
1897 }
1898
1899 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001900 * @return true if some activity was finished (or would have finished if doit were true).
1901 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001902 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1903 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001904 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1906 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001907 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001908 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001909 if (stack.finishDisabledPackageActivitiesLocked(
1910 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001911 didSomething = true;
1912 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001913 }
1914 }
1915 return didSomething;
1916 }
1917
Dianne Hackborna413dc02013-07-12 12:02:55 -07001918 void updatePreviousProcessLocked(ActivityRecord r) {
1919 // Now that this process has stopped, we may want to consider
1920 // it to be the previous app to try to keep around in case
1921 // the user wants to return to it.
1922
1923 // First, found out what is currently the foreground app, so that
1924 // we don't blow away the previous app if this activity is being
1925 // hosted by the process that is actually still the foreground.
1926 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1930 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001931 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001932 if (stack.mResumedActivity != null) {
1933 fgApp = stack.mResumedActivity.app;
1934 } else if (stack.mPausingActivity != null) {
1935 fgApp = stack.mPausingActivity.app;
1936 }
1937 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001938 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001939 }
1940 }
1941
1942 // Now set this one as the previous process, only if that really
1943 // makes sense to.
1944 if (r.app != null && fgApp != null && r.app != fgApp
1945 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001946 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001947 mService.mPreviousProcess = r.app;
1948 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1949 }
1950 }
1951
Wale Ogunwaled046a012015-12-24 13:05:59 -08001952 boolean resumeFocusedStackTopActivityLocked() {
1953 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001954 }
1955
Wale Ogunwaled046a012015-12-24 13:05:59 -08001956 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001957 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001958 if (targetStack != null && isFocusedStack(targetStack)) {
1959 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001960 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001961 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1962 if (r == null || r.state != RESUMED) {
1963 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001964 } else if (r.state == RESUMED) {
1965 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1966 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001967 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001968 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001969 }
1970
Todd Kennedy39bfee52016-02-24 10:28:21 -08001971 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1973 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1974 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1975 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1976 }
1977 }
1978 }
1979
Adrian Roos20d7df32016-01-12 18:59:43 +01001980 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1981 TaskRecord finishedTask = null;
1982 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001983 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1984 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001985 final int numStacks = stacks.size();
1986 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1987 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001988 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1989 if (stack == focusedStack || finishedTask == null) {
1990 finishedTask = t;
1991 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001992 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001993 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001994 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001995 }
1996
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001997 void finishVoiceTask(IVoiceInteractionSession session) {
1998 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1999 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2000 final int numStacks = stacks.size();
2001 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2002 final ActivityStack stack = stacks.get(stackNdx);
2003 stack.finishVoiceTask(session);
2004 }
2005 }
2006 }
2007
Andrii Kulianc27916642016-04-12 17:59:27 -07002008 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2009 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002010 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2011 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002012 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002013 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2014 // Caller wants the home activity moved with it. To accomplish this,
2015 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002016 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002017 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002018 ActivityStack currentStack = task.getStack();
2019 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002020 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2021 + task + " to front. Stack is null");
2022 return;
2023 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002024
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002025 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002026 int stackId = options.getLaunchStackId();
2027 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002028 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002029 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002030 if (stackId == INVALID_STACK_ID) {
2031 stackId = task.getLaunchStackId();
2032 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002033 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002034 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2035 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002036 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002037 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2038 // still need moveTaskToFrontLocked() below for any transition settings.
2039 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002040 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2041 resizeStackLocked(stackId, bounds,
2042 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002043 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002044 } else {
2045 // WM resizeTask must be done after the task is moved to the correct stack,
2046 // because Task's setBounds() also updates dim layer's bounds, but that has
2047 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002048 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002049 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002050 }
2051 }
2052
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002053 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002054 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002055 r == null ? null : r.appTimeTracker, reason);
2056
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002057 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002058 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002059
Andrii Kulian02b7a832016-10-06 23:11:56 -07002060 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002061 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002062 }
2063
Wale Ogunwale854809c2015-12-27 16:18:19 -08002064 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002065 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002066 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002067 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002068 return false;
2069 }
2070 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2071 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002072 }
2073
Winson Chung55893332017-02-17 17:13:10 -08002074 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002075 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002076 }
2077
Winson Chung55893332017-02-17 17:13:10 -08002078 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2079 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002080 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002081 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002082 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002083 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002084 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002085 return null;
2086 }
Matthew Ng330757d2017-02-28 14:19:17 -08002087 if (stackId == DOCKED_STACK_ID) {
2088 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2089 // the other side of the docked stack and we make visibility decisions based on that.
2090 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2091 }
Winson Chung55893332017-02-17 17:13:10 -08002092 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002093 }
2094
Andrii Kulian16802aa2016-11-02 12:21:33 -07002095 /**
2096 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2097 * If there is no such stack, new dynamic stack can be created.
2098 * @param displayId Target display.
2099 * @param r Activity that should be launched there.
2100 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2101 */
2102 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2103 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2104 if (activityDisplay == null) {
2105 throw new IllegalArgumentException(
2106 "Display with displayId=" + displayId + " not found.");
2107 }
2108
2109 // Return the topmost valid stack on the display.
2110 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2111 final ActivityStack stack = activityDisplay.mStacks.get(i);
2112 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2113 return stack;
2114 }
2115 }
2116
2117 // If there is no valid stack on the external display - check if new dynamic stack will do.
2118 if (displayId != Display.DEFAULT_DISPLAY) {
2119 final int newDynamicStackId = getNextStackId();
2120 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2121 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2122 }
2123 }
2124
2125 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2126 return null;
2127 }
2128
Craig Mautner967212c2013-04-13 21:10:58 -07002129 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002130 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2132 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002133 }
2134 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002135 }
2136
Jorim Jaggife762342016-10-13 14:33:27 +02002137 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2138 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2139 }
2140
Andrii Kulian7fc22812016-12-28 13:04:11 -08002141 /**
2142 * Get next focusable stack in the system. This will search across displays and stacks
2143 * in last-focused order for a focusable and visible stack, different from the target stack.
2144 *
2145 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2146 * searching for next candidate.
2147 * @return Next focusable {@link ActivityStack}, null if not found.
2148 */
2149 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2150 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2151
2152 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2153 final int displayId = mTmpOrderedDisplayIds.get(i);
2154 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2155 if (stacks == null) {
2156 continue;
2157 }
2158 for (int j = stacks.size() - 1; j >= 0; --j) {
2159 final ActivityStack stack = stacks.get(j);
2160 if (stack != currentFocus && stack.isFocusable()
2161 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2162 return stack;
2163 }
2164 }
2165 }
2166
2167 return null;
2168 }
2169
Craig Mautneree2e45a2014-06-27 12:10:03 -07002170 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002171 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002172 }
2173
2174 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002175 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2176 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2177 final TaskRecord task = tasks.get(taskNdx);
2178 if (task.isHomeTask()) {
2179 final ArrayList<ActivityRecord> activities = task.mActivities;
2180 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2181 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002182 if (r.isHomeActivity()
2183 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002184 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002185 }
2186 }
2187 }
2188 }
2189 return null;
2190 }
2191
Chong Zhangb15758a2015-11-17 12:12:03 -08002192 /**
2193 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2194 * the docked stack itself, or if it's side-by-side to the docked stack.
2195 */
2196 boolean isStackDockedInEffect(int stackId) {
2197 return stackId == DOCKED_STACK_ID ||
2198 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2199 }
2200
Todd Kennedyca4d8422015-01-15 15:19:22 -08002201 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002202 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002203 ActivityContainer activityContainer =
2204 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002205 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002206 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2207 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002208 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002209 return activityContainer;
2210 }
2211
Craig Mautner34b73df2014-01-12 21:11:08 -08002212 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002213 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2214 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2215 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002216 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2217 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002218 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002219 }
2220 }
2221
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002222 void deleteActivityContainerRecord(int stackId) {
2223 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2224 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2225 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002226 }
2227
Jorim Jaggidc249c42015-12-15 14:57:31 -08002228 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002229 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002230 if (stackId == DOCKED_STACK_ID) {
2231 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002232 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002233 return;
2234 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002235 final ActivityStack stack = getStack(stackId);
2236 if (stack == null) {
2237 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2238 return;
2239 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002240
Jorim Jaggidc249c42015-12-15 14:57:31 -08002241 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002242 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2243 // stack size changing so things don't get out of sync.
2244 return;
2245 }
2246
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002247 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002248 mWindowManager.deferSurfaceLayout();
2249 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002250 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002251 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002252 stack.ensureVisibleActivitiesConfigurationLocked(
2253 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002254 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002255 } finally {
2256 mWindowManager.continueSurfaceLayout();
2257 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002258 }
2259 }
2260
Jorim Jaggi192086e2016-03-11 17:17:03 +01002261 void deferUpdateBounds(int stackId) {
2262 final ActivityStack stack = getStack(stackId);
2263 if (stack != null) {
2264 stack.deferUpdateBounds();
2265 }
2266 }
2267
2268 void continueUpdateBounds(int stackId) {
2269 final ActivityStack stack = getStack(stackId);
2270 if (stack != null) {
2271 stack.continueUpdateBounds();
2272 }
2273 }
2274
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002275 void notifyAppTransitionDone() {
2276 continueUpdateBounds(HOME_STACK_ID);
2277 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2278 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002279 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002280 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002281 if (task != null) {
2282 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002283 }
2284 }
2285 mResizingTasksDuringAnimation.clear();
2286 }
2287
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002288 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2289 final ActivityStack stack = getStack(fromStackId);
2290 if (stack == null) {
2291 return;
2292 }
2293
2294 mWindowManager.deferSurfaceLayout();
2295 try {
2296 if (fromStackId == DOCKED_STACK_ID) {
2297
2298 // We are moving all tasks from the docked stack to the fullscreen stack,
2299 // which is dismissing the docked stack, so resize all other stacks to
2300 // fullscreen here already so we don't end up with resize trashing.
2301 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2302 if (StackId.isResizeableByDockedStack(i)) {
2303 ActivityStack otherStack = getStack(i);
2304 if (otherStack != null) {
2305 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2306 true /* allowResizeInDockedMode */, DEFER_RESUME);
2307 }
2308 }
2309 }
2310
2311 // Also disable docked stack resizing since we have manually adjusted the
2312 // size of other stacks above and we don't want to trigger a docked stack
2313 // resize when we remove task from it below and it is detached from the
2314 // display because it no longer contains any tasks.
2315 mAllowDockedStackResize = false;
2316 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002317 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002318 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2319 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002320 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2321 final int size = tasks.size();
2322 if (onTop) {
2323 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002324 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002325 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002326 if (fromStackId == PINNED_STACK_ID) {
2327 // Update the return-to to reflect where the pinned stack task was moved
2328 // from so that we retain the stack that was previously visible if the
2329 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002330 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2331 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002332 MetricsLogger.action(mService.mContext,
2333 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002334 }
Winson Chung74666102017-02-22 17:49:24 -08002335 // Defer resume until all the tasks have been moved to the fullscreen stack
2336 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2337 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
2338 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002339 }
2340 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002341 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002342 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002343 final int position = fullscreenStack != null
2344 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2345 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002346 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung74666102017-02-22 17:49:24 -08002347 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
2348 DEFER_RESUME, "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002349 }
2350 }
Winson Chung74666102017-02-22 17:49:24 -08002351
2352 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2353 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002354 } finally {
2355 mAllowDockedStackResize = true;
2356 mWindowManager.continueSurfaceLayout();
2357 }
2358 }
2359
Jorim Jaggidc249c42015-12-15 14:57:31 -08002360 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002361 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2362 boolean preserveWindows) {
2363 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2364 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2365 false /* deferResume */);
2366 }
2367
2368 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2369 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2370 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002371
2372 if (!mAllowDockedStackResize) {
2373 // Docked stack resize currently disabled.
2374 return;
2375 }
2376
Jorim Jaggidc249c42015-12-15 14:57:31 -08002377 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2378 if (stack == null) {
2379 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2380 return;
2381 }
2382
2383 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2384 mWindowManager.deferSurfaceLayout();
2385 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002386 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2387 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002388 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002389 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002390
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002391 // TODO: Checking for isAttached might not be needed as if the user passes in null
2392 // dockedBounds then they want the docked stack to be dismissed.
2393 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2394 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002395 // In this case we make all other static stacks fullscreen and move all
2396 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002397 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002398
2399 // stack shouldn't contain anymore activities, so nothing to resume.
2400 r = null;
2401 } else {
2402 // Docked stacks occupy a dedicated region on screen so the size of all other
2403 // static stacks need to be adjusted so they don't overlap with the docked stack.
2404 // We get the bounds to use from window manager which has been adjusted for any
2405 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002406 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002407 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002408 final ActivityStack current = getStack(i);
2409 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002410 current.getStackDockedModeBounds(
2411 tempOtherTaskBounds /* currentTempTaskBounds */,
2412 tempRect /* outStackBounds */,
2413 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2414
Matthew Nge15352e2016-12-20 15:36:29 -08002415 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002416 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2417 tempOtherTaskInsetBounds, preserveWindows,
2418 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002419 }
2420 }
2421 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002422 if (!deferResume) {
2423 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2424 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002425 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002426 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002427 mWindowManager.continueSurfaceLayout();
2428 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2429 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002430 }
2431
Robert Carr0d00c2e2016-02-29 17:45:02 -08002432 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2433 final ActivityStack stack = getStack(PINNED_STACK_ID);
2434 if (stack == null) {
2435 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2436 return;
2437 }
2438 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2439 mWindowManager.deferSurfaceLayout();
2440 try {
2441 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002442 Rect insetBounds = null;
2443 if (tempPinnedTaskBounds != null) {
2444 // We always use 0,0 as the position for the inset rect because
2445 // if we are getting insets at all in the pinned stack it must mean
2446 // we are headed for fullscreen.
2447 insetBounds = tempRect;
2448 insetBounds.top = 0;
2449 insetBounds.left = 0;
2450 insetBounds.right = tempPinnedTaskBounds.width();
2451 insetBounds.bottom = tempPinnedTaskBounds.height();
2452 }
2453 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002454 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002455 } finally {
2456 mWindowManager.continueSurfaceLayout();
2457 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2458 }
2459 }
2460
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002461 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002462 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002463 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002464 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002465 }
2466
Wale Ogunwale1666e312016-12-16 11:27:18 -08002467 final ActivityContainer activityContainer =
2468 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002469 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002470 }
2471
Winson Chung010927a2016-12-15 16:12:35 -08002472 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002473 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002474 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2475 * instead moved back onto the fullscreen stack.
2476 */
2477 void removeStackLocked(int stackId) {
2478 final ActivityStack stack = getStack(stackId);
2479 if (stack == null) {
2480 return;
2481 }
2482
2483 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2484 if (stack.getStackId() == PINNED_STACK_ID) {
2485 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2486 if (fullscreenStack != null) {
2487 final boolean isFullscreenStackVisible =
2488 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2489 for (int i = 0; i < tasks.size(); i++) {
2490 // Insert the task either at the top of the fullscreen stack if it is hidden,
2491 // or just under the top task if it is currently visible
2492 final int insertPosition = isFullscreenStackVisible
2493 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2494 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002495 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002496 // Defer resume until we remove all the tasks
2497 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2498 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002499 }
2500 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2501 resumeFocusedStackTopActivityLocked();
2502 } else {
2503 // If there is no fullscreen stack, then create the stack and move all the tasks
2504 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002505 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002506 }
2507 } else {
2508 for (int i = tasks.size() - 1; i >= 0; i--) {
2509 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2510 REMOVE_FROM_RECENTS);
2511 }
2512 }
2513 }
2514
2515 /**
2516 * Removes the task with the specified task id.
2517 *
2518 * @param taskId Identifier of the task to be removed.
2519 * @param killProcess Kill any process associated with the task if possible.
2520 * @param removeFromRecents Whether to also remove the task from recents.
2521 * @return Returns true if the given task was found and removed.
2522 */
2523 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
Winson Chung7900bee2017-03-10 08:59:25 -08002524 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2525 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002526 if (tr != null) {
2527 tr.removeTaskActivitiesLocked();
2528 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2529 if (tr.isPersistable) {
2530 mService.notifyTaskPersisterLocked(null, true);
2531 }
2532 return true;
2533 }
2534 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2535 return false;
2536 }
2537
2538 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2539 if (removeFromRecents) {
2540 mRecentTasks.remove(tr);
2541 tr.removedFromRecents();
2542 }
2543 ComponentName component = tr.getBaseIntent().getComponent();
2544 if (component == null) {
2545 Slog.w(TAG, "No component for base intent of task: " + tr);
2546 return;
2547 }
2548
2549 // Find any running services associated with this app and stop if needed.
2550 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2551
2552 if (!killProcess) {
2553 return;
2554 }
2555
2556 // Determine if the process(es) for this task should be killed.
2557 final String pkg = component.getPackageName();
2558 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2559 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2560 for (int i = 0; i < pmap.size(); i++) {
2561
2562 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2563 for (int j = 0; j < uids.size(); j++) {
2564 ProcessRecord proc = uids.valueAt(j);
2565 if (proc.userId != tr.userId) {
2566 // Don't kill process for a different user.
2567 continue;
2568 }
2569 if (proc == mService.mHomeProcess) {
2570 // Don't kill the home process along with tasks from the same package.
2571 continue;
2572 }
2573 if (!proc.pkgList.containsKey(pkg)) {
2574 // Don't kill process that is not associated with this task.
2575 continue;
2576 }
2577
2578 for (int k = 0; k < proc.activities.size(); k++) {
2579 TaskRecord otherTask = proc.activities.get(k).task;
2580 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2581 // Don't kill process(es) that has an activity in a different task that is
2582 // also in recents.
2583 return;
2584 }
2585 }
2586
2587 if (proc.foregroundServices) {
2588 // Don't kill process(es) with foreground service.
2589 return;
2590 }
2591
2592 // Add process to kill list.
2593 procsToKill.add(proc);
2594 }
2595 }
2596
2597 // Kill the running processes.
2598 for (int i = 0; i < procsToKill.size(); i++) {
2599 ProcessRecord pr = procsToKill.get(i);
2600 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2601 && pr.curReceivers.isEmpty()) {
2602 pr.kill("remove task", true);
2603 } else {
2604 // We delay killing processes that are not in the background or running a receiver.
2605 pr.waitingToKill = "remove task";
2606 }
2607 }
2608 }
2609
Craig Mautner4a1cb222013-12-04 16:14:06 -08002610 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002611 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002612 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2613 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002614 break;
2615 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002616 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002617 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002618 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002619 }
2620
Chong Zhang5dcb2752015-08-18 13:50:26 -07002621 /**
2622 * Restores a recent task to a stack
2623 * @param task The recent task to be restored.
2624 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002625 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2626 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002627 * @return true if the task has been restored successfully.
2628 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002629 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002630 if (!StackId.isStaticStack(stackId)) {
2631 // If stack is not static (or stack id is invalid) - use the default one.
2632 // This means that tasks that were on external displays will be restored on the
2633 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002634 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002635 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002636 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2637 // Put it in the fullscreen stack.
2638 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002639 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002640
Andrii Kulian02b7a832016-10-06 23:11:56 -07002641 final ActivityStack currentStack = task.getStack();
2642 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002643 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002644 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002645 // Nothing else to do since it is already restored in the right stack.
2646 return true;
2647 }
2648 // Remove current stack association, so we can re-associate the task with the
2649 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002650 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002651 }
2652
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002653 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002654 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002655
2656 if (stack == null) {
2657 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002658 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2659 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002660 return false;
2661 }
2662
Wale Ogunwale72919d22016-12-08 18:58:50 -08002663 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002664 // TODO: move call for creation here and other place into Stack.addTask()
2665 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002666 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2667 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002668 final ArrayList<ActivityRecord> activities = task.mActivities;
2669 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002670 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002671 }
2672 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002673 }
2674
Wale Ogunwale040b4702015-08-06 18:10:50 -07002675 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002676 * Move stack with all its existing content to specified display.
2677 * @param stackId Id of stack to move.
2678 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002679 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002680 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002681 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002682 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2683 if (activityDisplay == null) {
2684 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2685 + displayId);
2686 }
2687 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2688 if (activityContainer != null) {
2689 if (activityContainer.isAttachedLocked()) {
2690 if (activityContainer.getDisplayId() == displayId) {
2691 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2692 + " to its current displayId=" + displayId);
2693 }
2694
Andrii Kulian250d6532017-02-08 23:30:45 -08002695 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002696 } else {
2697 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2698 + stackId + " is not attached to any display.");
2699 }
2700 } else {
2701 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2702 + stackId);
2703 }
2704
2705 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2706 !PRESERVE_WINDOWS);
2707 // TODO(multi-display): resize stacks properly if moved from split-screen.
2708 }
2709
2710 /**
Winson Chung74666102017-02-22 17:49:24 -08002711 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2712 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002713 */
Winson Chung74666102017-02-22 17:49:24 -08002714 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002715 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002716
Winson Chung74666102017-02-22 17:49:24 -08002717 // Check that we aren't reparenting to the same stack that the task is already in
2718 if (prevStack != null && prevStack.mStackId == stackId) {
2719 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2720 + " already in stackId=" + stackId);
2721 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002722 }
2723
Winson Chung74666102017-02-22 17:49:24 -08002724 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2725 // support
2726 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2727 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2728 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002729 }
2730
Winson Chung74666102017-02-22 17:49:24 -08002731 // Ensure that we aren't trying to move into a freeform stack without freeform
2732 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002733 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002734 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2735 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002736 }
2737
Winson Chung74666102017-02-22 17:49:24 -08002738 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2739 // used for split-screen mode and will cause things like the docked divider to show up. We
2740 // instead leave the task in its current stack or move it to the fullscreen stack if it
2741 // isn't currently in a stack.
2742 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2743 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2744 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2745 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002746 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002747
Winson Chung74666102017-02-22 17:49:24 -08002748 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2749 // example, a docked stack is created which will lead to the stack we are moving from being
2750 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002751 try {
Winson Chung74666102017-02-22 17:49:24 -08002752 task.mTemporarilyUnresizable = true;
2753 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002754 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002755 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002756 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002757 }
2758
Wale Ogunwale079a0042015-10-24 11:44:07 -07002759 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2760 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2761 if (stack == null) {
2762 throw new IllegalArgumentException(
2763 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2764 }
2765
2766 final ActivityRecord r = stack.topRunningActivityLocked();
2767 if (r == null) {
2768 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2769 + " in stack=" + stack);
2770 return false;
2771 }
2772
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002773 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002774 Slog.w(TAG,
2775 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002776 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002777 return false;
2778 }
2779
Winson Chungb5c41b72016-12-07 15:00:47 -08002780 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2781 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002782 return true;
2783 }
2784
Winson Chungb5c41b72016-12-07 15:00:47 -08002785 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2786 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002787 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002788
Wale Ogunwale1666e312016-12-16 11:27:18 -08002789 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002790 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002791
Wale Ogunwale480dca02016-02-06 13:58:29 -08002792 try {
2793 final TaskRecord task = r.task;
2794
Andrii Kulian02b7a832016-10-06 23:11:56 -07002795 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002796 // An activity can't be pinned and visible behind at the same time. Go ahead and
2797 // release it from been visible behind before pinning.
2798 requestVisibleBehindLocked(r, false);
2799 }
2800
Wale Ogunwale480dca02016-02-06 13:58:29 -08002801 // Resize the pinned stack to match the current size of the task the activity we are
2802 // going to be moving is currently contained in. We do this to have the right starting
2803 // animation bounds for the pinned stack to the desired bounds the caller wants.
2804 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2805 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002806 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002807
2808 if (task.mActivities.size() == 1) {
2809 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002810 // the stack without re-parenting the activity in a different task. We don't
2811 // move the home stack forward if we are currently entering picture-in-picture
2812 // while pausing because that changes the focused stack and may prevent the new
2813 // starting activity from resuming.
2814 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002815 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002816 // Move the home stack forward if the task we just moved to the pinned stack
2817 // was launched from home so home should be visible behind it.
2818 moveHomeStackToFront(reason);
2819 }
Winson Chung74666102017-02-22 17:49:24 -08002820 // Defer resume until below
2821 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2822 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002823 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002824 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002825 // reveal/leave the other activities in their original task.
2826
2827 // Currently, we don't support reparenting activities across tasks in two different
2828 // stacks, so instead, just create a new task in the same stack, reparent the
2829 // activity into that task, and then reparent the whole task to the new stack. This
2830 // ensures that all the necessary work to migrate states in the old and new stacks
2831 // is also done.
2832 final TaskRecord newTask = task.getStack().createTaskRecord(
2833 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2834 r.mActivityType);
2835 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2836
2837 // Defer resume until below
2838 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2839 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002840 }
Winson Chungc2baac02017-01-11 13:34:47 -08002841
2842 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2843 // to the pinned stack
2844 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002845 } finally {
2846 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002847 }
2848
Wale Ogunwale480dca02016-02-06 13:58:29 -08002849 // The task might have already been running and its visibility needs to be synchronized
2850 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002851 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002852 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002853
Winson Chung55893332017-02-17 17:13:10 -08002854 stack.animateResizePinnedStack(bounds, -1 /* animationDuration */);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002855 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002856 }
2857
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002858 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002859 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2860 if (r == null || !r.isFocusable()) {
2861 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2862 "moveActivityStackToFront: unfocusable r=" + r);
2863 return false;
2864 }
2865
2866 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002867 final ActivityStack stack = r.getStack();
2868 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002869 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2870 + r + " task=" + task);
2871 return false;
2872 }
2873
Chong Zhang6cda19c2016-06-14 19:07:56 -07002874 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2875 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2876 "moveActivityStackToFront: already on top, r=" + r);
2877 return false;
2878 }
2879
2880 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2881 "moveActivityStackToFront: r=" + r);
2882
2883 stack.moveToFront(reason, task);
2884 return true;
2885 }
2886
David Stevensc6b91c62017-02-08 14:23:58 -08002887 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002888 mTmpFindTaskResult.r = null;
2889 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002890 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002891 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002892 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2893 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002894 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2895 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002896 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002897 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002898 continue;
2899 }
2900 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002901 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2902 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002903 continue;
2904 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002905 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002906 // It is possible to have tasks in multiple stacks with the same root affinity, so
2907 // we should keep looking after finding an affinity match to see if there is a
2908 // better match in another stack. Also, task affinity isn't a good enough reason
2909 // to target a display which isn't the source of the intent, so skip any affinity
2910 // matches not on the specified display.
2911 if (mTmpFindTaskResult.r != null) {
2912 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2913 return mTmpFindTaskResult.r;
2914 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2915 affinityMatch = mTmpFindTaskResult.r;
2916 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002917 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002918 }
2919 }
David Stevensc6b91c62017-02-08 14:23:58 -08002920 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2921 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002922 }
2923
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002924 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2925 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002926 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2927 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002928 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002929 final ActivityRecord ar = stacks.get(stackNdx)
2930 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002931 if (ar != null) {
2932 return ar;
2933 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002934 }
2935 }
2936 return null;
2937 }
2938
Craig Mautner8d341ef2013-03-26 09:03:27 -07002939 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002940 scheduleSleepTimeout();
2941 if (!mGoingToSleep.isHeld()) {
2942 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002943 if (mLaunchingActivity.isHeld()) {
2944 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2945 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002946 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002947 mLaunchingActivity.release();
2948 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002949 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002951 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002952 }
2953
2954 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002955 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002956
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002957 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002958 final long endTime = System.currentTimeMillis() + timeout;
2959 while (true) {
2960 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002961 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2962 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002963 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2964 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2965 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002966 }
2967 if (cantShutdown) {
2968 long timeRemaining = endTime - System.currentTimeMillis();
2969 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002971 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002972 } catch (InterruptedException e) {
2973 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002974 } else {
2975 Slog.w(TAG, "Activity manager shutdown timed out");
2976 timedout = true;
2977 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002979 } else {
2980 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002981 }
2982 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002983
2984 // Force checkReadyForSleep to complete.
2985 mSleepTimeout = true;
2986 checkReadyForSleepLocked();
2987
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 return timedout;
2989 }
2990
2991 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002992 removeSleepTimeouts();
2993 if (mGoingToSleep.isHeld()) {
2994 mGoingToSleep.release();
2995 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002996 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2997 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002998 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2999 final ActivityStack stack = stacks.get(stackNdx);
3000 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003001 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003002 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003003 }
Craig Mautner5314a402013-09-26 12:40:16 -07003004 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003006 mGoingToSleepActivities.clear();
3007 }
3008
3009 void activitySleptLocked(ActivityRecord r) {
3010 mGoingToSleepActivities.remove(r);
3011 checkReadyForSleepLocked();
3012 }
3013
3014 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003015 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003016 // Do not care.
3017 return;
3018 }
3019
3020 if (!mSleepTimeout) {
3021 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003022 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3023 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003024 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3025 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3026 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003027 }
3028
3029 if (mStoppingActivities.size() > 0) {
3030 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003031 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003032 + mStoppingActivities.size() + " activities");
3033 scheduleIdleLocked();
3034 dontSleep = true;
3035 }
3036
3037 if (mGoingToSleepActivities.size() > 0) {
3038 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003039 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003040 + mGoingToSleepActivities.size() + " activities");
3041 dontSleep = true;
3042 }
3043
3044 if (dontSleep) {
3045 return;
3046 }
3047 }
3048
Wei Wang65c7a152016-06-02 18:51:22 -07003049 // Send launch end powerhint before going sleep
3050 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3051
Craig Mautnere0a38842013-12-16 16:14:02 -08003052 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3053 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3055 stacks.get(stackNdx).goToSleep();
3056 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003057 }
3058
3059 removeSleepTimeouts();
3060
3061 if (mGoingToSleep.isHeld()) {
3062 mGoingToSleep.release();
3063 }
3064 if (mService.mShuttingDown) {
3065 mService.notifyAll();
3066 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003067 }
3068
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003069 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003070 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003071 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003072 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003073 }
3074 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003075 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003076 mWindowManager.executeAppTransition();
3077 return true;
3078 }
3079 return false;
3080 }
3081
Craig Mautner8d341ef2013-03-26 09:03:27 -07003082 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003083 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3084 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003085 int stackNdx = stacks.size() - 1;
3086 while (stackNdx >= 0) {
3087 stacks.get(stackNdx).handleAppCrashLocked(app);
3088 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003089 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003090 }
3091 }
3092
Jose Lima4b6c6692014-08-12 17:41:12 -07003093 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003094 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003095 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003096 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3097 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003098 return false;
3099 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003100
3101 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3102 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3103 + " visible=" + visible + " stackId=" + stack.mStackId
3104 + " can't contain visible behind activities");
3105 return false;
3106 }
3107
Jose Lima4b6c6692014-08-12 17:41:12 -07003108 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003109 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3110 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003111
3112 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003113 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003114 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003115 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003116 return true;
3117 }
3118
3119 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003120 if (visible && top.fullscreen) {
3121 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003122 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3123 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3124 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3125 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003126 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003127 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3128 // Only the activity set as currently visible behind should actively reset its
3129 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003130 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3131 "requestVisibleBehind: returning visible=" + visible
3132 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3133 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003134 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003135 }
3136
Jose Lima4b6c6692014-08-12 17:41:12 -07003137 stack.setVisibleBehindActivity(visible ? r : null);
3138 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003139 // If there is a translucent home activity, we need to force it stop being translucent,
3140 // because we can't depend on the application to necessarily perform that operation.
3141 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003142 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003143 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003144 mService.convertFromTranslucent(next.appToken);
3145 }
3146 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003147 if (top.app != null && top.app.thread != null) {
3148 // Notify the top app of the change.
3149 try {
3150 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3151 } catch (RemoteException e) {
3152 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003153 }
3154 return true;
3155 }
3156
Craig Mautnerbb742462014-07-07 15:28:55 -07003157 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003158 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003159 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003160 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003161
3162 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003163 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003164 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003165 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003166 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003167
3168 // When launching tasks behind, update the last active time of the top task after the new
3169 // task has been shown briefly
3170 final ActivityRecord top = stack.topActivity();
3171 if (top != null) {
3172 top.task.touchActiveTime();
3173 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003174 }
3175
3176 void scheduleLaunchTaskBehindComplete(IBinder token) {
3177 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3178 }
3179
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003180 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3181 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003182 mKeyguardController.beginActivityVisibilityUpdate();
3183 try {
3184 // First the front stacks. In case any are not fullscreen and are in front of home.
3185 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3186 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3187 final int topStackNdx = stacks.size() - 1;
3188 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3189 final ActivityStack stack = stacks.get(stackNdx);
3190 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3191 }
Craig Mautner580ea812013-04-25 12:58:38 -07003192 }
Jorim Jaggife762342016-10-13 14:33:27 +02003193 } finally {
3194 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003195 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003196 }
3197
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003198 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3199 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3200 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3201 final int topStackNdx = stacks.size() - 1;
3202 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3203 final ActivityStack stack = stacks.get(stackNdx);
3204 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3205 }
3206 }
3207 }
3208
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003209 void invalidateTaskLayers() {
3210 mTaskLayersChanged = true;
3211 }
3212
3213 void rankTaskLayersIfNeeded() {
3214 if (!mTaskLayersChanged) {
3215 return;
3216 }
3217 mTaskLayersChanged = false;
3218 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3219 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3220 int baseLayer = 0;
3221 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3222 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3223 }
3224 }
3225 }
3226
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003227 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3228 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3229 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3230 final int topStackNdx = stacks.size() - 1;
3231 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3232 final ActivityStack stack = stacks.get(stackNdx);
3233 stack.clearOtherAppTimeTrackers(except);
3234 }
3235 }
3236 }
3237
Craig Mautner8d341ef2013-03-26 09:03:27 -07003238 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003239 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3240 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003241 final int numStacks = stacks.size();
3242 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3243 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003244 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003245 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003246 }
3247 }
3248
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003249 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3250 // Examine all activities currently running in the process.
3251 TaskRecord firstTask = null;
3252 // Tasks is non-null only if two or more tasks are found.
3253 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003254 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3255 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003256 ActivityRecord r = app.activities.get(i);
3257 // First, if we find an activity that is in the process of being destroyed,
3258 // then we just aren't going to do anything for now; we want things to settle
3259 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003260 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003261 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003262 return;
3263 }
3264 // Don't consider any activies that are currently not in a state where they
3265 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003266 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3267 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003268 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003269 continue;
3270 }
3271 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003272 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003273 + " from " + r);
3274 if (firstTask == null) {
3275 firstTask = r.task;
3276 } else if (firstTask != r.task) {
3277 if (tasks == null) {
3278 tasks = new ArraySet<>();
3279 tasks.add(firstTask);
3280 }
3281 tasks.add(r.task);
3282 }
3283 }
3284 }
3285 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003286 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003287 return;
3288 }
3289 // If we have activities in multiple tasks that are in a position to be destroyed,
3290 // let's iterate through the tasks and release the oldest one.
3291 final int numDisplays = mActivityDisplays.size();
3292 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3293 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3294 // Step through all stacks starting from behind, to hit the oldest things first.
3295 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3296 final ActivityStack stack = stacks.get(stackNdx);
3297 // Try to release activities in this stack; if we manage to, we are done.
3298 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3299 return;
3300 }
3301 }
3302 }
3303 }
3304
Amith Yamasani37a40c22015-06-17 13:25:42 -07003305 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003306 final int focusStackId = mFocusedStack.getStackId();
3307 // We dismiss the docked stack whenever we switch users.
3308 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003309 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3310 // also cause all tasks to be moved to the fullscreen stack at a position that is
3311 // appropriate.
3312 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003313
3314 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003315 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003316 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003317
Craig Mautner858d8a62013-04-23 17:08:34 -07003318 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003319 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3320 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003321 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003322 final ActivityStack stack = stacks.get(stackNdx);
3323 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003324 TaskRecord task = stack.topTask();
3325 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003326 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003327 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003328 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003329 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003330
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003331 ActivityStack stack = getStack(restoreStackId);
3332 if (stack == null) {
3333 stack = mHomeStack;
3334 }
3335 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003336 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003337 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003338 } else {
3339 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003340 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003341 }
Craig Mautner93529a42013-10-04 15:03:13 -07003342 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003343 }
3344
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003345 /** Checks whether the userid is a profile of the current user. */
3346 boolean isCurrentProfileLocked(int userId) {
3347 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003348 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003349 }
3350
Winson Chung4dabf232017-01-25 13:25:22 -08003351 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3352 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003353 ArrayList<ActivityRecord> stops = null;
3354
3355 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003356 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3357 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003358 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3359 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003360 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003361 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003362 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3363 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003364 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003365 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003366 if (s.finishing) {
3367 // If this activity is finishing, it is sitting on top of
3368 // everyone else but we now know it is no longer needed...
3369 // so get rid of it. Otherwise, we need to go through the
3370 // normal flow and hide it once we determine that it is
3371 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003372 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003373 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003374 }
3375 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003376 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003377 if (!processPausingActivities && s.state == PAUSING) {
3378 // Defer processing pausing activities in this iteration and reschedule
3379 // a delayed idle to reprocess it again
3380 removeTimeoutsForActivityLocked(idleActivity);
3381 scheduleIdleTimeoutLocked(idleActivity);
3382 continue;
3383 }
3384
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003385 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003386 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003387 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003388 }
3389 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003390 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003391 }
3392 }
3393
3394 return stops;
3395 }
3396
Craig Mautnercf910b02013-04-23 11:23:27 -07003397 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3399 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3400 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3401 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003402 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003403 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003404 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003405 if (r == null) Slog.e(TAG,
3406 "validateTop...: null top activity, stack=" + stack);
3407 else {
3408 final ActivityRecord pausing = stack.mPausingActivity;
3409 if (pausing != null && pausing == r) Slog.e(TAG,
3410 "validateTop...: top stack has pausing activity r=" + r
3411 + " state=" + state);
3412 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3413 "validateTop...: activity in front not resumed r=" + r
3414 + " state=" + state);
3415 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003416 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003417 final ActivityRecord resumed = stack.mResumedActivity;
3418 if (resumed != null && resumed == r) Slog.e(TAG,
3419 "validateTop...: back stack has resumed activity r=" + r
3420 + " state=" + state);
3421 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3422 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003423 }
3424 }
3425 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003426 }
3427
Craig Mautnere0570202015-05-13 13:06:11 -07003428 private String lockTaskModeToString() {
3429 switch (mLockTaskModeState) {
3430 case LOCK_TASK_MODE_LOCKED:
3431 return "LOCKED";
3432 case LOCK_TASK_MODE_PINNED:
3433 return "PINNED";
3434 case LOCK_TASK_MODE_NONE:
3435 return "NONE";
3436 default: return "unknown=" + mLockTaskModeState;
3437 }
3438 }
3439
Craig Mautner27084302013-03-25 08:05:25 -07003440 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003441 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003442 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003443 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003444 pw.print(prefix);
3445 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003446 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003447 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003448 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003449 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3450 if (packages.size() > 0) {
3451 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3452 for (int i = 0; i < packages.size(); ++i) {
3453 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3454 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3455 }
3456 }
3457 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3458 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003459 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003460
Winson43d1f262016-06-14 16:05:55 -07003461 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003462 * Dump all connected displays' configurations.
3463 * @param prefix Prefix to apply to each line of the dump.
3464 */
3465 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3466 pw.print(prefix); pw.println("Display override configurations:");
3467 final int displayCount = mActivityDisplays.size();
3468 for (int i = 0; i < displayCount; i++) {
3469 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3470 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3471 pw.println(activityDisplay.getOverrideConfiguration());
3472 }
3473 }
3474
3475 /**
Winson43d1f262016-06-14 16:05:55 -07003476 * Dumps the activities matching the given {@param name} in the either the focused stack
3477 * or all visible stacks if {@param dumpVisibleStacks} is true.
3478 */
Winson Chung6998bc42017-02-28 17:07:05 -08003479 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3480 boolean dumpFocusedStackOnly) {
3481 if (dumpFocusedStackOnly) {
3482 return mFocusedStack.getDumpActivitiesLocked(name);
3483 } else {
Winson43d1f262016-06-14 16:05:55 -07003484 ArrayList<ActivityRecord> activities = new ArrayList<>();
3485 int numDisplays = mActivityDisplays.size();
3486 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3487 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3489 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003490 if (!dumpVisibleStacksOnly ||
3491 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003492 activities.addAll(stack.getDumpActivitiesLocked(name));
3493 }
3494 }
3495 }
3496 return activities;
Winson43d1f262016-06-14 16:05:55 -07003497 }
Craig Mautner20e72272013-04-01 13:45:53 -07003498 }
3499
Dianne Hackborn390517b2013-05-30 15:03:32 -07003500 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3501 boolean needSep, String prefix) {
3502 if (activity != null) {
3503 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3504 if (needSep) {
3505 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003506 }
3507 pw.print(prefix);
3508 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003509 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003510 }
3511 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003512 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003513 }
3514
Craig Mautner8d341ef2013-03-26 09:03:27 -07003515 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3516 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003517 boolean printed = false;
3518 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003519 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3520 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003521 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003522 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003523 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003525 final ActivityStack stack = stacks.get(stackNdx);
3526 StringBuilder stackHeader = new StringBuilder(128);
3527 stackHeader.append(" Stack #");
3528 stackHeader.append(stack.mStackId);
3529 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003530 stackHeader.append("\n");
3531 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3532 stackHeader.append("\n");
3533 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3535 needSep, stackHeader.toString());
3536 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3537 !dumpAll, false, dumpPackage, true,
3538 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 needSep = printed;
3541 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3542 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003543 if (pr) {
3544 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003545 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003546 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003547 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3548 " mResumedActivity: ");
3549 if (pr) {
3550 printed = true;
3551 needSep = false;
3552 }
3553 if (dumpAll) {
3554 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3555 " mLastPausedActivity: ");
3556 if (pr) {
3557 printed = true;
3558 needSep = true;
3559 }
3560 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3561 needSep, " mLastNoHistoryActivity: ");
3562 }
3563 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003564 }
3565 }
3566
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003567 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3568 false, dumpPackage, true, " Activities waiting to finish:", null);
3569 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3570 false, dumpPackage, true, " Activities waiting to stop:", null);
3571 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3572 false, dumpPackage, true, " Activities waiting for another to become visible:",
3573 null);
3574 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3575 false, dumpPackage, true, " Activities waiting to sleep:", null);
3576 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3577 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003578
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003579 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003580 }
3581
Dianne Hackborn390517b2013-05-30 15:03:32 -07003582 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003583 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003584 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003585 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003586 String innerPrefix = null;
3587 String[] args = null;
3588 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003589 for (int i=list.size()-1; i>=0; i--) {
3590 final ActivityRecord r = list.get(i);
3591 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3592 continue;
3593 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003594 if (innerPrefix == null) {
3595 innerPrefix = prefix + " ";
3596 args = new String[0];
3597 }
3598 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003599 final boolean full = !brief && (complete || !r.isInHistory());
3600 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003601 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003602 needNL = false;
3603 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003604 if (header1 != null) {
3605 pw.println(header1);
3606 header1 = null;
3607 }
3608 if (header2 != null) {
3609 pw.println(header2);
3610 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003611 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003612 if (lastTask != r.task) {
3613 lastTask = r.task;
3614 pw.print(prefix);
3615 pw.print(full ? "* " : " ");
3616 pw.println(lastTask);
3617 if (full) {
3618 lastTask.dump(pw, prefix + " ");
3619 } else if (complete) {
3620 // Complete + brief == give a summary. Isn't that obvious?!?
3621 if (lastTask.intent != null) {
3622 pw.print(prefix); pw.print(" ");
3623 pw.println(lastTask.intent.toInsecureStringWithClip());
3624 }
3625 }
3626 }
3627 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3628 pw.print(" #"); pw.print(i); pw.print(": ");
3629 pw.println(r);
3630 if (full) {
3631 r.dump(pw, innerPrefix);
3632 } else if (complete) {
3633 // Complete + brief == give a summary. Isn't that obvious?!?
3634 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3635 if (r.app != null) {
3636 pw.print(innerPrefix); pw.println(r.app);
3637 }
3638 }
3639 if (client && r.app != null && r.app.thread != null) {
3640 // flush anything that is already in the PrintWriter since the thread is going
3641 // to write to the file descriptor directly
3642 pw.flush();
3643 try {
3644 TransferPipe tp = new TransferPipe();
3645 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003646 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003647 // Short timeout, since blocking here can
3648 // deadlock with the application.
3649 tp.go(fd, 2000);
3650 } finally {
3651 tp.kill();
3652 }
3653 } catch (IOException e) {
3654 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3655 } catch (RemoteException e) {
3656 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3657 }
3658 needNL = true;
3659 }
3660 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003661 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003662 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003663
Craig Mautnerf3333272013-04-22 10:55:53 -07003664 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003665 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3666 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003667 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3668 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003669 }
3670
3671 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003672 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003673 }
3674
3675 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003676 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3677 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003678 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3679 }
3680
Craig Mautner05d29032013-05-03 13:40:13 -07003681 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003682 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3683 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3684 }
Craig Mautner05d29032013-05-03 13:40:13 -07003685 }
3686
Craig Mautner0eea92c2013-05-16 13:35:39 -07003687 void removeSleepTimeouts() {
3688 mSleepTimeout = false;
3689 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3690 }
3691
3692 final void scheduleSleepTimeout() {
3693 removeSleepTimeouts();
3694 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3695 }
3696
Craig Mautner4a1cb222013-12-04 16:14:06 -08003697 @Override
3698 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003699 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003700 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3701 }
3702
3703 @Override
3704 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003705 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003706 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3707 }
3708
3709 @Override
3710 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003711 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003712 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3713 }
3714
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003715 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003716 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003717 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003718 newDisplay = mActivityDisplays.get(displayId) == null;
3719 if (newDisplay) {
3720 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003721 if (activityDisplay.mDisplay == null) {
3722 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3723 return;
3724 }
Craig Mautner4504de52013-12-20 09:06:56 -08003725 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003726 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003727 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003728 }
Craig Mautner4504de52013-12-20 09:06:56 -08003729 if (newDisplay) {
3730 mWindowManager.onDisplayAdded(displayId);
3731 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003732 }
3733
Andrii Kulianca007a62016-11-22 16:33:28 -08003734 /** Check if display with specified id is added to the list. */
3735 boolean isDisplayAdded(int displayId) {
3736 return mActivityDisplays.get(displayId) != null;
3737 }
3738
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003739 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003740 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003741 mService.mContext.getResources().getDimensionPixelSize(
3742 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003743 }
3744
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003745 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003746 if (displayId == DEFAULT_DISPLAY) {
3747 throw new IllegalArgumentException("Can't remove the primary display.");
3748 }
3749
Craig Mautner4a1cb222013-12-04 16:14:06 -08003750 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003751 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3752 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003753 final boolean destroyContentOnRemoval
3754 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003755 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3756 while (!stacks.isEmpty()) {
3757 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003758 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003759 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3760 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003761 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003762 } else {
3763 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3764 // a valid launch stack for all activities. This way the task history from
3765 // external display will be preserved on primary after move.
3766 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003767 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003769 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003770 }
3771 }
3772 mWindowManager.onDisplayRemoved(displayId);
3773 }
3774
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003775 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003777 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3778 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779 // TODO: Update the bounds.
3780 }
3781 }
3782 mWindowManager.onDisplayChanged(displayId);
3783 }
3784
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003785 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003786 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003788 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003789 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003791 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003792 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003793 info.position = display != null
3794 ? display.mStacks.indexOf(stack)
3795 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796
3797 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3798 final int numTasks = tasks.size();
3799 int[] taskIds = new int[numTasks];
3800 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003801 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003802 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003803 for (int i = 0; i < numTasks; ++i) {
3804 final TaskRecord task = tasks.get(i);
3805 taskIds[i] = task.taskId;
3806 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3807 : task.realActivity != null ? task.realActivity.flattenToString()
3808 : task.getTopActivity() != null ? task.getTopActivity().packageName
3809 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003810 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003811 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003812 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 }
3814 info.taskIds = taskIds;
3815 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003816 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003817 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003818
3819 final ActivityRecord top = stack.topRunningActivityLocked();
3820 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 return info;
3822 }
3823
3824 StackInfo getStackInfoLocked(int stackId) {
3825 ActivityStack stack = getStack(stackId);
3826 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003827 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 }
3829 return null;
3830 }
3831
3832 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003833 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003834 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3835 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003837 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 }
3839 }
3840 return list;
3841 }
3842
Craig Mautner15df08a2015-04-01 12:17:18 -07003843 TaskRecord getLockedTaskLocked() {
3844 final int top = mLockTaskModeTasks.size() - 1;
3845 if (top >= 0) {
3846 return mLockTaskModeTasks.get(top);
3847 }
3848 return null;
3849 }
3850
3851 boolean isLockedTask(TaskRecord task) {
3852 return mLockTaskModeTasks.contains(task);
3853 }
3854
3855 boolean isLastLockedTask(TaskRecord task) {
3856 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3857 }
3858
3859 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003860 if (!mLockTaskModeTasks.remove(task)) {
3861 return;
3862 }
3863 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3864 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003865 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003866 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3867 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003868 final Message lockTaskMsg = Message.obtain();
3869 lockTaskMsg.arg1 = task.userId;
3870 lockTaskMsg.what = LOCK_TASK_END_MSG;
3871 mHandler.sendMessage(lockTaskMsg);
3872 }
3873 }
3874
Andrii Kulianc27916642016-04-12 17:59:27 -07003875 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3876 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3877 false /* forceNonResizable */);
3878 }
3879
Jorim Jaggid53f0922016-04-06 22:16:23 -07003880 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003881 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003882 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3883 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003884 return;
3885 }
3886
Jorim Jaggicd13d332016-04-27 15:40:20 -07003887 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003888 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003889 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003890 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003891
Andrii Kulianc27916642016-04-12 17:59:27 -07003892 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3893 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003894 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003895 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003896 && !topActivity.noDisplay) {
3897 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003898 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3899 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003900 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003901 }
3902
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003903 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003904 if (mLockTaskNotify != null) {
3905 mLockTaskNotify.showToast(mLockTaskModeState);
3906 }
Jason Monka8f569c2014-07-07 12:15:21 -04003907 }
3908
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003909 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3910 if (mLockTaskModeTasks.contains(task)) {
3911 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3912 }
3913 }
3914
Craig Mautner432f64e2015-05-20 14:59:57 -07003915 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3916 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003917 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003918 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003919 final TaskRecord lockedTask = getLockedTaskLocked();
3920 if (lockedTask != null) {
3921 removeLockedTaskLocked(lockedTask);
3922 if (!mLockTaskModeTasks.isEmpty()) {
3923 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003924 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3925 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003926 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003927 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003928 return;
3929 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003930 }
Craig Mautnere0570202015-05-13 13:06:11 -07003931 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3932 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003933 return;
3934 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003935
3936 // Should have already been checked, but do it again.
3937 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003938 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3939 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003940 return;
3941 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003942 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003943 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003944 return;
3945 }
3946
3947 if (mLockTaskModeTasks.isEmpty()) {
3948 // First locktask.
3949 final Message lockTaskMsg = Message.obtain();
3950 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3951 lockTaskMsg.arg1 = task.userId;
3952 lockTaskMsg.what = LOCK_TASK_START_MSG;
3953 lockTaskMsg.arg2 = lockTaskModeState;
3954 mHandler.sendMessage(lockTaskMsg);
3955 }
3956 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003957 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3958 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003959 mLockTaskModeTasks.remove(task);
3960 mLockTaskModeTasks.add(task);
3961
3962 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003963 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003964 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003965
3966 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003967 findTaskToMoveToFrontLocked(task, 0, null, reason,
3968 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003969 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003970 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003971 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003972 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003973 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003974 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003975 }
3976
3977 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003978 return isLockTaskModeViolation(task, false);
3979 }
3980
3981 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3982 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003983 return false;
3984 }
3985 final int lockTaskAuth = task.mLockTaskAuth;
3986 switch (lockTaskAuth) {
3987 case LOCK_TASK_AUTH_DONT_LOCK:
3988 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003989 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003990 case LOCK_TASK_AUTH_LAUNCHABLE:
3991 case LOCK_TASK_AUTH_WHITELISTED:
3992 return false;
3993 case LOCK_TASK_AUTH_PINNABLE:
3994 // Pinnable tasks can't be launched on top of locktask tasks.
3995 return !mLockTaskModeTasks.isEmpty();
3996 default:
3997 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3998 return true;
3999 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004000 }
4001
Craig Mautner15df08a2015-04-01 12:17:18 -07004002 void onLockTaskPackagesUpdatedLocked() {
4003 boolean didSomething = false;
4004 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4005 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004006 final boolean wasWhitelisted =
4007 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4008 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004009 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004010 final boolean isWhitelisted =
4011 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4012 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4013 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004014 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004015 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4016 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004017 removeLockedTaskLocked(lockedTask);
4018 lockedTask.performClearTaskLocked();
4019 didSomething = true;
4020 }
4021 }
4022 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4023 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4024 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4025 final ActivityStack stack = stacks.get(stackNdx);
4026 stack.onLockTaskPackagesUpdatedLocked();
4027 }
4028 }
Craig Mautnere0570202015-05-13 13:06:11 -07004029 final ActivityRecord r = topRunningActivityLocked();
4030 final TaskRecord task = r != null ? r.task : null;
4031 if (mLockTaskModeTasks.isEmpty() && task != null
4032 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4033 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004034 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4035 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4036 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4037 false);
4038 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004039 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004040 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004041 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004042 }
4043 }
4044
Benjamin Franz43261142015-02-11 15:59:44 +00004045 int getLockTaskModeState() {
4046 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004047 }
4048
Jorim Jaggife89d122015-12-22 16:28:44 +01004049 void activityRelaunchedLocked(IBinder token) {
4050 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004051 if (mService.isSleepingOrShuttingDownLocked()) {
4052 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4053 if (r != null) {
4054 r.setSleeping(true, true);
4055 }
4056 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004057 }
4058
4059 void activityRelaunchingLocked(ActivityRecord r) {
4060 mWindowManager.notifyAppRelaunching(r.appToken);
4061 }
4062
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004063 void logStackState() {
4064 mActivityMetricsLogger.logWindowState();
4065 }
4066
Andrii Kulian933076d2016-03-29 17:04:42 -07004067 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004068 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4069 final ActivityRecord r = task.mActivities.get(i);
4070 if (r.app != null && r.app.thread != null) {
4071 mMultiWindowModeChangedActivities.add(r);
4072 }
4073 }
4074
4075 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4076 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4077 }
4078 }
4079
Andrii Kulian933076d2016-03-29 17:04:42 -07004080 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004081 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004082 if (prevStack == null || prevStack == stack
4083 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4084 return;
4085 }
4086
4087 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4088 final ActivityRecord r = task.mActivities.get(i);
4089 if (r.app != null && r.app.thread != null) {
4090 mPipModeChangedActivities.add(r);
4091 }
4092 }
4093
4094 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4095 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4096 }
4097 }
4098
Tony Mak853304c2016-04-18 15:17:41 +01004099 void setDockedStackMinimized(boolean minimized) {
4100 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004101 }
4102
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004103 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004104
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004105 public ActivityStackSupervisorHandler(Looper looper) {
4106 super(looper);
4107 }
4108
Winson Chung4dabf232017-01-25 13:25:22 -08004109 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004110 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004111 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4112 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004113 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004114 }
4115
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004116 @Override
4117 public void handleMessage(Message msg) {
4118 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004119 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4120 synchronized (mService) {
4121 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4122 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004123 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004124 }
4125 }
4126 } break;
4127 case REPORT_PIP_MODE_CHANGED_MSG: {
4128 synchronized (mService) {
4129 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4130 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004131 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004132 }
4133 }
4134 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004135 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004136 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4137 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004138 if (mService.mDidDexOpt) {
4139 mService.mDidDexOpt = false;
4140 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4141 nmsg.obj = msg.obj;
4142 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4143 return;
4144 }
4145 // We don't at this point know if the activity is fullscreen,
4146 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004147 activityIdleInternal((ActivityRecord) msg.obj,
4148 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004149 } break;
4150 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004151 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004152 activityIdleInternal((ActivityRecord) msg.obj,
4153 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004154 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004155 case RESUME_TOP_ACTIVITY_MSG: {
4156 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004157 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004158 }
4159 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004160 case SLEEP_TIMEOUT_MSG: {
4161 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004162 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004163 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4164 mSleepTimeout = true;
4165 checkReadyForSleepLocked();
4166 }
4167 }
4168 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004169 case LAUNCH_TIMEOUT_MSG: {
4170 if (mService.mDidDexOpt) {
4171 mService.mDidDexOpt = false;
4172 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4173 return;
4174 }
4175 synchronized (mService) {
4176 if (mLaunchingActivity.isHeld()) {
4177 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4178 if (VALIDATE_WAKE_LOCK_CALLER
4179 && Binder.getCallingUid() != Process.myUid()) {
4180 throw new IllegalStateException("Calling must be system uid");
4181 }
4182 mLaunchingActivity.release();
4183 }
4184 }
4185 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004187 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 } break;
4189 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004190 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004191 } break;
4192 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004193 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004194 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004195 case CONTAINER_CALLBACK_VISIBILITY: {
4196 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004197 final IActivityContainerCallback callback = container.mCallback;
4198 if (callback != null) {
4199 try {
4200 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4201 } catch (RemoteException e) {
4202 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004203 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004204 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004205 case LOCK_TASK_START_MSG: {
4206 // When lock task starts, we disable the status bars.
4207 try {
Jason Monk62515be2014-05-21 16:06:19 -04004208 if (mLockTaskNotify == null) {
4209 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004210 }
Jason Monk62515be2014-05-21 16:06:19 -04004211 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004212 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004213 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004214 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004215 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004216 flags = StatusBarManager.DISABLE_MASK
4217 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004218 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004219 flags = StatusBarManager.DISABLE_MASK
4220 & (~StatusBarManager.DISABLE_BACK)
4221 & (~StatusBarManager.DISABLE_HOME)
4222 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004223 }
4224 getStatusBarService().disable(flags, mToken,
4225 mService.mContext.getPackageName());
4226 }
4227 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004228 if (getDevicePolicyManager() != null) {
4229 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004230 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004231 }
justinzhang5286d3f2014-05-12 17:06:01 -04004232 } catch (RemoteException ex) {
4233 throw new RuntimeException(ex);
4234 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004235 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004236 case LOCK_TASK_END_MSG: {
4237 // When lock task ends, we enable the status bars.
4238 try {
Jason Monk62515be2014-05-21 16:06:19 -04004239 if (getStatusBarService() != null) {
4240 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4241 mService.mContext.getPackageName());
4242 }
4243 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004244 if (getDevicePolicyManager() != null) {
4245 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4246 msg.arg1);
4247 }
Jason Monk62515be2014-05-21 16:06:19 -04004248 if (mLockTaskNotify == null) {
4249 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4250 }
4251 mLockTaskNotify.show(false);
4252 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004253 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004254 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004255 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004256 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004257 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004258 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004259 new LockPatternUtils(mService.mContext)
4260 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004261 }
4262 } catch (SettingNotFoundException e) {
4263 // No setting, don't lock.
4264 }
justinzhang5286d3f2014-05-12 17:06:01 -04004265 } catch (RemoteException ex) {
4266 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004267 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004268 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004269 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004270 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004271 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4272 if (mLockTaskNotify == null) {
4273 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4274 }
4275 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4276 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004277 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4278 final ActivityContainer container = (ActivityContainer) msg.obj;
4279 final IActivityContainerCallback callback = container.mCallback;
4280 if (callback != null) {
4281 try {
4282 callback.onAllActivitiesComplete(container.asBinder());
4283 } catch (RemoteException e) {
4284 }
4285 }
4286 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004287 case LAUNCH_TASK_BEHIND_COMPLETE: {
4288 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004289 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004290 if (r != null) {
4291 handleLaunchTaskBehindCompleteLocked(r);
4292 }
4293 }
4294 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004295
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004296 }
4297 }
4298 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004299
Ying Wangb081a592014-04-22 15:20:16 -07004300 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004301 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4302 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004303 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004304 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004305 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004306 ActivityRecord mParentActivity = null;
4307 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004308
Craig Mautnere3a00d72014-04-16 08:31:19 -07004309 boolean mVisible = true;
4310
Craig Mautner4a1cb222013-12-04 16:14:06 -08004311 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004312 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004313
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004314 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4315 final static int CONTAINER_STATE_NO_SURFACE = 1;
4316 final static int CONTAINER_STATE_FINISHING = 2;
4317 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4318
Wale Ogunwale1666e312016-12-16 11:27:18 -08004319 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004320 synchronized (mService) {
4321 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004322 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004323 switch (mStackId) {
4324 case PINNED_STACK_ID:
4325 new PinnedActivityStack(this, mRecentTasks, onTop);
4326 break;
4327 default:
4328 new ActivityStack(this, mRecentTasks, onTop);
4329 break;
4330 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004331 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004332 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004333 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004334 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004335
Andrii Kulian839def92016-11-02 10:58:58 -07004336 /**
4337 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004338 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004339 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004340 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004341 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004342 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004343 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004344 if (mActivityDisplay != null) {
4345 throw new IllegalStateException("ActivityContainer is already attached, " +
4346 "displayId=" + mActivityDisplay.mDisplayId);
4347 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004348 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004349 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004350 }
4351
4352 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004353 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004354 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004355 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4356 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004357 return;
4358 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004359 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004360 }
4361 }
4362
4363 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004364 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004365 synchronized (mService) {
4366 if (mActivityDisplay != null) {
4367 return mActivityDisplay.mDisplayId;
4368 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004369 }
4370 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 }
4372
Jeff Brownca9bc702014-02-11 14:32:56 -08004373 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004374 public int getStackId() {
4375 synchronized (mService) {
4376 return mStackId;
4377 }
4378 }
4379
4380 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004381 public boolean injectEvent(InputEvent event) {
4382 final long origId = Binder.clearCallingIdentity();
4383 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004384 synchronized (mService) {
4385 if (mActivityDisplay != null) {
4386 return mInputManagerInternal.injectInputEvent(event,
4387 mActivityDisplay.mDisplayId,
4388 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4389 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004390 }
4391 return false;
4392 } finally {
4393 Binder.restoreCallingIdentity(origId);
4394 }
4395 }
4396
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004397 @Override
4398 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004399 synchronized (mService) {
4400 if (mContainerState == CONTAINER_STATE_FINISHING) {
4401 return;
4402 }
4403 mContainerState = CONTAINER_STATE_FINISHING;
4404
Craig Mautnerd163e752014-06-13 17:18:47 -07004405 long origId = Binder.clearCallingIdentity();
4406 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004407 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004408 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004409 } finally {
4410 Binder.restoreCallingIdentity(origId);
4411 }
4412 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004413 }
4414
Andrii Kulian03c403d2016-11-11 11:14:12 -08004415 /**
4416 * Remove the stack completely. Must be called only when there are no tasks left in it,
4417 * as this method does not finish running activities.
4418 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004419 void removeLocked() {
4420 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004421 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004422 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004423 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004424 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004425 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004426 }
4427
Andrii Kulian839def92016-11-02 10:58:58 -07004428 /**
4429 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4430 * completely or re-parented.
4431 */
4432 private void removeFromDisplayLocked() {
4433 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4434 + " current displayId=" + mActivityDisplay.mDisplayId);
4435
Wale Ogunwale1666e312016-12-16 11:27:18 -08004436 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004437 mActivityDisplay = null;
4438 }
4439
4440 /**
4441 * Move the stack to specified display.
4442 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004443 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004444 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004445 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004446 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4447 + mActivityDisplay + " to display=" + activityDisplay
4448 + " Callers=" + Debug.getCallers(2));
4449
4450 removeFromDisplayLocked();
4451
4452 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004453 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004454 }
4455
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004457 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004458 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004459 }
4460
4461 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004462 public final int startActivityIntentSender(IIntentSender intentSender)
4463 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004464 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4465
4466 if (!(intentSender instanceof PendingIntentRecord)) {
4467 throw new IllegalArgumentException("Bad PendingIntent object");
4468 }
4469
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004470 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004471 Binder.getCallingUid(), mCurrentUser, false,
4472 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004473
4474 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4475 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4476 pendingIntent.key.requestResolvedType);
4477
4478 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4479 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4480 }
4481
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004482 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004483 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004484 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004485 throw new SecurityException(
4486 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4487 }
4488 }
4489
Craig Mautnerdf88d732014-01-27 09:21:32 -08004490 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491 public IBinder asBinder() {
4492 return this;
4493 }
4494
Craig Mautner4504de52013-12-20 09:06:56 -08004495 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004496 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004497 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004498 }
4499
Craig Mautner4a1cb222013-12-04 16:14:06 -08004500 ActivityStackSupervisor getOuter() {
4501 return ActivityStackSupervisor.this;
4502 }
4503
Craig Mautnerd163e752014-06-13 17:18:47 -07004504 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004505 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004506 }
4507
Craig Mautner6985bad2014-04-21 15:22:06 -07004508 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004509 void setVisible(boolean visible) {
4510 if (mVisible != visible) {
4511 mVisible = visible;
4512 if (mCallback != null) {
4513 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4514 0 /* unused */, this).sendToTarget();
4515 }
4516 }
4517 }
4518
Craig Mautner6985bad2014-04-21 15:22:06 -07004519 void setDrawn() {
4520 }
4521
Craig Mautner1b4bf852014-05-26 15:06:32 -07004522 // You can always start a new task on a regular ActivityStack.
4523 boolean isEligibleForNewTasks() {
4524 return true;
4525 }
4526
Craig Mautnerd163e752014-06-13 17:18:47 -07004527 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004528 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004529 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004530 }
4531
Craig Mautner34b73df2014-01-12 21:11:08 -08004532 @Override
4533 public String toString() {
4534 return mIdString + (mActivityDisplay == null ? "N" : "A");
4535 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004536 }
4537
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004538 private class VirtualActivityContainer extends ActivityContainer {
4539 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004540 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004541
4542 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004543 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4544 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004545 mParentActivity = parent;
4546 mCallback = callback;
4547 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004548 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004549 }
4550
4551 @Override
4552 public void setSurface(Surface surface, int width, int height, int density) {
4553 super.setSurface(surface, width, height, density);
4554
4555 synchronized (mService) {
4556 final long origId = Binder.clearCallingIdentity();
4557 try {
4558 setSurfaceLocked(surface, width, height, density);
4559 } finally {
4560 Binder.restoreCallingIdentity(origId);
4561 }
4562 }
4563 }
4564
4565 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4566 if (mContainerState == CONTAINER_STATE_FINISHING) {
4567 return;
4568 }
4569 VirtualActivityDisplay virtualActivityDisplay =
4570 (VirtualActivityDisplay) mActivityDisplay;
4571 if (virtualActivityDisplay == null) {
4572 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004573 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004574 mActivityDisplay = virtualActivityDisplay;
4575 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004576 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004577 }
4578
4579 if (mSurface != null) {
4580 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004581 }
4582
Craig Mautner6985bad2014-04-21 15:22:06 -07004583 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004584 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004585 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004586 } else {
4587 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004588 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004589 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004590 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004591 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004592 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004593
Craig Mautnerd163e752014-06-13 17:18:47 -07004594 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004595
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004596 if (DEBUG_STACK) Slog.d(TAG_STACK,
4597 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004598 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004599
Craig Mautner6985bad2014-04-21 15:22:06 -07004600 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004601 boolean isAttachedLocked() {
4602 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004603 }
4604
4605 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004606 void setDrawn() {
4607 synchronized (mService) {
4608 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004609 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004610 }
4611 }
4612
Craig Mautner1b4bf852014-05-26 15:06:32 -07004613 // Never start a new task on an ActivityView if it isn't explicitly specified.
4614 @Override
4615 boolean isEligibleForNewTasks() {
4616 return false;
4617 }
4618
Craig Mautnerd163e752014-06-13 17:18:47 -07004619 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004620 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004621 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4622 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4623 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4624 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4625 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004626 }
4627 }
4628
Craig Mautner4a1cb222013-12-04 16:14:06 -08004629 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4630 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004631 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004632 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004633 int mDisplayId;
4634 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004635
Craig Mautner4a1cb222013-12-04 16:14:06 -08004636 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4637 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004638 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004639
Jose Lima4b6c6692014-08-12 17:41:12 -07004640 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004641
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004642 /** Array of all UIDs that are present on the display. */
4643 private IntArray mDisplayAccessUIDs = new IntArray();
4644
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004645 ActivityDisplay() {
4646 }
Craig Mautner4504de52013-12-20 09:06:56 -08004647
Craig Mautner1a70a162014-09-13 12:09:31 -07004648 // After instantiation, check that mDisplay is not null before using this. The alternative
4649 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004650 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004651 final Display display = mDisplayManager.getDisplay(displayId);
4652 if (display == null) {
4653 return;
4654 }
4655 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004656 }
4657
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004658 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004659 mDisplay = display;
4660 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004661 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004662
Wale Ogunwale1666e312016-12-16 11:27:18 -08004663 void attachStack(ActivityStack stack, boolean onTop) {
4664 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
4665 + " to displayId=" + mDisplayId + " onTop=" + onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004666 if (onTop) {
4667 mStacks.add(stack);
4668 } else {
4669 mStacks.add(0, stack);
4670 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004671 }
4672
Wale Ogunwale1666e312016-12-16 11:27:18 -08004673 void detachStack(ActivityStack stack) {
4674 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004675 + " from displayId=" + mDisplayId);
4676 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004677 }
4678
Jose Lima4b6c6692014-08-12 17:41:12 -07004679 void setVisibleBehindActivity(ActivityRecord r) {
4680 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004681 }
4682
Jose Lima4b6c6692014-08-12 17:41:12 -07004683 boolean hasVisibleBehindActivity() {
4684 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004685 }
4686
Craig Mautner34b73df2014-01-12 21:11:08 -08004687 @Override
4688 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004689 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4690 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004691
4692 @Override
4693 protected int getChildCount() {
4694 return mStacks.size();
4695 }
4696
4697 @Override
4698 protected ConfigurationContainer getChildAt(int index) {
4699 return mStacks.get(index);
4700 }
4701
4702 @Override
4703 protected ConfigurationContainer getParent() {
4704 return ActivityStackSupervisor.this;
4705 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004706
4707 boolean isPrivate() {
4708 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4709 }
4710
4711 boolean isUidPresent(int uid) {
4712 for (ActivityStack stack : mStacks) {
4713 if (stack.isUidPresent(uid)) {
4714 return true;
4715 }
4716 }
4717 return false;
4718 }
4719
4720 /** Update and get all UIDs that are present on the display and have access to it. */
4721 private IntArray getPresentUIDs() {
4722 mDisplayAccessUIDs.clear();
4723 for (ActivityStack stack : mStacks) {
4724 stack.getPresentUIDs(mDisplayAccessUIDs);
4725 }
4726 return mDisplayAccessUIDs;
4727 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004728
4729 boolean shouldDestroyContentOnRemove() {
4730 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4731 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004732 }
4733
4734 class VirtualActivityDisplay extends ActivityDisplay {
4735 VirtualDisplay mVirtualDisplay;
4736
Craig Mautner6985bad2014-04-21 15:22:06 -07004737 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004738 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004739 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4740 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4741 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4742 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004743
4744 init(mVirtualDisplay.getDisplay());
4745
4746 mWindowManager.handleDisplayAdded(mDisplayId);
4747 }
4748
4749 void setSurface(Surface surface) {
4750 if (mVirtualDisplay != null) {
4751 mVirtualDisplay.setSurface(surface);
4752 }
4753 }
4754
4755 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004756 void detachStack(ActivityStack stack) {
4757 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004758 if (mVirtualDisplay != null) {
4759 mVirtualDisplay.release();
4760 mVirtualDisplay = null;
4761 }
4762 }
4763
4764 @Override
4765 public String toString() {
4766 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004767 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004768 }
Jose Lima58e66d62014-05-27 20:00:27 -07004769
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004770 ActivityStack findStackBehind(ActivityStack stack) {
4771 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004772 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004773 if (display == null) {
4774 return null;
4775 }
4776 final ArrayList<ActivityStack> stacks = display.mStacks;
4777 for (int i = stacks.size() - 1; i >= 0; i--) {
4778 if (stacks.get(i) == stack && i > 0) {
4779 return stacks.get(i - 1);
4780 }
4781 }
4782 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4783 + " in=" + stacks);
4784 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004785
Jorim Jaggic69bd222016-03-15 14:38:37 +01004786 /**
4787 * Puts a task into resizing mode during the next app transition.
4788 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004789 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004790 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004791 private void setResizingDuringAnimation(TaskRecord task) {
4792 mResizingTasksDuringAnimation.add(task.taskId);
4793 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004794 }
4795
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004796 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4797 final TaskRecord task;
4798 final int callingUid;
4799 final String callingPackage;
4800 final Intent intent;
4801 final int userId;
4802 final ActivityOptions activityOptions = (bOptions != null)
4803 ? new ActivityOptions(bOptions) : null;
4804 final int launchStackId = (activityOptions != null)
4805 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004806 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004807 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004808 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004809 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004810
4811 if (launchStackId == DOCKED_STACK_ID) {
4812 mWindowManager.setDockedStackCreateState(
4813 activityOptions.getDockCreateMode(), null /* initialBounds */);
4814
4815 // Defer updating the stack in which recents is until the app transition is done, to
4816 // not run into issues where we still need to draw the task in recents but the
4817 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004818 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004819 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4820 }
4821
Winson Chung7900bee2017-03-10 08:59:25 -08004822 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4823 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004824 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004825 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004826 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004827 throw new IllegalArgumentException(
4828 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4829 }
4830
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004831 // Since we don't have an actual source record here, we assume that the currently focused
4832 // activity was the source.
4833 final ActivityStack focusedStack = getFocusedStack();
4834 final ActivityRecord sourceRecord =
4835 focusedStack != null ? focusedStack.topActivity() : null;
4836
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004837 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004838 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004839 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4840 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004841 }
4842 }
4843
4844 // If the user must confirm credentials (e.g. when first launching a work app and the
4845 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4846 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4847 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004848 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004849 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004850 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004851 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4852 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004853
4854 // If we are launching the task in the docked stack, put it into resizing mode so
4855 // the window renders full-screen with the background filling the void. Also only
4856 // call this at the end to make sure that tasks exists on the window manager side.
4857 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004858 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004859 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004860
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004861 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004862 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004863 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4864 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004865 return ActivityManager.START_TASK_TO_FRONT;
4866 }
4867 callingUid = task.mCallingUid;
4868 callingPackage = task.mCallingPackage;
4869 intent = task.intent;
4870 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4871 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004872 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4873 null, null, 0, 0, bOptions, userId, null, task);
4874 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004875 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004876 }
4877 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004878 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004879
4880 /**
4881 * @return a list of activities which are the top ones in each visible stack. The first
4882 * entry will be the focused activity.
4883 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004884 List<IBinder> getTopVisibleActivities() {
4885 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4886 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004887 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004888 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4889 // Traverse all stacks on a display.
4890 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4891 final ActivityStack stack = display.mStacks.get(j);
4892 // Get top activity from a visible stack and add it to the list.
4893 if (stack.getStackVisibilityLocked(null /* starting */)
4894 == ActivityStack.STACK_VISIBLE) {
4895 final ActivityRecord top = stack.topActivity();
4896 if (top != null) {
4897 if (stack == mFocusedStack) {
4898 topActivityTokens.add(0, top.appToken);
4899 } else {
4900 topActivityTokens.add(top.appToken);
4901 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004902 }
4903 }
4904 }
4905 }
4906 return topActivityTokens;
4907 }
Craig Mautner27084302013-03-25 08:05:25 -07004908}