blob: 42efe0b5d8e0e9415d02cd40044b56a73ce8eedd [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
Craig Mautnerac6f8432013-07-17 13:24:59 -07002887 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002888 mTmpFindTaskResult.r = null;
2889 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002890 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002891 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2892 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002893 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2894 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002895 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002896 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002897 continue;
2898 }
2899 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002900 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2901 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 continue;
2903 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002904 stack.findTaskLocked(r, mTmpFindTaskResult);
2905 // It is possible to have task in multiple stacks with the same root affinity.
2906 // If the match we found was based on root affinity we keep on looking to see if
2907 // there is a better match in another stack. We eventually return the match based
2908 // on root affinity if we don't find a better match.
2909 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2910 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002911 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002912 }
2913 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002914 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2915 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002916 }
2917
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002918 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2919 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002920 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2921 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002922 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002923 final ActivityRecord ar = stacks.get(stackNdx)
2924 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002925 if (ar != null) {
2926 return ar;
2927 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002928 }
2929 }
2930 return null;
2931 }
2932
Craig Mautner8d341ef2013-03-26 09:03:27 -07002933 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002934 scheduleSleepTimeout();
2935 if (!mGoingToSleep.isHeld()) {
2936 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002937 if (mLaunchingActivity.isHeld()) {
2938 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2939 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002940 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002941 mLaunchingActivity.release();
2942 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002943 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002944 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002945 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
2947
2948 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002949 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002950
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002951 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002952 final long endTime = System.currentTimeMillis() + timeout;
2953 while (true) {
2954 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002955 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2956 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002957 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2958 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2959 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002960 }
2961 if (cantShutdown) {
2962 long timeRemaining = endTime - System.currentTimeMillis();
2963 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002964 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002965 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002966 } catch (InterruptedException e) {
2967 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002968 } else {
2969 Slog.w(TAG, "Activity manager shutdown timed out");
2970 timedout = true;
2971 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002972 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002973 } else {
2974 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 }
2976 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002977
2978 // Force checkReadyForSleep to complete.
2979 mSleepTimeout = true;
2980 checkReadyForSleepLocked();
2981
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 return timedout;
2983 }
2984
2985 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002986 removeSleepTimeouts();
2987 if (mGoingToSleep.isHeld()) {
2988 mGoingToSleep.release();
2989 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2991 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2993 final ActivityStack stack = stacks.get(stackNdx);
2994 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002995 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002996 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002997 }
Craig Mautner5314a402013-09-26 12:40:16 -07002998 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002999 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003000 mGoingToSleepActivities.clear();
3001 }
3002
3003 void activitySleptLocked(ActivityRecord r) {
3004 mGoingToSleepActivities.remove(r);
3005 checkReadyForSleepLocked();
3006 }
3007
3008 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003009 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003010 // Do not care.
3011 return;
3012 }
3013
3014 if (!mSleepTimeout) {
3015 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3017 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3019 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3020 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021 }
3022
3023 if (mStoppingActivities.size() > 0) {
3024 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003025 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003026 + mStoppingActivities.size() + " activities");
3027 scheduleIdleLocked();
3028 dontSleep = true;
3029 }
3030
3031 if (mGoingToSleepActivities.size() > 0) {
3032 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003033 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003034 + mGoingToSleepActivities.size() + " activities");
3035 dontSleep = true;
3036 }
3037
3038 if (dontSleep) {
3039 return;
3040 }
3041 }
3042
Wei Wang65c7a152016-06-02 18:51:22 -07003043 // Send launch end powerhint before going sleep
3044 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3045
Craig Mautnere0a38842013-12-16 16:14:02 -08003046 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3047 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003048 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3049 stacks.get(stackNdx).goToSleep();
3050 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003051 }
3052
3053 removeSleepTimeouts();
3054
3055 if (mGoingToSleep.isHeld()) {
3056 mGoingToSleep.release();
3057 }
3058 if (mService.mShuttingDown) {
3059 mService.notifyAll();
3060 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003061 }
3062
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003063 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003064 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003065 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003066 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003067 }
3068 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003069 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003070 mWindowManager.executeAppTransition();
3071 return true;
3072 }
3073 return false;
3074 }
3075
Craig Mautner8d341ef2013-03-26 09:03:27 -07003076 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3078 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003079 int stackNdx = stacks.size() - 1;
3080 while (stackNdx >= 0) {
3081 stacks.get(stackNdx).handleAppCrashLocked(app);
3082 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003083 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003084 }
3085 }
3086
Jose Lima4b6c6692014-08-12 17:41:12 -07003087 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003088 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003089 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003090 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3091 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003092 return false;
3093 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003094
3095 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3096 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3097 + " visible=" + visible + " stackId=" + stack.mStackId
3098 + " can't contain visible behind activities");
3099 return false;
3100 }
3101
Jose Lima4b6c6692014-08-12 17:41:12 -07003102 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003103 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3104 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003105
3106 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003107 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003108 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003109 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003110 return true;
3111 }
3112
3113 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003114 if (visible && top.fullscreen) {
3115 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003116 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3117 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3118 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3119 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003120 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003121 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3122 // Only the activity set as currently visible behind should actively reset its
3123 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003124 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3125 "requestVisibleBehind: returning visible=" + visible
3126 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3127 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003128 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003129 }
3130
Jose Lima4b6c6692014-08-12 17:41:12 -07003131 stack.setVisibleBehindActivity(visible ? r : null);
3132 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003133 // If there is a translucent home activity, we need to force it stop being translucent,
3134 // because we can't depend on the application to necessarily perform that operation.
3135 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003136 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003137 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003138 mService.convertFromTranslucent(next.appToken);
3139 }
3140 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003141 if (top.app != null && top.app.thread != null) {
3142 // Notify the top app of the change.
3143 try {
3144 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3145 } catch (RemoteException e) {
3146 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003147 }
3148 return true;
3149 }
3150
Craig Mautnerbb742462014-07-07 15:28:55 -07003151 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003152 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003153 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003154 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003155
3156 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003157 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003158 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003159 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003160 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003161
3162 // When launching tasks behind, update the last active time of the top task after the new
3163 // task has been shown briefly
3164 final ActivityRecord top = stack.topActivity();
3165 if (top != null) {
3166 top.task.touchActiveTime();
3167 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003168 }
3169
3170 void scheduleLaunchTaskBehindComplete(IBinder token) {
3171 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3172 }
3173
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003174 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3175 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003176 mKeyguardController.beginActivityVisibilityUpdate();
3177 try {
3178 // First the front stacks. In case any are not fullscreen and are in front of home.
3179 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3180 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3181 final int topStackNdx = stacks.size() - 1;
3182 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3183 final ActivityStack stack = stacks.get(stackNdx);
3184 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3185 }
Craig Mautner580ea812013-04-25 12:58:38 -07003186 }
Jorim Jaggife762342016-10-13 14:33:27 +02003187 } finally {
3188 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003189 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003190 }
3191
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003192 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3193 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3194 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3195 final int topStackNdx = stacks.size() - 1;
3196 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3197 final ActivityStack stack = stacks.get(stackNdx);
3198 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3199 }
3200 }
3201 }
3202
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003203 void invalidateTaskLayers() {
3204 mTaskLayersChanged = true;
3205 }
3206
3207 void rankTaskLayersIfNeeded() {
3208 if (!mTaskLayersChanged) {
3209 return;
3210 }
3211 mTaskLayersChanged = false;
3212 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3213 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3214 int baseLayer = 0;
3215 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3216 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3217 }
3218 }
3219 }
3220
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003221 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3222 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3223 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3224 final int topStackNdx = stacks.size() - 1;
3225 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3226 final ActivityStack stack = stacks.get(stackNdx);
3227 stack.clearOtherAppTimeTrackers(except);
3228 }
3229 }
3230 }
3231
Craig Mautner8d341ef2013-03-26 09:03:27 -07003232 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003233 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3234 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003235 final int numStacks = stacks.size();
3236 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3237 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003238 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003240 }
3241 }
3242
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003243 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3244 // Examine all activities currently running in the process.
3245 TaskRecord firstTask = null;
3246 // Tasks is non-null only if two or more tasks are found.
3247 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003248 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3249 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003250 ActivityRecord r = app.activities.get(i);
3251 // First, if we find an activity that is in the process of being destroyed,
3252 // then we just aren't going to do anything for now; we want things to settle
3253 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003254 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003255 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003256 return;
3257 }
3258 // Don't consider any activies that are currently not in a state where they
3259 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003260 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3261 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003262 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003263 continue;
3264 }
3265 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003266 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003267 + " from " + r);
3268 if (firstTask == null) {
3269 firstTask = r.task;
3270 } else if (firstTask != r.task) {
3271 if (tasks == null) {
3272 tasks = new ArraySet<>();
3273 tasks.add(firstTask);
3274 }
3275 tasks.add(r.task);
3276 }
3277 }
3278 }
3279 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003280 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003281 return;
3282 }
3283 // If we have activities in multiple tasks that are in a position to be destroyed,
3284 // let's iterate through the tasks and release the oldest one.
3285 final int numDisplays = mActivityDisplays.size();
3286 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3287 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3288 // Step through all stacks starting from behind, to hit the oldest things first.
3289 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3290 final ActivityStack stack = stacks.get(stackNdx);
3291 // Try to release activities in this stack; if we manage to, we are done.
3292 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3293 return;
3294 }
3295 }
3296 }
3297 }
3298
Amith Yamasani37a40c22015-06-17 13:25:42 -07003299 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003300 final int focusStackId = mFocusedStack.getStackId();
3301 // We dismiss the docked stack whenever we switch users.
3302 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003303 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3304 // also cause all tasks to be moved to the fullscreen stack at a position that is
3305 // appropriate.
3306 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003307
3308 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003309 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003310 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003311
Craig Mautner858d8a62013-04-23 17:08:34 -07003312 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003313 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3314 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003316 final ActivityStack stack = stacks.get(stackNdx);
3317 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003318 TaskRecord task = stack.topTask();
3319 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003320 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003321 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003323 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003324
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003325 ActivityStack stack = getStack(restoreStackId);
3326 if (stack == null) {
3327 stack = mHomeStack;
3328 }
3329 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003330 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003331 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003332 } else {
3333 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003334 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003335 }
Craig Mautner93529a42013-10-04 15:03:13 -07003336 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003337 }
3338
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003339 /** Checks whether the userid is a profile of the current user. */
3340 boolean isCurrentProfileLocked(int userId) {
3341 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003342 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003343 }
3344
Winson Chung4dabf232017-01-25 13:25:22 -08003345 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3346 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003347 ArrayList<ActivityRecord> stops = null;
3348
3349 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003350 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3351 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003352 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3353 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003354 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003355 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003356 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3357 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003358 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003359 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003360 if (s.finishing) {
3361 // If this activity is finishing, it is sitting on top of
3362 // everyone else but we now know it is no longer needed...
3363 // so get rid of it. Otherwise, we need to go through the
3364 // normal flow and hide it once we determine that it is
3365 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003366 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003367 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003368 }
3369 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003370 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003371 if (!processPausingActivities && s.state == PAUSING) {
3372 // Defer processing pausing activities in this iteration and reschedule
3373 // a delayed idle to reprocess it again
3374 removeTimeoutsForActivityLocked(idleActivity);
3375 scheduleIdleTimeoutLocked(idleActivity);
3376 continue;
3377 }
3378
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003379 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003380 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003381 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003382 }
3383 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003384 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003385 }
3386 }
3387
3388 return stops;
3389 }
3390
Craig Mautnercf910b02013-04-23 11:23:27 -07003391 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003392 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3393 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3394 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3395 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003396 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003397 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003398 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003399 if (r == null) Slog.e(TAG,
3400 "validateTop...: null top activity, stack=" + stack);
3401 else {
3402 final ActivityRecord pausing = stack.mPausingActivity;
3403 if (pausing != null && pausing == r) Slog.e(TAG,
3404 "validateTop...: top stack has pausing activity r=" + r
3405 + " state=" + state);
3406 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3407 "validateTop...: activity in front not resumed r=" + r
3408 + " state=" + state);
3409 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003410 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003411 final ActivityRecord resumed = stack.mResumedActivity;
3412 if (resumed != null && resumed == r) Slog.e(TAG,
3413 "validateTop...: back stack has resumed activity r=" + r
3414 + " state=" + state);
3415 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3416 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003417 }
3418 }
3419 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003420 }
3421
Craig Mautnere0570202015-05-13 13:06:11 -07003422 private String lockTaskModeToString() {
3423 switch (mLockTaskModeState) {
3424 case LOCK_TASK_MODE_LOCKED:
3425 return "LOCKED";
3426 case LOCK_TASK_MODE_PINNED:
3427 return "PINNED";
3428 case LOCK_TASK_MODE_NONE:
3429 return "NONE";
3430 default: return "unknown=" + mLockTaskModeState;
3431 }
3432 }
3433
Craig Mautner27084302013-03-25 08:05:25 -07003434 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003435 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003436 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003437 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003438 pw.print(prefix);
3439 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003440 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003441 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003442 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003443 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3444 if (packages.size() > 0) {
3445 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3446 for (int i = 0; i < packages.size(); ++i) {
3447 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3448 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3449 }
3450 }
3451 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3452 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003453 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003454
Winson43d1f262016-06-14 16:05:55 -07003455 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003456 * Dump all connected displays' configurations.
3457 * @param prefix Prefix to apply to each line of the dump.
3458 */
3459 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3460 pw.print(prefix); pw.println("Display override configurations:");
3461 final int displayCount = mActivityDisplays.size();
3462 for (int i = 0; i < displayCount; i++) {
3463 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3464 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3465 pw.println(activityDisplay.getOverrideConfiguration());
3466 }
3467 }
3468
3469 /**
Winson43d1f262016-06-14 16:05:55 -07003470 * Dumps the activities matching the given {@param name} in the either the focused stack
3471 * or all visible stacks if {@param dumpVisibleStacks} is true.
3472 */
Winson Chung6998bc42017-02-28 17:07:05 -08003473 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3474 boolean dumpFocusedStackOnly) {
3475 if (dumpFocusedStackOnly) {
3476 return mFocusedStack.getDumpActivitiesLocked(name);
3477 } else {
Winson43d1f262016-06-14 16:05:55 -07003478 ArrayList<ActivityRecord> activities = new ArrayList<>();
3479 int numDisplays = mActivityDisplays.size();
3480 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3481 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3482 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3483 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003484 if (!dumpVisibleStacksOnly ||
3485 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003486 activities.addAll(stack.getDumpActivitiesLocked(name));
3487 }
3488 }
3489 }
3490 return activities;
Winson43d1f262016-06-14 16:05:55 -07003491 }
Craig Mautner20e72272013-04-01 13:45:53 -07003492 }
3493
Dianne Hackborn390517b2013-05-30 15:03:32 -07003494 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3495 boolean needSep, String prefix) {
3496 if (activity != null) {
3497 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3498 if (needSep) {
3499 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003500 }
3501 pw.print(prefix);
3502 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003503 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003504 }
3505 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003506 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003507 }
3508
Craig Mautner8d341ef2013-03-26 09:03:27 -07003509 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3510 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003511 boolean printed = false;
3512 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003513 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3514 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003515 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003516 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003517 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003518 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003519 final ActivityStack stack = stacks.get(stackNdx);
3520 StringBuilder stackHeader = new StringBuilder(128);
3521 stackHeader.append(" Stack #");
3522 stackHeader.append(stack.mStackId);
3523 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003524 stackHeader.append("\n");
3525 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3526 stackHeader.append("\n");
3527 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003528 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3529 needSep, stackHeader.toString());
3530 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3531 !dumpAll, false, dumpPackage, true,
3532 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003533
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 needSep = printed;
3535 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3536 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003537 if (pr) {
3538 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003539 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003540 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003541 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3542 " mResumedActivity: ");
3543 if (pr) {
3544 printed = true;
3545 needSep = false;
3546 }
3547 if (dumpAll) {
3548 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3549 " mLastPausedActivity: ");
3550 if (pr) {
3551 printed = true;
3552 needSep = true;
3553 }
3554 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3555 needSep, " mLastNoHistoryActivity: ");
3556 }
3557 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003558 }
3559 }
3560
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003561 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3562 false, dumpPackage, true, " Activities waiting to finish:", null);
3563 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3564 false, dumpPackage, true, " Activities waiting to stop:", null);
3565 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3566 false, dumpPackage, true, " Activities waiting for another to become visible:",
3567 null);
3568 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3569 false, dumpPackage, true, " Activities waiting to sleep:", null);
3570 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3571 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003572
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003573 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003574 }
3575
Dianne Hackborn390517b2013-05-30 15:03:32 -07003576 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003577 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003578 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003579 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003580 String innerPrefix = null;
3581 String[] args = null;
3582 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003583 for (int i=list.size()-1; i>=0; i--) {
3584 final ActivityRecord r = list.get(i);
3585 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3586 continue;
3587 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003588 if (innerPrefix == null) {
3589 innerPrefix = prefix + " ";
3590 args = new String[0];
3591 }
3592 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003593 final boolean full = !brief && (complete || !r.isInHistory());
3594 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003595 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003596 needNL = false;
3597 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003598 if (header1 != null) {
3599 pw.println(header1);
3600 header1 = null;
3601 }
3602 if (header2 != null) {
3603 pw.println(header2);
3604 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003605 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003606 if (lastTask != r.task) {
3607 lastTask = r.task;
3608 pw.print(prefix);
3609 pw.print(full ? "* " : " ");
3610 pw.println(lastTask);
3611 if (full) {
3612 lastTask.dump(pw, prefix + " ");
3613 } else if (complete) {
3614 // Complete + brief == give a summary. Isn't that obvious?!?
3615 if (lastTask.intent != null) {
3616 pw.print(prefix); pw.print(" ");
3617 pw.println(lastTask.intent.toInsecureStringWithClip());
3618 }
3619 }
3620 }
3621 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3622 pw.print(" #"); pw.print(i); pw.print(": ");
3623 pw.println(r);
3624 if (full) {
3625 r.dump(pw, innerPrefix);
3626 } else if (complete) {
3627 // Complete + brief == give a summary. Isn't that obvious?!?
3628 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3629 if (r.app != null) {
3630 pw.print(innerPrefix); pw.println(r.app);
3631 }
3632 }
3633 if (client && r.app != null && r.app.thread != null) {
3634 // flush anything that is already in the PrintWriter since the thread is going
3635 // to write to the file descriptor directly
3636 pw.flush();
3637 try {
3638 TransferPipe tp = new TransferPipe();
3639 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003640 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003641 // Short timeout, since blocking here can
3642 // deadlock with the application.
3643 tp.go(fd, 2000);
3644 } finally {
3645 tp.kill();
3646 }
3647 } catch (IOException e) {
3648 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3649 } catch (RemoteException e) {
3650 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3651 }
3652 needNL = true;
3653 }
3654 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003655 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003656 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003657
Craig Mautnerf3333272013-04-22 10:55:53 -07003658 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003659 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3660 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003661 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3662 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003663 }
3664
3665 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003666 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003667 }
3668
3669 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003670 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3671 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003672 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3673 }
3674
Craig Mautner05d29032013-05-03 13:40:13 -07003675 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003676 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3677 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3678 }
Craig Mautner05d29032013-05-03 13:40:13 -07003679 }
3680
Craig Mautner0eea92c2013-05-16 13:35:39 -07003681 void removeSleepTimeouts() {
3682 mSleepTimeout = false;
3683 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3684 }
3685
3686 final void scheduleSleepTimeout() {
3687 removeSleepTimeouts();
3688 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3689 }
3690
Craig Mautner4a1cb222013-12-04 16:14:06 -08003691 @Override
3692 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003693 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003694 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3695 }
3696
3697 @Override
3698 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003699 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003700 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3701 }
3702
3703 @Override
3704 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003705 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003706 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3707 }
3708
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003709 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003710 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003711 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003712 newDisplay = mActivityDisplays.get(displayId) == null;
3713 if (newDisplay) {
3714 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003715 if (activityDisplay.mDisplay == null) {
3716 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3717 return;
3718 }
Craig Mautner4504de52013-12-20 09:06:56 -08003719 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003720 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003721 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003722 }
Craig Mautner4504de52013-12-20 09:06:56 -08003723 if (newDisplay) {
3724 mWindowManager.onDisplayAdded(displayId);
3725 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003726 }
3727
Andrii Kulianca007a62016-11-22 16:33:28 -08003728 /** Check if display with specified id is added to the list. */
3729 boolean isDisplayAdded(int displayId) {
3730 return mActivityDisplays.get(displayId) != null;
3731 }
3732
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003733 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003734 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003735 mService.mContext.getResources().getDimensionPixelSize(
3736 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003737 }
3738
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003739 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003740 if (displayId == DEFAULT_DISPLAY) {
3741 throw new IllegalArgumentException("Can't remove the primary display.");
3742 }
3743
Craig Mautner4a1cb222013-12-04 16:14:06 -08003744 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003745 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3746 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003747 final boolean destroyContentOnRemoval
3748 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003749 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3750 while (!stacks.isEmpty()) {
3751 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003752 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003753 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3754 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003755 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003756 } else {
3757 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3758 // a valid launch stack for all activities. This way the task history from
3759 // external display will be preserved on primary after move.
3760 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003761 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003763 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003764 }
3765 }
3766 mWindowManager.onDisplayRemoved(displayId);
3767 }
3768
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003769 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003770 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003771 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3772 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 // TODO: Update the bounds.
3774 }
3775 }
3776 mWindowManager.onDisplayChanged(displayId);
3777 }
3778
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003779 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003780 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003781 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003782 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003783 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003785 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003786 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003787 info.position = display != null
3788 ? display.mStacks.indexOf(stack)
3789 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790
3791 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3792 final int numTasks = tasks.size();
3793 int[] taskIds = new int[numTasks];
3794 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003795 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003796 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 for (int i = 0; i < numTasks; ++i) {
3798 final TaskRecord task = tasks.get(i);
3799 taskIds[i] = task.taskId;
3800 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3801 : task.realActivity != null ? task.realActivity.flattenToString()
3802 : task.getTopActivity() != null ? task.getTopActivity().packageName
3803 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003804 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003805 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003806 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 }
3808 info.taskIds = taskIds;
3809 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003810 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003811 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003812
3813 final ActivityRecord top = stack.topRunningActivityLocked();
3814 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 return info;
3816 }
3817
3818 StackInfo getStackInfoLocked(int stackId) {
3819 ActivityStack stack = getStack(stackId);
3820 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003821 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003822 }
3823 return null;
3824 }
3825
3826 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003827 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003831 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 }
3833 }
3834 return list;
3835 }
3836
Craig Mautner15df08a2015-04-01 12:17:18 -07003837 TaskRecord getLockedTaskLocked() {
3838 final int top = mLockTaskModeTasks.size() - 1;
3839 if (top >= 0) {
3840 return mLockTaskModeTasks.get(top);
3841 }
3842 return null;
3843 }
3844
3845 boolean isLockedTask(TaskRecord task) {
3846 return mLockTaskModeTasks.contains(task);
3847 }
3848
3849 boolean isLastLockedTask(TaskRecord task) {
3850 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3851 }
3852
3853 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003854 if (!mLockTaskModeTasks.remove(task)) {
3855 return;
3856 }
3857 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3858 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003859 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003860 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3861 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003862 final Message lockTaskMsg = Message.obtain();
3863 lockTaskMsg.arg1 = task.userId;
3864 lockTaskMsg.what = LOCK_TASK_END_MSG;
3865 mHandler.sendMessage(lockTaskMsg);
3866 }
3867 }
3868
Andrii Kulianc27916642016-04-12 17:59:27 -07003869 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3870 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3871 false /* forceNonResizable */);
3872 }
3873
Jorim Jaggid53f0922016-04-06 22:16:23 -07003874 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003875 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003876 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3877 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003878 return;
3879 }
3880
Jorim Jaggicd13d332016-04-27 15:40:20 -07003881 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003882 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003883 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003884 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003885
Andrii Kulianc27916642016-04-12 17:59:27 -07003886 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3887 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003888 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003889 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003890 && !topActivity.noDisplay) {
3891 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003892 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3893 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003894 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003895 }
3896
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003897 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003898 if (mLockTaskNotify != null) {
3899 mLockTaskNotify.showToast(mLockTaskModeState);
3900 }
Jason Monka8f569c2014-07-07 12:15:21 -04003901 }
3902
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003903 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3904 if (mLockTaskModeTasks.contains(task)) {
3905 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3906 }
3907 }
3908
Craig Mautner432f64e2015-05-20 14:59:57 -07003909 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3910 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003911 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003912 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003913 final TaskRecord lockedTask = getLockedTaskLocked();
3914 if (lockedTask != null) {
3915 removeLockedTaskLocked(lockedTask);
3916 if (!mLockTaskModeTasks.isEmpty()) {
3917 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003918 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3919 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003920 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003921 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003922 return;
3923 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003924 }
Craig Mautnere0570202015-05-13 13:06:11 -07003925 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3926 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003927 return;
3928 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003929
3930 // Should have already been checked, but do it again.
3931 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003932 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3933 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003934 return;
3935 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003936 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003937 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003938 return;
3939 }
3940
3941 if (mLockTaskModeTasks.isEmpty()) {
3942 // First locktask.
3943 final Message lockTaskMsg = Message.obtain();
3944 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3945 lockTaskMsg.arg1 = task.userId;
3946 lockTaskMsg.what = LOCK_TASK_START_MSG;
3947 lockTaskMsg.arg2 = lockTaskModeState;
3948 mHandler.sendMessage(lockTaskMsg);
3949 }
3950 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003951 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3952 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003953 mLockTaskModeTasks.remove(task);
3954 mLockTaskModeTasks.add(task);
3955
3956 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003957 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003958 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003959
3960 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003961 findTaskToMoveToFrontLocked(task, 0, null, reason,
3962 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003963 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003964 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003965 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003966 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003967 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003968 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003969 }
3970
3971 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003972 return isLockTaskModeViolation(task, false);
3973 }
3974
3975 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3976 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003977 return false;
3978 }
3979 final int lockTaskAuth = task.mLockTaskAuth;
3980 switch (lockTaskAuth) {
3981 case LOCK_TASK_AUTH_DONT_LOCK:
3982 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003983 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003984 case LOCK_TASK_AUTH_LAUNCHABLE:
3985 case LOCK_TASK_AUTH_WHITELISTED:
3986 return false;
3987 case LOCK_TASK_AUTH_PINNABLE:
3988 // Pinnable tasks can't be launched on top of locktask tasks.
3989 return !mLockTaskModeTasks.isEmpty();
3990 default:
3991 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3992 return true;
3993 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003994 }
3995
Craig Mautner15df08a2015-04-01 12:17:18 -07003996 void onLockTaskPackagesUpdatedLocked() {
3997 boolean didSomething = false;
3998 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3999 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004000 final boolean wasWhitelisted =
4001 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4002 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004003 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004004 final boolean isWhitelisted =
4005 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4006 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4007 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004008 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004009 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4010 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004011 removeLockedTaskLocked(lockedTask);
4012 lockedTask.performClearTaskLocked();
4013 didSomething = true;
4014 }
4015 }
4016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4017 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4019 final ActivityStack stack = stacks.get(stackNdx);
4020 stack.onLockTaskPackagesUpdatedLocked();
4021 }
4022 }
Craig Mautnere0570202015-05-13 13:06:11 -07004023 final ActivityRecord r = topRunningActivityLocked();
4024 final TaskRecord task = r != null ? r.task : null;
4025 if (mLockTaskModeTasks.isEmpty() && task != null
4026 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4027 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004028 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4029 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4030 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4031 false);
4032 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004033 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004034 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004035 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004036 }
4037 }
4038
Benjamin Franz43261142015-02-11 15:59:44 +00004039 int getLockTaskModeState() {
4040 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004041 }
4042
Jorim Jaggife89d122015-12-22 16:28:44 +01004043 void activityRelaunchedLocked(IBinder token) {
4044 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004045 if (mService.isSleepingOrShuttingDownLocked()) {
4046 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4047 if (r != null) {
4048 r.setSleeping(true, true);
4049 }
4050 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004051 }
4052
4053 void activityRelaunchingLocked(ActivityRecord r) {
4054 mWindowManager.notifyAppRelaunching(r.appToken);
4055 }
4056
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004057 void logStackState() {
4058 mActivityMetricsLogger.logWindowState();
4059 }
4060
Andrii Kulian933076d2016-03-29 17:04:42 -07004061 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004062 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4063 final ActivityRecord r = task.mActivities.get(i);
4064 if (r.app != null && r.app.thread != null) {
4065 mMultiWindowModeChangedActivities.add(r);
4066 }
4067 }
4068
4069 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4070 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4071 }
4072 }
4073
Andrii Kulian933076d2016-03-29 17:04:42 -07004074 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004075 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004076 if (prevStack == null || prevStack == stack
4077 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4078 return;
4079 }
4080
4081 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4082 final ActivityRecord r = task.mActivities.get(i);
4083 if (r.app != null && r.app.thread != null) {
4084 mPipModeChangedActivities.add(r);
4085 }
4086 }
4087
4088 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4089 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4090 }
4091 }
4092
Tony Mak853304c2016-04-18 15:17:41 +01004093 void setDockedStackMinimized(boolean minimized) {
4094 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004095 }
4096
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004097 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004098
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004099 public ActivityStackSupervisorHandler(Looper looper) {
4100 super(looper);
4101 }
4102
Winson Chung4dabf232017-01-25 13:25:22 -08004103 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004104 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004105 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4106 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004107 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004108 }
4109
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004110 @Override
4111 public void handleMessage(Message msg) {
4112 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004113 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4114 synchronized (mService) {
4115 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4116 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004117 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004118 }
4119 }
4120 } break;
4121 case REPORT_PIP_MODE_CHANGED_MSG: {
4122 synchronized (mService) {
4123 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4124 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004125 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004126 }
4127 }
4128 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004129 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004130 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4131 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004132 if (mService.mDidDexOpt) {
4133 mService.mDidDexOpt = false;
4134 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4135 nmsg.obj = msg.obj;
4136 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4137 return;
4138 }
4139 // We don't at this point know if the activity is fullscreen,
4140 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004141 activityIdleInternal((ActivityRecord) msg.obj,
4142 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004143 } break;
4144 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004145 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004146 activityIdleInternal((ActivityRecord) msg.obj,
4147 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004148 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004149 case RESUME_TOP_ACTIVITY_MSG: {
4150 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004151 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004152 }
4153 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004154 case SLEEP_TIMEOUT_MSG: {
4155 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004156 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004157 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4158 mSleepTimeout = true;
4159 checkReadyForSleepLocked();
4160 }
4161 }
4162 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004163 case LAUNCH_TIMEOUT_MSG: {
4164 if (mService.mDidDexOpt) {
4165 mService.mDidDexOpt = false;
4166 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4167 return;
4168 }
4169 synchronized (mService) {
4170 if (mLaunchingActivity.isHeld()) {
4171 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4172 if (VALIDATE_WAKE_LOCK_CALLER
4173 && Binder.getCallingUid() != Process.myUid()) {
4174 throw new IllegalStateException("Calling must be system uid");
4175 }
4176 mLaunchingActivity.release();
4177 }
4178 }
4179 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004181 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004182 } break;
4183 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004184 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004185 } break;
4186 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004187 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004189 case CONTAINER_CALLBACK_VISIBILITY: {
4190 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004191 final IActivityContainerCallback callback = container.mCallback;
4192 if (callback != null) {
4193 try {
4194 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4195 } catch (RemoteException e) {
4196 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004197 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004198 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004199 case LOCK_TASK_START_MSG: {
4200 // When lock task starts, we disable the status bars.
4201 try {
Jason Monk62515be2014-05-21 16:06:19 -04004202 if (mLockTaskNotify == null) {
4203 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004204 }
Jason Monk62515be2014-05-21 16:06:19 -04004205 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004206 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004207 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004208 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004209 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004210 flags = StatusBarManager.DISABLE_MASK
4211 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004212 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004213 flags = StatusBarManager.DISABLE_MASK
4214 & (~StatusBarManager.DISABLE_BACK)
4215 & (~StatusBarManager.DISABLE_HOME)
4216 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004217 }
4218 getStatusBarService().disable(flags, mToken,
4219 mService.mContext.getPackageName());
4220 }
4221 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004222 if (getDevicePolicyManager() != null) {
4223 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004224 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004225 }
justinzhang5286d3f2014-05-12 17:06:01 -04004226 } catch (RemoteException ex) {
4227 throw new RuntimeException(ex);
4228 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004229 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004230 case LOCK_TASK_END_MSG: {
4231 // When lock task ends, we enable the status bars.
4232 try {
Jason Monk62515be2014-05-21 16:06:19 -04004233 if (getStatusBarService() != null) {
4234 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4235 mService.mContext.getPackageName());
4236 }
4237 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004238 if (getDevicePolicyManager() != null) {
4239 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4240 msg.arg1);
4241 }
Jason Monk62515be2014-05-21 16:06:19 -04004242 if (mLockTaskNotify == null) {
4243 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4244 }
4245 mLockTaskNotify.show(false);
4246 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004247 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004248 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004249 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004250 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004251 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004252 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004253 new LockPatternUtils(mService.mContext)
4254 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004255 }
4256 } catch (SettingNotFoundException e) {
4257 // No setting, don't lock.
4258 }
justinzhang5286d3f2014-05-12 17:06:01 -04004259 } catch (RemoteException ex) {
4260 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004261 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004262 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004263 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004264 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004265 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4266 if (mLockTaskNotify == null) {
4267 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4268 }
4269 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4270 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004271 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4272 final ActivityContainer container = (ActivityContainer) msg.obj;
4273 final IActivityContainerCallback callback = container.mCallback;
4274 if (callback != null) {
4275 try {
4276 callback.onAllActivitiesComplete(container.asBinder());
4277 } catch (RemoteException e) {
4278 }
4279 }
4280 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004281 case LAUNCH_TASK_BEHIND_COMPLETE: {
4282 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004283 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004284 if (r != null) {
4285 handleLaunchTaskBehindCompleteLocked(r);
4286 }
4287 }
4288 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004290 }
4291 }
4292 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004293
Ying Wangb081a592014-04-22 15:20:16 -07004294 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004295 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4296 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004297 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004298 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004299 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004300 ActivityRecord mParentActivity = null;
4301 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004302
Craig Mautnere3a00d72014-04-16 08:31:19 -07004303 boolean mVisible = true;
4304
Craig Mautner4a1cb222013-12-04 16:14:06 -08004305 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004306 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004307
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004308 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4309 final static int CONTAINER_STATE_NO_SURFACE = 1;
4310 final static int CONTAINER_STATE_FINISHING = 2;
4311 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4312
Wale Ogunwale1666e312016-12-16 11:27:18 -08004313 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004314 synchronized (mService) {
4315 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004316 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004317 switch (mStackId) {
4318 case PINNED_STACK_ID:
4319 new PinnedActivityStack(this, mRecentTasks, onTop);
4320 break;
4321 default:
4322 new ActivityStack(this, mRecentTasks, onTop);
4323 break;
4324 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004325 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004326 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004327 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004328 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004329
Andrii Kulian839def92016-11-02 10:58:58 -07004330 /**
4331 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004332 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004333 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004334 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004335 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004336 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004337 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004338 if (mActivityDisplay != null) {
4339 throw new IllegalStateException("ActivityContainer is already attached, " +
4340 "displayId=" + mActivityDisplay.mDisplayId);
4341 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004342 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004343 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004344 }
4345
4346 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004347 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004349 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4350 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004351 return;
4352 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004353 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004354 }
4355 }
4356
4357 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004358 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004359 synchronized (mService) {
4360 if (mActivityDisplay != null) {
4361 return mActivityDisplay.mDisplayId;
4362 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004363 }
4364 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004365 }
4366
Jeff Brownca9bc702014-02-11 14:32:56 -08004367 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004368 public int getStackId() {
4369 synchronized (mService) {
4370 return mStackId;
4371 }
4372 }
4373
4374 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004375 public boolean injectEvent(InputEvent event) {
4376 final long origId = Binder.clearCallingIdentity();
4377 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004378 synchronized (mService) {
4379 if (mActivityDisplay != null) {
4380 return mInputManagerInternal.injectInputEvent(event,
4381 mActivityDisplay.mDisplayId,
4382 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4383 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004384 }
4385 return false;
4386 } finally {
4387 Binder.restoreCallingIdentity(origId);
4388 }
4389 }
4390
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004391 @Override
4392 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004393 synchronized (mService) {
4394 if (mContainerState == CONTAINER_STATE_FINISHING) {
4395 return;
4396 }
4397 mContainerState = CONTAINER_STATE_FINISHING;
4398
Craig Mautnerd163e752014-06-13 17:18:47 -07004399 long origId = Binder.clearCallingIdentity();
4400 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004401 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004402 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004403 } finally {
4404 Binder.restoreCallingIdentity(origId);
4405 }
4406 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004407 }
4408
Andrii Kulian03c403d2016-11-11 11:14:12 -08004409 /**
4410 * Remove the stack completely. Must be called only when there are no tasks left in it,
4411 * as this method does not finish running activities.
4412 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004413 void removeLocked() {
4414 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004415 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004416 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004417 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004418 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004419 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004420 }
4421
Andrii Kulian839def92016-11-02 10:58:58 -07004422 /**
4423 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4424 * completely or re-parented.
4425 */
4426 private void removeFromDisplayLocked() {
4427 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4428 + " current displayId=" + mActivityDisplay.mDisplayId);
4429
Wale Ogunwale1666e312016-12-16 11:27:18 -08004430 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004431 mActivityDisplay = null;
4432 }
4433
4434 /**
4435 * Move the stack to specified display.
4436 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004437 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004438 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004439 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004440 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4441 + mActivityDisplay + " to display=" + activityDisplay
4442 + " Callers=" + Debug.getCallers(2));
4443
4444 removeFromDisplayLocked();
4445
4446 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004447 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004448 }
4449
Craig Mautner4a1cb222013-12-04 16:14:06 -08004450 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004451 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004452 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 }
4454
4455 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004456 public final int startActivityIntentSender(IIntentSender intentSender)
4457 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004458 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4459
4460 if (!(intentSender instanceof PendingIntentRecord)) {
4461 throw new IllegalArgumentException("Bad PendingIntent object");
4462 }
4463
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004464 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004465 Binder.getCallingUid(), mCurrentUser, false,
4466 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004467
4468 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4469 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4470 pendingIntent.key.requestResolvedType);
4471
4472 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4473 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4474 }
4475
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004476 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004477 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004478 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004479 throw new SecurityException(
4480 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4481 }
4482 }
4483
Craig Mautnerdf88d732014-01-27 09:21:32 -08004484 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004485 public IBinder asBinder() {
4486 return this;
4487 }
4488
Craig Mautner4504de52013-12-20 09:06:56 -08004489 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004490 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004491 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004492 }
4493
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 ActivityStackSupervisor getOuter() {
4495 return ActivityStackSupervisor.this;
4496 }
4497
Craig Mautnerd163e752014-06-13 17:18:47 -07004498 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004499 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004500 }
4501
Craig Mautner6985bad2014-04-21 15:22:06 -07004502 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004503 void setVisible(boolean visible) {
4504 if (mVisible != visible) {
4505 mVisible = visible;
4506 if (mCallback != null) {
4507 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4508 0 /* unused */, this).sendToTarget();
4509 }
4510 }
4511 }
4512
Craig Mautner6985bad2014-04-21 15:22:06 -07004513 void setDrawn() {
4514 }
4515
Craig Mautner1b4bf852014-05-26 15:06:32 -07004516 // You can always start a new task on a regular ActivityStack.
4517 boolean isEligibleForNewTasks() {
4518 return true;
4519 }
4520
Craig Mautnerd163e752014-06-13 17:18:47 -07004521 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004522 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004523 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004524 }
4525
Craig Mautner34b73df2014-01-12 21:11:08 -08004526 @Override
4527 public String toString() {
4528 return mIdString + (mActivityDisplay == null ? "N" : "A");
4529 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004530 }
4531
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004532 private class VirtualActivityContainer extends ActivityContainer {
4533 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004534 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004535
4536 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004537 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4538 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004539 mParentActivity = parent;
4540 mCallback = callback;
4541 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004542 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004543 }
4544
4545 @Override
4546 public void setSurface(Surface surface, int width, int height, int density) {
4547 super.setSurface(surface, width, height, density);
4548
4549 synchronized (mService) {
4550 final long origId = Binder.clearCallingIdentity();
4551 try {
4552 setSurfaceLocked(surface, width, height, density);
4553 } finally {
4554 Binder.restoreCallingIdentity(origId);
4555 }
4556 }
4557 }
4558
4559 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4560 if (mContainerState == CONTAINER_STATE_FINISHING) {
4561 return;
4562 }
4563 VirtualActivityDisplay virtualActivityDisplay =
4564 (VirtualActivityDisplay) mActivityDisplay;
4565 if (virtualActivityDisplay == null) {
4566 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004567 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004568 mActivityDisplay = virtualActivityDisplay;
4569 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004570 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004571 }
4572
4573 if (mSurface != null) {
4574 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004575 }
4576
Craig Mautner6985bad2014-04-21 15:22:06 -07004577 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004578 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004579 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004580 } else {
4581 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004582 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004583 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004584 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004585 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004586 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004587
Craig Mautnerd163e752014-06-13 17:18:47 -07004588 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004589
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004590 if (DEBUG_STACK) Slog.d(TAG_STACK,
4591 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004592 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004593
Craig Mautner6985bad2014-04-21 15:22:06 -07004594 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004595 boolean isAttachedLocked() {
4596 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004597 }
4598
4599 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004600 void setDrawn() {
4601 synchronized (mService) {
4602 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004603 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004604 }
4605 }
4606
Craig Mautner1b4bf852014-05-26 15:06:32 -07004607 // Never start a new task on an ActivityView if it isn't explicitly specified.
4608 @Override
4609 boolean isEligibleForNewTasks() {
4610 return false;
4611 }
4612
Craig Mautnerd163e752014-06-13 17:18:47 -07004613 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004614 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004615 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4616 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4617 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4618 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4619 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004620 }
4621 }
4622
Craig Mautner4a1cb222013-12-04 16:14:06 -08004623 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4624 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004625 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004626 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004627 int mDisplayId;
4628 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004629
Craig Mautner4a1cb222013-12-04 16:14:06 -08004630 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4631 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004632 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004633
Jose Lima4b6c6692014-08-12 17:41:12 -07004634 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004635
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004636 /** Array of all UIDs that are present on the display. */
4637 private IntArray mDisplayAccessUIDs = new IntArray();
4638
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004639 ActivityDisplay() {
4640 }
Craig Mautner4504de52013-12-20 09:06:56 -08004641
Craig Mautner1a70a162014-09-13 12:09:31 -07004642 // After instantiation, check that mDisplay is not null before using this. The alternative
4643 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004644 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004645 final Display display = mDisplayManager.getDisplay(displayId);
4646 if (display == null) {
4647 return;
4648 }
4649 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004650 }
4651
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004652 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004653 mDisplay = display;
4654 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004655 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004656
Winson Chung7c3ede32017-03-14 14:18:34 -07004657 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004658 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004659 + " to displayId=" + mDisplayId + " position=" + position);
4660 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004661 }
4662
Wale Ogunwale1666e312016-12-16 11:27:18 -08004663 void detachStack(ActivityStack stack) {
4664 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004665 + " from displayId=" + mDisplayId);
4666 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004667 }
4668
Jose Lima4b6c6692014-08-12 17:41:12 -07004669 void setVisibleBehindActivity(ActivityRecord r) {
4670 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004671 }
4672
Jose Lima4b6c6692014-08-12 17:41:12 -07004673 boolean hasVisibleBehindActivity() {
4674 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004675 }
4676
Craig Mautner34b73df2014-01-12 21:11:08 -08004677 @Override
4678 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004679 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4680 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004681
4682 @Override
4683 protected int getChildCount() {
4684 return mStacks.size();
4685 }
4686
4687 @Override
4688 protected ConfigurationContainer getChildAt(int index) {
4689 return mStacks.get(index);
4690 }
4691
4692 @Override
4693 protected ConfigurationContainer getParent() {
4694 return ActivityStackSupervisor.this;
4695 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004696
4697 boolean isPrivate() {
4698 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4699 }
4700
4701 boolean isUidPresent(int uid) {
4702 for (ActivityStack stack : mStacks) {
4703 if (stack.isUidPresent(uid)) {
4704 return true;
4705 }
4706 }
4707 return false;
4708 }
4709
4710 /** Update and get all UIDs that are present on the display and have access to it. */
4711 private IntArray getPresentUIDs() {
4712 mDisplayAccessUIDs.clear();
4713 for (ActivityStack stack : mStacks) {
4714 stack.getPresentUIDs(mDisplayAccessUIDs);
4715 }
4716 return mDisplayAccessUIDs;
4717 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004718
4719 boolean shouldDestroyContentOnRemove() {
4720 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4721 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004722 }
4723
4724 class VirtualActivityDisplay extends ActivityDisplay {
4725 VirtualDisplay mVirtualDisplay;
4726
Craig Mautner6985bad2014-04-21 15:22:06 -07004727 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004728 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004729 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4730 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4731 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4732 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004733
4734 init(mVirtualDisplay.getDisplay());
4735
4736 mWindowManager.handleDisplayAdded(mDisplayId);
4737 }
4738
4739 void setSurface(Surface surface) {
4740 if (mVirtualDisplay != null) {
4741 mVirtualDisplay.setSurface(surface);
4742 }
4743 }
4744
4745 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004746 void detachStack(ActivityStack stack) {
4747 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004748 if (mVirtualDisplay != null) {
4749 mVirtualDisplay.release();
4750 mVirtualDisplay = null;
4751 }
4752 }
4753
4754 @Override
4755 public String toString() {
4756 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004757 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004758 }
Jose Lima58e66d62014-05-27 20:00:27 -07004759
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004760 ActivityStack findStackBehind(ActivityStack stack) {
4761 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004762 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004763 if (display == null) {
4764 return null;
4765 }
4766 final ArrayList<ActivityStack> stacks = display.mStacks;
4767 for (int i = stacks.size() - 1; i >= 0; i--) {
4768 if (stacks.get(i) == stack && i > 0) {
4769 return stacks.get(i - 1);
4770 }
4771 }
4772 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4773 + " in=" + stacks);
4774 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004775
Jorim Jaggic69bd222016-03-15 14:38:37 +01004776 /**
4777 * Puts a task into resizing mode during the next app transition.
4778 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004779 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004780 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004781 private void setResizingDuringAnimation(TaskRecord task) {
4782 mResizingTasksDuringAnimation.add(task.taskId);
4783 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004784 }
4785
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004786 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4787 final TaskRecord task;
4788 final int callingUid;
4789 final String callingPackage;
4790 final Intent intent;
4791 final int userId;
4792 final ActivityOptions activityOptions = (bOptions != null)
4793 ? new ActivityOptions(bOptions) : null;
4794 final int launchStackId = (activityOptions != null)
4795 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004796 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004797 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004798 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004799 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004800
4801 if (launchStackId == DOCKED_STACK_ID) {
4802 mWindowManager.setDockedStackCreateState(
4803 activityOptions.getDockCreateMode(), null /* initialBounds */);
4804
4805 // Defer updating the stack in which recents is until the app transition is done, to
4806 // not run into issues where we still need to draw the task in recents but the
4807 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004808 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004809 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4810 }
4811
Winson Chung7900bee2017-03-10 08:59:25 -08004812 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4813 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004814 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004815 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004816 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004817 throw new IllegalArgumentException(
4818 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4819 }
4820
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004821 // Since we don't have an actual source record here, we assume that the currently focused
4822 // activity was the source.
4823 final ActivityStack focusedStack = getFocusedStack();
4824 final ActivityRecord sourceRecord =
4825 focusedStack != null ? focusedStack.topActivity() : null;
4826
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004827 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004828 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004829 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4830 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004831 }
4832 }
4833
4834 // If the user must confirm credentials (e.g. when first launching a work app and the
4835 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4836 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4837 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004838 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004839 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004840 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004841 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4842 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004843
4844 // If we are launching the task in the docked stack, put it into resizing mode so
4845 // the window renders full-screen with the background filling the void. Also only
4846 // call this at the end to make sure that tasks exists on the window manager side.
4847 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004848 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004849 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004850
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004851 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004852 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004853 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4854 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004855 return ActivityManager.START_TASK_TO_FRONT;
4856 }
4857 callingUid = task.mCallingUid;
4858 callingPackage = task.mCallingPackage;
4859 intent = task.intent;
4860 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4861 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004862 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4863 null, null, 0, 0, bOptions, userId, null, task);
4864 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004865 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004866 }
4867 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004868 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004869
4870 /**
4871 * @return a list of activities which are the top ones in each visible stack. The first
4872 * entry will be the focused activity.
4873 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004874 List<IBinder> getTopVisibleActivities() {
4875 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4876 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004877 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004878 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4879 // Traverse all stacks on a display.
4880 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4881 final ActivityStack stack = display.mStacks.get(j);
4882 // Get top activity from a visible stack and add it to the list.
4883 if (stack.getStackVisibilityLocked(null /* starting */)
4884 == ActivityStack.STACK_VISIBLE) {
4885 final ActivityRecord top = stack.topActivity();
4886 if (top != null) {
4887 if (stack == mFocusedStack) {
4888 topActivityTokens.add(0, top.appToken);
4889 } else {
4890 topActivityTokens.add(top.appToken);
4891 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004892 }
4893 }
4894 }
4895 }
4896 return topActivityTokens;
4897 }
Craig Mautner27084302013-03-25 08:05:25 -07004898}