blob: 9b33a09fca0d5fe549967a67b2163c5b50ff1f87 [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
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020020import static android.Manifest.permission.START_ANY_ACTIVITY;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
23import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
24import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.START_TASK_TO_FRONT;
26import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.HOME_STACK_ID;
32import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
33import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
34import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080035import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020036import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070041import static android.view.Display.FLAG_PRIVATE;
42import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080043import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020044
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
74import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
77import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
78import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
81import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
84import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
85import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
86import static com.android.server.am.ActivityStack.STACK_VISIBLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080092import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
93import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
94import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020095import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080096import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020097
Svetoslav7008b512015-06-24 18:47:07 -070098import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080099import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700100import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100101import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800109import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700110import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700113import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400114import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Intent;
119import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700121import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.pm.PackageManager;
123import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100124import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800126import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800127import android.hardware.display.DisplayManager;
128import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800129import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800130import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800131import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800132import android.hardware.input.InputManager;
133import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100135import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700136import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700137import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700140import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700142import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700144import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400145import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700147import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700148import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700149import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100150import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700151import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700152import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400153import android.provider.Settings;
154import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700155import android.service.voice.IVoiceInteractionSession;
Bryce Lee4a194382017-04-04 14:32:48 -0700156import android.text.TextUtils;
Svetoslav7008b512015-06-24 18:47:07 -0700157import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700158import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700159import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800160import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700161import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800164import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800165import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800166import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800167
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800168import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800169import com.android.internal.logging.MetricsLogger;
170import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700171import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400172import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700173import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400174import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800175import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700176import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700177import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700178
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179import java.io.FileDescriptor;
180import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700181import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800182import java.lang.annotation.Retention;
183import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700184import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700185import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700186import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700187import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700188
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800189public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800190 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700192 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700194 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700200 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800201 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800203
Craig Mautnerf3333272013-04-22 10:55:53 -0700204 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700206
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700209
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Craig Mautner05d29032013-05-03 13:40:13 -0700213 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
214 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
215 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700216 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800218 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
219 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
220 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700221 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400222 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
223 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700224 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700225 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700226 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800227 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
228 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800229
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700231
Jason Monk62515be2014-05-21 16:06:19 -0400232 private static final String LOCK_TASK_TAG = "Lock-to-App";
233
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234 // Used to indicate if an object (e.g. stack) that we are trying to get
235 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800236 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700238 // Used to indicate that windows of activities should be preserved during the resize.
239 static final boolean PRESERVE_WINDOWS = true;
240
Wale Ogunwale040b4702015-08-06 18:10:50 -0700241 // Used to indicate if an object (e.g. task) should be moved/created
242 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700243 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700244
245 // Used to indicate that an objects (e.g. task) removal from its container
246 // (e.g. stack) is due to it moving to another container.
247 static final boolean MOVING = true;
248
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700249 // Force the focus to change to the stack we are moving a task to..
250 static final boolean FORCE_FOCUS = true;
251
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700252 // Don't execute any calls to resume.
253 static final boolean DEFER_RESUME = true;
254
Winson Chung010927a2016-12-15 16:12:35 -0800255 // Used to indicate that a task is removed it should also be removed from recents.
256 static final boolean REMOVE_FROM_RECENTS = true;
257
Winson Chung6954fc92017-03-24 16:22:12 -0700258 // Used to indicate that pausing an activity should occur immediately without waiting for
259 // the activity callback indicating that it has completed pausing
260 static final boolean PAUSE_IMMEDIATELY = true;
261
Winson Chung7900bee2017-03-10 08:59:25 -0800262 /**
263 * The modes which affect which tasks are returned when calling
264 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
265 */
266 @Retention(RetentionPolicy.SOURCE)
267 @IntDef({
268 MATCH_TASK_IN_STACKS_ONLY,
269 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
270 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
271 })
272 public @interface AnyTaskForIdMatchTaskMode {}
273 // Match only tasks in the current stacks
274 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
275 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
276 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
277 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
278 // provided stack id
279 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
280
Svetoslav7008b512015-06-24 18:47:07 -0700281 // Activity actions an app cannot start if it uses a permission which is not granted.
282 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
283 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700284
Svetoslav7008b512015-06-24 18:47:07 -0700285 static {
286 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
287 Manifest.permission.CAMERA);
288 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
289 Manifest.permission.CAMERA);
290 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
291 Manifest.permission.CALL_PHONE);
292 }
293
Svet Ganov99b60432015-06-27 13:15:22 -0700294 /** Action restriction: launching the activity is not restricted. */
295 private static final int ACTIVITY_RESTRICTION_NONE = 0;
296 /** Action restriction: launching the activity is restricted by a permission. */
297 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
298 /** Action restriction: launching the activity is restricted by an app op. */
299 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700300
justinzhang5286d3f2014-05-12 17:06:01 -0400301 /** Status Bar Service **/
302 private IBinder mToken = new Binder();
303 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400304 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400305
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700306 // For debugging to make sure the caller when acquiring/releasing our
307 // wake lock is the system process.
308 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800309 /** The number of distinct task ids that can be assigned to the tasks of a single user */
310 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700311
Craig Mautner27084302013-03-25 08:05:25 -0700312 final ActivityManagerService mService;
313
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800314 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800315
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316 final ActivityStackSupervisorHandler mHandler;
317
318 /** Short cut */
319 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700322 /** Counter for next free stack ID to use for dynamic activity stacks. */
323 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700324
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800325 /**
326 * Maps the task identifier that activities are currently being started in to the userId of the
327 * task. Each time a new task is created, the entry for the userId of the task is incremented
328 */
329 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Craig Mautner2420ead2013-04-01 17:13:20 -0700331 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800332 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700333
Craig Mautnere0a38842013-12-16 16:14:02 -0800334 /** The stack containing the launcher app. Assumed to always be attached to
335 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800336 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700337
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800339 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700340
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
342 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800343 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800344 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700345
346 /** List of activities that are waiting for a new activity to become visible before completing
347 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700348 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
349 // mStoppingActivities when something else comes up.
350 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
Bryce Lee4a194382017-04-04 14:32:48 -0700352 /** List of processes waiting to find out when a specific activity becomes visible. */
353 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354
355 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700356 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700357
Craig Mautnerde4ef022013-04-07 19:01:33 -0700358 /** List of activities that are ready to be stopped, but waiting for the next activity to
359 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800360 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700361
Craig Mautnerf3333272013-04-22 10:55:53 -0700362 /** List of activities that are ready to be finished, but waiting for the previous activity to
363 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800367 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Wale Ogunwale22e25262016-02-01 10:32:02 -0800369 /** List of activities whose multi-window mode changed that we need to report to the
370 * application */
371 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
372
373 /** List of activities whose picture-in-picture mode changed that we need to report to the
374 * application */
375 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
376
Winson Chung5af42fc2017-03-24 17:11:33 -0700377 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
378 * the application */
379 Rect mPipModeChangedTargetStackBounds;
380
Craig Mautnerf3333272013-04-22 10:55:53 -0700381 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700382 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700383
Craig Mautnerde4ef022013-04-07 19:01:33 -0700384 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
385 * is being brought in front of us. */
386 boolean mUserLeaving = false;
387
Craig Mautner0eea92c2013-05-16 13:35:39 -0700388 /** Set when we have taken too long waiting to go to sleep. */
389 boolean mSleepTimeout = false;
390
391 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700392 * We don't want to allow the device to go to sleep while in the process
393 * of launching an activity. This is primarily to allow alarm intent
394 * receivers to launch an activity and get that to run before the device
395 * goes back to sleep.
396 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700397 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700398
399 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700400 * Set when the system is going to sleep, until we have
401 * successfully paused the current activity and released our wake lock.
402 * At that point the system is allowed to actually sleep.
403 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700404 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700405
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700406 /** Stack id of the front stack when user switched, indexed by userId. */
407 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700408
Craig Mautner4504de52013-12-20 09:06:56 -0800409 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800411 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800412
Bryce Leeaf3a4002017-03-20 10:37:12 -0700413 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800414 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700415 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800417 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
418
419 private DisplayManagerInternal mDisplayManagerInternal;
420 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800421
Craig Mautner15df08a2015-04-01 12:17:18 -0700422 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
423 * may be finished until there is only one entry left. If this is empty the system is not
424 * in lockTask mode. */
425 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000426 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700427 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
428 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000429 */
430 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400431 /**
432 * Notifies the user when entering/exiting lock-task.
433 */
434 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800435
Wale Ogunwaled046a012015-12-24 13:05:59 -0800436 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800437 boolean inResumeTopActivity;
438
Andrii Kulian1e32e022016-09-16 15:29:34 -0700439 /**
440 * Temporary rect used during docked stack resize calculation so we don't need to create a new
441 * object each time.
442 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700443 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700444
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700445 // The default minimal size that will be used if the activity doesn't specify its minimal size.
446 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700447 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700448
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700449 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
450 private boolean mTaskLayersChanged = true;
451
Jorim Jaggi275561a2016-02-23 10:11:02 -0500452 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800453
Andrii Kulian1779e612016-10-12 21:58:25 -0700454 @Override
455 protected int getChildCount() {
456 return mActivityDisplays.size();
457 }
458
459 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700460 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700461 return mActivityDisplays.valueAt(index);
462 }
463
464 @Override
465 protected ConfigurationContainer getParent() {
466 return null;
467 }
468
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700469 Configuration getDisplayOverrideConfiguration(int displayId) {
470 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
471 if (activityDisplay == null) {
472 throw new IllegalArgumentException("No display found with id: " + displayId);
473 }
474
475 return activityDisplay.getOverrideConfiguration();
476 }
477
478 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
479 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
480 if (activityDisplay == null) {
481 throw new IllegalArgumentException("No display found with id: " + displayId);
482 }
483
484 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
485 }
486
Wale Ogunwale39381972015-12-17 17:15:29 -0800487 static class FindTaskResult {
488 ActivityRecord r;
489 boolean matchedByRootAffinity;
490 }
491 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
492
Craig Mautneree36c772014-07-16 14:56:05 -0700493 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800494 * Temp storage for display ids sorted in focus order.
495 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
496 * it's more efficient, as the number of displays is usually small.
497 */
498 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
499
500 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100501 * Used to keep track whether app visibilities got changed since the last pause. Useful to
502 * determine whether to invoke the task stack change listener after pausing.
503 */
504 boolean mAppVisibilitiesChangedSinceLastPause;
505
506 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100507 * Set of tasks that are in resizing mode during an app transition to fill the "void".
508 */
509 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
510
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700511
512 /**
513 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
514 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
515 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
516 * like the docked stack going empty.
517 */
518 private boolean mAllowDockedStackResize = true;
519
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100520 /**
Tony Mak853304c2016-04-18 15:17:41 +0100521 * Is dock currently minimized.
522 */
523 boolean mIsDockMinimized;
524
Jorim Jaggife762342016-10-13 14:33:27 +0200525 final KeyguardController mKeyguardController;
526
Tony Mak853304c2016-04-18 15:17:41 +0100527 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700528 * Description of a request to start a new activity, which has been held
529 * due to app switches being disabled.
530 */
531 static class PendingActivityLaunch {
532 final ActivityRecord r;
533 final ActivityRecord sourceRecord;
534 final int startFlags;
535 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700536 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700537
538 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700539 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700540 r = _r;
541 sourceRecord = _sourceRecord;
542 startFlags = _startFlags;
543 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700544 callerApp = _callerApp;
545 }
546
547 void sendErrorResult(String message) {
548 try {
549 if (callerApp.thread != null) {
550 callerApp.thread.scheduleCrash(message);
551 }
552 } catch (RemoteException e) {
553 Slog.e(TAG, "Exception scheduling crash of failed "
554 + "activity launcher sourceRecord=" + sourceRecord, e);
555 }
Craig Mautneree36c772014-07-16 14:56:05 -0700556 }
557 }
558
Bryce Leeaf691c02017-03-20 14:20:22 -0700559 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700560 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700561 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800562 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200563 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700564 }
565
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800566 void setRecentTasks(RecentTasks recentTasks) {
567 mRecentTasks = recentTasks;
568 }
569
Jeff Brown2c43c332014-06-12 22:38:59 -0700570 /**
571 * At the time when the constructor runs, the power manager has not yet been
572 * initialized. So we initialize our wakelocks afterwards.
573 */
574 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800575 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700576 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700577 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700578 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700579 }
580
justinzhang5286d3f2014-05-12 17:06:01 -0400581 // This function returns a IStatusBarService. The value is from ServiceManager.
582 // getService and is cached.
583 private IStatusBarService getStatusBarService() {
584 synchronized (mService) {
585 if (mStatusBarService == null) {
586 mStatusBarService = IStatusBarService.Stub.asInterface(
587 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
588 if (mStatusBarService == null) {
589 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
590 }
591 }
592 return mStatusBarService;
593 }
594 }
595
Jason Monk35c62a42014-06-17 10:24:47 -0400596 private IDevicePolicyManager getDevicePolicyManager() {
597 synchronized (mService) {
598 if (mDevicePolicyManager == null) {
599 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
600 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
601 if (mDevicePolicyManager == null) {
602 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
603 }
604 }
605 return mDevicePolicyManager;
606 }
607 }
608
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700609 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 synchronized (mService) {
611 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200612 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613
614 mDisplayManager =
615 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
616 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800617 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618
619 Display[] displays = mDisplayManager.getDisplays();
620 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
621 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700623 if (activityDisplay.mDisplay == null) {
624 throw new IllegalStateException("Default Display does not exist");
625 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800626 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700627 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 }
629
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800630 mHomeStack = mFocusedStack = mLastFocusedStack =
631 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800632
633 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 }
Craig Mautner27084302013-03-25 08:05:25 -0700635 }
636
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700637 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700639 }
640
Craig Mautnerde4ef022013-04-07 19:01:33 -0700641 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700643 }
644
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700645 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700646 if (stack == null) {
647 return false;
648 }
649
Craig Mautnerdf88d732014-01-27 09:21:32 -0800650 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
651 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700652 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800653 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800654 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700655 }
656
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800657 /** The top most stack on its display. */
658 boolean isFrontStackOnDisplay(ActivityStack stack) {
659 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
660 }
661
662 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700663 if (stack == null) {
664 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700666
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700667 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
668 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700669 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800670 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800671 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700672 }
673
Wale Ogunwaled046a012015-12-24 13:05:59 -0800674 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800675 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
676 if (!focusCandidate.isFocusable()) {
677 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800678 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800679 }
680
681 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800682 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800683 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800684
Wale Ogunwaled046a012015-12-24 13:05:59 -0800685 EventLogTags.writeAmFocusedStack(
686 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
687 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
688 }
689
690 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800691 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800692 if (r != null && r.idle) {
693 checkFinishBootingLocked();
694 }
695 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700696 }
697
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700698 void moveHomeStackToFront(String reason) {
699 mHomeStack.moveToFront(reason);
700 }
701
Matthew Ng330757d2017-02-28 14:19:17 -0800702 void moveRecentsStackToFront(String reason) {
703 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
704 if (recentsStack != null) {
705 recentsStack.moveToFront(reason);
706 }
707 }
708
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700709 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800710 boolean moveHomeStackTaskToTop(String reason) {
711 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700712
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700713 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700714 if (top == null) {
715 return false;
716 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700717 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700718 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700719 }
720
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800721 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700722 if (!mService.mBooting && !mService.mBooted) {
723 // Not ready yet!
724 return false;
725 }
726
Craig Mautner84984fa2014-06-19 11:19:20 -0700727 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700728 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700729 }
730
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800731 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700732 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800733 final String myReason = reason + " resumeHomeStackTask";
734
Mark Lua56ea122015-10-08 13:31:01 +0800735 // Only resume home activity if isn't finishing.
736 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700737 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800738 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700739 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800740 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700741 }
742
Craig Mautner8d341ef2013-03-26 09:03:27 -0700743 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800744 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
745 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700746 }
747
748 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700749 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700750 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800751 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700752 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800753 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
754 * valid if the matchMode is
755 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700756 */
Winson Chung7900bee2017-03-10 08:59:25 -0800757 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
758 // If there is a stack id set, ensure that we are attempting to actually restore a task
759 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
760 stackId != INVALID_STACK_ID) {
761 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
762 }
763
Craig Mautnere0a38842013-12-16 16:14:02 -0800764 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800765 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
768 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700769 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 if (task != null) {
771 return task;
772 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700773 }
774 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800775
Winson Chung7900bee2017-03-10 08:59:25 -0800776 // If we are matching stack tasks only, return now
777 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800778 return null;
779 }
780
Winson Chung7900bee2017-03-10 08:59:25 -0800781 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
782 // the task from recents
783 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700784 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
785
786 if (task == null) {
787 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800788 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
789 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700790
791 return null;
792 }
793
794 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700795 return task;
796 }
797
Winson Chung7900bee2017-03-10 08:59:25 -0800798 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700799 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700800 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
801 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800802 return null;
803 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700804 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800805 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700806 }
807
Craig Mautner6170f732013-04-02 13:05:23 -0700808 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800809 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800811 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
813 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
814 if (r != null) {
815 return r;
816 }
Craig Mautner6170f732013-04-02 13:05:23 -0700817 }
818 }
819 return null;
820 }
821
Tony Mak853304c2016-04-18 15:17:41 +0100822 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000823 * Detects whether we should show a lock screen in front of this task for a locked user.
824 * <p>
825 * We'll do this if either of the following holds:
826 * <ul>
827 * <li>The top activity explicitly belongs to {@param userId}.</li>
828 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
829 * </ul>
830 *
831 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100832 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000833 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
834 // To handle the case that work app is in the task but just is not the top one.
835 final ActivityRecord activityRecord = task.getTopActivity();
836 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
837
838 return (activityRecord != null && activityRecord.userId == userId)
839 || (resultTo != null && resultTo.userId == userId);
840 }
841
842 /**
843 * Find all visible task stacks containing {@param userId} and intercept them with an activity
844 * to block out the contents and possibly start a credential-confirming intent.
845 *
846 * @param userId user handle for the locked managed profile.
847 */
848 void lockAllProfileTasks(@UserIdInt int userId) {
849 mWindowManager.deferSurfaceLayout();
850 try {
851 final List<ActivityStack> stacks = getStacks();
852 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
853 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
854 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
855 final TaskRecord task = tasks.get(taskNdx);
856
857 // Check the task for a top activity belonging to userId, or returning a result
858 // to an activity belonging to userId. Example case: a document picker for
859 // personal files, opened by a work app, should still get locked.
860 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000861 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
862 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000863 }
Tony Mak853304c2016-04-18 15:17:41 +0100864 }
865 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000866 } finally {
867 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100868 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000869 }
870
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800871 void setNextTaskIdForUserLocked(int taskId, int userId) {
872 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
873 if (taskId > currentTaskId) {
874 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700875 }
876 }
877
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700878 static int nextTaskIdForUser(int taskId, int userId) {
879 int nextTaskId = taskId + 1;
880 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
881 // Wrap around as there will be smaller task ids that are available now.
882 nextTaskId -= MAX_TASK_IDS_PER_USER;
883 }
884 return nextTaskId;
885 }
886
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800887 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800888 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
889 // for a userId u, a taskId can only be in the range
890 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
891 // 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 -0700892 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800893 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800894 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800895 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700896 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800897 if (candidateTaskId == currentTaskId) {
898 // Something wrong!
899 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
900 throw new IllegalStateException("Cannot get an available task id."
901 + " Reached limit of " + MAX_TASK_IDS_PER_USER
902 + " running tasks per user.");
903 }
904 }
905 mCurTaskIdForUser.put(userId, candidateTaskId);
906 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700907 }
908
Chong Zhang6cda19c2016-06-14 19:07:56 -0700909 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800910 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700911 if (stack == null) {
912 return null;
913 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 ActivityRecord resumedActivity = stack.mResumedActivity;
915 if (resumedActivity == null || resumedActivity.app == null) {
916 resumedActivity = stack.mPausingActivity;
917 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700918 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700919 }
920 }
921 return resumedActivity;
922 }
923
Dianne Hackbornff072722014-09-24 10:56:28 -0700924 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700925 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
928 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
930 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700931 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 continue;
933 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700934 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 if (hr != null) {
936 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
937 && processName.equals(hr.processName)) {
938 try {
George Mount2c92c972014-03-20 09:38:23 -0700939 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800940 didSomething = true;
941 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700942 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 Slog.w(TAG, "Exception in new application when starting activity "
944 + hr.intent.getComponent().flattenToShortString(), e);
945 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700946 }
Craig Mautner20e72272013-04-01 13:45:53 -0700947 }
Craig Mautner20e72272013-04-01 13:45:53 -0700948 }
949 }
950 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700951 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700952 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700953 }
Craig Mautner20e72272013-04-01 13:45:53 -0700954 return didSomething;
955 }
956
957 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800958 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
959 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
961 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700962 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 continue;
964 }
965 final ActivityRecord resumedActivity = stack.mResumedActivity;
966 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700967 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800968 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 return false;
970 }
Craig Mautner20e72272013-04-01 13:45:53 -0700971 }
972 }
Wei Wang65c7a152016-06-02 18:51:22 -0700973 // Send launch end powerhint when idle
974 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700975 return true;
976 }
977
Craig Mautnerde4ef022013-04-07 19:01:33 -0700978 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800979 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
980 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
982 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700983 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700985 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 return false;
987 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 }
989 }
990 }
991 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700992 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
994 mLastFocusedStack + " to=" + mFocusedStack);
995 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 return true;
997 }
998
999 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001000 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1002 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001003 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1004 final ActivityStack stack = stacks.get(stackNdx);
1005 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001006 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001007 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001008 return false;
1009 }
1010 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001011 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 }
1013 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001014 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 }
1016
Craig Mautner2acc3892013-09-23 10:28:14 -07001017 /**
1018 * Pause all activities in either all of the stacks or just the back stacks.
1019 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001020 * @param resuming The resuming activity.
1021 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1022 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001023 * @return true if any activity was paused as a result of this call.
1024 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001025 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001026 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001027 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1028 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001029 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1030 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001031 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001032 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001034 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1035 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001037 }
1038 }
1039 return someActivityPaused;
1040 }
1041
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001043 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001044 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1045 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001046 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1047 final ActivityStack stack = stacks.get(stackNdx);
1048 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001049 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001050 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001051 Slog.d(TAG_STATES,
1052 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001053 pausing = false;
1054 } else {
1055 return false;
1056 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001057 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001058 }
1059 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001060 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001061 }
1062
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001063 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001064 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001065 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1067 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1069 final ActivityStack stack = stacks.get(stackNdx);
1070 if (stack.mResumedActivity != null &&
1071 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001072 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001073 }
1074 }
1075 }
1076 }
1077
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001078 void cancelInitializingActivities() {
1079 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1080 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1081 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1082 stacks.get(stackNdx).cancelInitializingActivities();
1083 }
1084 }
1085 }
1086
Bryce Lee4a194382017-04-04 14:32:48 -07001087 void waitActivityVisible(ComponentName name, WaitResult result) {
1088 final WaitInfo waitInfo = new WaitInfo(name, result);
1089 mWaitingForActivityVisible.add(waitInfo);
1090 }
1091
1092 void cleanupActivity(ActivityRecord r) {
1093 // Make sure this record is no longer in the pending finishes list.
1094 // This could happen, for example, if we are trimming activities
1095 // down to the max limit while they are still waiting to finish.
1096 mFinishingActivities.remove(r);
1097 mActivitiesWaitingForVisibleActivity.remove(r);
1098
1099 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1100 if (mWaitingForActivityVisible.get(i).matches(r)) {
1101 mWaitingForActivityVisible.remove(i);
1102 }
1103 }
1104 }
1105
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001106 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001107 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001108 }
1109
1110 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1111 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001112 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1113 final WaitInfo w = mWaitingForActivityVisible.get(i);
1114 if (w.matches(r)) {
1115 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001117 result.timeout = false;
1118 result.who = w.getComponent();
1119 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1120 result.thisTime = result.totalTime;
1121 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 if (changed) {
1125 mService.notifyAll();
1126 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001127 }
1128
Chong Zhang5022da32016-06-21 16:31:37 -07001129 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1130 boolean changed = false;
1131 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1132 WaitResult w = mWaitingActivityLaunched.remove(i);
1133 if (w.who == null) {
1134 changed = true;
1135 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1136 // the starting activity ends up moving another activity to front, and it should
1137 // wait for this new activity to become visible instead.
1138 // Do not modify other fields.
1139 w.result = START_TASK_TO_FRONT;
1140 }
1141 }
1142 if (changed) {
1143 mService.notifyAll();
1144 }
1145 }
1146
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001147 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1148 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001149 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001150 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001151 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001152 if (w.who == null) {
1153 changed = true;
1154 w.timeout = timeout;
1155 if (r != null) {
1156 w.who = new ComponentName(r.info.packageName, r.info.name);
1157 }
1158 w.thisTime = thisTime;
1159 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001160 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001161 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001162 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001163 if (changed) {
1164 mService.notifyAll();
1165 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001166 }
1167
Craig Mautner29219d92013-04-16 20:19:12 -07001168 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001169 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001170 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001171 if (r != null) {
1172 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001173 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001174
Andrii Kulian7318d632016-07-20 18:59:28 -07001175 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001176 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1177
1178 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1179 final int displayId = mTmpOrderedDisplayIds.get(i);
1180 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1181 if (stacks == null) {
1182 continue;
1183 }
1184 for (int j = stacks.size() - 1; j >= 0; --j) {
1185 final ActivityStack stack = stacks.get(j);
1186 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1187 r = stack.topRunningActivityLocked();
1188 if (r != null) {
1189 return r;
1190 }
Craig Mautner29219d92013-04-16 20:19:12 -07001191 }
1192 }
1193 }
1194 return null;
1195 }
1196
Dianne Hackborn09233282014-04-30 11:33:59 -07001197 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001198 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001199 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1200 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001201 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001202 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001203 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001204 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1205 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001206 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001207 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001208 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001209 }
1210 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001211
1212 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1213 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1214 while (maxNum > 0) {
1215 long mostRecentActiveTime = Long.MIN_VALUE;
1216 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001217 final int numTaskLists = runningTaskLists.size();
1218 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1219 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001220 if (!stackTaskList.isEmpty()) {
1221 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1222 if (lastActiveTime > mostRecentActiveTime) {
1223 mostRecentActiveTime = lastActiveTime;
1224 selectedStackList = stackTaskList;
1225 }
1226 }
1227 }
1228 if (selectedStackList != null) {
1229 list.add(selectedStackList.remove(0));
1230 --maxNum;
1231 } else {
1232 break;
1233 }
1234 }
Craig Mautner20e72272013-04-01 13:45:53 -07001235 }
1236
Todd Kennedy7440f172015-12-09 14:31:22 -08001237 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1238 ProfilerInfo profilerInfo) {
1239 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001240 if (aInfo != null) {
1241 // Store the found target back into the intent, because now that
1242 // we have it we never want to do this again. For example, if the
1243 // user navigates back to this point in the history, we should
1244 // always restart the exact same activity.
1245 intent.setComponent(new ComponentName(
1246 aInfo.applicationInfo.packageName, aInfo.name));
1247
1248 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001249 if (!aInfo.processName.equals("system")) {
1250 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001251 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001252 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001253
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001254 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1255 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1256 }
1257
Man Caocfa78b22015-06-11 20:14:34 -07001258 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1259 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1260 }
1261
1262 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001263 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001264 }
1265 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001266 final String intentLaunchToken = intent.getLaunchToken();
1267 if (aInfo.launchToken == null && intentLaunchToken != null) {
1268 aInfo.launchToken = intentLaunchToken;
1269 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001270 }
1271 return aInfo;
1272 }
1273
Todd Kennedy7440f172015-12-09 14:31:22 -08001274 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001275 return resolveIntent(intent, resolvedType, userId, 0);
1276 }
1277
1278 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001279 synchronized (mService) {
1280 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001281 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001282 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001283 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001284 }
1285
1286 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1287 ProfilerInfo profilerInfo, int userId) {
1288 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1289 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1290 }
1291
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001292 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1293 boolean andResume, boolean checkConfig) throws RemoteException {
1294
1295 if (!allPausedActivitiesComplete()) {
1296 // While there are activities pausing we skipping starting any new activities until
1297 // pauses are complete. NOTE: that we also do this for activities that are starting in
1298 // the paused state because they will first be resumed then paused on the client side.
1299 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1300 "realStartActivityLocked: Skipping start of r=" + r
1301 + " some activities pausing...");
1302 return false;
1303 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001304
Andrii Kulianada10292017-02-09 23:19:29 -08001305 r.startFreezingScreenLocked(app, 0);
1306 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001307
Andrii Kulianada10292017-02-09 23:19:29 -08001308 // schedule launch ticks to collect information about slow apps.
1309 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001310
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001311 // Have the window manager re-evaluate the orientation of the screen based on the new
1312 // activity order. Note that as a result of this, it can call back into the activity
1313 // manager with a new orientation. We don't care about that, because the activity is not
1314 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001316 final int displayId = r.getDisplayId();
1317 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1318 getDisplayOverrideConfiguration(displayId),
1319 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001320 // Deferring resume here because we're going to launch new activity shortly.
1321 // We don't want to perform a redundant launch of the same record while ensuring
1322 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001323 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1324 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001325 }
1326
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001327 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001328 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001329 }
1330
Craig Mautner2420ead2013-04-01 17:13:20 -07001331 r.app = app;
1332 app.waitingToKill = null;
1333 r.launchCount++;
1334 r.lastLaunchTime = SystemClock.uptimeMillis();
1335
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001336 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001337
1338 int idx = app.activities.indexOf(r);
1339 if (idx < 0) {
1340 app.activities.add(r);
1341 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001342 mService.updateLruProcessLocked(app, true, null);
1343 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001344
Bryce Leeaf691c02017-03-20 14:20:22 -07001345 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001346 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1347 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001348 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001349 }
1350
Andrii Kulian02b7a832016-10-06 23:11:56 -07001351 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 try {
1353 if (app.thread == null) {
1354 throw new RemoteException();
1355 }
1356 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001357 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001359 // We don't need to deliver new intents and/or set results if activity is going
1360 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001361 results = r.results;
1362 newIntents = r.newIntents;
1363 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001364 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1365 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1366 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001367 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1368 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001369 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001370 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001371 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001373 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1374 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001375 r.sleeping = false;
1376 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001377 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 mService.showAskCompatModeDialogLocked(r);
1379 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001380 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001381 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1382 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1383 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001384 final String profileFile = mService.mProfileFile;
1385 if (profileFile != null) {
1386 ParcelFileDescriptor profileFd = mService.mProfileFd;
1387 if (profileFd != null) {
1388 try {
1389 profileFd = profileFd.dup();
1390 } catch (IOException e) {
1391 if (profileFd != null) {
1392 try {
1393 profileFd.close();
1394 } catch (IOException o) {
1395 }
1396 profileFd = null;
1397 }
1398 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001400
1401 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001402 mService.mSamplingInterval, mService.mAutoStopProfiler,
1403 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001404 }
1405 }
1406 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001407
Andrii Kulianada10292017-02-09 23:19:29 -08001408 app.hasShownUi = true;
1409 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001410 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001411 // Because we could be starting an Activity in the system process this may not go across
1412 // a Binder interface which would create a new Configuration. Consequently we have to
1413 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001414
1415 final Configuration globalConfiguration =
1416 new Configuration(mService.getGlobalConfiguration());
1417 r.setLastReportedGlobalConfiguration(globalConfiguration);
1418 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001419 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001420 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1421
Craig Mautner2420ead2013-04-01 17:13:20 -07001422 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001423 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001424 globalConfiguration,
1425 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001426 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1427 r.persistentState, results, newIntents, !andResume,
1428 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001429
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001430 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001431 // This may be a heavy-weight process! Note that the package
1432 // manager will ensure that only activity can run in the main
1433 // process of the .apk, which is the only thing that will be
1434 // considered heavy-weight.
1435 if (app.processName.equals(app.info.packageName)) {
1436 if (mService.mHeavyWeightProcess != null
1437 && mService.mHeavyWeightProcess != app) {
1438 Slog.w(TAG, "Starting new heavy weight process " + app
1439 + " when already running "
1440 + mService.mHeavyWeightProcess);
1441 }
1442 mService.mHeavyWeightProcess = app;
1443 Message msg = mService.mHandler.obtainMessage(
1444 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1445 msg.obj = r;
1446 mService.mHandler.sendMessage(msg);
1447 }
1448 }
1449
1450 } catch (RemoteException e) {
1451 if (r.launchFailed) {
1452 // This is the second time we failed -- finish activity
1453 // and give up.
1454 Slog.e(TAG, "Second failure launching "
1455 + r.intent.getComponent().flattenToShortString()
1456 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001457 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001458 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1459 "2nd-crash", false);
1460 return false;
1461 }
1462
1463 // This is the first time we failed -- restart process and
1464 // retry.
1465 app.activities.remove(r);
1466 throw e;
1467 }
1468
1469 r.launchFailed = false;
1470 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001471 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001472 }
1473
1474 if (andResume) {
1475 // As part of the process of launching, ActivityThread also performs
1476 // a resume.
1477 stack.minimalResumeActivityLocked(r);
1478 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001479 // This activity is not starting in the resumed state... which should look like we asked
1480 // it to pause+stop (but remain visible), and it has done so and reported back the
1481 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001482 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001483 "Moving to PAUSED: " + r + " (starting in paused state)");
1484 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001485 }
1486
1487 // Launch the new version setup screen if needed. We do this -after-
1488 // launching the initial activity (that is, home), so that it can have
1489 // a chance to initialize itself while in the background, making the
1490 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001491 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001492 mService.startSetupActivityLocked();
1493 }
1494
Dianne Hackborn465fa392014-09-14 14:21:18 -07001495 // Update any services we are bound to that might care about whether
1496 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001497 if (r.app != null) {
1498 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1499 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001500
Craig Mautner2420ead2013-04-01 17:13:20 -07001501 return true;
1502 }
1503
Craig Mautnere79d42682013-04-01 19:01:53 -07001504 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001505 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001506 // Is this activity's application already running?
1507 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001508 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001509
Andrii Kulian02b7a832016-10-06 23:11:56 -07001510 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001511
1512 if (app != null && app.thread != null) {
1513 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001514 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1515 || !"android".equals(r.info.packageName)) {
1516 // Don't add this if it is a platform component that is marked
1517 // to run in multiple processes, because this is actually
1518 // part of the framework so doesn't make sense to track as a
1519 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001520 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1521 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001522 }
George Mount2c92c972014-03-20 09:38:23 -07001523 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001524 return;
1525 } catch (RemoteException e) {
1526 Slog.w(TAG, "Exception when starting activity "
1527 + r.intent.getComponent().flattenToShortString(), e);
1528 }
1529
1530 // If a dead object exception was thrown -- fall through to
1531 // restart the application.
1532 }
1533
1534 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001535 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001536 }
1537
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001538 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001539 String resultWho, int requestCode, int callingPid, int callingUid,
1540 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001541 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001542 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1543 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001544 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001545 return true;
1546 }
1547 final int componentRestriction = getComponentRestrictionForCallingPackage(
1548 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1549 final int actionRestriction = getActionRestrictionForCallingPackage(
1550 intent.getAction(), callingPackage, callingPid, callingUid);
1551 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1552 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1553 if (resultRecord != null) {
1554 resultStack.sendActivityResultLocked(-1,
1555 resultRecord, resultWho, requestCode,
1556 Activity.RESULT_CANCELED, null);
1557 }
1558 final String msg;
1559 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1560 msg = "Permission Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ")" + " with revoked permission "
1563 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1564 } else if (!aInfo.exported) {
1565 msg = "Permission Denial: starting " + intent.toString()
1566 + " from " + callerApp + " (pid=" + callingPid
1567 + ", uid=" + callingUid + ")"
1568 + " not exported from uid " + aInfo.applicationInfo.uid;
1569 } else {
1570 msg = "Permission Denial: starting " + intent.toString()
1571 + " from " + callerApp + " (pid=" + callingPid
1572 + ", uid=" + callingUid + ")"
1573 + " requires " + aInfo.permission;
1574 }
1575 Slog.w(TAG, msg);
1576 throw new SecurityException(msg);
1577 }
1578
1579 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1580 final String message = "Appop Denial: starting " + intent.toString()
1581 + " from " + callerApp + " (pid=" + callingPid
1582 + ", uid=" + callingUid + ")"
1583 + " requires " + AppOpsManager.permissionToOp(
1584 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1585 Slog.w(TAG, message);
1586 return false;
1587 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1588 final String message = "Appop Denial: starting " + intent.toString()
1589 + " from " + callerApp + " (pid=" + callingPid
1590 + ", uid=" + callingUid + ")"
1591 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1592 Slog.w(TAG, message);
1593 return false;
1594 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001595 if (options != null) {
1596 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1597 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1598 callingPid, callingUid);
1599 if (startInTaskPerm != PERMISSION_GRANTED) {
1600 final String msg = "Permission Denial: starting " + intent.toString()
1601 + " from " + callerApp + " (pid=" + callingPid
1602 + ", uid=" + callingUid + ") with launchTaskId="
1603 + options.getLaunchTaskId();
1604 Slog.w(TAG, msg);
1605 throw new SecurityException(msg);
1606 }
1607 }
1608 // Check if someone tries to launch an activity on a private display with a different
1609 // owner.
1610 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001611 if (launchDisplayId != INVALID_DISPLAY
1612 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1613 final String msg = "Permission Denial: starting " + intent.toString()
1614 + " from " + callerApp + " (pid=" + callingPid
1615 + ", uid=" + callingUid + ") with launchDisplayId="
1616 + launchDisplayId;
1617 Slog.w(TAG, msg);
1618 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001619 }
1620 }
1621
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001622 return true;
1623 }
1624
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001625 /** Check if caller is allowed to launch activities on specified display. */
1626 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1627 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1628 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1629
1630 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1631 if (activityDisplay == null) {
1632 Slog.w(TAG, "Launch on display check: display not found");
1633 return false;
1634 }
1635
1636 if (!activityDisplay.isPrivate()) {
1637 // Anyone can launch on a public display.
1638 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1639 + " allow launch on public display");
1640 return true;
1641 }
1642
1643 // Check if the caller is the owner of the display.
1644 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1645 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1646 + " allow launch for owner of the display");
1647 return true;
1648 }
1649
1650 // Check if caller is present on display
1651 if (activityDisplay.isUidPresent(callingUid)) {
1652 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1653 + " allow launch for caller present on the display");
1654 return true;
1655 }
1656
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001657 // Check if the caller can manage activity stacks.
1658 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001659 callingUid);
1660 if (startAnyPerm == PERMISSION_GRANTED) {
1661 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1662 + " allow launch any on display");
1663 return true;
1664 }
1665
1666 Slog.w(TAG, "Launch on display check: denied");
1667 return false;
1668 }
1669
1670 /** Update lists of UIDs that are present on displays and have access to them. */
1671 void updateUIDsPresentOnDisplay() {
1672 mDisplayAccessUIDs.clear();
1673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1674 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001675 // Only bother calculating the whitelist for private displays
1676 if (activityDisplay.isPrivate()) {
1677 mDisplayAccessUIDs.append(
1678 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1679 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001680 }
1681 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1682 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1683 }
1684
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001685 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001686 final long identity = Binder.clearCallingIdentity();
1687 try {
1688 return UserManager.get(mService.mContext).getUserInfo(userId);
1689 } finally {
1690 Binder.restoreCallingIdentity(identity);
1691 }
1692 }
1693
Svet Ganov99b60432015-06-27 13:15:22 -07001694 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001695 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001696 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1697 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001698 == PackageManager.PERMISSION_DENIED) {
1699 return ACTIVITY_RESTRICTION_PERMISSION;
1700 }
1701
Christopher Tateff7add02015-08-17 10:23:22 -07001702 if (activityInfo.permission == null) {
1703 return ACTIVITY_RESTRICTION_NONE;
1704 }
1705
Svet Ganov99b60432015-06-27 13:15:22 -07001706 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1707 if (opCode == AppOpsManager.OP_NONE) {
1708 return ACTIVITY_RESTRICTION_NONE;
1709 }
1710
1711 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1712 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001713 if (!ignoreTargetSecurity) {
1714 return ACTIVITY_RESTRICTION_APPOP;
1715 }
Svet Ganov99b60432015-06-27 13:15:22 -07001716 }
1717
1718 return ACTIVITY_RESTRICTION_NONE;
1719 }
1720
Svetoslav7008b512015-06-24 18:47:07 -07001721 private int getActionRestrictionForCallingPackage(String action,
1722 String callingPackage, int callingPid, int callingUid) {
1723 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001724 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001725 }
1726
1727 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1728 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001729 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001730 }
1731
1732 final PackageInfo packageInfo;
1733 try {
1734 packageInfo = mService.mContext.getPackageManager()
1735 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1736 } catch (PackageManager.NameNotFoundException e) {
1737 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
1741 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 if (mService.checkPermission(permission, callingPid, callingUid) ==
1746 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001747 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001748 }
1749
1750 final int opCode = AppOpsManager.permissionToOpCode(permission);
1751 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001752 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001753 }
1754
1755 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1756 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001757 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001758 }
1759
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001763 void setLaunchSource(int uid) {
1764 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1765 }
1766
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001767 void acquireLaunchWakelock() {
1768 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1769 throw new IllegalStateException("Calling must be system uid");
1770 }
1771 mLaunchingActivity.acquire();
1772 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1773 // To be safe, don't allow the wake lock to be held for too long.
1774 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1775 }
1776 }
1777
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001778 /**
1779 * Called when the frontmost task is idle.
1780 * @return the state of mService.mBooting before this was called.
1781 */
1782 private boolean checkFinishBootingLocked() {
1783 final boolean booting = mService.mBooting;
1784 boolean enableScreen = false;
1785 mService.mBooting = false;
1786 if (!mService.mBooted) {
1787 mService.mBooted = true;
1788 enableScreen = true;
1789 }
1790 if (booting || enableScreen) {
1791 mService.postFinishBooting(booting, enableScreen);
1792 }
1793 return booting;
1794 }
1795
Craig Mautnerf3333272013-04-22 10:55:53 -07001796 // Checked.
1797 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001798 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001799 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001800
Craig Mautnerf3333272013-04-22 10:55:53 -07001801 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001802 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001803 int NS = 0;
1804 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001805 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001806 boolean activityRemoved = false;
1807
Wale Ogunwale7d701172015-03-11 15:36:30 -07001808 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001809 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001810 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1811 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001812 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1813 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001814 if (fromTimeout) {
1815 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001817
1818 // This is a hack to semi-deal with a race condition
1819 // in the client where it can be constructed with a
1820 // newer configuration from when we asked it to launch.
1821 // We'll update with whatever configuration it now says
1822 // it used to launch.
1823 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001824 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001825 }
1826
1827 // We are now idle. If someone is waiting for a thumbnail from
1828 // us, we can now deliver.
1829 r.idle = true;
1830
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001831 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001832 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001833 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834 }
1835 }
1836
1837 if (allResumedActivitiesIdle()) {
1838 if (r != null) {
1839 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001840 }
1841
1842 if (mLaunchingActivity.isHeld()) {
1843 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1844 if (VALIDATE_WAKE_LOCK_CALLER &&
1845 Binder.getCallingUid() != Process.myUid()) {
1846 throw new IllegalStateException("Calling must be system uid");
1847 }
1848 mLaunchingActivity.release();
1849 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001850 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001851 }
1852
1853 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001854 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1855 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001856 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001857 if ((NF = mFinishingActivities.size()) > 0) {
1858 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001859 mFinishingActivities.clear();
1860 }
1861
Craig Mautnerf3333272013-04-22 10:55:53 -07001862 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001863 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001864 mStartingUsers.clear();
1865 }
1866
Craig Mautnerf3333272013-04-22 10:55:53 -07001867 // Stop any activities that are scheduled to do so but have been
1868 // waiting for the next one to start.
1869 for (int i = 0; i < NS; i++) {
1870 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001871 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001872 if (stack != null) {
1873 if (r.finishing) {
1874 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1875 } else {
1876 stack.stopActivityLocked(r);
1877 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001878 }
1879 }
1880
1881 // Finish any activities that are scheduled to do so but have been
1882 // waiting for the next one to start.
1883 for (int i = 0; i < NF; i++) {
1884 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001885 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001886 if (stack != null) {
1887 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1888 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001889 }
1890
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001891 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001892 // Complete user switch
1893 if (startingUsers != null) {
1894 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001895 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001896 }
1897 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 }
1899
1900 mService.trimApplications();
1901 //dump();
1902 //mWindowManager.dump();
1903
Craig Mautnerf3333272013-04-22 10:55:53 -07001904 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001905 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 }
1907
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001908 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 }
1910
Craig Mautner8e569572013-10-11 17:36:59 -07001911 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001912 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001913 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1914 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001915 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1916 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1917 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001918 }
Craig Mautner19091252013-10-05 00:03:53 -07001919 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001920 }
1921
1922 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001923 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1924 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1926 stacks.get(stackNdx).closeSystemDialogsLocked();
1927 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001928 }
1929 }
1930
Craig Mautner93529a42013-10-04 15:03:13 -07001931 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001932 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001933 }
1934
Craig Mautner8d341ef2013-03-26 09:03:27 -07001935 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001936 * Update the last used stack id for non-current user (current user's last
1937 * used stack is the focused stack)
1938 */
1939 void updateUserStackLocked(int userId, ActivityStack stack) {
1940 if (userId != mCurrentUser) {
1941 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1942 }
1943 }
1944
1945 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001946 * @return true if some activity was finished (or would have finished if doit were true).
1947 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001948 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1949 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001950 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001954 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001955 if (stack.finishDisabledPackageActivitiesLocked(
1956 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001957 didSomething = true;
1958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001959 }
1960 }
1961 return didSomething;
1962 }
1963
Dianne Hackborna413dc02013-07-12 12:02:55 -07001964 void updatePreviousProcessLocked(ActivityRecord r) {
1965 // Now that this process has stopped, we may want to consider
1966 // it to be the previous app to try to keep around in case
1967 // the user wants to return to it.
1968
1969 // First, found out what is currently the foreground app, so that
1970 // we don't blow away the previous app if this activity is being
1971 // hosted by the process that is actually still the foreground.
1972 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1974 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1976 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001977 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001978 if (stack.mResumedActivity != null) {
1979 fgApp = stack.mResumedActivity.app;
1980 } else if (stack.mPausingActivity != null) {
1981 fgApp = stack.mPausingActivity.app;
1982 }
1983 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001984 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001985 }
1986 }
1987
1988 // Now set this one as the previous process, only if that really
1989 // makes sense to.
1990 if (r.app != null && fgApp != null && r.app != fgApp
1991 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001992 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001993 mService.mPreviousProcess = r.app;
1994 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1995 }
1996 }
1997
Wale Ogunwaled046a012015-12-24 13:05:59 -08001998 boolean resumeFocusedStackTopActivityLocked() {
1999 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002000 }
2001
Wale Ogunwaled046a012015-12-24 13:05:59 -08002002 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002003 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002004 if (targetStack != null && isFocusedStack(targetStack)) {
2005 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002006 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002007 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2008 if (r == null || r.state != RESUMED) {
2009 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002010 } else if (r.state == RESUMED) {
2011 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2012 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002013 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002014 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002015 }
2016
Todd Kennedy39bfee52016-02-24 10:28:21 -08002017 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2019 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2020 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2021 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2022 }
2023 }
2024 }
2025
Adrian Roos20d7df32016-01-12 18:59:43 +01002026 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2027 TaskRecord finishedTask = null;
2028 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002029 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2030 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002031 final int numStacks = stacks.size();
2032 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2033 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002034 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2035 if (stack == focusedStack || finishedTask == null) {
2036 finishedTask = t;
2037 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002038 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002039 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002040 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002041 }
2042
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002043 void finishVoiceTask(IVoiceInteractionSession session) {
2044 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2045 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2046 final int numStacks = stacks.size();
2047 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2048 final ActivityStack stack = stacks.get(stackNdx);
2049 stack.finishVoiceTask(session);
2050 }
2051 }
2052 }
2053
Andrii Kulianc27916642016-04-12 17:59:27 -07002054 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2055 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002056 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2057 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002058 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002059 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2060 // Caller wants the home activity moved with it. To accomplish this,
2061 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002062 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002063 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002064 ActivityStack currentStack = task.getStack();
2065 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002066 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2067 + task + " to front. Stack is null");
2068 return;
2069 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002070
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002071 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002072 int stackId = options.getLaunchStackId();
2073 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002074 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002075 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002076 if (stackId == INVALID_STACK_ID) {
2077 stackId = task.getLaunchStackId();
2078 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002079 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002080 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2081 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002082 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002083 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2084 // still need moveTaskToFrontLocked() below for any transition settings.
2085 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002086 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2087 resizeStackLocked(stackId, bounds,
2088 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002089 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002090 } else {
2091 // WM resizeTask must be done after the task is moved to the correct stack,
2092 // because Task's setBounds() also updates dim layer's bounds, but that has
2093 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002094 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002095 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002096 }
2097 }
2098
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002099 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002100 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002101 r == null ? null : r.appTimeTracker, reason);
2102
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002103 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002104 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002105
Andrii Kulian02b7a832016-10-06 23:11:56 -07002106 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002107 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002108 }
2109
Wale Ogunwale854809c2015-12-27 16:18:19 -08002110 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002111 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002112 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002113 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002114 return false;
2115 }
2116 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2117 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002118 }
2119
Winson Chung55893332017-02-17 17:13:10 -08002120 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002121 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002122 }
2123
Winson Chung55893332017-02-17 17:13:10 -08002124 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2125 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002126 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002127 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002128 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002129 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002130 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002131 return null;
2132 }
Matthew Ng330757d2017-02-28 14:19:17 -08002133 if (stackId == DOCKED_STACK_ID) {
2134 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2135 // the other side of the docked stack and we make visibility decisions based on that.
2136 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2137 }
Winson Chung55893332017-02-17 17:13:10 -08002138 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 }
2140
Andrii Kulian16802aa2016-11-02 12:21:33 -07002141 /**
2142 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2143 * If there is no such stack, new dynamic stack can be created.
2144 * @param displayId Target display.
2145 * @param r Activity that should be launched there.
2146 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2147 */
2148 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2149 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2150 if (activityDisplay == null) {
2151 throw new IllegalArgumentException(
2152 "Display with displayId=" + displayId + " not found.");
2153 }
2154
2155 // Return the topmost valid stack on the display.
2156 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2157 final ActivityStack stack = activityDisplay.mStacks.get(i);
2158 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2159 return stack;
2160 }
2161 }
2162
2163 // If there is no valid stack on the external display - check if new dynamic stack will do.
2164 if (displayId != Display.DEFAULT_DISPLAY) {
2165 final int newDynamicStackId = getNextStackId();
2166 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2167 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2168 }
2169 }
2170
2171 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2172 return null;
2173 }
2174
Craig Mautner967212c2013-04-13 21:10:58 -07002175 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002176 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002177 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2178 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002179 }
2180 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002181 }
2182
Jorim Jaggife762342016-10-13 14:33:27 +02002183 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2184 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2185 }
2186
Andrii Kulian7fc22812016-12-28 13:04:11 -08002187 /**
2188 * Get next focusable stack in the system. This will search across displays and stacks
2189 * in last-focused order for a focusable and visible stack, different from the target stack.
2190 *
2191 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2192 * searching for next candidate.
2193 * @return Next focusable {@link ActivityStack}, null if not found.
2194 */
2195 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2196 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2197
2198 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2199 final int displayId = mTmpOrderedDisplayIds.get(i);
2200 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2201 if (stacks == null) {
2202 continue;
2203 }
2204 for (int j = stacks.size() - 1; j >= 0; --j) {
2205 final ActivityStack stack = stacks.get(j);
2206 if (stack != currentFocus && stack.isFocusable()
2207 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2208 return stack;
2209 }
2210 }
2211 }
2212
2213 return null;
2214 }
2215
Craig Mautneree2e45a2014-06-27 12:10:03 -07002216 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002217 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002218 }
2219
2220 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002221 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2222 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2223 final TaskRecord task = tasks.get(taskNdx);
2224 if (task.isHomeTask()) {
2225 final ArrayList<ActivityRecord> activities = task.mActivities;
2226 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2227 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002228 if (r.isHomeActivity()
2229 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002230 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002231 }
2232 }
2233 }
2234 }
2235 return null;
2236 }
2237
Chong Zhangb15758a2015-11-17 12:12:03 -08002238 /**
2239 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2240 * the docked stack itself, or if it's side-by-side to the docked stack.
2241 */
2242 boolean isStackDockedInEffect(int stackId) {
2243 return stackId == DOCKED_STACK_ID ||
2244 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2245 }
2246
Todd Kennedyca4d8422015-01-15 15:19:22 -08002247 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002248 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002249 ActivityContainer activityContainer =
2250 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002251 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002252 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2253 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002254 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002255 return activityContainer;
2256 }
2257
Craig Mautner34b73df2014-01-12 21:11:08 -08002258 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002259 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2260 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2261 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002262 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2263 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002264 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002265 }
2266 }
2267
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002268 void deleteActivityContainerRecord(int stackId) {
2269 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2270 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2271 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002272 }
2273
Jorim Jaggidc249c42015-12-15 14:57:31 -08002274 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002275 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002276 if (stackId == DOCKED_STACK_ID) {
2277 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002278 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002279 return;
2280 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002281 final ActivityStack stack = getStack(stackId);
2282 if (stack == null) {
2283 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2284 return;
2285 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002286
Winson Chunga2249ac2017-03-30 18:15:30 -07002287 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2288 getStack(DOCKED_STACK_ID) != null) {
2289 // If the docked stack exists, don't resize non-floating stacks independently of the
2290 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002291 return;
2292 }
2293
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002294 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002295 mWindowManager.deferSurfaceLayout();
2296 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002297 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002298 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002299 stack.ensureVisibleActivitiesConfigurationLocked(
2300 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002301 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002302 } finally {
2303 mWindowManager.continueSurfaceLayout();
2304 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002305 }
2306 }
2307
Jorim Jaggi192086e2016-03-11 17:17:03 +01002308 void deferUpdateBounds(int stackId) {
2309 final ActivityStack stack = getStack(stackId);
2310 if (stack != null) {
2311 stack.deferUpdateBounds();
2312 }
2313 }
2314
2315 void continueUpdateBounds(int stackId) {
2316 final ActivityStack stack = getStack(stackId);
2317 if (stack != null) {
2318 stack.continueUpdateBounds();
2319 }
2320 }
2321
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002322 void notifyAppTransitionDone() {
2323 continueUpdateBounds(HOME_STACK_ID);
2324 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2325 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002326 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002327 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002328 if (task != null) {
2329 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002330 }
2331 }
2332 mResizingTasksDuringAnimation.clear();
2333 }
2334
Robert Carre7cc44d2017-03-20 19:04:30 -07002335 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2336 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002337
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002338 final ActivityStack stack = getStack(fromStackId);
2339 if (stack == null) {
2340 return;
2341 }
2342
2343 mWindowManager.deferSurfaceLayout();
2344 try {
2345 if (fromStackId == DOCKED_STACK_ID) {
2346
2347 // We are moving all tasks from the docked stack to the fullscreen stack,
2348 // which is dismissing the docked stack, so resize all other stacks to
2349 // fullscreen here already so we don't end up with resize trashing.
2350 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2351 if (StackId.isResizeableByDockedStack(i)) {
2352 ActivityStack otherStack = getStack(i);
2353 if (otherStack != null) {
2354 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2355 true /* allowResizeInDockedMode */, DEFER_RESUME);
2356 }
2357 }
2358 }
2359
2360 // Also disable docked stack resizing since we have manually adjusted the
2361 // size of other stacks above and we don't want to trigger a docked stack
2362 // resize when we remove task from it below and it is detached from the
2363 // display because it no longer contains any tasks.
2364 mAllowDockedStackResize = false;
2365 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002366 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002367 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2368 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002369 // If we are moving from the pinned stack, then the animation takes care of updating
2370 // the picture-in-picture mode.
2371 final boolean schedulePictureInPictureModeChange = (fromStackId != PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002372 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2373 final int size = tasks.size();
2374 if (onTop) {
2375 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002376 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002377 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002378 if (fromStackId == PINNED_STACK_ID) {
2379 // Update the return-to to reflect where the pinned stack task was moved
2380 // from so that we retain the stack that was previously visible if the
2381 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002382 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2383 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002384 MetricsLogger.action(mService.mContext,
2385 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002386 }
Winson Chung74666102017-02-22 17:49:24 -08002387 // Defer resume until all the tasks have been moved to the fullscreen stack
2388 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2389 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002390 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002391 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002392 }
2393 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002394 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002395 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002396 final int position = fullscreenStack != null
2397 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2398 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002399 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002400 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2401 schedulePictureInPictureModeChange,
2402 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002403 }
2404 }
Winson Chung74666102017-02-22 17:49:24 -08002405
2406 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2407 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002408 } finally {
2409 mAllowDockedStackResize = true;
2410 mWindowManager.continueSurfaceLayout();
2411 }
2412 }
2413
Robert Carr6914f082017-03-20 19:04:30 -07002414 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002415 mWindowManager.inSurfaceTransaction(
2416 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002417 }
2418
Jorim Jaggidc249c42015-12-15 14:57:31 -08002419 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002420 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2421 boolean preserveWindows) {
2422 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2423 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2424 false /* deferResume */);
2425 }
2426
2427 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2428 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2429 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002430
2431 if (!mAllowDockedStackResize) {
2432 // Docked stack resize currently disabled.
2433 return;
2434 }
2435
Jorim Jaggidc249c42015-12-15 14:57:31 -08002436 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2437 if (stack == null) {
2438 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2439 return;
2440 }
2441
2442 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2443 mWindowManager.deferSurfaceLayout();
2444 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002445 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2446 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002447 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002448 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002449
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002450 // TODO: Checking for isAttached might not be needed as if the user passes in null
2451 // dockedBounds then they want the docked stack to be dismissed.
2452 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2453 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002454 // In this case we make all other static stacks fullscreen and move all
2455 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002456 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002457
2458 // stack shouldn't contain anymore activities, so nothing to resume.
2459 r = null;
2460 } else {
2461 // Docked stacks occupy a dedicated region on screen so the size of all other
2462 // static stacks need to be adjusted so they don't overlap with the docked stack.
2463 // We get the bounds to use from window manager which has been adjusted for any
2464 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002465 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002466 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002467 final ActivityStack current = getStack(i);
2468 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002469 current.getStackDockedModeBounds(
2470 tempOtherTaskBounds /* currentTempTaskBounds */,
2471 tempRect /* outStackBounds */,
2472 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2473
Matthew Nge15352e2016-12-20 15:36:29 -08002474 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002475 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2476 tempOtherTaskInsetBounds, preserveWindows,
2477 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002478 }
2479 }
2480 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002481 if (!deferResume) {
2482 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2483 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002484 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002485 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002486 mWindowManager.continueSurfaceLayout();
2487 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2488 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002489 }
2490
Robert Carr0d00c2e2016-02-29 17:45:02 -08002491 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2492 final ActivityStack stack = getStack(PINNED_STACK_ID);
2493 if (stack == null) {
2494 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2495 return;
2496 }
2497 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2498 mWindowManager.deferSurfaceLayout();
2499 try {
2500 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002501 Rect insetBounds = null;
2502 if (tempPinnedTaskBounds != null) {
2503 // We always use 0,0 as the position for the inset rect because
2504 // if we are getting insets at all in the pinned stack it must mean
2505 // we are headed for fullscreen.
2506 insetBounds = tempRect;
2507 insetBounds.top = 0;
2508 insetBounds.left = 0;
2509 insetBounds.right = tempPinnedTaskBounds.width();
2510 insetBounds.bottom = tempPinnedTaskBounds.height();
2511 }
2512 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002513 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002514 } finally {
2515 mWindowManager.continueSurfaceLayout();
2516 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2517 }
2518 }
2519
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002520 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002521 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002522 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002523 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002524 }
2525
Wale Ogunwale1666e312016-12-16 11:27:18 -08002526 final ActivityContainer activityContainer =
2527 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002528 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002529 }
2530
Robert Carr6914f082017-03-20 19:04:30 -07002531
Robert Carre7cc44d2017-03-20 19:04:30 -07002532 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002533 final ActivityStack stack = getStack(stackId);
2534 if (stack == null) {
2535 return;
2536 }
2537
2538 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2539 if (stack.getStackId() == PINNED_STACK_ID) {
2540 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2541 if (fullscreenStack != null) {
2542 final boolean isFullscreenStackVisible =
2543 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2544 for (int i = 0; i < tasks.size(); i++) {
2545 // Insert the task either at the top of the fullscreen stack if it is hidden,
Winson Chungd62acec2017-03-31 19:42:32 -07002546 // or to the bottom if it is currently visible
2547 final int insertPosition = isFullscreenStackVisible ? 0
Winson Chung010927a2016-12-15 16:12:35 -08002548 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002549 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002550 // Defer resume until we remove all the tasks
2551 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2552 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002553 }
2554 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2555 resumeFocusedStackTopActivityLocked();
2556 } else {
2557 // If there is no fullscreen stack, then create the stack and move all the tasks
2558 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002559 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002560 }
2561 } else {
2562 for (int i = tasks.size() - 1; i >= 0; i--) {
2563 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2564 REMOVE_FROM_RECENTS);
2565 }
2566 }
2567 }
2568
2569 /**
Robert Carr6914f082017-03-20 19:04:30 -07002570 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2571 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2572 * instead moved back onto the fullscreen stack.
2573 */
2574 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002575 mWindowManager.inSurfaceTransaction(
2576 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002577 }
2578
2579 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002580 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2581 */
2582 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2583 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2584 }
2585
2586 /**
Winson Chung010927a2016-12-15 16:12:35 -08002587 * Removes the task with the specified task id.
2588 *
2589 * @param taskId Identifier of the task to be removed.
2590 * @param killProcess Kill any process associated with the task if possible.
2591 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002592 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2593 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002594 * @return Returns true if the given task was found and removed.
2595 */
Winson Chung6954fc92017-03-24 16:22:12 -07002596 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2597 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002598 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2599 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002600 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002601 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002602 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2603 if (tr.isPersistable) {
2604 mService.notifyTaskPersisterLocked(null, true);
2605 }
2606 return true;
2607 }
2608 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2609 return false;
2610 }
2611
2612 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2613 if (removeFromRecents) {
2614 mRecentTasks.remove(tr);
2615 tr.removedFromRecents();
2616 }
2617 ComponentName component = tr.getBaseIntent().getComponent();
2618 if (component == null) {
2619 Slog.w(TAG, "No component for base intent of task: " + tr);
2620 return;
2621 }
2622
2623 // Find any running services associated with this app and stop if needed.
2624 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2625
2626 if (!killProcess) {
2627 return;
2628 }
2629
2630 // Determine if the process(es) for this task should be killed.
2631 final String pkg = component.getPackageName();
2632 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2633 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2634 for (int i = 0; i < pmap.size(); i++) {
2635
2636 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2637 for (int j = 0; j < uids.size(); j++) {
2638 ProcessRecord proc = uids.valueAt(j);
2639 if (proc.userId != tr.userId) {
2640 // Don't kill process for a different user.
2641 continue;
2642 }
2643 if (proc == mService.mHomeProcess) {
2644 // Don't kill the home process along with tasks from the same package.
2645 continue;
2646 }
2647 if (!proc.pkgList.containsKey(pkg)) {
2648 // Don't kill process that is not associated with this task.
2649 continue;
2650 }
2651
2652 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002653 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002654 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2655 // Don't kill process(es) that has an activity in a different task that is
2656 // also in recents.
2657 return;
2658 }
2659 }
2660
2661 if (proc.foregroundServices) {
2662 // Don't kill process(es) with foreground service.
2663 return;
2664 }
2665
2666 // Add process to kill list.
2667 procsToKill.add(proc);
2668 }
2669 }
2670
2671 // Kill the running processes.
2672 for (int i = 0; i < procsToKill.size(); i++) {
2673 ProcessRecord pr = procsToKill.get(i);
2674 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2675 && pr.curReceivers.isEmpty()) {
2676 pr.kill("remove task", true);
2677 } else {
2678 // We delay killing processes that are not in the background or running a receiver.
2679 pr.waitingToKill = "remove task";
2680 }
2681 }
2682 }
2683
Craig Mautner4a1cb222013-12-04 16:14:06 -08002684 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002685 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002686 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2687 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002688 break;
2689 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002690 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002691 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002692 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002693 }
2694
Chong Zhang5dcb2752015-08-18 13:50:26 -07002695 /**
2696 * Restores a recent task to a stack
2697 * @param task The recent task to be restored.
2698 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002699 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2700 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002701 * @return true if the task has been restored successfully.
2702 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002703 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002704 if (!StackId.isStaticStack(stackId)) {
2705 // If stack is not static (or stack id is invalid) - use the default one.
2706 // This means that tasks that were on external displays will be restored on the
2707 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002708 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002709 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002710 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2711 // Put it in the fullscreen stack.
2712 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002713 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002714
Andrii Kulian02b7a832016-10-06 23:11:56 -07002715 final ActivityStack currentStack = task.getStack();
2716 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002717 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002718 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002719 // Nothing else to do since it is already restored in the right stack.
2720 return true;
2721 }
2722 // Remove current stack association, so we can re-associate the task with the
2723 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002724 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002725 }
2726
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002727 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002728 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002729
2730 if (stack == null) {
2731 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002732 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2733 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002734 return false;
2735 }
2736
Wale Ogunwale72919d22016-12-08 18:58:50 -08002737 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002738 // TODO: move call for creation here and other place into Stack.addTask()
2739 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002740 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2741 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002742 final ArrayList<ActivityRecord> activities = task.mActivities;
2743 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002744 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002745 }
2746 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002747 }
2748
Wale Ogunwale040b4702015-08-06 18:10:50 -07002749 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002750 * Move stack with all its existing content to specified display.
2751 * @param stackId Id of stack to move.
2752 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002753 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002754 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002755 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002756 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2757 if (activityDisplay == null) {
2758 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2759 + displayId);
2760 }
2761 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2762 if (activityContainer != null) {
2763 if (activityContainer.isAttachedLocked()) {
2764 if (activityContainer.getDisplayId() == displayId) {
2765 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2766 + " to its current displayId=" + displayId);
2767 }
2768
Andrii Kulian250d6532017-02-08 23:30:45 -08002769 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002770 } else {
2771 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2772 + stackId + " is not attached to any display.");
2773 }
2774 } else {
2775 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2776 + stackId);
2777 }
2778
2779 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2780 !PRESERVE_WINDOWS);
2781 // TODO(multi-display): resize stacks properly if moved from split-screen.
2782 }
2783
2784 /**
Winson Chung74666102017-02-22 17:49:24 -08002785 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2786 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002787 */
Winson Chung74666102017-02-22 17:49:24 -08002788 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002789 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002790
Winson Chung74666102017-02-22 17:49:24 -08002791 // Check that we aren't reparenting to the same stack that the task is already in
2792 if (prevStack != null && prevStack.mStackId == stackId) {
2793 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2794 + " already in stackId=" + stackId);
2795 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002796 }
2797
Winson Chung74666102017-02-22 17:49:24 -08002798 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2799 // support
2800 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2801 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2802 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002803 }
2804
Winson Chung74666102017-02-22 17:49:24 -08002805 // Ensure that we aren't trying to move into a freeform stack without freeform
2806 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002807 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002808 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2809 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002810 }
2811
Winson Chung74666102017-02-22 17:49:24 -08002812 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2813 // used for split-screen mode and will cause things like the docked divider to show up. We
2814 // instead leave the task in its current stack or move it to the fullscreen stack if it
2815 // isn't currently in a stack.
2816 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2817 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2818 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2819 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002820 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002821
Winson Chung74666102017-02-22 17:49:24 -08002822 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2823 // example, a docked stack is created which will lead to the stack we are moving from being
2824 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002825 try {
Winson Chung74666102017-02-22 17:49:24 -08002826 task.mTemporarilyUnresizable = true;
2827 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002828 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002829 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002830 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002831 }
2832
Winson Chung08f81892017-03-02 15:40:51 -08002833 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002834 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2835 if (stack == null) {
2836 throw new IllegalArgumentException(
2837 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2838 }
2839
2840 final ActivityRecord r = stack.topRunningActivityLocked();
2841 if (r == null) {
2842 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2843 + " in stack=" + stack);
2844 return false;
2845 }
2846
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002847 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002848 Slog.w(TAG,
2849 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002850 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002851 return false;
2852 }
2853
Winson Chung08f81892017-03-02 15:40:51 -08002854 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2855 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002856 return true;
2857 }
2858
Winson Chung08f81892017-03-02 15:40:51 -08002859 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2860 boolean moveHomeStackToFront, String reason) {
2861
Wale Ogunwale480dca02016-02-06 13:58:29 -08002862 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002863
Wale Ogunwale1666e312016-12-16 11:27:18 -08002864 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002865 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002866
Wale Ogunwale480dca02016-02-06 13:58:29 -08002867 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002868 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002869
Andrii Kulian02b7a832016-10-06 23:11:56 -07002870 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002871 // An activity can't be pinned and visible behind at the same time. Go ahead and
2872 // release it from been visible behind before pinning.
2873 requestVisibleBehindLocked(r, false);
2874 }
2875
Wale Ogunwale480dca02016-02-06 13:58:29 -08002876 // Resize the pinned stack to match the current size of the task the activity we are
2877 // going to be moving is currently contained in. We do this to have the right starting
2878 // animation bounds for the pinned stack to the desired bounds the caller wants.
2879 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2880 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002881 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002882
2883 if (task.mActivities.size() == 1) {
2884 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002885 // the stack without re-parenting the activity in a different task. We don't
2886 // move the home stack forward if we are currently entering picture-in-picture
2887 // while pausing because that changes the focused stack and may prevent the new
2888 // starting activity from resuming.
2889 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002890 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002891 // Move the home stack forward if the task we just moved to the pinned stack
2892 // was launched from home so home should be visible behind it.
2893 moveHomeStackToFront(reason);
2894 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002895 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002896 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002897 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002898 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002899 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002900 // reveal/leave the other activities in their original task.
2901
2902 // Currently, we don't support reparenting activities across tasks in two different
2903 // stacks, so instead, just create a new task in the same stack, reparent the
2904 // activity into that task, and then reparent the whole task to the new stack. This
2905 // ensures that all the necessary work to migrate states in the old and new stacks
2906 // is also done.
2907 final TaskRecord newTask = task.getStack().createTaskRecord(
2908 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2909 r.mActivityType);
2910 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2911
Winson Chung5af42fc2017-03-24 17:11:33 -07002912 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002913 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002914 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002915 }
Winson Chungc2baac02017-01-11 13:34:47 -08002916
2917 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2918 // to the pinned stack
2919 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002920 } finally {
2921 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002922 }
2923
Wale Ogunwale480dca02016-02-06 13:58:29 -08002924 // The task might have already been running and its visibility needs to be synchronized
2925 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002926 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002927 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002928
Winson Chung08f81892017-03-02 15:40:51 -08002929 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002930 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002931 }
2932
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002933 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002934 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2935 if (r == null || !r.isFocusable()) {
2936 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2937 "moveActivityStackToFront: unfocusable r=" + r);
2938 return false;
2939 }
2940
Bryce Leeaf691c02017-03-20 14:20:22 -07002941 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002942 final ActivityStack stack = r.getStack();
2943 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002944 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2945 + r + " task=" + task);
2946 return false;
2947 }
2948
Chong Zhang6cda19c2016-06-14 19:07:56 -07002949 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2950 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2951 "moveActivityStackToFront: already on top, r=" + r);
2952 return false;
2953 }
2954
2955 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2956 "moveActivityStackToFront: r=" + r);
2957
2958 stack.moveToFront(reason, task);
2959 return true;
2960 }
2961
David Stevensc6b91c62017-02-08 14:23:58 -08002962 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002963 mTmpFindTaskResult.r = null;
2964 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002965 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002966 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002967 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2968 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002969 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2970 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002971 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002972 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002973 continue;
2974 }
2975 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002976 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2977 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002978 continue;
2979 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002980 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002981 // It is possible to have tasks in multiple stacks with the same root affinity, so
2982 // we should keep looking after finding an affinity match to see if there is a
2983 // better match in another stack. Also, task affinity isn't a good enough reason
2984 // to target a display which isn't the source of the intent, so skip any affinity
2985 // matches not on the specified display.
2986 if (mTmpFindTaskResult.r != null) {
2987 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2988 return mTmpFindTaskResult.r;
2989 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2990 affinityMatch = mTmpFindTaskResult.r;
2991 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002993 }
2994 }
David Stevensc6b91c62017-02-08 14:23:58 -08002995 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2996 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002997 }
2998
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002999 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3000 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003001 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3002 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003003 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003004 final ActivityRecord ar = stacks.get(stackNdx)
3005 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003006 if (ar != null) {
3007 return ar;
3008 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003009 }
3010 }
3011 return null;
3012 }
3013
Craig Mautner8d341ef2013-03-26 09:03:27 -07003014 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003015 scheduleSleepTimeout();
3016 if (!mGoingToSleep.isHeld()) {
3017 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003018 if (mLaunchingActivity.isHeld()) {
3019 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3020 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003022 mLaunchingActivity.release();
3023 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003024 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003025 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003026 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 }
3028
3029 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003030 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003031
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003032 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003033 final long endTime = System.currentTimeMillis() + timeout;
3034 while (true) {
3035 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003036 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3037 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003038 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3039 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3040 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003041 }
3042 if (cantShutdown) {
3043 long timeRemaining = endTime - System.currentTimeMillis();
3044 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003045 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003046 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003047 } catch (InterruptedException e) {
3048 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003049 } else {
3050 Slog.w(TAG, "Activity manager shutdown timed out");
3051 timedout = true;
3052 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003053 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003054 } else {
3055 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003056 }
3057 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003058
3059 // Force checkReadyForSleep to complete.
3060 mSleepTimeout = true;
3061 checkReadyForSleepLocked();
3062
Craig Mautner8d341ef2013-03-26 09:03:27 -07003063 return timedout;
3064 }
3065
3066 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003067 removeSleepTimeouts();
3068 if (mGoingToSleep.isHeld()) {
3069 mGoingToSleep.release();
3070 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003071 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3072 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003073 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3074 final ActivityStack stack = stacks.get(stackNdx);
3075 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003076 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003077 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003078 }
Craig Mautner5314a402013-09-26 12:40:16 -07003079 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003080 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003081 mGoingToSleepActivities.clear();
3082 }
3083
3084 void activitySleptLocked(ActivityRecord r) {
3085 mGoingToSleepActivities.remove(r);
3086 checkReadyForSleepLocked();
3087 }
3088
3089 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003090 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003091 // Do not care.
3092 return;
3093 }
3094
3095 if (!mSleepTimeout) {
3096 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003097 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3098 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003099 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3100 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3101 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003102 }
3103
3104 if (mStoppingActivities.size() > 0) {
3105 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003106 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003107 + mStoppingActivities.size() + " activities");
3108 scheduleIdleLocked();
3109 dontSleep = true;
3110 }
3111
3112 if (mGoingToSleepActivities.size() > 0) {
3113 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003114 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003115 + mGoingToSleepActivities.size() + " activities");
3116 dontSleep = true;
3117 }
3118
3119 if (dontSleep) {
3120 return;
3121 }
3122 }
3123
Wei Wang65c7a152016-06-02 18:51:22 -07003124 // Send launch end powerhint before going sleep
3125 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3126
Craig Mautnere0a38842013-12-16 16:14:02 -08003127 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3128 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3130 stacks.get(stackNdx).goToSleep();
3131 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003132 }
3133
3134 removeSleepTimeouts();
3135
3136 if (mGoingToSleep.isHeld()) {
3137 mGoingToSleep.release();
3138 }
3139 if (mService.mShuttingDown) {
3140 mService.notifyAll();
3141 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003142 }
3143
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003144 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003145 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003146 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003147 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003148 }
3149 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003150 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003151 mWindowManager.executeAppTransition();
3152 return true;
3153 }
3154 return false;
3155 }
3156
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003158 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3159 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003160 int stackNdx = stacks.size() - 1;
3161 while (stackNdx >= 0) {
3162 stacks.get(stackNdx).handleAppCrashLocked(app);
3163 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003164 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003165 }
3166 }
3167
Jose Lima4b6c6692014-08-12 17:41:12 -07003168 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003169 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003170 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003171 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3172 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003173 return false;
3174 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003175
3176 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3177 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3178 + " visible=" + visible + " stackId=" + stack.mStackId
3179 + " can't contain visible behind activities");
3180 return false;
3181 }
3182
Jose Lima4b6c6692014-08-12 17:41:12 -07003183 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003184 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3185 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003186
3187 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003188 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003189 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003190 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003191 return true;
3192 }
3193
3194 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003195 if (visible && top.fullscreen) {
3196 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003197 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3198 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3199 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3200 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003201 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003202 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3203 // Only the activity set as currently visible behind should actively reset its
3204 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003205 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3206 "requestVisibleBehind: returning visible=" + visible
3207 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3208 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003209 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003210 }
3211
Jose Lima4b6c6692014-08-12 17:41:12 -07003212 stack.setVisibleBehindActivity(visible ? r : null);
3213 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003214 // If there is a translucent home activity, we need to force it stop being translucent,
3215 // because we can't depend on the application to necessarily perform that operation.
3216 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003217 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003218 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003219 mService.convertFromTranslucent(next.appToken);
3220 }
3221 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003222 if (top.app != null && top.app.thread != null) {
3223 // Notify the top app of the change.
3224 try {
3225 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3226 } catch (RemoteException e) {
3227 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003228 }
3229 return true;
3230 }
3231
Craig Mautnerbb742462014-07-07 15:28:55 -07003232 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003233 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003234 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003235 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003236
3237 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003238 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003239 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003240 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003241 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003242
3243 // When launching tasks behind, update the last active time of the top task after the new
3244 // task has been shown briefly
3245 final ActivityRecord top = stack.topActivity();
3246 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003247 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003248 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003249 }
3250
3251 void scheduleLaunchTaskBehindComplete(IBinder token) {
3252 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3253 }
3254
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003255 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3256 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003257 mKeyguardController.beginActivityVisibilityUpdate();
3258 try {
3259 // First the front stacks. In case any are not fullscreen and are in front of home.
3260 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3261 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3262 final int topStackNdx = stacks.size() - 1;
3263 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3264 final ActivityStack stack = stacks.get(stackNdx);
3265 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3266 }
Craig Mautner580ea812013-04-25 12:58:38 -07003267 }
Jorim Jaggife762342016-10-13 14:33:27 +02003268 } finally {
3269 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003270 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003271 }
3272
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003273 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3274 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3275 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3276 final int topStackNdx = stacks.size() - 1;
3277 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3278 final ActivityStack stack = stacks.get(stackNdx);
3279 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3280 }
3281 }
3282 }
3283
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003284 void invalidateTaskLayers() {
3285 mTaskLayersChanged = true;
3286 }
3287
3288 void rankTaskLayersIfNeeded() {
3289 if (!mTaskLayersChanged) {
3290 return;
3291 }
3292 mTaskLayersChanged = false;
3293 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3294 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3295 int baseLayer = 0;
3296 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3297 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3298 }
3299 }
3300 }
3301
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003302 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3303 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3304 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3305 final int topStackNdx = stacks.size() - 1;
3306 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3307 final ActivityStack stack = stacks.get(stackNdx);
3308 stack.clearOtherAppTimeTrackers(except);
3309 }
3310 }
3311 }
3312
Craig Mautner8d341ef2013-03-26 09:03:27 -07003313 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003314 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3315 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003316 final int numStacks = stacks.size();
3317 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3318 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003319 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003321 }
3322 }
3323
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003324 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3325 // Examine all activities currently running in the process.
3326 TaskRecord firstTask = null;
3327 // Tasks is non-null only if two or more tasks are found.
3328 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003329 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3330 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003331 ActivityRecord r = app.activities.get(i);
3332 // First, if we find an activity that is in the process of being destroyed,
3333 // then we just aren't going to do anything for now; we want things to settle
3334 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003335 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003336 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003337 return;
3338 }
3339 // Don't consider any activies that are currently not in a state where they
3340 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003341 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3342 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003343 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003344 continue;
3345 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003346
3347 final TaskRecord task = r.getTask();
3348 if (task != null) {
3349 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003350 + " from " + r);
3351 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003352 firstTask = task;
3353 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003354 if (tasks == null) {
3355 tasks = new ArraySet<>();
3356 tasks.add(firstTask);
3357 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003358 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003359 }
3360 }
3361 }
3362 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003363 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003364 return;
3365 }
3366 // If we have activities in multiple tasks that are in a position to be destroyed,
3367 // let's iterate through the tasks and release the oldest one.
3368 final int numDisplays = mActivityDisplays.size();
3369 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3370 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3371 // Step through all stacks starting from behind, to hit the oldest things first.
3372 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3373 final ActivityStack stack = stacks.get(stackNdx);
3374 // Try to release activities in this stack; if we manage to, we are done.
3375 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3376 return;
3377 }
3378 }
3379 }
3380 }
3381
Amith Yamasani37a40c22015-06-17 13:25:42 -07003382 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003383 final int focusStackId = mFocusedStack.getStackId();
3384 // We dismiss the docked stack whenever we switch users.
3385 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003386 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3387 // also cause all tasks to be moved to the fullscreen stack at a position that is
3388 // appropriate.
3389 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003390
3391 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003392 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003393 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003394
Craig Mautner858d8a62013-04-23 17:08:34 -07003395 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003396 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3397 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003398 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003399 final ActivityStack stack = stacks.get(stackNdx);
3400 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003401 TaskRecord task = stack.topTask();
3402 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003403 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003404 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003405 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003406 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003407
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003408 ActivityStack stack = getStack(restoreStackId);
3409 if (stack == null) {
3410 stack = mHomeStack;
3411 }
3412 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003413 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003414 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003415 } else {
3416 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003417 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003418 }
Craig Mautner93529a42013-10-04 15:03:13 -07003419 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003420 }
3421
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003422 /** Checks whether the userid is a profile of the current user. */
3423 boolean isCurrentProfileLocked(int userId) {
3424 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003425 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003426 }
3427
Winson Chung4dabf232017-01-25 13:25:22 -08003428 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3429 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003430 ArrayList<ActivityRecord> stops = null;
3431
3432 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003433 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3434 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003435 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003436 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003437 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3438 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003439 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003440 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003441 if (s.finishing) {
3442 // If this activity is finishing, it is sitting on top of
3443 // everyone else but we now know it is no longer needed...
3444 // so get rid of it. Otherwise, we need to go through the
3445 // normal flow and hide it once we determine that it is
3446 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003447 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003448 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003449 }
3450 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003451 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003452 if (!processPausingActivities && s.state == PAUSING) {
3453 // Defer processing pausing activities in this iteration and reschedule
3454 // a delayed idle to reprocess it again
3455 removeTimeoutsForActivityLocked(idleActivity);
3456 scheduleIdleTimeoutLocked(idleActivity);
3457 continue;
3458 }
3459
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003460 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003461 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003462 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003463 }
3464 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003465 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003466 }
3467 }
3468
3469 return stops;
3470 }
3471
Craig Mautnercf910b02013-04-23 11:23:27 -07003472 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3474 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3475 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3476 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003477 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003478 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003479 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003480 if (r == null) Slog.e(TAG,
3481 "validateTop...: null top activity, stack=" + stack);
3482 else {
3483 final ActivityRecord pausing = stack.mPausingActivity;
3484 if (pausing != null && pausing == r) Slog.e(TAG,
3485 "validateTop...: top stack has pausing activity r=" + r
3486 + " state=" + state);
3487 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3488 "validateTop...: activity in front not resumed r=" + r
3489 + " state=" + state);
3490 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003491 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003492 final ActivityRecord resumed = stack.mResumedActivity;
3493 if (resumed != null && resumed == r) Slog.e(TAG,
3494 "validateTop...: back stack has resumed activity r=" + r
3495 + " state=" + state);
3496 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3497 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003498 }
3499 }
3500 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003501 }
3502
Craig Mautnere0570202015-05-13 13:06:11 -07003503 private String lockTaskModeToString() {
3504 switch (mLockTaskModeState) {
3505 case LOCK_TASK_MODE_LOCKED:
3506 return "LOCKED";
3507 case LOCK_TASK_MODE_PINNED:
3508 return "PINNED";
3509 case LOCK_TASK_MODE_NONE:
3510 return "NONE";
3511 default: return "unknown=" + mLockTaskModeState;
3512 }
3513 }
3514
Craig Mautner27084302013-03-25 08:05:25 -07003515 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003516 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003517 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003518 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003519 pw.print(prefix);
3520 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003521 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003522 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003523 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003524 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3525 if (packages.size() > 0) {
3526 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3527 for (int i = 0; i < packages.size(); ++i) {
3528 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3529 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3530 }
3531 }
Bryce Lee4a194382017-04-04 14:32:48 -07003532 if (!mWaitingForActivityVisible.isEmpty()) {
3533 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3534 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3535 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3536 }
3537 }
3538
Jorim Jaggi8d786932016-10-26 19:08:36 -07003539 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3540 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003541 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003542
Winson43d1f262016-06-14 16:05:55 -07003543 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003544 * Dump all connected displays' configurations.
3545 * @param prefix Prefix to apply to each line of the dump.
3546 */
3547 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3548 pw.print(prefix); pw.println("Display override configurations:");
3549 final int displayCount = mActivityDisplays.size();
3550 for (int i = 0; i < displayCount; i++) {
3551 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3552 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3553 pw.println(activityDisplay.getOverrideConfiguration());
3554 }
3555 }
3556
3557 /**
Winson43d1f262016-06-14 16:05:55 -07003558 * Dumps the activities matching the given {@param name} in the either the focused stack
3559 * or all visible stacks if {@param dumpVisibleStacks} is true.
3560 */
Winson Chung6998bc42017-02-28 17:07:05 -08003561 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3562 boolean dumpFocusedStackOnly) {
3563 if (dumpFocusedStackOnly) {
3564 return mFocusedStack.getDumpActivitiesLocked(name);
3565 } else {
Winson43d1f262016-06-14 16:05:55 -07003566 ArrayList<ActivityRecord> activities = new ArrayList<>();
3567 int numDisplays = mActivityDisplays.size();
3568 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3569 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3571 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003572 if (!dumpVisibleStacksOnly ||
3573 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003574 activities.addAll(stack.getDumpActivitiesLocked(name));
3575 }
3576 }
3577 }
3578 return activities;
Winson43d1f262016-06-14 16:05:55 -07003579 }
Craig Mautner20e72272013-04-01 13:45:53 -07003580 }
3581
Dianne Hackborn390517b2013-05-30 15:03:32 -07003582 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3583 boolean needSep, String prefix) {
3584 if (activity != null) {
3585 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3586 if (needSep) {
3587 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003588 }
3589 pw.print(prefix);
3590 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003591 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003592 }
3593 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003594 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003595 }
3596
Craig Mautner8d341ef2013-03-26 09:03:27 -07003597 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3598 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003599 boolean printed = false;
3600 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003601 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3602 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003603 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003604 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003605 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003606 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003607 final ActivityStack stack = stacks.get(stackNdx);
3608 StringBuilder stackHeader = new StringBuilder(128);
3609 stackHeader.append(" Stack #");
3610 stackHeader.append(stack.mStackId);
3611 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003612 stackHeader.append("\n");
3613 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3614 stackHeader.append("\n");
3615 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003616
3617 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3618 dumpClient, dumpPackage, needSep, stackHeader.toString());
3619 printed |= printedStackHeader;
3620 if (!printedStackHeader) {
3621 // Ensure we always dump the stack header even if there are no activities
3622 pw.println();
3623 pw.println(stackHeader);
3624 }
3625
Craig Mautner4a1cb222013-12-04 16:14:06 -08003626 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3627 !dumpAll, false, dumpPackage, true,
3628 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 needSep = printed;
3631 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3632 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003633 if (pr) {
3634 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003635 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003636 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003637 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3638 " mResumedActivity: ");
3639 if (pr) {
3640 printed = true;
3641 needSep = false;
3642 }
3643 if (dumpAll) {
3644 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3645 " mLastPausedActivity: ");
3646 if (pr) {
3647 printed = true;
3648 needSep = true;
3649 }
3650 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3651 needSep, " mLastNoHistoryActivity: ");
3652 }
3653 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 }
3655 }
3656
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003657 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3658 false, dumpPackage, true, " Activities waiting to finish:", null);
3659 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3660 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003661 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3662 false, !dumpAll, false, dumpPackage, true,
3663 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003664 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3665 false, dumpPackage, true, " Activities waiting to sleep:", null);
3666 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3667 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003668
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003669 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003670 }
3671
Dianne Hackborn390517b2013-05-30 15:03:32 -07003672 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003673 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003674 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003675 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003676 String innerPrefix = null;
3677 String[] args = null;
3678 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003679 for (int i=list.size()-1; i>=0; i--) {
3680 final ActivityRecord r = list.get(i);
3681 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3682 continue;
3683 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003684 if (innerPrefix == null) {
3685 innerPrefix = prefix + " ";
3686 args = new String[0];
3687 }
3688 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003689 final boolean full = !brief && (complete || !r.isInHistory());
3690 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003691 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003692 needNL = false;
3693 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003694 if (header1 != null) {
3695 pw.println(header1);
3696 header1 = null;
3697 }
3698 if (header2 != null) {
3699 pw.println(header2);
3700 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003701 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003702 if (lastTask != r.getTask()) {
3703 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003704 pw.print(prefix);
3705 pw.print(full ? "* " : " ");
3706 pw.println(lastTask);
3707 if (full) {
3708 lastTask.dump(pw, prefix + " ");
3709 } else if (complete) {
3710 // Complete + brief == give a summary. Isn't that obvious?!?
3711 if (lastTask.intent != null) {
3712 pw.print(prefix); pw.print(" ");
3713 pw.println(lastTask.intent.toInsecureStringWithClip());
3714 }
3715 }
3716 }
3717 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3718 pw.print(" #"); pw.print(i); pw.print(": ");
3719 pw.println(r);
3720 if (full) {
3721 r.dump(pw, innerPrefix);
3722 } else if (complete) {
3723 // Complete + brief == give a summary. Isn't that obvious?!?
3724 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3725 if (r.app != null) {
3726 pw.print(innerPrefix); pw.println(r.app);
3727 }
3728 }
3729 if (client && r.app != null && r.app.thread != null) {
3730 // flush anything that is already in the PrintWriter since the thread is going
3731 // to write to the file descriptor directly
3732 pw.flush();
3733 try {
3734 TransferPipe tp = new TransferPipe();
3735 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003736 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003737 // Short timeout, since blocking here can
3738 // deadlock with the application.
3739 tp.go(fd, 2000);
3740 } finally {
3741 tp.kill();
3742 }
3743 } catch (IOException e) {
3744 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3745 } catch (RemoteException e) {
3746 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3747 }
3748 needNL = true;
3749 }
3750 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003751 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003752 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003753
Craig Mautnerf3333272013-04-22 10:55:53 -07003754 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003755 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3756 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003757 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3758 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003759 }
3760
3761 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003762 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003763 }
3764
3765 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003766 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3767 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003768 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3769 }
3770
Craig Mautner05d29032013-05-03 13:40:13 -07003771 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003772 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3773 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3774 }
Craig Mautner05d29032013-05-03 13:40:13 -07003775 }
3776
Craig Mautner0eea92c2013-05-16 13:35:39 -07003777 void removeSleepTimeouts() {
3778 mSleepTimeout = false;
3779 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3780 }
3781
3782 final void scheduleSleepTimeout() {
3783 removeSleepTimeouts();
3784 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3785 }
3786
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 @Override
3788 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003789 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3791 }
3792
3793 @Override
3794 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003795 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3797 }
3798
3799 @Override
3800 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003801 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3803 }
3804
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003805 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003806 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003808 newDisplay = mActivityDisplays.get(displayId) == null;
3809 if (newDisplay) {
3810 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003811 if (activityDisplay.mDisplay == null) {
3812 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3813 return;
3814 }
Craig Mautner4504de52013-12-20 09:06:56 -08003815 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003816 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003817 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003818 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 }
3821
Andrii Kulianca007a62016-11-22 16:33:28 -08003822 /** Check if display with specified id is added to the list. */
3823 boolean isDisplayAdded(int displayId) {
3824 return mActivityDisplays.get(displayId) != null;
3825 }
3826
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003827 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003828 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003829 mService.mContext.getResources().getDimensionPixelSize(
3830 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003831 }
3832
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003833 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003834 if (displayId == DEFAULT_DISPLAY) {
3835 throw new IllegalArgumentException("Can't remove the primary display.");
3836 }
3837
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003839 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3840 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003841 final boolean destroyContentOnRemoval
3842 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003843 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3844 while (!stacks.isEmpty()) {
3845 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003846 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003847 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3848 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003849 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003850 } else {
3851 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3852 // a valid launch stack for all activities. This way the task history from
3853 // external display will be preserved on primary after move.
3854 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003855 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003857 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003858 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003859 }
3860 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003861 }
3862
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003863 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003865 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3866 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003867 // TODO: Update the bounds.
3868 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003869 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003870 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871 }
3872
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003873 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003874 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003875 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003876 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003877 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003878 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003879 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003880 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003881 info.position = display != null
3882 ? display.mStacks.indexOf(stack)
3883 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003884
3885 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3886 final int numTasks = tasks.size();
3887 int[] taskIds = new int[numTasks];
3888 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003889 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003890 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003891 for (int i = 0; i < numTasks; ++i) {
3892 final TaskRecord task = tasks.get(i);
3893 taskIds[i] = task.taskId;
3894 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3895 : task.realActivity != null ? task.realActivity.flattenToString()
3896 : task.getTopActivity() != null ? task.getTopActivity().packageName
3897 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003898 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003899 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003900 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 }
3902 info.taskIds = taskIds;
3903 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003904 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003905 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003906
3907 final ActivityRecord top = stack.topRunningActivityLocked();
3908 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003909 return info;
3910 }
3911
3912 StackInfo getStackInfoLocked(int stackId) {
3913 ActivityStack stack = getStack(stackId);
3914 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003915 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003916 }
3917 return null;
3918 }
3919
3920 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003921 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003922 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3923 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003925 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003926 }
3927 }
3928 return list;
3929 }
3930
Craig Mautner15df08a2015-04-01 12:17:18 -07003931 TaskRecord getLockedTaskLocked() {
3932 final int top = mLockTaskModeTasks.size() - 1;
3933 if (top >= 0) {
3934 return mLockTaskModeTasks.get(top);
3935 }
3936 return null;
3937 }
3938
3939 boolean isLockedTask(TaskRecord task) {
3940 return mLockTaskModeTasks.contains(task);
3941 }
3942
3943 boolean isLastLockedTask(TaskRecord task) {
3944 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3945 }
3946
3947 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003948 if (!mLockTaskModeTasks.remove(task)) {
3949 return;
3950 }
3951 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3952 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003953 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003954 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3955 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003956 final Message lockTaskMsg = Message.obtain();
3957 lockTaskMsg.arg1 = task.userId;
3958 lockTaskMsg.what = LOCK_TASK_END_MSG;
3959 mHandler.sendMessage(lockTaskMsg);
3960 }
3961 }
3962
Andrii Kulianc27916642016-04-12 17:59:27 -07003963 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3964 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3965 false /* forceNonResizable */);
3966 }
3967
Jorim Jaggid53f0922016-04-06 22:16:23 -07003968 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003969 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003970 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3971 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003972 return;
3973 }
3974
Jorim Jaggicd13d332016-04-27 15:40:20 -07003975 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003976 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003977 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003978 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003979
Andrii Kulianc27916642016-04-12 17:59:27 -07003980 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3981 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003982 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003983 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003984 && !topActivity.noDisplay) {
3985 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003986 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3987 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003988 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003989 }
3990
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003991 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003992 if (mLockTaskNotify != null) {
3993 mLockTaskNotify.showToast(mLockTaskModeState);
3994 }
Jason Monka8f569c2014-07-07 12:15:21 -04003995 }
3996
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003997 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3998 if (mLockTaskModeTasks.contains(task)) {
3999 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4000 }
4001 }
4002
Craig Mautner432f64e2015-05-20 14:59:57 -07004003 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4004 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004005 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004006 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004007 final TaskRecord lockedTask = getLockedTaskLocked();
4008 if (lockedTask != null) {
4009 removeLockedTaskLocked(lockedTask);
4010 if (!mLockTaskModeTasks.isEmpty()) {
4011 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004012 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4013 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004014 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004015 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004016 return;
4017 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004018 }
Craig Mautnere0570202015-05-13 13:06:11 -07004019 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4020 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004021 return;
4022 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004023
4024 // Should have already been checked, but do it again.
4025 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004026 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4027 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004028 return;
4029 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004030 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004031 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004032 return;
4033 }
4034
4035 if (mLockTaskModeTasks.isEmpty()) {
4036 // First locktask.
4037 final Message lockTaskMsg = Message.obtain();
4038 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4039 lockTaskMsg.arg1 = task.userId;
4040 lockTaskMsg.what = LOCK_TASK_START_MSG;
4041 lockTaskMsg.arg2 = lockTaskModeState;
4042 mHandler.sendMessage(lockTaskMsg);
4043 }
4044 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004045 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4046 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004047 mLockTaskModeTasks.remove(task);
4048 mLockTaskModeTasks.add(task);
4049
4050 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004051 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004052 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004053
4054 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004055 findTaskToMoveToFrontLocked(task, 0, null, reason,
4056 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004057 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004058 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004059 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004060 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004061 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004062 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004063 }
4064
4065 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004066 return isLockTaskModeViolation(task, false);
4067 }
4068
4069 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4070 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004071 return false;
4072 }
4073 final int lockTaskAuth = task.mLockTaskAuth;
4074 switch (lockTaskAuth) {
4075 case LOCK_TASK_AUTH_DONT_LOCK:
4076 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004077 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004078 case LOCK_TASK_AUTH_LAUNCHABLE:
4079 case LOCK_TASK_AUTH_WHITELISTED:
4080 return false;
4081 case LOCK_TASK_AUTH_PINNABLE:
4082 // Pinnable tasks can't be launched on top of locktask tasks.
4083 return !mLockTaskModeTasks.isEmpty();
4084 default:
4085 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4086 return true;
4087 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004088 }
4089
Craig Mautner15df08a2015-04-01 12:17:18 -07004090 void onLockTaskPackagesUpdatedLocked() {
4091 boolean didSomething = false;
4092 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4093 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004094 final boolean wasWhitelisted =
4095 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4096 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004097 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004098 final boolean isWhitelisted =
4099 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4100 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4101 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004102 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004103 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4104 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004105 removeLockedTaskLocked(lockedTask);
4106 lockedTask.performClearTaskLocked();
4107 didSomething = true;
4108 }
4109 }
4110 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4111 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4112 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4113 final ActivityStack stack = stacks.get(stackNdx);
4114 stack.onLockTaskPackagesUpdatedLocked();
4115 }
4116 }
Craig Mautnere0570202015-05-13 13:06:11 -07004117 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004118 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004119 if (mLockTaskModeTasks.isEmpty() && task != null
4120 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4121 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004122 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4123 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4124 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4125 false);
4126 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004127 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004128 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004129 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004130 }
4131 }
4132
Benjamin Franz43261142015-02-11 15:59:44 +00004133 int getLockTaskModeState() {
4134 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004135 }
4136
Jorim Jaggife89d122015-12-22 16:28:44 +01004137 void activityRelaunchedLocked(IBinder token) {
4138 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004139 if (mService.isSleepingOrShuttingDownLocked()) {
4140 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4141 if (r != null) {
4142 r.setSleeping(true, true);
4143 }
4144 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004145 }
4146
4147 void activityRelaunchingLocked(ActivityRecord r) {
4148 mWindowManager.notifyAppRelaunching(r.appToken);
4149 }
4150
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004151 void logStackState() {
4152 mActivityMetricsLogger.logWindowState();
4153 }
4154
Winson Chung5af42fc2017-03-24 17:11:33 -07004155 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004156 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4157 final ActivityRecord r = task.mActivities.get(i);
4158 if (r.app != null && r.app.thread != null) {
4159 mMultiWindowModeChangedActivities.add(r);
4160 }
4161 }
4162
4163 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4164 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4165 }
4166 }
4167
Winson Chung5af42fc2017-03-24 17:11:33 -07004168 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004169 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004170 if (prevStack == null || prevStack == stack
4171 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4172 return;
4173 }
4174
Winson Chung5af42fc2017-03-24 17:11:33 -07004175 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4176 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004177
Winson Chung5af42fc2017-03-24 17:11:33 -07004178 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4179 boolean immediate) {
4180
4181 if (immediate) {
4182 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4183 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4184 final ActivityRecord r = task.mActivities.get(i);
4185 if (r.app != null && r.app.thread != null) {
4186 r.updatePictureInPictureMode(targetStackBounds);
4187 }
4188 }
4189 } else {
4190 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4191 final ActivityRecord r = task.mActivities.get(i);
4192 if (r.app != null && r.app.thread != null) {
4193 mPipModeChangedActivities.add(r);
4194 }
4195 }
4196 mPipModeChangedTargetStackBounds = targetStackBounds;
4197
4198 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4199 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4200 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004201 }
4202 }
4203
Tony Mak853304c2016-04-18 15:17:41 +01004204 void setDockedStackMinimized(boolean minimized) {
4205 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004206 }
4207
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004208 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004209
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004210 public ActivityStackSupervisorHandler(Looper looper) {
4211 super(looper);
4212 }
4213
Winson Chung4dabf232017-01-25 13:25:22 -08004214 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004215 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004216 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4217 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004218 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004219 }
4220
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004221 @Override
4222 public void handleMessage(Message msg) {
4223 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004224 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4225 synchronized (mService) {
4226 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4227 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004228 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004229 }
4230 }
4231 } break;
4232 case REPORT_PIP_MODE_CHANGED_MSG: {
4233 synchronized (mService) {
4234 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4235 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004236 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004237 }
4238 }
4239 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004240 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004241 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4242 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004243 if (mService.mDidDexOpt) {
4244 mService.mDidDexOpt = false;
4245 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4246 nmsg.obj = msg.obj;
4247 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4248 return;
4249 }
4250 // We don't at this point know if the activity is fullscreen,
4251 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004252 activityIdleInternal((ActivityRecord) msg.obj,
4253 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004254 } break;
4255 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004256 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004257 activityIdleInternal((ActivityRecord) msg.obj,
4258 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004259 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004260 case RESUME_TOP_ACTIVITY_MSG: {
4261 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004262 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004263 }
4264 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004265 case SLEEP_TIMEOUT_MSG: {
4266 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004267 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004268 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4269 mSleepTimeout = true;
4270 checkReadyForSleepLocked();
4271 }
4272 }
4273 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004274 case LAUNCH_TIMEOUT_MSG: {
4275 if (mService.mDidDexOpt) {
4276 mService.mDidDexOpt = false;
4277 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4278 return;
4279 }
4280 synchronized (mService) {
4281 if (mLaunchingActivity.isHeld()) {
4282 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4283 if (VALIDATE_WAKE_LOCK_CALLER
4284 && Binder.getCallingUid() != Process.myUid()) {
4285 throw new IllegalStateException("Calling must be system uid");
4286 }
4287 mLaunchingActivity.release();
4288 }
4289 }
4290 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004291 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004292 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004293 } break;
4294 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004295 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004296 } break;
4297 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004298 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004299 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004300 case CONTAINER_CALLBACK_VISIBILITY: {
4301 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004302 final IActivityContainerCallback callback = container.mCallback;
4303 if (callback != null) {
4304 try {
4305 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4306 } catch (RemoteException e) {
4307 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004308 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004309 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004310 case LOCK_TASK_START_MSG: {
4311 // When lock task starts, we disable the status bars.
4312 try {
Jason Monk62515be2014-05-21 16:06:19 -04004313 if (mLockTaskNotify == null) {
4314 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004315 }
Jason Monk62515be2014-05-21 16:06:19 -04004316 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004317 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004318 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004319 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004320 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004321 flags = StatusBarManager.DISABLE_MASK
4322 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004323 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004324 flags = StatusBarManager.DISABLE_MASK
4325 & (~StatusBarManager.DISABLE_BACK)
4326 & (~StatusBarManager.DISABLE_HOME)
4327 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004328 }
4329 getStatusBarService().disable(flags, mToken,
4330 mService.mContext.getPackageName());
4331 }
4332 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004333 if (getDevicePolicyManager() != null) {
4334 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004335 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004336 }
justinzhang5286d3f2014-05-12 17:06:01 -04004337 } catch (RemoteException ex) {
4338 throw new RuntimeException(ex);
4339 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004340 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004341 case LOCK_TASK_END_MSG: {
4342 // When lock task ends, we enable the status bars.
4343 try {
Jason Monk62515be2014-05-21 16:06:19 -04004344 if (getStatusBarService() != null) {
4345 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4346 mService.mContext.getPackageName());
4347 }
4348 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004349 if (getDevicePolicyManager() != null) {
4350 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4351 msg.arg1);
4352 }
Jason Monk62515be2014-05-21 16:06:19 -04004353 if (mLockTaskNotify == null) {
4354 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4355 }
4356 mLockTaskNotify.show(false);
4357 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004358 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004359 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004360 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004361 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004362 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004363 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004364 new LockPatternUtils(mService.mContext)
4365 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004366 }
4367 } catch (SettingNotFoundException e) {
4368 // No setting, don't lock.
4369 }
justinzhang5286d3f2014-05-12 17:06:01 -04004370 } catch (RemoteException ex) {
4371 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004372 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004373 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004374 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004375 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004376 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4377 if (mLockTaskNotify == null) {
4378 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4379 }
4380 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4381 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004382 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4383 final ActivityContainer container = (ActivityContainer) msg.obj;
4384 final IActivityContainerCallback callback = container.mCallback;
4385 if (callback != null) {
4386 try {
4387 callback.onAllActivitiesComplete(container.asBinder());
4388 } catch (RemoteException e) {
4389 }
4390 }
4391 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004392 case LAUNCH_TASK_BEHIND_COMPLETE: {
4393 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004394 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004395 if (r != null) {
4396 handleLaunchTaskBehindCompleteLocked(r);
4397 }
4398 }
4399 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004400
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004401 }
4402 }
4403 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004404
Ying Wangb081a592014-04-22 15:20:16 -07004405 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004406 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4407 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004408 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004409 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004410 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004411 ActivityRecord mParentActivity = null;
4412 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004413
Craig Mautnere3a00d72014-04-16 08:31:19 -07004414 boolean mVisible = true;
4415
Craig Mautner4a1cb222013-12-04 16:14:06 -08004416 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004417 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004418
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004419 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4420 final static int CONTAINER_STATE_NO_SURFACE = 1;
4421 final static int CONTAINER_STATE_FINISHING = 2;
4422 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4423
Wale Ogunwale1666e312016-12-16 11:27:18 -08004424 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004425 synchronized (mService) {
4426 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004427 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004428 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004429
4430 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004431 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004432 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004433 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004434
Bryce Leeaf691c02017-03-20 14:20:22 -07004435 protected void createStack(int stackId, boolean onTop) {
4436 switch (stackId) {
4437 case PINNED_STACK_ID:
4438 new PinnedActivityStack(this, mRecentTasks, onTop);
4439 break;
4440 default:
4441 new ActivityStack(this, mRecentTasks, onTop);
4442 break;
4443 }
4444 }
4445
Andrii Kulian839def92016-11-02 10:58:58 -07004446 /**
4447 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004448 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004449 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004450 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004451 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004452 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004453 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004454 if (mActivityDisplay != null) {
4455 throw new IllegalStateException("ActivityContainer is already attached, " +
4456 "displayId=" + mActivityDisplay.mDisplayId);
4457 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004458 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004459 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004460 }
4461
4462 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004463 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004465 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4466 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004467 return;
4468 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004469 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470 }
4471 }
4472
4473 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004474 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004475 synchronized (mService) {
4476 if (mActivityDisplay != null) {
4477 return mActivityDisplay.mDisplayId;
4478 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004479 }
4480 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004481 }
4482
Jeff Brownca9bc702014-02-11 14:32:56 -08004483 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004484 public int getStackId() {
4485 synchronized (mService) {
4486 return mStackId;
4487 }
4488 }
4489
4490 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004491 public boolean injectEvent(InputEvent event) {
4492 final long origId = Binder.clearCallingIdentity();
4493 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004494 synchronized (mService) {
4495 if (mActivityDisplay != null) {
4496 return mInputManagerInternal.injectInputEvent(event,
4497 mActivityDisplay.mDisplayId,
4498 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4499 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004500 }
4501 return false;
4502 } finally {
4503 Binder.restoreCallingIdentity(origId);
4504 }
4505 }
4506
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004507 @Override
4508 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004509 synchronized (mService) {
4510 if (mContainerState == CONTAINER_STATE_FINISHING) {
4511 return;
4512 }
4513 mContainerState = CONTAINER_STATE_FINISHING;
4514
Craig Mautnerd163e752014-06-13 17:18:47 -07004515 long origId = Binder.clearCallingIdentity();
4516 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004517 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004518 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004519 } finally {
4520 Binder.restoreCallingIdentity(origId);
4521 }
4522 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004523 }
4524
Andrii Kulian03c403d2016-11-11 11:14:12 -08004525 /**
4526 * Remove the stack completely. Must be called only when there are no tasks left in it,
4527 * as this method does not finish running activities.
4528 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004529 void removeLocked() {
4530 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004531 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004532 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004533 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004535 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004536 }
4537
Andrii Kulian839def92016-11-02 10:58:58 -07004538 /**
4539 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4540 * completely or re-parented.
4541 */
4542 private void removeFromDisplayLocked() {
4543 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4544 + " current displayId=" + mActivityDisplay.mDisplayId);
4545
Wale Ogunwale1666e312016-12-16 11:27:18 -08004546 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004547 mActivityDisplay = null;
4548 }
4549
4550 /**
4551 * Move the stack to specified display.
4552 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004553 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004554 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004555 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004556 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4557 + mActivityDisplay + " to display=" + activityDisplay
4558 + " Callers=" + Debug.getCallers(2));
4559
4560 removeFromDisplayLocked();
4561
4562 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004563 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004564 }
4565
Craig Mautner4a1cb222013-12-04 16:14:06 -08004566 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004567 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004568 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004569 }
4570
4571 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004572 public final int startActivityIntentSender(IIntentSender intentSender)
4573 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004574 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4575
4576 if (!(intentSender instanceof PendingIntentRecord)) {
4577 throw new IllegalArgumentException("Bad PendingIntent object");
4578 }
4579
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004580 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004581 Binder.getCallingUid(), mCurrentUser, false,
4582 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004583
4584 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4585 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4586 pendingIntent.key.requestResolvedType);
4587
4588 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4589 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4590 }
4591
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004592 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004593 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004594 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004595 throw new SecurityException(
4596 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4597 }
4598 }
4599
Craig Mautnerdf88d732014-01-27 09:21:32 -08004600 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004601 public IBinder asBinder() {
4602 return this;
4603 }
4604
Craig Mautner4504de52013-12-20 09:06:56 -08004605 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004606 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004607 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004608 }
4609
Craig Mautner4a1cb222013-12-04 16:14:06 -08004610 ActivityStackSupervisor getOuter() {
4611 return ActivityStackSupervisor.this;
4612 }
4613
Craig Mautnerd163e752014-06-13 17:18:47 -07004614 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004615 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004616 }
4617
Craig Mautner6985bad2014-04-21 15:22:06 -07004618 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004619 void setVisible(boolean visible) {
4620 if (mVisible != visible) {
4621 mVisible = visible;
4622 if (mCallback != null) {
4623 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4624 0 /* unused */, this).sendToTarget();
4625 }
4626 }
4627 }
4628
Craig Mautner6985bad2014-04-21 15:22:06 -07004629 void setDrawn() {
4630 }
4631
Craig Mautner1b4bf852014-05-26 15:06:32 -07004632 // You can always start a new task on a regular ActivityStack.
4633 boolean isEligibleForNewTasks() {
4634 return true;
4635 }
4636
Craig Mautnerd163e752014-06-13 17:18:47 -07004637 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004638 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004639 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004640 }
4641
Craig Mautner34b73df2014-01-12 21:11:08 -08004642 @Override
4643 public String toString() {
4644 return mIdString + (mActivityDisplay == null ? "N" : "A");
4645 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004646 }
4647
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004648 private class VirtualActivityContainer extends ActivityContainer {
4649 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004650 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004651
4652 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004653 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4654 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004655 mParentActivity = parent;
4656 mCallback = callback;
4657 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004658 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004659 }
4660
4661 @Override
4662 public void setSurface(Surface surface, int width, int height, int density) {
4663 super.setSurface(surface, width, height, density);
4664
4665 synchronized (mService) {
4666 final long origId = Binder.clearCallingIdentity();
4667 try {
4668 setSurfaceLocked(surface, width, height, density);
4669 } finally {
4670 Binder.restoreCallingIdentity(origId);
4671 }
4672 }
4673 }
4674
4675 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4676 if (mContainerState == CONTAINER_STATE_FINISHING) {
4677 return;
4678 }
4679 VirtualActivityDisplay virtualActivityDisplay =
4680 (VirtualActivityDisplay) mActivityDisplay;
4681 if (virtualActivityDisplay == null) {
4682 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004683 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004684 mActivityDisplay = virtualActivityDisplay;
4685 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004686 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004687 }
4688
4689 if (mSurface != null) {
4690 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004691 }
4692
Craig Mautner6985bad2014-04-21 15:22:06 -07004693 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004694 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004695 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004696 } else {
4697 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004698 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004699 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004700 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004701 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004702 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004703
Craig Mautnerd163e752014-06-13 17:18:47 -07004704 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004705
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004706 if (DEBUG_STACK) Slog.d(TAG_STACK,
4707 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004708 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004709
Craig Mautner6985bad2014-04-21 15:22:06 -07004710 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004711 boolean isAttachedLocked() {
4712 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004713 }
4714
4715 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004716 void setDrawn() {
4717 synchronized (mService) {
4718 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004719 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004720 }
4721 }
4722
Craig Mautner1b4bf852014-05-26 15:06:32 -07004723 // Never start a new task on an ActivityView if it isn't explicitly specified.
4724 @Override
4725 boolean isEligibleForNewTasks() {
4726 return false;
4727 }
4728
Craig Mautnerd163e752014-06-13 17:18:47 -07004729 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004730 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004731 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4732 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4733 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4734 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4735 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004736 }
4737 }
4738
Craig Mautner4a1cb222013-12-04 16:14:06 -08004739 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4740 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004741 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004742 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004743 int mDisplayId;
4744 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004745
Craig Mautner4a1cb222013-12-04 16:14:06 -08004746 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4747 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004748 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004749
Jose Lima4b6c6692014-08-12 17:41:12 -07004750 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004751
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004752 /** Array of all UIDs that are present on the display. */
4753 private IntArray mDisplayAccessUIDs = new IntArray();
4754
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004755 ActivityDisplay() {
4756 }
Craig Mautner4504de52013-12-20 09:06:56 -08004757
Craig Mautner1a70a162014-09-13 12:09:31 -07004758 // After instantiation, check that mDisplay is not null before using this. The alternative
4759 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004760 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004761 final Display display = mDisplayManager.getDisplay(displayId);
4762 if (display == null) {
4763 return;
4764 }
4765 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004766 }
4767
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004768 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004769 mDisplay = display;
4770 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004771 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004772
Winson Chung7c3ede32017-03-14 14:18:34 -07004773 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004774 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004775 + " to displayId=" + mDisplayId + " position=" + position);
4776 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004777 }
4778
Wale Ogunwale1666e312016-12-16 11:27:18 -08004779 void detachStack(ActivityStack stack) {
4780 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004781 + " from displayId=" + mDisplayId);
4782 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004783 }
4784
Jose Lima4b6c6692014-08-12 17:41:12 -07004785 void setVisibleBehindActivity(ActivityRecord r) {
4786 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004787 }
4788
Jose Lima4b6c6692014-08-12 17:41:12 -07004789 boolean hasVisibleBehindActivity() {
4790 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004791 }
4792
Craig Mautner34b73df2014-01-12 21:11:08 -08004793 @Override
4794 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004795 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4796 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004797
4798 @Override
4799 protected int getChildCount() {
4800 return mStacks.size();
4801 }
4802
4803 @Override
4804 protected ConfigurationContainer getChildAt(int index) {
4805 return mStacks.get(index);
4806 }
4807
4808 @Override
4809 protected ConfigurationContainer getParent() {
4810 return ActivityStackSupervisor.this;
4811 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004812
4813 boolean isPrivate() {
4814 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4815 }
4816
4817 boolean isUidPresent(int uid) {
4818 for (ActivityStack stack : mStacks) {
4819 if (stack.isUidPresent(uid)) {
4820 return true;
4821 }
4822 }
4823 return false;
4824 }
4825
4826 /** Update and get all UIDs that are present on the display and have access to it. */
4827 private IntArray getPresentUIDs() {
4828 mDisplayAccessUIDs.clear();
4829 for (ActivityStack stack : mStacks) {
4830 stack.getPresentUIDs(mDisplayAccessUIDs);
4831 }
4832 return mDisplayAccessUIDs;
4833 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004834
4835 boolean shouldDestroyContentOnRemove() {
4836 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4837 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004838 }
4839
4840 class VirtualActivityDisplay extends ActivityDisplay {
4841 VirtualDisplay mVirtualDisplay;
4842
Craig Mautner6985bad2014-04-21 15:22:06 -07004843 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004844 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004845 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4846 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4847 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4848 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004849
4850 init(mVirtualDisplay.getDisplay());
4851
Bryce Leeaf3a4002017-03-20 10:37:12 -07004852 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004853 }
4854
4855 void setSurface(Surface surface) {
4856 if (mVirtualDisplay != null) {
4857 mVirtualDisplay.setSurface(surface);
4858 }
4859 }
4860
4861 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004862 void detachStack(ActivityStack stack) {
4863 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004864 if (mVirtualDisplay != null) {
4865 mVirtualDisplay.release();
4866 mVirtualDisplay = null;
4867 }
4868 }
4869
4870 @Override
4871 public String toString() {
4872 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004873 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004874 }
Jose Lima58e66d62014-05-27 20:00:27 -07004875
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004876 ActivityStack findStackBehind(ActivityStack stack) {
4877 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004878 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004879 if (display == null) {
4880 return null;
4881 }
4882 final ArrayList<ActivityStack> stacks = display.mStacks;
4883 for (int i = stacks.size() - 1; i >= 0; i--) {
4884 if (stacks.get(i) == stack && i > 0) {
4885 return stacks.get(i - 1);
4886 }
4887 }
4888 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4889 + " in=" + stacks);
4890 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004891
Jorim Jaggic69bd222016-03-15 14:38:37 +01004892 /**
4893 * Puts a task into resizing mode during the next app transition.
4894 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004895 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004896 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004897 private void setResizingDuringAnimation(TaskRecord task) {
4898 mResizingTasksDuringAnimation.add(task.taskId);
4899 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004900 }
4901
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004902 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4903 final TaskRecord task;
4904 final int callingUid;
4905 final String callingPackage;
4906 final Intent intent;
4907 final int userId;
4908 final ActivityOptions activityOptions = (bOptions != null)
4909 ? new ActivityOptions(bOptions) : null;
4910 final int launchStackId = (activityOptions != null)
4911 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004912 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004913 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004914 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004915 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004916
4917 if (launchStackId == DOCKED_STACK_ID) {
4918 mWindowManager.setDockedStackCreateState(
4919 activityOptions.getDockCreateMode(), null /* initialBounds */);
4920
4921 // Defer updating the stack in which recents is until the app transition is done, to
4922 // not run into issues where we still need to draw the task in recents but the
4923 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004924 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004925 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4926 }
4927
Winson Chung7900bee2017-03-10 08:59:25 -08004928 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4929 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004930 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004931 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004932 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004933 throw new IllegalArgumentException(
4934 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4935 }
4936
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004937 // Since we don't have an actual source record here, we assume that the currently focused
4938 // activity was the source.
4939 final ActivityStack focusedStack = getFocusedStack();
4940 final ActivityRecord sourceRecord =
4941 focusedStack != null ? focusedStack.topActivity() : null;
4942
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004943 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004944 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004945 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4946 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004947 }
4948 }
4949
4950 // If the user must confirm credentials (e.g. when first launching a work app and the
4951 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4952 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4953 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004954 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004955 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004956 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004957 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4958 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004959
4960 // If we are launching the task in the docked stack, put it into resizing mode so
4961 // the window renders full-screen with the background filling the void. Also only
4962 // call this at the end to make sure that tasks exists on the window manager side.
4963 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004964 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004965 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004966
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004967 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004968 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07004969 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004970 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004971 return ActivityManager.START_TASK_TO_FRONT;
4972 }
4973 callingUid = task.mCallingUid;
4974 callingPackage = task.mCallingPackage;
4975 intent = task.intent;
4976 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4977 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004978 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4979 null, null, 0, 0, bOptions, userId, null, task);
4980 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004981 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004982 }
4983 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004984 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004985
4986 /**
4987 * @return a list of activities which are the top ones in each visible stack. The first
4988 * entry will be the focused activity.
4989 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004990 List<IBinder> getTopVisibleActivities() {
4991 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4992 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004993 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004994 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4995 // Traverse all stacks on a display.
4996 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4997 final ActivityStack stack = display.mStacks.get(j);
4998 // Get top activity from a visible stack and add it to the list.
4999 if (stack.getStackVisibilityLocked(null /* starting */)
5000 == ActivityStack.STACK_VISIBLE) {
5001 final ActivityRecord top = stack.topActivity();
5002 if (top != null) {
5003 if (stack == mFocusedStack) {
5004 topActivityTokens.add(0, top.appToken);
5005 } else {
5006 topActivityTokens.add(top.appToken);
5007 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005008 }
5009 }
5010 }
5011 }
5012 return topActivityTokens;
5013 }
Bryce Lee4a194382017-04-04 14:32:48 -07005014
5015 /**
5016 * Internal container to store a match qualifier alongside a WaitResult.
5017 */
5018 static class WaitInfo {
5019 private final ComponentName mTargetComponent;
5020 private final WaitResult mResult;
5021
5022 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5023 this.mTargetComponent = targetComponent;
5024 this.mResult = result;
5025 }
5026
5027 public boolean matches(ActivityRecord record) {
5028 return mTargetComponent == null ||
5029 (TextUtils.equals(mTargetComponent.getPackageName(), record.info.packageName)
5030 && TextUtils.equals(mTargetComponent.getClassName(), record.info.name));
5031 }
5032
5033 public WaitResult getResult() {
5034 return mResult;
5035 }
5036
5037 public ComponentName getComponent() {
5038 return mTargetComponent;
5039 }
5040
5041 public void dump(PrintWriter pw, String prefix) {
5042 pw.println(prefix + "WaitInfo:");
5043 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5044 pw.println(prefix + " mResult=");
5045 mResult.dump(pw, prefix);
5046 }
5047 }
Craig Mautner27084302013-03-25 08:05:25 -07005048}