blob: fb8c161ea35db662c266a4a8f56ac857bae2125e [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;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070036import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
37import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020038import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
39import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
40import static android.content.pm.PackageManager.PERMISSION_GRANTED;
41import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
42import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070043import static android.view.Display.FLAG_PRIVATE;
44import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080045import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020046
Winson Chung47900652017-04-06 18:44:25 -070047import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020048import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
73import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
74import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
75import static com.android.server.am.ActivityManagerService.ANIMATE;
76import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
77import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
78import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020079import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
80import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
81import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
82import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
83import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
84import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
85import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
86import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
87import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
88import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
89import static com.android.server.am.ActivityStack.STACK_VISIBLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
93import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
94import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080095import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
96import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
97import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020098import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080099import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200100
Svetoslav7008b512015-06-24 18:47:07 -0700101import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800102import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700103import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100104import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700105import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800107import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700108import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800109import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700111import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112import android.app.IActivityContainerCallback;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700113import android.app.ITaskStackListener;
Jeff Hao1b012d32014-08-20 10:35:34 -0700114import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400116import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700117import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400118import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700120import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.content.Intent;
123import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700125import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.pm.PackageManager;
127import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100128import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800130import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800131import android.hardware.display.DisplayManager;
132import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800133import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800134import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800135import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800136import android.hardware.input.InputManager;
137import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100139import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700140import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700141import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700143import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700144import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700145import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700146import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700147import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700148import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400149import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700150import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700151import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700152import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700153import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100154import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700155import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400157import android.provider.Settings;
158import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700159import android.service.voice.IVoiceInteractionSession;
Bryce Lee4a194382017-04-04 14:32:48 -0700160import android.text.TextUtils;
Svetoslav7008b512015-06-24 18:47:07 -0700161import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700162import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700163import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800164import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700165import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800166import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800168import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800169import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800170import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800171
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800172import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800173import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700174import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400175import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700176import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400177import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800178import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700179import com.android.server.am.ActivityStack.ActivityState;
Winson Chung19953ca2017-04-11 11:19:23 -0700180import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700181import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700182
Craig Mautner8d341ef2013-03-26 09:03:27 -0700183import java.io.FileDescriptor;
184import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700185import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800186import java.lang.annotation.Retention;
187import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700188import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700189import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700190import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700191import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700192
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800193public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700196 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700198 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700200 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700202 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700204 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800205 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800207
Craig Mautnerf3333272013-04-22 10:55:53 -0700208 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700210
Craig Mautner0eea92c2013-05-16 13:35:39 -0700211 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700212 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700213
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700214 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700215 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216
Craig Mautner05d29032013-05-03 13:40:13 -0700217 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
218 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
219 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700221 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800222 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
223 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
224 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700225 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400226 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
227 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700228 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700229 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700230 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800231 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
232 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800233
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700235
Jason Monk62515be2014-05-21 16:06:19 -0400236 private static final String LOCK_TASK_TAG = "Lock-to-App";
237
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238 // Used to indicate if an object (e.g. stack) that we are trying to get
239 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800240 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700241
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700242 // Used to indicate that windows of activities should be preserved during the resize.
243 static final boolean PRESERVE_WINDOWS = true;
244
Wale Ogunwale040b4702015-08-06 18:10:50 -0700245 // Used to indicate if an object (e.g. task) should be moved/created
246 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700247 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700248
249 // Used to indicate that an objects (e.g. task) removal from its container
250 // (e.g. stack) is due to it moving to another container.
251 static final boolean MOVING = true;
252
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700253 // Force the focus to change to the stack we are moving a task to..
254 static final boolean FORCE_FOCUS = true;
255
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700256 // Don't execute any calls to resume.
257 static final boolean DEFER_RESUME = true;
258
Winson Chung010927a2016-12-15 16:12:35 -0800259 // Used to indicate that a task is removed it should also be removed from recents.
260 static final boolean REMOVE_FROM_RECENTS = true;
261
Winson Chung6954fc92017-03-24 16:22:12 -0700262 // Used to indicate that pausing an activity should occur immediately without waiting for
263 // the activity callback indicating that it has completed pausing
264 static final boolean PAUSE_IMMEDIATELY = true;
265
Winson Chung7900bee2017-03-10 08:59:25 -0800266 /**
267 * The modes which affect which tasks are returned when calling
268 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
269 */
270 @Retention(RetentionPolicy.SOURCE)
271 @IntDef({
272 MATCH_TASK_IN_STACKS_ONLY,
273 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
274 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
275 })
276 public @interface AnyTaskForIdMatchTaskMode {}
277 // Match only tasks in the current stacks
278 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
279 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
280 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
281 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
282 // provided stack id
283 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
284
Svetoslav7008b512015-06-24 18:47:07 -0700285 // Activity actions an app cannot start if it uses a permission which is not granted.
286 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
287 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700288
Svetoslav7008b512015-06-24 18:47:07 -0700289 static {
290 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
291 Manifest.permission.CAMERA);
292 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
293 Manifest.permission.CAMERA);
294 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
295 Manifest.permission.CALL_PHONE);
296 }
297
Svet Ganov99b60432015-06-27 13:15:22 -0700298 /** Action restriction: launching the activity is not restricted. */
299 private static final int ACTIVITY_RESTRICTION_NONE = 0;
300 /** Action restriction: launching the activity is restricted by a permission. */
301 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
302 /** Action restriction: launching the activity is restricted by an app op. */
303 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700304
justinzhang5286d3f2014-05-12 17:06:01 -0400305 /** Status Bar Service **/
306 private IBinder mToken = new Binder();
307 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400308 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400309
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700310 // For debugging to make sure the caller when acquiring/releasing our
311 // wake lock is the system process.
312 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800313 /** The number of distinct task ids that can be assigned to the tasks of a single user */
314 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700315
Craig Mautner27084302013-03-25 08:05:25 -0700316 final ActivityManagerService mService;
317
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800318 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800319
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320 final ActivityStackSupervisorHandler mHandler;
321
322 /** Short cut */
323 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800324 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700326 /** Counter for next free stack ID to use for dynamic activity stacks. */
327 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700328
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800329 /**
330 * Maps the task identifier that activities are currently being started in to the userId of the
331 * task. Each time a new task is created, the entry for the userId of the task is incremented
332 */
333 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700334
Craig Mautner2420ead2013-04-01 17:13:20 -0700335 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800336 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700337
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 /** The stack containing the launcher app. Assumed to always be attached to
339 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800340 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700341
Craig Mautnere0a38842013-12-16 16:14:02 -0800342 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800343 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700344
Craig Mautner4a1cb222013-12-04 16:14:06 -0800345 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
346 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800347 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800348 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700349
350 /** List of activities that are waiting for a new activity to become visible before completing
351 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700352 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
353 // mStoppingActivities when something else comes up.
354 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700355
Bryce Lee4a194382017-04-04 14:32:48 -0700356 /** List of processes waiting to find out when a specific activity becomes visible. */
357 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700358
359 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700360 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700361
Craig Mautnerde4ef022013-04-07 19:01:33 -0700362 /** List of activities that are ready to be stopped, but waiting for the next activity to
363 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700365
Craig Mautnerf3333272013-04-22 10:55:53 -0700366 /** List of activities that are ready to be finished, but waiting for the previous activity to
367 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800368 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700369
Craig Mautner0eea92c2013-05-16 13:35:39 -0700370 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800371 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372
Wale Ogunwale22e25262016-02-01 10:32:02 -0800373 /** List of activities whose multi-window mode changed that we need to report to the
374 * application */
375 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
376
377 /** List of activities whose picture-in-picture mode changed that we need to report to the
378 * application */
379 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
380
Winson Chung5af42fc2017-03-24 17:11:33 -0700381 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
382 * the application */
383 Rect mPipModeChangedTargetStackBounds;
384
Craig Mautnerf3333272013-04-22 10:55:53 -0700385 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700386 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700387
Craig Mautnerde4ef022013-04-07 19:01:33 -0700388 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
389 * is being brought in front of us. */
390 boolean mUserLeaving = false;
391
Craig Mautner0eea92c2013-05-16 13:35:39 -0700392 /** Set when we have taken too long waiting to go to sleep. */
393 boolean mSleepTimeout = false;
394
395 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700396 * We don't want to allow the device to go to sleep while in the process
397 * of launching an activity. This is primarily to allow alarm intent
398 * receivers to launch an activity and get that to run before the device
399 * goes back to sleep.
400 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700401 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700402
403 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700404 * Set when the system is going to sleep, until we have
405 * successfully paused the current activity and released our wake lock.
406 * At that point the system is allowed to actually sleep.
407 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700408 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700409
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700410 /** Stack id of the front stack when user switched, indexed by userId. */
411 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700412
Craig Mautner4504de52013-12-20 09:06:56 -0800413 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800414 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800415 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416
Bryce Leeaf3a4002017-03-20 10:37:12 -0700417 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700419 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800421 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
422
423 private DisplayManagerInternal mDisplayManagerInternal;
424 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800425
Craig Mautner15df08a2015-04-01 12:17:18 -0700426 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
427 * may be finished until there is only one entry left. If this is empty the system is not
428 * in lockTask mode. */
429 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000430 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700431 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
432 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000433 */
434 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400435 /**
436 * Notifies the user when entering/exiting lock-task.
437 */
438 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800439
Wale Ogunwaled046a012015-12-24 13:05:59 -0800440 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800441 boolean inResumeTopActivity;
442
Andrii Kulian1e32e022016-09-16 15:29:34 -0700443 /**
444 * Temporary rect used during docked stack resize calculation so we don't need to create a new
445 * object each time.
446 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700447 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700448
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700449 // The default minimal size that will be used if the activity doesn't specify its minimal size.
450 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700451 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700452
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700453 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
454 private boolean mTaskLayersChanged = true;
455
Jorim Jaggi275561a2016-02-23 10:11:02 -0500456 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800457
Andrii Kulian1779e612016-10-12 21:58:25 -0700458 @Override
459 protected int getChildCount() {
460 return mActivityDisplays.size();
461 }
462
463 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700464 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700465 return mActivityDisplays.valueAt(index);
466 }
467
468 @Override
469 protected ConfigurationContainer getParent() {
470 return null;
471 }
472
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700473 Configuration getDisplayOverrideConfiguration(int displayId) {
474 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
475 if (activityDisplay == null) {
476 throw new IllegalArgumentException("No display found with id: " + displayId);
477 }
478
479 return activityDisplay.getOverrideConfiguration();
480 }
481
482 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
483 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
484 if (activityDisplay == null) {
485 throw new IllegalArgumentException("No display found with id: " + displayId);
486 }
487
488 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
489 }
490
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700491 /** Check if placing task or activity on specified display is allowed. */
492 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
493 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
494 && (resizeable || displayConfigMatchesGlobal(displayId)));
495 }
496
497 /**
498 * Check if configuration of specified display matches current global config.
499 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
500 * the same config as on the default display.
501 * @param displayId Id of the display to check.
502 * @return {@code true} if configuration matches.
503 */
504 private boolean displayConfigMatchesGlobal(int displayId) {
505 if (displayId == DEFAULT_DISPLAY) {
506 return true;
507 }
508 if (displayId == INVALID_DISPLAY) {
509 return false;
510 }
511 final ActivityDisplay targetDisplay = mActivityDisplays.get(displayId);
512 if (targetDisplay == null) {
513 throw new IllegalArgumentException("No display found with id: " + displayId);
514 }
515 return getConfiguration().equals(targetDisplay.getConfiguration());
516 }
517
Wale Ogunwale39381972015-12-17 17:15:29 -0800518 static class FindTaskResult {
519 ActivityRecord r;
520 boolean matchedByRootAffinity;
521 }
522 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
523
Craig Mautneree36c772014-07-16 14:56:05 -0700524 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800525 * Temp storage for display ids sorted in focus order.
526 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
527 * it's more efficient, as the number of displays is usually small.
528 */
529 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
530
531 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100532 * Used to keep track whether app visibilities got changed since the last pause. Useful to
533 * determine whether to invoke the task stack change listener after pausing.
534 */
535 boolean mAppVisibilitiesChangedSinceLastPause;
536
537 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100538 * Set of tasks that are in resizing mode during an app transition to fill the "void".
539 */
540 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
541
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700542
543 /**
544 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
545 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
546 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
547 * like the docked stack going empty.
548 */
549 private boolean mAllowDockedStackResize = true;
550
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100551 /**
Tony Mak853304c2016-04-18 15:17:41 +0100552 * Is dock currently minimized.
553 */
554 boolean mIsDockMinimized;
555
Jorim Jaggife762342016-10-13 14:33:27 +0200556 final KeyguardController mKeyguardController;
557
Tony Mak853304c2016-04-18 15:17:41 +0100558 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700559 * Description of a request to start a new activity, which has been held
560 * due to app switches being disabled.
561 */
562 static class PendingActivityLaunch {
563 final ActivityRecord r;
564 final ActivityRecord sourceRecord;
565 final int startFlags;
566 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700567 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700568
569 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700570 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700571 r = _r;
572 sourceRecord = _sourceRecord;
573 startFlags = _startFlags;
574 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700575 callerApp = _callerApp;
576 }
577
578 void sendErrorResult(String message) {
579 try {
580 if (callerApp.thread != null) {
581 callerApp.thread.scheduleCrash(message);
582 }
583 } catch (RemoteException e) {
584 Slog.e(TAG, "Exception scheduling crash of failed "
585 + "activity launcher sourceRecord=" + sourceRecord, e);
586 }
Craig Mautneree36c772014-07-16 14:56:05 -0700587 }
588 }
589
Bryce Leeaf691c02017-03-20 14:20:22 -0700590 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700591 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700592 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800593 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200594 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700595 }
596
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800597 void setRecentTasks(RecentTasks recentTasks) {
598 mRecentTasks = recentTasks;
599 }
600
Jeff Brown2c43c332014-06-12 22:38:59 -0700601 /**
602 * At the time when the constructor runs, the power manager has not yet been
603 * initialized. So we initialize our wakelocks afterwards.
604 */
605 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700607 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700608 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700609 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700610 }
611
justinzhang5286d3f2014-05-12 17:06:01 -0400612 // This function returns a IStatusBarService. The value is from ServiceManager.
613 // getService and is cached.
614 private IStatusBarService getStatusBarService() {
615 synchronized (mService) {
616 if (mStatusBarService == null) {
617 mStatusBarService = IStatusBarService.Stub.asInterface(
618 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
619 if (mStatusBarService == null) {
620 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
621 }
622 }
623 return mStatusBarService;
624 }
625 }
626
Jason Monk35c62a42014-06-17 10:24:47 -0400627 private IDevicePolicyManager getDevicePolicyManager() {
628 synchronized (mService) {
629 if (mDevicePolicyManager == null) {
630 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
631 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
632 if (mDevicePolicyManager == null) {
633 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
634 }
635 }
636 return mDevicePolicyManager;
637 }
638 }
639
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700640 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 synchronized (mService) {
642 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200643 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644
645 mDisplayManager =
646 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
647 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800648 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800649
650 Display[] displays = mDisplayManager.getDisplays();
651 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
652 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800653 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700654 if (activityDisplay.mDisplay == null) {
655 throw new IllegalStateException("Default Display does not exist");
656 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800657 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700658 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 }
660
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800661 mHomeStack = mFocusedStack = mLastFocusedStack =
662 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800663
664 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 }
Craig Mautner27084302013-03-25 08:05:25 -0700666 }
667
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700668 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800669 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700670 }
671
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700674 }
675
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700676 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700677 if (stack == null) {
678 return false;
679 }
680
Craig Mautnerdf88d732014-01-27 09:21:32 -0800681 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
682 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700683 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800684 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800685 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700686 }
687
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800688 /** The top most stack on its display. */
689 boolean isFrontStackOnDisplay(ActivityStack stack) {
690 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
691 }
692
693 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700694 if (stack == null) {
695 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700697
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700698 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
699 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700700 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800701 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800702 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700703 }
704
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800706 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
707 if (!focusCandidate.isFocusable()) {
708 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800709 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800710 }
711
712 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800713 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800714 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800715
Wale Ogunwaled046a012015-12-24 13:05:59 -0800716 EventLogTags.writeAmFocusedStack(
717 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
718 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
719 }
720
721 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800722 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800723 if (r != null && r.idle) {
724 checkFinishBootingLocked();
725 }
726 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700727 }
728
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700729 void moveHomeStackToFront(String reason) {
730 mHomeStack.moveToFront(reason);
731 }
732
Matthew Ng330757d2017-02-28 14:19:17 -0800733 void moveRecentsStackToFront(String reason) {
734 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
735 if (recentsStack != null) {
736 recentsStack.moveToFront(reason);
737 }
738 }
739
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700740 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800741 boolean moveHomeStackTaskToTop(String reason) {
742 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700743
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700744 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700745 if (top == null) {
746 return false;
747 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700748 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700749 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700750 }
751
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800752 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700753 if (!mService.mBooting && !mService.mBooted) {
754 // Not ready yet!
755 return false;
756 }
757
Craig Mautner84984fa2014-06-19 11:19:20 -0700758 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700759 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700760 }
761
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800762 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700763 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800764 final String myReason = reason + " resumeHomeStackTask";
765
Mark Lua56ea122015-10-08 13:31:01 +0800766 // Only resume home activity if isn't finishing.
767 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700768 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800769 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700770 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800771 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700772 }
773
Craig Mautner8d341ef2013-03-26 09:03:27 -0700774 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800775 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
776 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700777 }
778
779 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700780 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700781 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800782 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700783 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800784 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
785 * valid if the matchMode is
786 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700787 */
Winson Chung7900bee2017-03-10 08:59:25 -0800788 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
789 // If there is a stack id set, ensure that we are attempting to actually restore a task
790 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
791 stackId != INVALID_STACK_ID) {
792 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
793 }
794
Craig Mautnere0a38842013-12-16 16:14:02 -0800795 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800797 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800798 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
799 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700800 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 if (task != null) {
802 return task;
803 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700804 }
805 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800806
Winson Chung7900bee2017-03-10 08:59:25 -0800807 // If we are matching stack tasks only, return now
808 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800809 return null;
810 }
811
Winson Chung7900bee2017-03-10 08:59:25 -0800812 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
813 // the task from recents
814 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700815 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
816
817 if (task == null) {
818 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800819 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
820 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700821
822 return null;
823 }
824
825 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700826 return task;
827 }
828
Winson Chung7900bee2017-03-10 08:59:25 -0800829 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700830 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700831 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
832 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800833 return null;
834 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700835 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800836 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700837 }
838
Craig Mautner6170f732013-04-02 13:05:23 -0700839 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800840 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800842 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
844 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
845 if (r != null) {
846 return r;
847 }
Craig Mautner6170f732013-04-02 13:05:23 -0700848 }
849 }
850 return null;
851 }
852
Tony Mak853304c2016-04-18 15:17:41 +0100853 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000854 * Detects whether we should show a lock screen in front of this task for a locked user.
855 * <p>
856 * We'll do this if either of the following holds:
857 * <ul>
858 * <li>The top activity explicitly belongs to {@param userId}.</li>
859 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
860 * </ul>
861 *
862 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100863 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000864 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
865 // To handle the case that work app is in the task but just is not the top one.
866 final ActivityRecord activityRecord = task.getTopActivity();
867 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
868
869 return (activityRecord != null && activityRecord.userId == userId)
870 || (resultTo != null && resultTo.userId == userId);
871 }
872
873 /**
874 * Find all visible task stacks containing {@param userId} and intercept them with an activity
875 * to block out the contents and possibly start a credential-confirming intent.
876 *
877 * @param userId user handle for the locked managed profile.
878 */
879 void lockAllProfileTasks(@UserIdInt int userId) {
880 mWindowManager.deferSurfaceLayout();
881 try {
882 final List<ActivityStack> stacks = getStacks();
883 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
884 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
885 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
886 final TaskRecord task = tasks.get(taskNdx);
887
888 // Check the task for a top activity belonging to userId, or returning a result
889 // to an activity belonging to userId. Example case: a document picker for
890 // personal files, opened by a work app, should still get locked.
891 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000892 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
893 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000894 }
Tony Mak853304c2016-04-18 15:17:41 +0100895 }
896 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000897 } finally {
898 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100899 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000900 }
901
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800902 void setNextTaskIdForUserLocked(int taskId, int userId) {
903 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
904 if (taskId > currentTaskId) {
905 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700906 }
907 }
908
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700909 static int nextTaskIdForUser(int taskId, int userId) {
910 int nextTaskId = taskId + 1;
911 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
912 // Wrap around as there will be smaller task ids that are available now.
913 nextTaskId -= MAX_TASK_IDS_PER_USER;
914 }
915 return nextTaskId;
916 }
917
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800918 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800919 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
920 // for a userId u, a taskId can only be in the range
921 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
922 // 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 -0700923 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800924 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800925 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800926 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700927 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800928 if (candidateTaskId == currentTaskId) {
929 // Something wrong!
930 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
931 throw new IllegalStateException("Cannot get an available task id."
932 + " Reached limit of " + MAX_TASK_IDS_PER_USER
933 + " running tasks per user.");
934 }
935 }
936 mCurTaskIdForUser.put(userId, candidateTaskId);
937 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700938 }
939
Chong Zhang6cda19c2016-06-14 19:07:56 -0700940 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800941 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700942 if (stack == null) {
943 return null;
944 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700945 ActivityRecord resumedActivity = stack.mResumedActivity;
946 if (resumedActivity == null || resumedActivity.app == null) {
947 resumedActivity = stack.mPausingActivity;
948 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700949 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700950 }
951 }
952 return resumedActivity;
953 }
954
Dianne Hackbornff072722014-09-24 10:56:28 -0700955 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700956 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 boolean didSomething = false;
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)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 continue;
964 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700965 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 if (hr != null) {
967 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
968 && processName.equals(hr.processName)) {
969 try {
George Mount2c92c972014-03-20 09:38:23 -0700970 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 didSomething = true;
972 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700973 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 Slog.w(TAG, "Exception in new application when starting activity "
975 + hr.intent.getComponent().flattenToShortString(), e);
976 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700977 }
Craig Mautner20e72272013-04-01 13:45:53 -0700978 }
Craig Mautner20e72272013-04-01 13:45:53 -0700979 }
980 }
981 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700982 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700983 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700984 }
Craig Mautner20e72272013-04-01 13:45:53 -0700985 return didSomething;
986 }
987
988 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
990 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800991 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
992 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700993 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 continue;
995 }
996 final ActivityRecord resumedActivity = stack.mResumedActivity;
997 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700998 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800999 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 return false;
1001 }
Craig Mautner20e72272013-04-01 13:45:53 -07001002 }
1003 }
Wei Wang65c7a152016-06-02 18:51:22 -07001004 // Send launch end powerhint when idle
1005 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001006 return true;
1007 }
1008
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1011 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001012 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1013 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001014 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001015 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001016 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 return false;
1018 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001019 }
1020 }
1021 }
1022 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001023 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001024 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1025 mLastFocusedStack + " to=" + mFocusedStack);
1026 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001027 return true;
1028 }
1029
1030 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001031 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1033 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1035 final ActivityStack stack = stacks.get(stackNdx);
1036 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001037 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001038 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001039 return false;
1040 }
1041 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001042 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 }
1044 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001045 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001046 }
1047
Craig Mautner2acc3892013-09-23 10:28:14 -07001048 /**
1049 * Pause all activities in either all of the stacks or just the back stacks.
1050 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001051 * @param resuming The resuming activity.
1052 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1053 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001054 * @return true if any activity was paused as a result of this call.
1055 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001056 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001057 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001058 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1059 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001060 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1061 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001062 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001063 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001064 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001065 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1066 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001067 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001068 }
1069 }
1070 return someActivityPaused;
1071 }
1072
Craig Mautnerde4ef022013-04-07 19:01:33 -07001073 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001074 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001075 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1076 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001077 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1078 final ActivityStack stack = stacks.get(stackNdx);
1079 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001080 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001081 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001082 Slog.d(TAG_STATES,
1083 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001084 pausing = false;
1085 } else {
1086 return false;
1087 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001088 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001089 }
1090 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001091 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001092 }
1093
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001094 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001095 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001096 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001097 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1098 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1099 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1100 final ActivityStack stack = stacks.get(stackNdx);
1101 if (stack.mResumedActivity != null &&
1102 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001103 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001104 }
1105 }
1106 }
1107 }
1108
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001109 void cancelInitializingActivities() {
1110 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1111 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1112 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1113 stacks.get(stackNdx).cancelInitializingActivities();
1114 }
1115 }
1116 }
1117
Bryce Lee4a194382017-04-04 14:32:48 -07001118 void waitActivityVisible(ComponentName name, WaitResult result) {
1119 final WaitInfo waitInfo = new WaitInfo(name, result);
1120 mWaitingForActivityVisible.add(waitInfo);
1121 }
1122
1123 void cleanupActivity(ActivityRecord r) {
1124 // Make sure this record is no longer in the pending finishes list.
1125 // This could happen, for example, if we are trimming activities
1126 // down to the max limit while they are still waiting to finish.
1127 mFinishingActivities.remove(r);
1128 mActivitiesWaitingForVisibleActivity.remove(r);
1129
1130 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1131 if (mWaitingForActivityVisible.get(i).matches(r)) {
1132 mWaitingForActivityVisible.remove(i);
1133 }
1134 }
1135 }
1136
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001137 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001138 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001139 }
1140
1141 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1142 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001143 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1144 final WaitInfo w = mWaitingForActivityVisible.get(i);
1145 if (w.matches(r)) {
1146 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001147 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001148 result.timeout = false;
1149 result.who = w.getComponent();
1150 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1151 result.thisTime = result.totalTime;
1152 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001153 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001154 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001155 if (changed) {
1156 mService.notifyAll();
1157 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001158 }
1159
Chong Zhang5022da32016-06-21 16:31:37 -07001160 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1161 boolean changed = false;
1162 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1163 WaitResult w = mWaitingActivityLaunched.remove(i);
1164 if (w.who == null) {
1165 changed = true;
1166 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1167 // the starting activity ends up moving another activity to front, and it should
1168 // wait for this new activity to become visible instead.
1169 // Do not modify other fields.
1170 w.result = START_TASK_TO_FRONT;
1171 }
1172 }
1173 if (changed) {
1174 mService.notifyAll();
1175 }
1176 }
1177
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001178 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1179 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001180 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001181 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001182 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001183 if (w.who == null) {
1184 changed = true;
1185 w.timeout = timeout;
1186 if (r != null) {
1187 w.who = new ComponentName(r.info.packageName, r.info.name);
1188 }
1189 w.thisTime = thisTime;
1190 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001191 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001192 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001193 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001194 if (changed) {
1195 mService.notifyAll();
1196 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001197 }
1198
Craig Mautner29219d92013-04-16 20:19:12 -07001199 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001200 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001201 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001202 if (r != null) {
1203 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001204 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001205
Andrii Kulian7318d632016-07-20 18:59:28 -07001206 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001207 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1208
1209 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1210 final int displayId = mTmpOrderedDisplayIds.get(i);
1211 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1212 if (stacks == null) {
1213 continue;
1214 }
1215 for (int j = stacks.size() - 1; j >= 0; --j) {
1216 final ActivityStack stack = stacks.get(j);
1217 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1218 r = stack.topRunningActivityLocked();
1219 if (r != null) {
1220 return r;
1221 }
Craig Mautner29219d92013-04-16 20:19:12 -07001222 }
1223 }
1224 }
1225 return null;
1226 }
1227
Dianne Hackborn09233282014-04-30 11:33:59 -07001228 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001229 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001230 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1231 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001232 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001233 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001234 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001235 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1236 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001237 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001238 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001239 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001240 }
1241 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001242
1243 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1244 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1245 while (maxNum > 0) {
1246 long mostRecentActiveTime = Long.MIN_VALUE;
1247 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001248 final int numTaskLists = runningTaskLists.size();
1249 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1250 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001251 if (!stackTaskList.isEmpty()) {
1252 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1253 if (lastActiveTime > mostRecentActiveTime) {
1254 mostRecentActiveTime = lastActiveTime;
1255 selectedStackList = stackTaskList;
1256 }
1257 }
1258 }
1259 if (selectedStackList != null) {
1260 list.add(selectedStackList.remove(0));
1261 --maxNum;
1262 } else {
1263 break;
1264 }
1265 }
Craig Mautner20e72272013-04-01 13:45:53 -07001266 }
1267
Todd Kennedy7440f172015-12-09 14:31:22 -08001268 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1269 ProfilerInfo profilerInfo) {
1270 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001271 if (aInfo != null) {
1272 // Store the found target back into the intent, because now that
1273 // we have it we never want to do this again. For example, if the
1274 // user navigates back to this point in the history, we should
1275 // always restart the exact same activity.
1276 intent.setComponent(new ComponentName(
1277 aInfo.applicationInfo.packageName, aInfo.name));
1278
1279 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001280 if (!aInfo.processName.equals("system")) {
1281 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001282 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001283 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001284
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001285 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1286 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1287 }
1288
Man Caocfa78b22015-06-11 20:14:34 -07001289 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1290 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1291 }
1292
1293 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001294 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001295 }
1296 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001297 final String intentLaunchToken = intent.getLaunchToken();
1298 if (aInfo.launchToken == null && intentLaunchToken != null) {
1299 aInfo.launchToken = intentLaunchToken;
1300 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001301 }
1302 return aInfo;
1303 }
1304
Todd Kennedy7440f172015-12-09 14:31:22 -08001305 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001306 return resolveIntent(intent, resolvedType, userId, 0);
1307 }
1308
1309 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001310 synchronized (mService) {
1311 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001312 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001313 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001314 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001315 }
1316
1317 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1318 ProfilerInfo profilerInfo, int userId) {
1319 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1320 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1321 }
1322
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001323 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1324 boolean andResume, boolean checkConfig) throws RemoteException {
1325
1326 if (!allPausedActivitiesComplete()) {
1327 // While there are activities pausing we skipping starting any new activities until
1328 // pauses are complete. NOTE: that we also do this for activities that are starting in
1329 // the paused state because they will first be resumed then paused on the client side.
1330 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1331 "realStartActivityLocked: Skipping start of r=" + r
1332 + " some activities pausing...");
1333 return false;
1334 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001335
Andrii Kulianada10292017-02-09 23:19:29 -08001336 r.startFreezingScreenLocked(app, 0);
1337 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001338
Andrii Kulianada10292017-02-09 23:19:29 -08001339 // schedule launch ticks to collect information about slow apps.
1340 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001341
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001342 // Have the window manager re-evaluate the orientation of the screen based on the new
1343 // activity order. Note that as a result of this, it can call back into the activity
1344 // manager with a new orientation. We don't care about that, because the activity is not
1345 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001346 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001347 final int displayId = r.getDisplayId();
1348 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1349 getDisplayOverrideConfiguration(displayId),
1350 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001351 // Deferring resume here because we're going to launch new activity shortly.
1352 // We don't want to perform a redundant launch of the same record while ensuring
1353 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001354 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1355 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 }
1357
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001358 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001359 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001360 }
1361
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 r.app = app;
1363 app.waitingToKill = null;
1364 r.launchCount++;
1365 r.lastLaunchTime = SystemClock.uptimeMillis();
1366
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001367 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001368
1369 int idx = app.activities.indexOf(r);
1370 if (idx < 0) {
1371 app.activities.add(r);
1372 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001373 mService.updateLruProcessLocked(app, true, null);
1374 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001375
Bryce Leeaf691c02017-03-20 14:20:22 -07001376 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001377 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1378 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001379 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001380 }
1381
Andrii Kulian02b7a832016-10-06 23:11:56 -07001382 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001383 try {
1384 if (app.thread == null) {
1385 throw new RemoteException();
1386 }
1387 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001388 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001389 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001390 // We don't need to deliver new intents and/or set results if activity is going
1391 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001392 results = r.results;
1393 newIntents = r.newIntents;
1394 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001395 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1396 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1397 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001398 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1399 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001400 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001401 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001402 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001403 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001404 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1405 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 r.sleeping = false;
1407 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001408 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001409 mService.showAskCompatModeDialogLocked(r);
1410 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001411 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001412 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1413 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1414 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001415 final String profileFile = mService.mProfileFile;
1416 if (profileFile != null) {
1417 ParcelFileDescriptor profileFd = mService.mProfileFd;
1418 if (profileFd != null) {
1419 try {
1420 profileFd = profileFd.dup();
1421 } catch (IOException e) {
1422 if (profileFd != null) {
1423 try {
1424 profileFd.close();
1425 } catch (IOException o) {
1426 }
1427 profileFd = null;
1428 }
1429 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001430 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001431
1432 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001433 mService.mSamplingInterval, mService.mAutoStopProfiler,
1434 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001435 }
1436 }
1437 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001438
Andrii Kulianada10292017-02-09 23:19:29 -08001439 app.hasShownUi = true;
1440 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001441 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001442 // Because we could be starting an Activity in the system process this may not go across
1443 // a Binder interface which would create a new Configuration. Consequently we have to
1444 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001445
1446 final Configuration globalConfiguration =
1447 new Configuration(mService.getGlobalConfiguration());
1448 r.setLastReportedGlobalConfiguration(globalConfiguration);
1449 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001450 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001451 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1452
Craig Mautner2420ead2013-04-01 17:13:20 -07001453 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001454 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001455 globalConfiguration,
1456 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001457 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1458 r.persistentState, results, newIntents, !andResume,
1459 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001460
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001461 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001462 // This may be a heavy-weight process! Note that the package
1463 // manager will ensure that only activity can run in the main
1464 // process of the .apk, which is the only thing that will be
1465 // considered heavy-weight.
1466 if (app.processName.equals(app.info.packageName)) {
1467 if (mService.mHeavyWeightProcess != null
1468 && mService.mHeavyWeightProcess != app) {
1469 Slog.w(TAG, "Starting new heavy weight process " + app
1470 + " when already running "
1471 + mService.mHeavyWeightProcess);
1472 }
1473 mService.mHeavyWeightProcess = app;
1474 Message msg = mService.mHandler.obtainMessage(
1475 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1476 msg.obj = r;
1477 mService.mHandler.sendMessage(msg);
1478 }
1479 }
1480
1481 } catch (RemoteException e) {
1482 if (r.launchFailed) {
1483 // This is the second time we failed -- finish activity
1484 // and give up.
1485 Slog.e(TAG, "Second failure launching "
1486 + r.intent.getComponent().flattenToShortString()
1487 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001488 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001489 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1490 "2nd-crash", false);
1491 return false;
1492 }
1493
1494 // This is the first time we failed -- restart process and
1495 // retry.
1496 app.activities.remove(r);
1497 throw e;
1498 }
1499
1500 r.launchFailed = false;
1501 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001502 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001503 }
1504
1505 if (andResume) {
1506 // As part of the process of launching, ActivityThread also performs
1507 // a resume.
1508 stack.minimalResumeActivityLocked(r);
1509 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001510 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001511 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001512 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001513 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001514 "Moving to PAUSED: " + r + " (starting in paused state)");
1515 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001516 }
1517
1518 // Launch the new version setup screen if needed. We do this -after-
1519 // launching the initial activity (that is, home), so that it can have
1520 // a chance to initialize itself while in the background, making the
1521 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001522 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001523 mService.startSetupActivityLocked();
1524 }
1525
Dianne Hackborn465fa392014-09-14 14:21:18 -07001526 // Update any services we are bound to that might care about whether
1527 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001528 if (r.app != null) {
1529 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1530 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001531
Craig Mautner2420ead2013-04-01 17:13:20 -07001532 return true;
1533 }
1534
Craig Mautnere79d42682013-04-01 19:01:53 -07001535 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001536 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001537 // Is this activity's application already running?
1538 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001539 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001540
Andrii Kulian02b7a832016-10-06 23:11:56 -07001541 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001542
1543 if (app != null && app.thread != null) {
1544 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001545 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1546 || !"android".equals(r.info.packageName)) {
1547 // Don't add this if it is a platform component that is marked
1548 // to run in multiple processes, because this is actually
1549 // part of the framework so doesn't make sense to track as a
1550 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001551 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1552 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001553 }
George Mount2c92c972014-03-20 09:38:23 -07001554 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001555 return;
1556 } catch (RemoteException e) {
1557 Slog.w(TAG, "Exception when starting activity "
1558 + r.intent.getComponent().flattenToShortString(), e);
1559 }
1560
1561 // If a dead object exception was thrown -- fall through to
1562 // restart the application.
1563 }
1564
1565 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001566 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001567 }
1568
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001569 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001570 String resultWho, int requestCode, int callingPid, int callingUid,
1571 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001572 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001573 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1574 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001575 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001576 return true;
1577 }
1578 final int componentRestriction = getComponentRestrictionForCallingPackage(
1579 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1580 final int actionRestriction = getActionRestrictionForCallingPackage(
1581 intent.getAction(), callingPackage, callingPid, callingUid);
1582 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1583 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1584 if (resultRecord != null) {
1585 resultStack.sendActivityResultLocked(-1,
1586 resultRecord, resultWho, requestCode,
1587 Activity.RESULT_CANCELED, null);
1588 }
1589 final String msg;
1590 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1591 msg = "Permission Denial: starting " + intent.toString()
1592 + " from " + callerApp + " (pid=" + callingPid
1593 + ", uid=" + callingUid + ")" + " with revoked permission "
1594 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1595 } else if (!aInfo.exported) {
1596 msg = "Permission Denial: starting " + intent.toString()
1597 + " from " + callerApp + " (pid=" + callingPid
1598 + ", uid=" + callingUid + ")"
1599 + " not exported from uid " + aInfo.applicationInfo.uid;
1600 } else {
1601 msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ")"
1604 + " requires " + aInfo.permission;
1605 }
1606 Slog.w(TAG, msg);
1607 throw new SecurityException(msg);
1608 }
1609
1610 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1611 final String message = "Appop Denial: starting " + intent.toString()
1612 + " from " + callerApp + " (pid=" + callingPid
1613 + ", uid=" + callingUid + ")"
1614 + " requires " + AppOpsManager.permissionToOp(
1615 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1616 Slog.w(TAG, message);
1617 return false;
1618 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1619 final String message = "Appop Denial: starting " + intent.toString()
1620 + " from " + callerApp + " (pid=" + callingPid
1621 + ", uid=" + callingUid + ")"
1622 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1623 Slog.w(TAG, message);
1624 return false;
1625 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001626 if (options != null) {
1627 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1628 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1629 callingPid, callingUid);
1630 if (startInTaskPerm != PERMISSION_GRANTED) {
1631 final String msg = "Permission Denial: starting " + intent.toString()
1632 + " from " + callerApp + " (pid=" + callingPid
1633 + ", uid=" + callingUid + ") with launchTaskId="
1634 + options.getLaunchTaskId();
1635 Slog.w(TAG, msg);
1636 throw new SecurityException(msg);
1637 }
1638 }
1639 // Check if someone tries to launch an activity on a private display with a different
1640 // owner.
1641 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001642 if (launchDisplayId != INVALID_DISPLAY
1643 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1644 final String msg = "Permission Denial: starting " + intent.toString()
1645 + " from " + callerApp + " (pid=" + callingPid
1646 + ", uid=" + callingUid + ") with launchDisplayId="
1647 + launchDisplayId;
1648 Slog.w(TAG, msg);
1649 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001650 }
1651 }
1652
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001653 return true;
1654 }
1655
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001656 /** Check if caller is allowed to launch activities on specified display. */
1657 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1658 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1659 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1660
1661 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1662 if (activityDisplay == null) {
1663 Slog.w(TAG, "Launch on display check: display not found");
1664 return false;
1665 }
1666
1667 if (!activityDisplay.isPrivate()) {
1668 // Anyone can launch on a public display.
1669 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1670 + " allow launch on public display");
1671 return true;
1672 }
1673
1674 // Check if the caller is the owner of the display.
1675 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1676 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1677 + " allow launch for owner of the display");
1678 return true;
1679 }
1680
1681 // Check if caller is present on display
1682 if (activityDisplay.isUidPresent(callingUid)) {
1683 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1684 + " allow launch for caller present on the display");
1685 return true;
1686 }
1687
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001688 // Check if the caller can manage activity stacks.
1689 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001690 callingUid);
1691 if (startAnyPerm == PERMISSION_GRANTED) {
1692 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1693 + " allow launch any on display");
1694 return true;
1695 }
1696
1697 Slog.w(TAG, "Launch on display check: denied");
1698 return false;
1699 }
1700
1701 /** Update lists of UIDs that are present on displays and have access to them. */
1702 void updateUIDsPresentOnDisplay() {
1703 mDisplayAccessUIDs.clear();
1704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1705 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001706 // Only bother calculating the whitelist for private displays
1707 if (activityDisplay.isPrivate()) {
1708 mDisplayAccessUIDs.append(
1709 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1710 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001711 }
1712 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1713 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1714 }
1715
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001716 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001717 final long identity = Binder.clearCallingIdentity();
1718 try {
1719 return UserManager.get(mService.mContext).getUserInfo(userId);
1720 } finally {
1721 Binder.restoreCallingIdentity(identity);
1722 }
1723 }
1724
Svet Ganov99b60432015-06-27 13:15:22 -07001725 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001726 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001727 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1728 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001729 == PackageManager.PERMISSION_DENIED) {
1730 return ACTIVITY_RESTRICTION_PERMISSION;
1731 }
1732
Christopher Tateff7add02015-08-17 10:23:22 -07001733 if (activityInfo.permission == null) {
1734 return ACTIVITY_RESTRICTION_NONE;
1735 }
1736
Svet Ganov99b60432015-06-27 13:15:22 -07001737 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1738 if (opCode == AppOpsManager.OP_NONE) {
1739 return ACTIVITY_RESTRICTION_NONE;
1740 }
1741
1742 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1743 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001744 if (!ignoreTargetSecurity) {
1745 return ACTIVITY_RESTRICTION_APPOP;
1746 }
Svet Ganov99b60432015-06-27 13:15:22 -07001747 }
1748
1749 return ACTIVITY_RESTRICTION_NONE;
1750 }
1751
Svetoslav7008b512015-06-24 18:47:07 -07001752 private int getActionRestrictionForCallingPackage(String action,
1753 String callingPackage, int callingPid, int callingUid) {
1754 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
1758 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1759 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
1763 final PackageInfo packageInfo;
1764 try {
1765 packageInfo = mService.mContext.getPackageManager()
1766 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1767 } catch (PackageManager.NameNotFoundException e) {
1768 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
1772 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001773 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001774 }
1775
1776 if (mService.checkPermission(permission, callingPid, callingUid) ==
1777 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001778 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001779 }
1780
1781 final int opCode = AppOpsManager.permissionToOpCode(permission);
1782 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001783 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001784 }
1785
1786 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1787 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001788 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001789 }
1790
Svet Ganov99b60432015-06-27 13:15:22 -07001791 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001792 }
1793
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001794 void setLaunchSource(int uid) {
1795 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1796 }
1797
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001798 void acquireLaunchWakelock() {
1799 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1800 throw new IllegalStateException("Calling must be system uid");
1801 }
1802 mLaunchingActivity.acquire();
1803 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1804 // To be safe, don't allow the wake lock to be held for too long.
1805 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1806 }
1807 }
1808
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001809 /**
1810 * Called when the frontmost task is idle.
1811 * @return the state of mService.mBooting before this was called.
1812 */
1813 private boolean checkFinishBootingLocked() {
1814 final boolean booting = mService.mBooting;
1815 boolean enableScreen = false;
1816 mService.mBooting = false;
1817 if (!mService.mBooted) {
1818 mService.mBooted = true;
1819 enableScreen = true;
1820 }
1821 if (booting || enableScreen) {
1822 mService.postFinishBooting(booting, enableScreen);
1823 }
1824 return booting;
1825 }
1826
Craig Mautnerf3333272013-04-22 10:55:53 -07001827 // Checked.
1828 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001829 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001830 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001831
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001833 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 int NS = 0;
1835 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001836 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001837 boolean activityRemoved = false;
1838
Wale Ogunwale7d701172015-03-11 15:36:30 -07001839 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001840 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001841 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1842 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001843 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1844 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001845 if (fromTimeout) {
1846 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001847 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001848
1849 // This is a hack to semi-deal with a race condition
1850 // in the client where it can be constructed with a
1851 // newer configuration from when we asked it to launch.
1852 // We'll update with whatever configuration it now says
1853 // it used to launch.
1854 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001855 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001856 }
1857
1858 // We are now idle. If someone is waiting for a thumbnail from
1859 // us, we can now deliver.
1860 r.idle = true;
1861
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001862 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001863 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001864 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001865 }
1866 }
1867
1868 if (allResumedActivitiesIdle()) {
1869 if (r != null) {
1870 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001871 }
1872
1873 if (mLaunchingActivity.isHeld()) {
1874 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1875 if (VALIDATE_WAKE_LOCK_CALLER &&
1876 Binder.getCallingUid() != Process.myUid()) {
1877 throw new IllegalStateException("Calling must be system uid");
1878 }
1879 mLaunchingActivity.release();
1880 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001881 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 }
1883
1884 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001885 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1886 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001888 if ((NF = mFinishingActivities.size()) > 0) {
1889 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 mFinishingActivities.clear();
1891 }
1892
Craig Mautnerf3333272013-04-22 10:55:53 -07001893 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001894 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 mStartingUsers.clear();
1896 }
1897
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 // Stop any activities that are scheduled to do so but have been
1899 // waiting for the next one to start.
1900 for (int i = 0; i < NS; i++) {
1901 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001902 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001903 if (stack != null) {
1904 if (r.finishing) {
1905 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1906 } else {
1907 stack.stopActivityLocked(r);
1908 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 }
1910 }
1911
1912 // Finish any activities that are scheduled to do so but have been
1913 // waiting for the next one to start.
1914 for (int i = 0; i < NF; i++) {
1915 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001916 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001917 if (stack != null) {
1918 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1919 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001920 }
1921
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001922 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001923 // Complete user switch
1924 if (startingUsers != null) {
1925 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001926 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001927 }
1928 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001929 }
1930
1931 mService.trimApplications();
1932 //dump();
1933 //mWindowManager.dump();
1934
Craig Mautnerf3333272013-04-22 10:55:53 -07001935 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001936 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001937 }
1938
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001939 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 }
1941
Craig Mautner8e569572013-10-11 17:36:59 -07001942 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001943 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1947 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1948 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001949 }
Craig Mautner19091252013-10-05 00:03:53 -07001950 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 }
1952
1953 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1955 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001956 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1957 stacks.get(stackNdx).closeSystemDialogsLocked();
1958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001959 }
1960 }
1961
Craig Mautner93529a42013-10-04 15:03:13 -07001962 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001963 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001964 }
1965
Craig Mautner8d341ef2013-03-26 09:03:27 -07001966 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001967 * Update the last used stack id for non-current user (current user's last
1968 * used stack is the focused stack)
1969 */
1970 void updateUserStackLocked(int userId, ActivityStack stack) {
1971 if (userId != mCurrentUser) {
1972 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1973 }
1974 }
1975
1976 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001977 * @return true if some activity was finished (or would have finished if doit were true).
1978 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001979 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1980 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001981 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001982 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1983 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001984 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001985 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001986 if (stack.finishDisabledPackageActivitiesLocked(
1987 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001988 didSomething = true;
1989 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001990 }
1991 }
1992 return didSomething;
1993 }
1994
Dianne Hackborna413dc02013-07-12 12:02:55 -07001995 void updatePreviousProcessLocked(ActivityRecord r) {
1996 // Now that this process has stopped, we may want to consider
1997 // it to be the previous app to try to keep around in case
1998 // the user wants to return to it.
1999
2000 // First, found out what is currently the foreground app, so that
2001 // we don't blow away the previous app if this activity is being
2002 // hosted by the process that is actually still the foreground.
2003 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002004 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2005 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002006 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2007 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002008 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002009 if (stack.mResumedActivity != null) {
2010 fgApp = stack.mResumedActivity.app;
2011 } else if (stack.mPausingActivity != null) {
2012 fgApp = stack.mPausingActivity.app;
2013 }
2014 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002015 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002016 }
2017 }
2018
2019 // Now set this one as the previous process, only if that really
2020 // makes sense to.
2021 if (r.app != null && fgApp != null && r.app != fgApp
2022 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002023 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002024 mService.mPreviousProcess = r.app;
2025 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2026 }
2027 }
2028
Wale Ogunwaled046a012015-12-24 13:05:59 -08002029 boolean resumeFocusedStackTopActivityLocked() {
2030 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002031 }
2032
Wale Ogunwaled046a012015-12-24 13:05:59 -08002033 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002034 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002035 if (targetStack != null && isFocusedStack(targetStack)) {
2036 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002037 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002038 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2039 if (r == null || r.state != RESUMED) {
2040 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002041 } else if (r.state == RESUMED) {
2042 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2043 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002044 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002045 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002046 }
2047
Todd Kennedy39bfee52016-02-24 10:28:21 -08002048 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2049 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2050 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2051 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2052 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2053 }
2054 }
2055 }
2056
Adrian Roos20d7df32016-01-12 18:59:43 +01002057 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2058 TaskRecord finishedTask = null;
2059 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002060 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2061 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002062 final int numStacks = stacks.size();
2063 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2064 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002065 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2066 if (stack == focusedStack || finishedTask == null) {
2067 finishedTask = t;
2068 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002069 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002070 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002071 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002072 }
2073
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002074 void finishVoiceTask(IVoiceInteractionSession session) {
2075 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2076 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2077 final int numStacks = stacks.size();
2078 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2079 final ActivityStack stack = stacks.get(stackNdx);
2080 stack.finishVoiceTask(session);
2081 }
2082 }
2083 }
2084
Andrii Kulianc27916642016-04-12 17:59:27 -07002085 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2086 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002087 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2088 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002089 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002090 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2091 // Caller wants the home activity moved with it. To accomplish this,
2092 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002093 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002094 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002095 ActivityStack currentStack = task.getStack();
2096 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002097 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2098 + task + " to front. Stack is null");
2099 return;
2100 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002101
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002102 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002103 int stackId = options.getLaunchStackId();
2104 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002105 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002106 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002107 if (stackId == INVALID_STACK_ID) {
2108 stackId = task.getLaunchStackId();
2109 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002110 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002111 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2112 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002113 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002114 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2115 // still need moveTaskToFrontLocked() below for any transition settings.
2116 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002117 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2118 resizeStackLocked(stackId, bounds,
2119 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002120 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002121 } else {
2122 // WM resizeTask must be done after the task is moved to the correct stack,
2123 // because Task's setBounds() also updates dim layer's bounds, but that has
2124 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002125 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002126 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002127 }
2128 }
2129
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002130 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002131 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002132 r == null ? null : r.appTimeTracker, reason);
2133
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002134 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002135 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002136
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002137 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2138 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 }
2140
Wale Ogunwale854809c2015-12-27 16:18:19 -08002141 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002142 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002143 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002144 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002145 return false;
2146 }
2147 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2148 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002149 }
2150
Winson Chung55893332017-02-17 17:13:10 -08002151 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002152 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002153 }
2154
Winson Chung55893332017-02-17 17:13:10 -08002155 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2156 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002157 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002158 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002159 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002160 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002161 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002162 return null;
2163 }
Matthew Ng330757d2017-02-28 14:19:17 -08002164 if (stackId == DOCKED_STACK_ID) {
2165 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2166 // the other side of the docked stack and we make visibility decisions based on that.
2167 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2168 }
Winson Chung55893332017-02-17 17:13:10 -08002169 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002170 }
2171
Andrii Kulian16802aa2016-11-02 12:21:33 -07002172 /**
2173 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2174 * If there is no such stack, new dynamic stack can be created.
2175 * @param displayId Target display.
2176 * @param r Activity that should be launched there.
2177 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2178 */
2179 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2180 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2181 if (activityDisplay == null) {
2182 throw new IllegalArgumentException(
2183 "Display with displayId=" + displayId + " not found.");
2184 }
2185
2186 // Return the topmost valid stack on the display.
2187 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2188 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002189 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002190 return stack;
2191 }
2192 }
2193
2194 // If there is no valid stack on the external display - check if new dynamic stack will do.
2195 if (displayId != Display.DEFAULT_DISPLAY) {
2196 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002197 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002198 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2199 }
2200 }
2201
2202 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2203 return null;
2204 }
2205
Craig Mautner967212c2013-04-13 21:10:58 -07002206 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002207 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002208 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2209 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002210 }
2211 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002212 }
2213
Jorim Jaggife762342016-10-13 14:33:27 +02002214 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2215 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2216 }
2217
Andrii Kulian7fc22812016-12-28 13:04:11 -08002218 /**
2219 * Get next focusable stack in the system. This will search across displays and stacks
2220 * in last-focused order for a focusable and visible stack, different from the target stack.
2221 *
2222 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2223 * searching for next candidate.
2224 * @return Next focusable {@link ActivityStack}, null if not found.
2225 */
2226 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2227 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2228
2229 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2230 final int displayId = mTmpOrderedDisplayIds.get(i);
2231 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2232 if (stacks == null) {
2233 continue;
2234 }
2235 for (int j = stacks.size() - 1; j >= 0; --j) {
2236 final ActivityStack stack = stacks.get(j);
2237 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002238 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002239 return stack;
2240 }
2241 }
2242 }
2243
2244 return null;
2245 }
2246
Craig Mautneree2e45a2014-06-27 12:10:03 -07002247 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002248 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002249 }
2250
2251 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002252 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2253 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2254 final TaskRecord task = tasks.get(taskNdx);
2255 if (task.isHomeTask()) {
2256 final ArrayList<ActivityRecord> activities = task.mActivities;
2257 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2258 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002259 if (r.isHomeActivity()
2260 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002261 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002262 }
2263 }
2264 }
2265 }
2266 return null;
2267 }
2268
Chong Zhangb15758a2015-11-17 12:12:03 -08002269 /**
2270 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2271 * the docked stack itself, or if it's side-by-side to the docked stack.
2272 */
2273 boolean isStackDockedInEffect(int stackId) {
2274 return stackId == DOCKED_STACK_ID ||
2275 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2276 }
2277
Todd Kennedyca4d8422015-01-15 15:19:22 -08002278 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002279 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002280 ActivityContainer activityContainer =
2281 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002282 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002283 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2284 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002285 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002286 return activityContainer;
2287 }
2288
Craig Mautner34b73df2014-01-12 21:11:08 -08002289 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002290 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2291 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2292 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002293 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2294 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002295 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002296 }
2297 }
2298
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002299 void deleteActivityContainerRecord(int stackId) {
2300 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2301 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2302 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002303 }
2304
Jorim Jaggidc249c42015-12-15 14:57:31 -08002305 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002306 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002307 if (stackId == DOCKED_STACK_ID) {
2308 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002309 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002310 return;
2311 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002312 final ActivityStack stack = getStack(stackId);
2313 if (stack == null) {
2314 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2315 return;
2316 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002317
Winson Chunga2249ac2017-03-30 18:15:30 -07002318 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2319 getStack(DOCKED_STACK_ID) != null) {
2320 // If the docked stack exists, don't resize non-floating stacks independently of the
2321 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002322 return;
2323 }
2324
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002325 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002326 mWindowManager.deferSurfaceLayout();
2327 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002328 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002329 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002330 stack.ensureVisibleActivitiesConfigurationLocked(
2331 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002332 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002333 } finally {
2334 mWindowManager.continueSurfaceLayout();
2335 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002336 }
2337 }
2338
Jorim Jaggi192086e2016-03-11 17:17:03 +01002339 void deferUpdateBounds(int stackId) {
2340 final ActivityStack stack = getStack(stackId);
2341 if (stack != null) {
2342 stack.deferUpdateBounds();
2343 }
2344 }
2345
2346 void continueUpdateBounds(int stackId) {
2347 final ActivityStack stack = getStack(stackId);
2348 if (stack != null) {
2349 stack.continueUpdateBounds();
2350 }
2351 }
2352
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002353 void notifyAppTransitionDone() {
2354 continueUpdateBounds(HOME_STACK_ID);
2355 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2356 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002357 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002358 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002359 if (task != null) {
2360 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002361 }
2362 }
2363 mResizingTasksDuringAnimation.clear();
2364 }
2365
Robert Carre7cc44d2017-03-20 19:04:30 -07002366 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2367 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002368
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002369 final ActivityStack stack = getStack(fromStackId);
2370 if (stack == null) {
2371 return;
2372 }
2373
2374 mWindowManager.deferSurfaceLayout();
2375 try {
2376 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002377 // We are moving all tasks from the docked stack to the fullscreen stack,
2378 // which is dismissing the docked stack, so resize all other stacks to
2379 // fullscreen here already so we don't end up with resize trashing.
2380 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2381 if (StackId.isResizeableByDockedStack(i)) {
2382 ActivityStack otherStack = getStack(i);
2383 if (otherStack != null) {
2384 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2385 true /* allowResizeInDockedMode */, DEFER_RESUME);
2386 }
2387 }
2388 }
2389
2390 // Also disable docked stack resizing since we have manually adjusted the
2391 // size of other stacks above and we don't want to trigger a docked stack
2392 // resize when we remove task from it below and it is detached from the
2393 // display because it no longer contains any tasks.
2394 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002395 } else if (fromStackId == PINNED_STACK_ID) {
2396 if (onTop) {
2397 // Log if we are expanding the PiP to fullscreen
2398 MetricsLogger.action(mService.mContext,
2399 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2400 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002401 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002402 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002403 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002404 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002405 // If we are moving from the pinned stack, then the animation takes care of updating
2406 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002407 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002408 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2409 final int size = tasks.size();
2410 if (onTop) {
2411 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002412 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002413 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002414 if (fromStackId == PINNED_STACK_ID) {
2415 // Update the return-to to reflect where the pinned stack task was moved
2416 // from so that we retain the stack that was previously visible if the
2417 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002418 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2419 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002420 }
Winson Chung74666102017-02-22 17:49:24 -08002421 // Defer resume until all the tasks have been moved to the fullscreen stack
2422 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2423 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002424 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002425 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002426 }
2427 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002428 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002429 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002430 // Position the tasks in the fullscreen stack in order at the bottom of the
2431 // stack. Also defer resume until all the tasks have been moved to the
2432 // fullscreen stack.
2433 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002434 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2435 schedulePictureInPictureModeChange,
2436 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002437 }
2438 }
Winson Chung74666102017-02-22 17:49:24 -08002439
2440 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2441 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002442 } finally {
2443 mAllowDockedStackResize = true;
2444 mWindowManager.continueSurfaceLayout();
2445 }
2446 }
2447
Robert Carr6914f082017-03-20 19:04:30 -07002448 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002449 mWindowManager.inSurfaceTransaction(
2450 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002451 }
2452
Jorim Jaggidc249c42015-12-15 14:57:31 -08002453 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002454 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2455 boolean preserveWindows) {
2456 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2457 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2458 false /* deferResume */);
2459 }
2460
2461 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2462 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2463 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002464
2465 if (!mAllowDockedStackResize) {
2466 // Docked stack resize currently disabled.
2467 return;
2468 }
2469
Jorim Jaggidc249c42015-12-15 14:57:31 -08002470 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2471 if (stack == null) {
2472 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2473 return;
2474 }
2475
2476 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2477 mWindowManager.deferSurfaceLayout();
2478 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002479 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2480 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002481 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002482 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002483
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002484 // TODO: Checking for isAttached might not be needed as if the user passes in null
2485 // dockedBounds then they want the docked stack to be dismissed.
2486 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2487 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002488 // In this case we make all other static stacks fullscreen and move all
2489 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002490 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002491
2492 // stack shouldn't contain anymore activities, so nothing to resume.
2493 r = null;
2494 } else {
2495 // Docked stacks occupy a dedicated region on screen so the size of all other
2496 // static stacks need to be adjusted so they don't overlap with the docked stack.
2497 // We get the bounds to use from window manager which has been adjusted for any
2498 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002499 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002500 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002501 final ActivityStack current = getStack(i);
2502 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002503 current.getStackDockedModeBounds(
2504 tempOtherTaskBounds /* currentTempTaskBounds */,
2505 tempRect /* outStackBounds */,
2506 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2507
Matthew Nge15352e2016-12-20 15:36:29 -08002508 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002509 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2510 tempOtherTaskInsetBounds, preserveWindows,
2511 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002512 }
2513 }
2514 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002515 if (!deferResume) {
2516 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2517 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002518 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002519 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002520 mWindowManager.continueSurfaceLayout();
2521 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2522 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002523 }
2524
Robert Carr0d00c2e2016-02-29 17:45:02 -08002525 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002526 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002527 if (stack == null) {
2528 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2529 return;
2530 }
Winson Chung19953ca2017-04-11 11:19:23 -07002531
2532 // It is possible for the bounds animation from the WM to call this but be delayed by
2533 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2534 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2535 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2536 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002537 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002538 return;
2539 }
2540
Robert Carr0d00c2e2016-02-29 17:45:02 -08002541 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2542 mWindowManager.deferSurfaceLayout();
2543 try {
2544 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002545 Rect insetBounds = null;
2546 if (tempPinnedTaskBounds != null) {
2547 // We always use 0,0 as the position for the inset rect because
2548 // if we are getting insets at all in the pinned stack it must mean
2549 // we are headed for fullscreen.
2550 insetBounds = tempRect;
2551 insetBounds.top = 0;
2552 insetBounds.left = 0;
2553 insetBounds.right = tempPinnedTaskBounds.width();
2554 insetBounds.bottom = tempPinnedTaskBounds.height();
2555 }
2556 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002557 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002558 } finally {
2559 mWindowManager.continueSurfaceLayout();
2560 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2561 }
2562 }
2563
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002564 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002565 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002566 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002567 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002568 }
2569
Wale Ogunwale1666e312016-12-16 11:27:18 -08002570 final ActivityContainer activityContainer =
2571 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002572 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002573 }
2574
Robert Carr6914f082017-03-20 19:04:30 -07002575
Robert Carre7cc44d2017-03-20 19:04:30 -07002576 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002577 final ActivityStack stack = getStack(stackId);
2578 if (stack == null) {
2579 return;
2580 }
2581
2582 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2583 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002584 /**
2585 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2586 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2587 * that the client receives onStop() before it is reparented. We do this by detaching
2588 * the stack from the display so that it will be considered invisible when
2589 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2590 * invisible as well and added to the stopping list. After which we process the
2591 * stopping list by handling the idle.
2592 */
2593 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2594 pinnedStack.mForceHidden = true;
2595 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2596 pinnedStack.mForceHidden = false;
2597 activityIdleInternalLocked(null, false /* fromTimeout */,
2598 true /* processPausingActivites */, null /* configuration */);
2599
2600 // Move all the tasks to the bottom of the fullscreen stack
2601 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002602 } else {
2603 for (int i = tasks.size() - 1; i >= 0; i--) {
2604 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2605 REMOVE_FROM_RECENTS);
2606 }
2607 }
2608 }
2609
2610 /**
Robert Carr6914f082017-03-20 19:04:30 -07002611 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2612 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2613 * instead moved back onto the fullscreen stack.
2614 */
2615 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002616 mWindowManager.inSurfaceTransaction(
2617 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002618 }
2619
2620 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002621 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2622 */
2623 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2624 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2625 }
2626
2627 /**
Winson Chung010927a2016-12-15 16:12:35 -08002628 * Removes the task with the specified task id.
2629 *
2630 * @param taskId Identifier of the task to be removed.
2631 * @param killProcess Kill any process associated with the task if possible.
2632 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002633 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2634 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002635 * @return Returns true if the given task was found and removed.
2636 */
Winson Chung6954fc92017-03-24 16:22:12 -07002637 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2638 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002639 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2640 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002641 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002642 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002643 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2644 if (tr.isPersistable) {
2645 mService.notifyTaskPersisterLocked(null, true);
2646 }
2647 return true;
2648 }
2649 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2650 return false;
2651 }
2652
2653 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2654 if (removeFromRecents) {
2655 mRecentTasks.remove(tr);
2656 tr.removedFromRecents();
2657 }
2658 ComponentName component = tr.getBaseIntent().getComponent();
2659 if (component == null) {
2660 Slog.w(TAG, "No component for base intent of task: " + tr);
2661 return;
2662 }
2663
2664 // Find any running services associated with this app and stop if needed.
2665 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2666
2667 if (!killProcess) {
2668 return;
2669 }
2670
2671 // Determine if the process(es) for this task should be killed.
2672 final String pkg = component.getPackageName();
2673 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2674 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2675 for (int i = 0; i < pmap.size(); i++) {
2676
2677 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2678 for (int j = 0; j < uids.size(); j++) {
2679 ProcessRecord proc = uids.valueAt(j);
2680 if (proc.userId != tr.userId) {
2681 // Don't kill process for a different user.
2682 continue;
2683 }
2684 if (proc == mService.mHomeProcess) {
2685 // Don't kill the home process along with tasks from the same package.
2686 continue;
2687 }
2688 if (!proc.pkgList.containsKey(pkg)) {
2689 // Don't kill process that is not associated with this task.
2690 continue;
2691 }
2692
2693 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002694 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002695 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2696 // Don't kill process(es) that has an activity in a different task that is
2697 // also in recents.
2698 return;
2699 }
2700 }
2701
2702 if (proc.foregroundServices) {
2703 // Don't kill process(es) with foreground service.
2704 return;
2705 }
2706
2707 // Add process to kill list.
2708 procsToKill.add(proc);
2709 }
2710 }
2711
2712 // Kill the running processes.
2713 for (int i = 0; i < procsToKill.size(); i++) {
2714 ProcessRecord pr = procsToKill.get(i);
2715 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2716 && pr.curReceivers.isEmpty()) {
2717 pr.kill("remove task", true);
2718 } else {
2719 // We delay killing processes that are not in the background or running a receiver.
2720 pr.waitingToKill = "remove task";
2721 }
2722 }
2723 }
2724
Craig Mautner4a1cb222013-12-04 16:14:06 -08002725 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002726 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002727 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2728 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002729 break;
2730 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002731 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002732 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002733 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 }
2735
Chong Zhang5dcb2752015-08-18 13:50:26 -07002736 /**
2737 * Restores a recent task to a stack
2738 * @param task The recent task to be restored.
2739 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002740 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2741 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002742 * @return true if the task has been restored successfully.
2743 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002744 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002745 if (!StackId.isStaticStack(stackId)) {
2746 // If stack is not static (or stack id is invalid) - use the default one.
2747 // This means that tasks that were on external displays will be restored on the
2748 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002749 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002750 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002751 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2752 // Put it in the fullscreen stack.
2753 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002754 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002755
Andrii Kulian02b7a832016-10-06 23:11:56 -07002756 final ActivityStack currentStack = task.getStack();
2757 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002758 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002759 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002760 // Nothing else to do since it is already restored in the right stack.
2761 return true;
2762 }
2763 // Remove current stack association, so we can re-associate the task with the
2764 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002765 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002766 }
2767
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002768 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002769 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002770
2771 if (stack == null) {
2772 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002773 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2774 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002775 return false;
2776 }
2777
Wale Ogunwale72919d22016-12-08 18:58:50 -08002778 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002779 // TODO: move call for creation here and other place into Stack.addTask()
2780 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002781 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2782 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002783 final ArrayList<ActivityRecord> activities = task.mActivities;
2784 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002785 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002786 }
2787 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002788 }
2789
Wale Ogunwale040b4702015-08-06 18:10:50 -07002790 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002791 * Move stack with all its existing content to specified display.
2792 * @param stackId Id of stack to move.
2793 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002794 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002795 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002796 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002797 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2798 if (activityDisplay == null) {
2799 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2800 + displayId);
2801 }
2802 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2803 if (activityContainer != null) {
2804 if (activityContainer.isAttachedLocked()) {
2805 if (activityContainer.getDisplayId() == displayId) {
2806 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2807 + " to its current displayId=" + displayId);
2808 }
2809
Andrii Kulian250d6532017-02-08 23:30:45 -08002810 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002811 } else {
2812 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2813 + stackId + " is not attached to any display.");
2814 }
2815 } else {
2816 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2817 + stackId);
2818 }
Andrii Kulian839def92016-11-02 10:58:58 -07002819 // TODO(multi-display): resize stacks properly if moved from split-screen.
2820 }
2821
2822 /**
Winson Chung74666102017-02-22 17:49:24 -08002823 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2824 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002825 */
Winson Chung74666102017-02-22 17:49:24 -08002826 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002827 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002828
Winson Chung74666102017-02-22 17:49:24 -08002829 // Check that we aren't reparenting to the same stack that the task is already in
2830 if (prevStack != null && prevStack.mStackId == stackId) {
2831 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2832 + " already in stackId=" + stackId);
2833 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002834 }
2835
Winson Chung74666102017-02-22 17:49:24 -08002836 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2837 // support
2838 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2839 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2840 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002841 }
2842
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002843 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2844 // multi-display.
2845 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2846 // TODO: Check ActivityView after fixing b/35349678.
2847 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2848 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2849 + " reparent task=" + task + " to stackId=" + stackId);
2850 }
2851
Winson Chung74666102017-02-22 17:49:24 -08002852 // Ensure that we aren't trying to move into a freeform stack without freeform
2853 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002854 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002855 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2856 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002857 }
2858
Winson Chung74666102017-02-22 17:49:24 -08002859 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2860 // used for split-screen mode and will cause things like the docked divider to show up. We
2861 // instead leave the task in its current stack or move it to the fullscreen stack if it
2862 // isn't currently in a stack.
2863 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2864 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2865 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2866 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002867 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002868
Winson Chung74666102017-02-22 17:49:24 -08002869 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2870 // example, a docked stack is created which will lead to the stack we are moving from being
2871 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002872 try {
Winson Chung74666102017-02-22 17:49:24 -08002873 task.mTemporarilyUnresizable = true;
2874 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002875 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002876 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002877 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002878 }
2879
Winson Chung08f81892017-03-02 15:40:51 -08002880 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002881 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2882 if (stack == null) {
2883 throw new IllegalArgumentException(
2884 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2885 }
2886
2887 final ActivityRecord r = stack.topRunningActivityLocked();
2888 if (r == null) {
2889 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2890 + " in stack=" + stack);
2891 return false;
2892 }
2893
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002894 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002895 Slog.w(TAG,
2896 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002897 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002898 return false;
2899 }
2900
Winson Chung3a682872017-04-10 14:38:05 -07002901 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002902 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002903 return true;
2904 }
2905
Winson Chung8bca9e42017-04-16 15:59:43 -07002906 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002907 boolean moveHomeStackToFront, String reason) {
2908
Wale Ogunwale480dca02016-02-06 13:58:29 -08002909 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002910
Bryce Lee04ab3462017-04-10 15:06:33 -07002911 // This will clear the pinned stack by moving an existing task to the full screen stack,
2912 // ensuring only one task is present.
2913 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2914
Wale Ogunwale1666e312016-12-16 11:27:18 -08002915 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002916 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002917
Wale Ogunwale480dca02016-02-06 13:58:29 -08002918 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002919 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002920
Andrii Kulian02b7a832016-10-06 23:11:56 -07002921 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002922 // An activity can't be pinned and visible behind at the same time. Go ahead and
2923 // release it from been visible behind before pinning.
2924 requestVisibleBehindLocked(r, false);
2925 }
2926
Wale Ogunwale480dca02016-02-06 13:58:29 -08002927 // Resize the pinned stack to match the current size of the task the activity we are
2928 // going to be moving is currently contained in. We do this to have the right starting
2929 // animation bounds for the pinned stack to the desired bounds the caller wants.
2930 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2931 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002932 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002933
2934 if (task.mActivities.size() == 1) {
2935 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002936 // the stack without re-parenting the activity in a different task. We don't
2937 // move the home stack forward if we are currently entering picture-in-picture
2938 // while pausing because that changes the focused stack and may prevent the new
2939 // starting activity from resuming.
2940 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002941 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002942 // Move the home stack forward if the task we just moved to the pinned stack
2943 // was launched from home so home should be visible behind it.
2944 moveHomeStackToFront(reason);
2945 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002946 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002947 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002948 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002949 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002950 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002951 // reveal/leave the other activities in their original task.
2952
2953 // Currently, we don't support reparenting activities across tasks in two different
2954 // stacks, so instead, just create a new task in the same stack, reparent the
2955 // activity into that task, and then reparent the whole task to the new stack. This
2956 // ensures that all the necessary work to migrate states in the old and new stacks
2957 // is also done.
2958 final TaskRecord newTask = task.getStack().createTaskRecord(
2959 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2960 r.mActivityType);
2961 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2962
Winson Chung5af42fc2017-03-24 17:11:33 -07002963 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002964 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002965 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002966 }
Winson Chungc2baac02017-01-11 13:34:47 -08002967
2968 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2969 // to the pinned stack
2970 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002971 } finally {
2972 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002973 }
2974
Wale Ogunwale480dca02016-02-06 13:58:29 -08002975 // The task might have already been running and its visibility needs to be synchronized
2976 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002977 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002978 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002979
Winson Chung3a682872017-04-10 14:38:05 -07002980 // Calculate the default bounds (don't use existing stack bounds as we may have just created
2981 // the stack
2982 final Rect destBounds = mWindowManager.getPictureInPictureBounds(DEFAULT_DISPLAY,
2983 aspectRatio, false /* useExistingStackBounds */);
2984
Winson Chung8bca9e42017-04-16 15:59:43 -07002985 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
2986 true /* schedulePipModeChangedOnAnimationEnd */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002987 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002988 }
2989
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002990 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002991 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2992 if (r == null || !r.isFocusable()) {
2993 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2994 "moveActivityStackToFront: unfocusable r=" + r);
2995 return false;
2996 }
2997
Bryce Leeaf691c02017-03-20 14:20:22 -07002998 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002999 final ActivityStack stack = r.getStack();
3000 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003001 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3002 + r + " task=" + task);
3003 return false;
3004 }
3005
Chong Zhang6cda19c2016-06-14 19:07:56 -07003006 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3007 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3008 "moveActivityStackToFront: already on top, r=" + r);
3009 return false;
3010 }
3011
3012 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3013 "moveActivityStackToFront: r=" + r);
3014
3015 stack.moveToFront(reason, task);
3016 return true;
3017 }
3018
David Stevensc6b91c62017-02-08 14:23:58 -08003019 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003020 mTmpFindTaskResult.r = null;
3021 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003022 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003023 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3025 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003026 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3027 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003028 if (!checkActivityBelongsInStack(r, stack)) {
3029 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3030 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003031 continue;
3032 }
3033 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003034 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3035 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 continue;
3037 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003038 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003039 // It is possible to have tasks in multiple stacks with the same root affinity, so
3040 // we should keep looking after finding an affinity match to see if there is a
3041 // better match in another stack. Also, task affinity isn't a good enough reason
3042 // to target a display which isn't the source of the intent, so skip any affinity
3043 // matches not on the specified display.
3044 if (mTmpFindTaskResult.r != null) {
3045 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3046 return mTmpFindTaskResult.r;
3047 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
3048 affinityMatch = mTmpFindTaskResult.r;
3049 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003051 }
3052 }
David Stevensc6b91c62017-02-08 14:23:58 -08003053 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3054 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003055 }
3056
Winson Chung91e5c882017-04-21 14:44:38 -07003057 /**
3058 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3059 * type.
3060 */
3061 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3062 if (r.isHomeActivity()) {
3063 return stack.isHomeStack();
3064 } else if (r.isRecentsActivity()) {
3065 return stack.isRecentsStack();
3066 } else if (r.isAssistantActivity()) {
3067 return stack.isAssistantStack();
3068 }
3069 return true;
3070 }
3071
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003072 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3073 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003074 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3075 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003076 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003077 final ActivityRecord ar = stacks.get(stackNdx)
3078 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003079 if (ar != null) {
3080 return ar;
3081 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003082 }
3083 }
3084 return null;
3085 }
3086
Craig Mautner8d341ef2013-03-26 09:03:27 -07003087 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003088 scheduleSleepTimeout();
3089 if (!mGoingToSleep.isHeld()) {
3090 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003091 if (mLaunchingActivity.isHeld()) {
3092 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3093 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003094 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003095 mLaunchingActivity.release();
3096 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003097 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003098 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003099 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003100 }
3101
3102 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003103 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003104
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003105 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003106 final long endTime = System.currentTimeMillis() + timeout;
3107 while (true) {
3108 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003109 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3110 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003111 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3112 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3113 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003114 }
3115 if (cantShutdown) {
3116 long timeRemaining = endTime - System.currentTimeMillis();
3117 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003118 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003119 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 } catch (InterruptedException e) {
3121 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003122 } else {
3123 Slog.w(TAG, "Activity manager shutdown timed out");
3124 timedout = true;
3125 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003126 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003127 } else {
3128 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003129 }
3130 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003131
3132 // Force checkReadyForSleep to complete.
3133 mSleepTimeout = true;
3134 checkReadyForSleepLocked();
3135
Craig Mautner8d341ef2013-03-26 09:03:27 -07003136 return timedout;
3137 }
3138
3139 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003140 removeSleepTimeouts();
3141 if (mGoingToSleep.isHeld()) {
3142 mGoingToSleep.release();
3143 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003144 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3145 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003146 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3147 final ActivityStack stack = stacks.get(stackNdx);
3148 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003149 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003150 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003151 }
Craig Mautner5314a402013-09-26 12:40:16 -07003152 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003153 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003154 mGoingToSleepActivities.clear();
3155 }
3156
3157 void activitySleptLocked(ActivityRecord r) {
3158 mGoingToSleepActivities.remove(r);
3159 checkReadyForSleepLocked();
3160 }
3161
3162 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003163 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003164 // Do not care.
3165 return;
3166 }
3167
3168 if (!mSleepTimeout) {
3169 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003170 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3171 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003172 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3173 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3174 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003175 }
3176
3177 if (mStoppingActivities.size() > 0) {
3178 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003179 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003180 + mStoppingActivities.size() + " activities");
3181 scheduleIdleLocked();
3182 dontSleep = true;
3183 }
3184
3185 if (mGoingToSleepActivities.size() > 0) {
3186 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003187 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003188 + mGoingToSleepActivities.size() + " activities");
3189 dontSleep = true;
3190 }
3191
3192 if (dontSleep) {
3193 return;
3194 }
3195 }
3196
Wei Wang65c7a152016-06-02 18:51:22 -07003197 // Send launch end powerhint before going sleep
3198 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3199
Craig Mautnere0a38842013-12-16 16:14:02 -08003200 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3201 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003202 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3203 stacks.get(stackNdx).goToSleep();
3204 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003205 }
3206
3207 removeSleepTimeouts();
3208
3209 if (mGoingToSleep.isHeld()) {
3210 mGoingToSleep.release();
3211 }
3212 if (mService.mShuttingDown) {
3213 mService.notifyAll();
3214 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003215 }
3216
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003218 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003219 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003220 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003221 }
3222 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003223 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003224 mWindowManager.executeAppTransition();
3225 return true;
3226 }
3227 return false;
3228 }
3229
Craig Mautner8d341ef2013-03-26 09:03:27 -07003230 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003231 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3232 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003233 int stackNdx = stacks.size() - 1;
3234 while (stackNdx >= 0) {
3235 stacks.get(stackNdx).handleAppCrashLocked(app);
3236 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003237 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003238 }
3239 }
3240
Jose Lima4b6c6692014-08-12 17:41:12 -07003241 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003242 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003243 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003244 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3245 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003246 return false;
3247 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003248
3249 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3250 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3251 + " visible=" + visible + " stackId=" + stack.mStackId
3252 + " can't contain visible behind activities");
3253 return false;
3254 }
3255
Jose Lima4b6c6692014-08-12 17:41:12 -07003256 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003257 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3258 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003259
3260 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003261 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003262 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003263 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003264 return true;
3265 }
3266
3267 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003268 if (visible && top.fullscreen) {
3269 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003270 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3271 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3272 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3273 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003274 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003275 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3276 // Only the activity set as currently visible behind should actively reset its
3277 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003278 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3279 "requestVisibleBehind: returning visible=" + visible
3280 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3281 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003282 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003283 }
3284
Jose Lima4b6c6692014-08-12 17:41:12 -07003285 stack.setVisibleBehindActivity(visible ? r : null);
3286 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003287 // If there is a translucent home activity, we need to force it stop being translucent,
3288 // because we can't depend on the application to necessarily perform that operation.
3289 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003290 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003291 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003292 mService.convertFromTranslucent(next.appToken);
3293 }
3294 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003295 if (top.app != null && top.app.thread != null) {
3296 // Notify the top app of the change.
3297 try {
3298 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3299 } catch (RemoteException e) {
3300 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003301 }
3302 return true;
3303 }
3304
Craig Mautnerbb742462014-07-07 15:28:55 -07003305 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003306 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003307 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003308 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003309
3310 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003311 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003312 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003313 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003314 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003315
3316 // When launching tasks behind, update the last active time of the top task after the new
3317 // task has been shown briefly
3318 final ActivityRecord top = stack.topActivity();
3319 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003320 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003321 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003322 }
3323
3324 void scheduleLaunchTaskBehindComplete(IBinder token) {
3325 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3326 }
3327
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003328 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3329 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003330 mKeyguardController.beginActivityVisibilityUpdate();
3331 try {
3332 // First the front stacks. In case any are not fullscreen and are in front of home.
3333 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3334 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3335 final int topStackNdx = stacks.size() - 1;
3336 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3337 final ActivityStack stack = stacks.get(stackNdx);
3338 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3339 }
Craig Mautner580ea812013-04-25 12:58:38 -07003340 }
Jorim Jaggife762342016-10-13 14:33:27 +02003341 } finally {
3342 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003343 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003344 }
3345
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003346 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3347 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3348 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3349 final int topStackNdx = stacks.size() - 1;
3350 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3351 final ActivityStack stack = stacks.get(stackNdx);
3352 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3353 }
3354 }
3355 }
3356
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003357 void invalidateTaskLayers() {
3358 mTaskLayersChanged = true;
3359 }
3360
3361 void rankTaskLayersIfNeeded() {
3362 if (!mTaskLayersChanged) {
3363 return;
3364 }
3365 mTaskLayersChanged = false;
3366 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3367 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3368 int baseLayer = 0;
3369 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3370 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3371 }
3372 }
3373 }
3374
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003375 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3376 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3377 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3378 final int topStackNdx = stacks.size() - 1;
3379 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3380 final ActivityStack stack = stacks.get(stackNdx);
3381 stack.clearOtherAppTimeTrackers(except);
3382 }
3383 }
3384 }
3385
Craig Mautner8d341ef2013-03-26 09:03:27 -07003386 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003387 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3388 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003389 final int numStacks = stacks.size();
3390 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3391 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003392 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003393 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003394 }
3395 }
3396
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003397 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3398 // Examine all activities currently running in the process.
3399 TaskRecord firstTask = null;
3400 // Tasks is non-null only if two or more tasks are found.
3401 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003402 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3403 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003404 ActivityRecord r = app.activities.get(i);
3405 // First, if we find an activity that is in the process of being destroyed,
3406 // then we just aren't going to do anything for now; we want things to settle
3407 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003408 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003409 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003410 return;
3411 }
3412 // Don't consider any activies that are currently not in a state where they
3413 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003414 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3415 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003416 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003417 continue;
3418 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003419
3420 final TaskRecord task = r.getTask();
3421 if (task != null) {
3422 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003423 + " from " + r);
3424 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003425 firstTask = task;
3426 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003427 if (tasks == null) {
3428 tasks = new ArraySet<>();
3429 tasks.add(firstTask);
3430 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003431 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003432 }
3433 }
3434 }
3435 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003436 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003437 return;
3438 }
3439 // If we have activities in multiple tasks that are in a position to be destroyed,
3440 // let's iterate through the tasks and release the oldest one.
3441 final int numDisplays = mActivityDisplays.size();
3442 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3443 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3444 // Step through all stacks starting from behind, to hit the oldest things first.
3445 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3446 final ActivityStack stack = stacks.get(stackNdx);
3447 // Try to release activities in this stack; if we manage to, we are done.
3448 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3449 return;
3450 }
3451 }
3452 }
3453 }
3454
Amith Yamasani37a40c22015-06-17 13:25:42 -07003455 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003456 final int focusStackId = mFocusedStack.getStackId();
3457 // We dismiss the docked stack whenever we switch users.
3458 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003459 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3460 // also cause all tasks to be moved to the fullscreen stack at a position that is
3461 // appropriate.
3462 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003463
3464 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003465 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003466 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003467
Craig Mautner858d8a62013-04-23 17:08:34 -07003468 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003469 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3470 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003472 final ActivityStack stack = stacks.get(stackNdx);
3473 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003474 TaskRecord task = stack.topTask();
3475 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003476 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003477 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003478 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003479 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003480
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003481 ActivityStack stack = getStack(restoreStackId);
3482 if (stack == null) {
3483 stack = mHomeStack;
3484 }
3485 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003486 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003487 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003488 } else {
3489 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003490 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003491 }
Craig Mautner93529a42013-10-04 15:03:13 -07003492 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003493 }
3494
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003495 /** Checks whether the userid is a profile of the current user. */
3496 boolean isCurrentProfileLocked(int userId) {
3497 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003498 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003499 }
3500
Winson Chung4dabf232017-01-25 13:25:22 -08003501 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3502 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003503 ArrayList<ActivityRecord> stops = null;
3504
3505 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003506 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3507 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003508 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003509 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003510 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3511 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003512 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003513 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003514 if (s.finishing) {
3515 // If this activity is finishing, it is sitting on top of
3516 // everyone else but we now know it is no longer needed...
3517 // so get rid of it. Otherwise, we need to go through the
3518 // normal flow and hide it once we determine that it is
3519 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003520 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003521 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003522 }
3523 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003524 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003525 if (!processPausingActivities && s.state == PAUSING) {
3526 // Defer processing pausing activities in this iteration and reschedule
3527 // a delayed idle to reprocess it again
3528 removeTimeoutsForActivityLocked(idleActivity);
3529 scheduleIdleTimeoutLocked(idleActivity);
3530 continue;
3531 }
3532
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003533 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003534 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003535 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003536 }
3537 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003538 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003539 }
3540 }
3541
3542 return stops;
3543 }
3544
Craig Mautnercf910b02013-04-23 11:23:27 -07003545 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003546 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3547 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3548 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3549 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003550 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003551 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003552 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003553 if (r == null) Slog.e(TAG,
3554 "validateTop...: null top activity, stack=" + stack);
3555 else {
3556 final ActivityRecord pausing = stack.mPausingActivity;
3557 if (pausing != null && pausing == r) Slog.e(TAG,
3558 "validateTop...: top stack has pausing activity r=" + r
3559 + " state=" + state);
3560 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3561 "validateTop...: activity in front not resumed r=" + r
3562 + " state=" + state);
3563 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003564 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003565 final ActivityRecord resumed = stack.mResumedActivity;
3566 if (resumed != null && resumed == r) Slog.e(TAG,
3567 "validateTop...: back stack has resumed activity r=" + r
3568 + " state=" + state);
3569 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3570 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003571 }
3572 }
3573 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003574 }
3575
Craig Mautnere0570202015-05-13 13:06:11 -07003576 private String lockTaskModeToString() {
3577 switch (mLockTaskModeState) {
3578 case LOCK_TASK_MODE_LOCKED:
3579 return "LOCKED";
3580 case LOCK_TASK_MODE_PINNED:
3581 return "PINNED";
3582 case LOCK_TASK_MODE_NONE:
3583 return "NONE";
3584 default: return "unknown=" + mLockTaskModeState;
3585 }
3586 }
3587
Craig Mautner27084302013-03-25 08:05:25 -07003588 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003589 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003591 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003592 pw.print(prefix);
3593 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003594 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003595 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003596 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003597 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3598 if (packages.size() > 0) {
3599 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3600 for (int i = 0; i < packages.size(); ++i) {
3601 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3602 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3603 }
3604 }
Bryce Lee4a194382017-04-04 14:32:48 -07003605 if (!mWaitingForActivityVisible.isEmpty()) {
3606 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3607 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3608 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3609 }
3610 }
3611
Jorim Jaggi8d786932016-10-26 19:08:36 -07003612 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3613 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003614 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003615
Winson43d1f262016-06-14 16:05:55 -07003616 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003617 * Dump all connected displays' configurations.
3618 * @param prefix Prefix to apply to each line of the dump.
3619 */
3620 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3621 pw.print(prefix); pw.println("Display override configurations:");
3622 final int displayCount = mActivityDisplays.size();
3623 for (int i = 0; i < displayCount; i++) {
3624 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3625 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3626 pw.println(activityDisplay.getOverrideConfiguration());
3627 }
3628 }
3629
3630 /**
Winson43d1f262016-06-14 16:05:55 -07003631 * Dumps the activities matching the given {@param name} in the either the focused stack
3632 * or all visible stacks if {@param dumpVisibleStacks} is true.
3633 */
Winson Chung6998bc42017-02-28 17:07:05 -08003634 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3635 boolean dumpFocusedStackOnly) {
3636 if (dumpFocusedStackOnly) {
3637 return mFocusedStack.getDumpActivitiesLocked(name);
3638 } else {
Winson43d1f262016-06-14 16:05:55 -07003639 ArrayList<ActivityRecord> activities = new ArrayList<>();
3640 int numDisplays = mActivityDisplays.size();
3641 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3642 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3644 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003645 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003646 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003647 activities.addAll(stack.getDumpActivitiesLocked(name));
3648 }
3649 }
3650 }
3651 return activities;
Winson43d1f262016-06-14 16:05:55 -07003652 }
Craig Mautner20e72272013-04-01 13:45:53 -07003653 }
3654
Dianne Hackborn390517b2013-05-30 15:03:32 -07003655 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3656 boolean needSep, String prefix) {
3657 if (activity != null) {
3658 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3659 if (needSep) {
3660 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003661 }
3662 pw.print(prefix);
3663 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003664 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003665 }
3666 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003667 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003668 }
3669
Craig Mautner8d341ef2013-03-26 09:03:27 -07003670 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3671 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003672 boolean printed = false;
3673 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003674 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3675 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003676 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003677 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003678 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003679 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003680 final ActivityStack stack = stacks.get(stackNdx);
3681 StringBuilder stackHeader = new StringBuilder(128);
3682 stackHeader.append(" Stack #");
3683 stackHeader.append(stack.mStackId);
3684 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003685 stackHeader.append("\n");
3686 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3687 stackHeader.append("\n");
3688 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003689
3690 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3691 dumpClient, dumpPackage, needSep, stackHeader.toString());
3692 printed |= printedStackHeader;
3693 if (!printedStackHeader) {
3694 // Ensure we always dump the stack header even if there are no activities
3695 pw.println();
3696 pw.println(stackHeader);
3697 }
3698
Craig Mautner4a1cb222013-12-04 16:14:06 -08003699 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3700 !dumpAll, false, dumpPackage, true,
3701 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003702
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 needSep = printed;
3704 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3705 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003706 if (pr) {
3707 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003708 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003709 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003710 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3711 " mResumedActivity: ");
3712 if (pr) {
3713 printed = true;
3714 needSep = false;
3715 }
3716 if (dumpAll) {
3717 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3718 " mLastPausedActivity: ");
3719 if (pr) {
3720 printed = true;
3721 needSep = true;
3722 }
3723 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3724 needSep, " mLastNoHistoryActivity: ");
3725 }
3726 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003727 }
3728 }
3729
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003730 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3731 false, dumpPackage, true, " Activities waiting to finish:", null);
3732 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3733 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003734 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3735 false, !dumpAll, false, dumpPackage, true,
3736 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003737 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3738 false, dumpPackage, true, " Activities waiting to sleep:", null);
3739 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3740 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003741
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003742 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003743 }
3744
Dianne Hackborn390517b2013-05-30 15:03:32 -07003745 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003746 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003747 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003748 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003749 String innerPrefix = null;
3750 String[] args = null;
3751 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003752 for (int i=list.size()-1; i>=0; i--) {
3753 final ActivityRecord r = list.get(i);
3754 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3755 continue;
3756 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003757 if (innerPrefix == null) {
3758 innerPrefix = prefix + " ";
3759 args = new String[0];
3760 }
3761 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003762 final boolean full = !brief && (complete || !r.isInHistory());
3763 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003764 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003765 needNL = false;
3766 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003767 if (header1 != null) {
3768 pw.println(header1);
3769 header1 = null;
3770 }
3771 if (header2 != null) {
3772 pw.println(header2);
3773 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003774 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003775 if (lastTask != r.getTask()) {
3776 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003777 pw.print(prefix);
3778 pw.print(full ? "* " : " ");
3779 pw.println(lastTask);
3780 if (full) {
3781 lastTask.dump(pw, prefix + " ");
3782 } else if (complete) {
3783 // Complete + brief == give a summary. Isn't that obvious?!?
3784 if (lastTask.intent != null) {
3785 pw.print(prefix); pw.print(" ");
3786 pw.println(lastTask.intent.toInsecureStringWithClip());
3787 }
3788 }
3789 }
3790 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3791 pw.print(" #"); pw.print(i); pw.print(": ");
3792 pw.println(r);
3793 if (full) {
3794 r.dump(pw, innerPrefix);
3795 } else if (complete) {
3796 // Complete + brief == give a summary. Isn't that obvious?!?
3797 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3798 if (r.app != null) {
3799 pw.print(innerPrefix); pw.println(r.app);
3800 }
3801 }
3802 if (client && r.app != null && r.app.thread != null) {
3803 // flush anything that is already in the PrintWriter since the thread is going
3804 // to write to the file descriptor directly
3805 pw.flush();
3806 try {
3807 TransferPipe tp = new TransferPipe();
3808 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003809 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003810 // Short timeout, since blocking here can
3811 // deadlock with the application.
3812 tp.go(fd, 2000);
3813 } finally {
3814 tp.kill();
3815 }
3816 } catch (IOException e) {
3817 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3818 } catch (RemoteException e) {
3819 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3820 }
3821 needNL = true;
3822 }
3823 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003824 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003825 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003826
Craig Mautnerf3333272013-04-22 10:55:53 -07003827 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003828 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3829 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003830 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3831 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003832 }
3833
3834 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003835 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003836 }
3837
3838 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003839 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3840 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003841 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3842 }
3843
Craig Mautner05d29032013-05-03 13:40:13 -07003844 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003845 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3846 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3847 }
Craig Mautner05d29032013-05-03 13:40:13 -07003848 }
3849
Craig Mautner0eea92c2013-05-16 13:35:39 -07003850 void removeSleepTimeouts() {
3851 mSleepTimeout = false;
3852 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3853 }
3854
3855 final void scheduleSleepTimeout() {
3856 removeSleepTimeouts();
3857 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3858 }
3859
Craig Mautner4a1cb222013-12-04 16:14:06 -08003860 @Override
3861 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003862 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003863 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3864 }
3865
3866 @Override
3867 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003868 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3870 }
3871
3872 @Override
3873 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003874 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003875 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3876 }
3877
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003878 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003879 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003881 newDisplay = mActivityDisplays.get(displayId) == null;
3882 if (newDisplay) {
3883 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003884 if (activityDisplay.mDisplay == null) {
3885 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3886 return;
3887 }
Craig Mautner4504de52013-12-20 09:06:56 -08003888 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003889 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003890 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003891 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003892 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003893 }
3894
Andrii Kulianca007a62016-11-22 16:33:28 -08003895 /** Check if display with specified id is added to the list. */
3896 boolean isDisplayAdded(int displayId) {
3897 return mActivityDisplays.get(displayId) != null;
3898 }
3899
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003900 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003901 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003902 mService.mContext.getResources().getDimensionPixelSize(
3903 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003904 }
3905
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003906 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003907 if (displayId == DEFAULT_DISPLAY) {
3908 throw new IllegalArgumentException("Can't remove the primary display.");
3909 }
3910
Craig Mautner4a1cb222013-12-04 16:14:06 -08003911 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003912 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3913 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003914 final boolean destroyContentOnRemoval
3915 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003916 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3917 while (!stacks.isEmpty()) {
3918 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003919 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003920 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3921 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003922 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003923 } else {
3924 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3925 // a valid launch stack for all activities. This way the task history from
3926 // external display will be preserved on primary after move.
3927 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003928 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003930 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003931 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003932 }
3933 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 }
3935
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003936 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003938 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3939 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 // TODO: Update the bounds.
3941 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003942 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003943 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003944 }
3945
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003946 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003947 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003949 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003950 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003952 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003953 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003954 info.position = display != null
3955 ? display.mStacks.indexOf(stack)
3956 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957
3958 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3959 final int numTasks = tasks.size();
3960 int[] taskIds = new int[numTasks];
3961 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003962 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003963 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 for (int i = 0; i < numTasks; ++i) {
3965 final TaskRecord task = tasks.get(i);
3966 taskIds[i] = task.taskId;
3967 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3968 : task.realActivity != null ? task.realActivity.flattenToString()
3969 : task.getTopActivity() != null ? task.getTopActivity().packageName
3970 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003971 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003972 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003973 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003974 }
3975 info.taskIds = taskIds;
3976 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003977 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003978 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003979
3980 final ActivityRecord top = stack.topRunningActivityLocked();
3981 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982 return info;
3983 }
3984
3985 StackInfo getStackInfoLocked(int stackId) {
3986 ActivityStack stack = getStack(stackId);
3987 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003988 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 }
3990 return null;
3991 }
3992
3993 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003994 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003995 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3996 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003998 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003999 }
4000 }
4001 return list;
4002 }
4003
Craig Mautner15df08a2015-04-01 12:17:18 -07004004 TaskRecord getLockedTaskLocked() {
4005 final int top = mLockTaskModeTasks.size() - 1;
4006 if (top >= 0) {
4007 return mLockTaskModeTasks.get(top);
4008 }
4009 return null;
4010 }
4011
4012 boolean isLockedTask(TaskRecord task) {
4013 return mLockTaskModeTasks.contains(task);
4014 }
4015
4016 boolean isLastLockedTask(TaskRecord task) {
4017 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4018 }
4019
4020 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004021 if (!mLockTaskModeTasks.remove(task)) {
4022 return;
4023 }
4024 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4025 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004026 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004027 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4028 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004029 final Message lockTaskMsg = Message.obtain();
4030 lockTaskMsg.arg1 = task.userId;
4031 lockTaskMsg.what = LOCK_TASK_END_MSG;
4032 mHandler.sendMessage(lockTaskMsg);
4033 }
4034 }
4035
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004036 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4037 int preferredDisplayId, int actualStackId) {
4038 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004039 false /* forceNonResizable */);
4040 }
4041
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004042 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4043 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4044 final boolean isSecondaryDisplayPreferred =
4045 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4046 || StackId.isDynamicStack(preferredStackId);
4047 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4048 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004049 return;
4050 }
4051
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004052 // Handle incorrect launch/move to secondary display if needed.
4053 final boolean launchOnSecondaryDisplayFailed;
4054 if (isSecondaryDisplayPreferred) {
4055 final int actualDisplayId = task.getStack().mDisplayId;
4056 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4057 // The task landed on an inappropriate display somehow, move it to the default
4058 // display.
4059 // TODO(multi-display): Find proper stack for the task on the default display.
4060 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4061 true /* toTop */);
4062 launchOnSecondaryDisplayFailed = true;
4063 } else {
4064 // The task might have landed on a display different from requested.
4065 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4066 || (preferredDisplayId != INVALID_DISPLAY
4067 && preferredDisplayId != actualDisplayId);
4068 }
4069 } else {
4070 // The task wasn't requested to be on a secondary display.
4071 launchOnSecondaryDisplayFailed = false;
4072 }
4073
Jorim Jaggicd13d332016-04-27 15:40:20 -07004074 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004075 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4076 if (launchOnSecondaryDisplayFailed) {
4077 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4078 // display with config different from global config.
4079 mService.mTaskChangeNotificationController
4080 .notifyActivityLaunchOnSecondaryDisplayFailed();
4081 } else {
4082 // Display a warning toast that we tried to put a non-dockable task in the docked
4083 // stack.
4084 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4085 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004086
Andrii Kulianc27916642016-04-12 17:59:27 -07004087 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4088 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004089 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004090 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004091 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004092 final String packageName = topActivity.appInfo.packageName;
4093 final int reason = isSecondaryDisplayPreferred
4094 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4095 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004096 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004097 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004098 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004099 }
4100
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004101 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004102 if (mLockTaskNotify != null) {
4103 mLockTaskNotify.showToast(mLockTaskModeState);
4104 }
Jason Monka8f569c2014-07-07 12:15:21 -04004105 }
4106
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004107 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4108 if (mLockTaskModeTasks.contains(task)) {
4109 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4110 }
4111 }
4112
Craig Mautner432f64e2015-05-20 14:59:57 -07004113 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4114 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004115 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004116 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004117 final TaskRecord lockedTask = getLockedTaskLocked();
4118 if (lockedTask != null) {
4119 removeLockedTaskLocked(lockedTask);
4120 if (!mLockTaskModeTasks.isEmpty()) {
4121 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004122 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4123 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004124 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004125 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004126 return;
4127 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004128 }
Craig Mautnere0570202015-05-13 13:06:11 -07004129 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4130 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004131 return;
4132 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004133
4134 // Should have already been checked, but do it again.
4135 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004136 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4137 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004138 return;
4139 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004140 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004141 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004142 return;
4143 }
4144
4145 if (mLockTaskModeTasks.isEmpty()) {
4146 // First locktask.
4147 final Message lockTaskMsg = Message.obtain();
4148 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4149 lockTaskMsg.arg1 = task.userId;
4150 lockTaskMsg.what = LOCK_TASK_START_MSG;
4151 lockTaskMsg.arg2 = lockTaskModeState;
4152 mHandler.sendMessage(lockTaskMsg);
4153 }
4154 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004155 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4156 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004157 mLockTaskModeTasks.remove(task);
4158 mLockTaskModeTasks.add(task);
4159
4160 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004161 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004162 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004163
4164 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004165 findTaskToMoveToFrontLocked(task, 0, null, reason,
4166 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004167 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004168 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004169 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004170 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4171 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004172 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004173 }
4174
4175 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004176 return isLockTaskModeViolation(task, false);
4177 }
4178
4179 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4180 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004181 return false;
4182 }
4183 final int lockTaskAuth = task.mLockTaskAuth;
4184 switch (lockTaskAuth) {
4185 case LOCK_TASK_AUTH_DONT_LOCK:
4186 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004187 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004188 case LOCK_TASK_AUTH_LAUNCHABLE:
4189 case LOCK_TASK_AUTH_WHITELISTED:
4190 return false;
4191 case LOCK_TASK_AUTH_PINNABLE:
4192 // Pinnable tasks can't be launched on top of locktask tasks.
4193 return !mLockTaskModeTasks.isEmpty();
4194 default:
4195 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4196 return true;
4197 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004198 }
4199
Craig Mautner15df08a2015-04-01 12:17:18 -07004200 void onLockTaskPackagesUpdatedLocked() {
4201 boolean didSomething = false;
4202 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4203 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004204 final boolean wasWhitelisted =
4205 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4206 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004207 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004208 final boolean isWhitelisted =
4209 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4210 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4211 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004212 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004213 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4214 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004215 removeLockedTaskLocked(lockedTask);
4216 lockedTask.performClearTaskLocked();
4217 didSomething = true;
4218 }
4219 }
4220 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4221 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4222 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4223 final ActivityStack stack = stacks.get(stackNdx);
4224 stack.onLockTaskPackagesUpdatedLocked();
4225 }
4226 }
Craig Mautnere0570202015-05-13 13:06:11 -07004227 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004228 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004229 if (mLockTaskModeTasks.isEmpty() && task != null
4230 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4231 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004232 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4233 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4234 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4235 false);
4236 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004237 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004238 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004239 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004240 }
4241 }
4242
Benjamin Franz43261142015-02-11 15:59:44 +00004243 int getLockTaskModeState() {
4244 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004245 }
4246
Jorim Jaggife89d122015-12-22 16:28:44 +01004247 void activityRelaunchedLocked(IBinder token) {
4248 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004249 if (mService.isSleepingOrShuttingDownLocked()) {
4250 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4251 if (r != null) {
4252 r.setSleeping(true, true);
4253 }
4254 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004255 }
4256
4257 void activityRelaunchingLocked(ActivityRecord r) {
4258 mWindowManager.notifyAppRelaunching(r.appToken);
4259 }
4260
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004261 void logStackState() {
4262 mActivityMetricsLogger.logWindowState();
4263 }
4264
Winson Chung5af42fc2017-03-24 17:11:33 -07004265 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004266 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4267 // end, then ensure that we defer all in between multi-window mode changes
4268 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4269 return;
4270 }
4271
Wale Ogunwale22e25262016-02-01 10:32:02 -08004272 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4273 final ActivityRecord r = task.mActivities.get(i);
4274 if (r.app != null && r.app.thread != null) {
4275 mMultiWindowModeChangedActivities.add(r);
4276 }
4277 }
4278
4279 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4280 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4281 }
4282 }
4283
Winson Chung5af42fc2017-03-24 17:11:33 -07004284 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004285 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004286 if (prevStack == null || prevStack == stack
4287 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4288 return;
4289 }
4290
Winson Chung5af42fc2017-03-24 17:11:33 -07004291 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4292 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004293
Winson Chung5af42fc2017-03-24 17:11:33 -07004294 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4295 boolean immediate) {
4296
4297 if (immediate) {
4298 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4299 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4300 final ActivityRecord r = task.mActivities.get(i);
4301 if (r.app != null && r.app.thread != null) {
4302 r.updatePictureInPictureMode(targetStackBounds);
4303 }
4304 }
4305 } else {
4306 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4307 final ActivityRecord r = task.mActivities.get(i);
4308 if (r.app != null && r.app.thread != null) {
4309 mPipModeChangedActivities.add(r);
4310 }
4311 }
4312 mPipModeChangedTargetStackBounds = targetStackBounds;
4313
4314 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4315 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4316 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004317 }
4318 }
4319
Tony Mak853304c2016-04-18 15:17:41 +01004320 void setDockedStackMinimized(boolean minimized) {
4321 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004322 }
4323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004324 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004325
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004326 public ActivityStackSupervisorHandler(Looper looper) {
4327 super(looper);
4328 }
4329
Winson Chung4dabf232017-01-25 13:25:22 -08004330 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004331 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004332 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4333 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004334 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004335 }
4336
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004337 @Override
4338 public void handleMessage(Message msg) {
4339 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004340 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4341 synchronized (mService) {
4342 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4343 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004344 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004345 }
4346 }
4347 } break;
4348 case REPORT_PIP_MODE_CHANGED_MSG: {
4349 synchronized (mService) {
4350 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4351 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004352 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004353 }
4354 }
4355 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004356 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004357 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4358 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004359 if (mService.mDidDexOpt) {
4360 mService.mDidDexOpt = false;
4361 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4362 nmsg.obj = msg.obj;
4363 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4364 return;
4365 }
4366 // We don't at this point know if the activity is fullscreen,
4367 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004368 activityIdleInternal((ActivityRecord) msg.obj,
4369 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004370 } break;
4371 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004372 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004373 activityIdleInternal((ActivityRecord) msg.obj,
4374 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004375 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004376 case RESUME_TOP_ACTIVITY_MSG: {
4377 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004378 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004379 }
4380 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004381 case SLEEP_TIMEOUT_MSG: {
4382 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004383 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004384 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4385 mSleepTimeout = true;
4386 checkReadyForSleepLocked();
4387 }
4388 }
4389 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004390 case LAUNCH_TIMEOUT_MSG: {
4391 if (mService.mDidDexOpt) {
4392 mService.mDidDexOpt = false;
4393 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4394 return;
4395 }
4396 synchronized (mService) {
4397 if (mLaunchingActivity.isHeld()) {
4398 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4399 if (VALIDATE_WAKE_LOCK_CALLER
4400 && Binder.getCallingUid() != Process.myUid()) {
4401 throw new IllegalStateException("Calling must be system uid");
4402 }
4403 mLaunchingActivity.release();
4404 }
4405 }
4406 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004407 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004408 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409 } break;
4410 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004411 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004412 } break;
4413 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004414 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004415 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004416 case CONTAINER_CALLBACK_VISIBILITY: {
4417 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004418 final IActivityContainerCallback callback = container.mCallback;
4419 if (callback != null) {
4420 try {
4421 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4422 } catch (RemoteException e) {
4423 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004424 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004425 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004426 case LOCK_TASK_START_MSG: {
4427 // When lock task starts, we disable the status bars.
4428 try {
Jason Monk62515be2014-05-21 16:06:19 -04004429 if (mLockTaskNotify == null) {
4430 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004431 }
Jason Monk62515be2014-05-21 16:06:19 -04004432 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004433 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004434 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004435 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004436 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004437 flags = StatusBarManager.DISABLE_MASK
4438 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004439 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004440 flags = StatusBarManager.DISABLE_MASK
4441 & (~StatusBarManager.DISABLE_BACK)
4442 & (~StatusBarManager.DISABLE_HOME)
4443 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004444 }
4445 getStatusBarService().disable(flags, mToken,
4446 mService.mContext.getPackageName());
4447 }
4448 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004449 if (getDevicePolicyManager() != null) {
4450 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004451 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004452 }
justinzhang5286d3f2014-05-12 17:06:01 -04004453 } catch (RemoteException ex) {
4454 throw new RuntimeException(ex);
4455 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004456 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004457 case LOCK_TASK_END_MSG: {
4458 // When lock task ends, we enable the status bars.
4459 try {
Jason Monk62515be2014-05-21 16:06:19 -04004460 if (getStatusBarService() != null) {
4461 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4462 mService.mContext.getPackageName());
4463 }
4464 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004465 if (getDevicePolicyManager() != null) {
4466 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4467 msg.arg1);
4468 }
Jason Monk62515be2014-05-21 16:06:19 -04004469 if (mLockTaskNotify == null) {
4470 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4471 }
4472 mLockTaskNotify.show(false);
4473 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004474 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004475 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004476 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004477 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004478 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004479 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004480 new LockPatternUtils(mService.mContext)
4481 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004482 }
4483 } catch (SettingNotFoundException e) {
4484 // No setting, don't lock.
4485 }
justinzhang5286d3f2014-05-12 17:06:01 -04004486 } catch (RemoteException ex) {
4487 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004488 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004489 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004490 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004491 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004492 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4493 if (mLockTaskNotify == null) {
4494 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4495 }
4496 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4497 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004498 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4499 final ActivityContainer container = (ActivityContainer) msg.obj;
4500 final IActivityContainerCallback callback = container.mCallback;
4501 if (callback != null) {
4502 try {
4503 callback.onAllActivitiesComplete(container.asBinder());
4504 } catch (RemoteException e) {
4505 }
4506 }
4507 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004508 case LAUNCH_TASK_BEHIND_COMPLETE: {
4509 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004510 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004511 if (r != null) {
4512 handleLaunchTaskBehindCompleteLocked(r);
4513 }
4514 }
4515 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004516
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004517 }
4518 }
4519 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004520
Ying Wangb081a592014-04-22 15:20:16 -07004521 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004522 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4523 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004524 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004525 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004526 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004527 ActivityRecord mParentActivity = null;
4528 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004529
Craig Mautnere3a00d72014-04-16 08:31:19 -07004530 boolean mVisible = true;
4531
Craig Mautner4a1cb222013-12-04 16:14:06 -08004532 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004533 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004535 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4536 final static int CONTAINER_STATE_NO_SURFACE = 1;
4537 final static int CONTAINER_STATE_FINISHING = 2;
4538 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4539
Wale Ogunwale1666e312016-12-16 11:27:18 -08004540 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004541 synchronized (mService) {
4542 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004543 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004544 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004545
4546 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004547 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004548 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004549 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004550
Bryce Leeaf691c02017-03-20 14:20:22 -07004551 protected void createStack(int stackId, boolean onTop) {
4552 switch (stackId) {
4553 case PINNED_STACK_ID:
4554 new PinnedActivityStack(this, mRecentTasks, onTop);
4555 break;
4556 default:
4557 new ActivityStack(this, mRecentTasks, onTop);
4558 break;
4559 }
4560 }
4561
Andrii Kulian839def92016-11-02 10:58:58 -07004562 /**
4563 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004564 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004565 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004566 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004567 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004568 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004569 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004570 if (mActivityDisplay != null) {
4571 throw new IllegalStateException("ActivityContainer is already attached, " +
4572 "displayId=" + mActivityDisplay.mDisplayId);
4573 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004574 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004575 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004576 }
4577
4578 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004579 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004580 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004581 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4582 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004583 return;
4584 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004585 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004586 }
4587 }
4588
4589 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004590 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004591 synchronized (mService) {
4592 if (mActivityDisplay != null) {
4593 return mActivityDisplay.mDisplayId;
4594 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004595 }
4596 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004597 }
4598
Jeff Brownca9bc702014-02-11 14:32:56 -08004599 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004600 public int getStackId() {
4601 synchronized (mService) {
4602 return mStackId;
4603 }
4604 }
4605
4606 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004607 public boolean injectEvent(InputEvent event) {
4608 final long origId = Binder.clearCallingIdentity();
4609 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004610 synchronized (mService) {
4611 if (mActivityDisplay != null) {
4612 return mInputManagerInternal.injectInputEvent(event,
4613 mActivityDisplay.mDisplayId,
4614 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4615 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004616 }
4617 return false;
4618 } finally {
4619 Binder.restoreCallingIdentity(origId);
4620 }
4621 }
4622
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004623 @Override
4624 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004625 synchronized (mService) {
4626 if (mContainerState == CONTAINER_STATE_FINISHING) {
4627 return;
4628 }
4629 mContainerState = CONTAINER_STATE_FINISHING;
4630
Craig Mautnerd163e752014-06-13 17:18:47 -07004631 long origId = Binder.clearCallingIdentity();
4632 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004633 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004634 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004635 } finally {
4636 Binder.restoreCallingIdentity(origId);
4637 }
4638 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004639 }
4640
Andrii Kulian03c403d2016-11-11 11:14:12 -08004641 /**
4642 * Remove the stack completely. Must be called only when there are no tasks left in it,
4643 * as this method does not finish running activities.
4644 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004645 void removeLocked() {
4646 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004647 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004648 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004649 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004650 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004651 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004652 }
4653
Andrii Kulian839def92016-11-02 10:58:58 -07004654 /**
4655 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4656 * completely or re-parented.
4657 */
4658 private void removeFromDisplayLocked() {
4659 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4660 + " current displayId=" + mActivityDisplay.mDisplayId);
4661
Wale Ogunwale1666e312016-12-16 11:27:18 -08004662 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004663 mActivityDisplay = null;
4664 }
4665
4666 /**
4667 * Move the stack to specified display.
4668 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004669 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004670 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004671 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004672 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4673 + mActivityDisplay + " to display=" + activityDisplay
4674 + " Callers=" + Debug.getCallers(2));
4675
4676 removeFromDisplayLocked();
4677
4678 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004679 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004680 }
4681
Craig Mautner4a1cb222013-12-04 16:14:06 -08004682 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004683 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004684 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004685 }
4686
4687 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004688 public final int startActivityIntentSender(IIntentSender intentSender)
4689 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004690 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4691
4692 if (!(intentSender instanceof PendingIntentRecord)) {
4693 throw new IllegalArgumentException("Bad PendingIntent object");
4694 }
4695
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004696 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004697 Binder.getCallingUid(), mCurrentUser, false,
4698 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004699
4700 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4701 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4702 pendingIntent.key.requestResolvedType);
4703
4704 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4705 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4706 }
4707
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004708 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004709 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004710 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004711 throw new SecurityException(
4712 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4713 }
4714 }
4715
Craig Mautnerdf88d732014-01-27 09:21:32 -08004716 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004717 public IBinder asBinder() {
4718 return this;
4719 }
4720
Craig Mautner4504de52013-12-20 09:06:56 -08004721 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004722 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004723 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004724 }
4725
Craig Mautner4a1cb222013-12-04 16:14:06 -08004726 ActivityStackSupervisor getOuter() {
4727 return ActivityStackSupervisor.this;
4728 }
4729
Craig Mautnerd163e752014-06-13 17:18:47 -07004730 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004731 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004732 }
4733
Craig Mautner6985bad2014-04-21 15:22:06 -07004734 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004735 void setVisible(boolean visible) {
4736 if (mVisible != visible) {
4737 mVisible = visible;
4738 if (mCallback != null) {
4739 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4740 0 /* unused */, this).sendToTarget();
4741 }
4742 }
4743 }
4744
Craig Mautner6985bad2014-04-21 15:22:06 -07004745 void setDrawn() {
4746 }
4747
Craig Mautner1b4bf852014-05-26 15:06:32 -07004748 // You can always start a new task on a regular ActivityStack.
4749 boolean isEligibleForNewTasks() {
4750 return true;
4751 }
4752
Craig Mautnerd163e752014-06-13 17:18:47 -07004753 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004754 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004755 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004756 }
4757
Craig Mautner34b73df2014-01-12 21:11:08 -08004758 @Override
4759 public String toString() {
4760 return mIdString + (mActivityDisplay == null ? "N" : "A");
4761 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004762 }
4763
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004764 private class VirtualActivityContainer extends ActivityContainer {
4765 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004766 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004767
4768 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004769 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4770 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004771 mParentActivity = parent;
4772 mCallback = callback;
4773 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004774 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004775 }
4776
4777 @Override
4778 public void setSurface(Surface surface, int width, int height, int density) {
4779 super.setSurface(surface, width, height, density);
4780
4781 synchronized (mService) {
4782 final long origId = Binder.clearCallingIdentity();
4783 try {
4784 setSurfaceLocked(surface, width, height, density);
4785 } finally {
4786 Binder.restoreCallingIdentity(origId);
4787 }
4788 }
4789 }
4790
4791 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4792 if (mContainerState == CONTAINER_STATE_FINISHING) {
4793 return;
4794 }
4795 VirtualActivityDisplay virtualActivityDisplay =
4796 (VirtualActivityDisplay) mActivityDisplay;
4797 if (virtualActivityDisplay == null) {
4798 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004799 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004800 mActivityDisplay = virtualActivityDisplay;
4801 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004802 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004803 }
4804
4805 if (mSurface != null) {
4806 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004807 }
4808
Craig Mautner6985bad2014-04-21 15:22:06 -07004809 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004810 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004811 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004812 } else {
4813 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004814 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004815 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004816 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004817 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004818 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004819
Craig Mautnerd163e752014-06-13 17:18:47 -07004820 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004821
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004822 if (DEBUG_STACK) Slog.d(TAG_STACK,
4823 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004824 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004825
Craig Mautner6985bad2014-04-21 15:22:06 -07004826 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004827 boolean isAttachedLocked() {
4828 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004829 }
4830
4831 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004832 void setDrawn() {
4833 synchronized (mService) {
4834 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004835 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004836 }
4837 }
4838
Craig Mautner1b4bf852014-05-26 15:06:32 -07004839 // Never start a new task on an ActivityView if it isn't explicitly specified.
4840 @Override
4841 boolean isEligibleForNewTasks() {
4842 return false;
4843 }
4844
Craig Mautnerd163e752014-06-13 17:18:47 -07004845 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004846 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004847 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4848 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4849 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4850 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4851 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004852 }
4853 }
4854
Craig Mautner4a1cb222013-12-04 16:14:06 -08004855 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4856 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004857 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004858 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004859 int mDisplayId;
4860 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004861
Craig Mautner4a1cb222013-12-04 16:14:06 -08004862 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4863 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004864 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004865
Jose Lima4b6c6692014-08-12 17:41:12 -07004866 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004867
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004868 /** Array of all UIDs that are present on the display. */
4869 private IntArray mDisplayAccessUIDs = new IntArray();
4870
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004871 ActivityDisplay() {
4872 }
Craig Mautner4504de52013-12-20 09:06:56 -08004873
Craig Mautner1a70a162014-09-13 12:09:31 -07004874 // After instantiation, check that mDisplay is not null before using this. The alternative
4875 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004876 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004877 final Display display = mDisplayManager.getDisplay(displayId);
4878 if (display == null) {
4879 return;
4880 }
4881 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004882 }
4883
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004884 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004885 mDisplay = display;
4886 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004887 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004888
Winson Chung7c3ede32017-03-14 14:18:34 -07004889 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004890 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004891 + " to displayId=" + mDisplayId + " position=" + position);
4892 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004893 }
4894
Wale Ogunwale1666e312016-12-16 11:27:18 -08004895 void detachStack(ActivityStack stack) {
4896 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004897 + " from displayId=" + mDisplayId);
4898 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004899 }
4900
Jose Lima4b6c6692014-08-12 17:41:12 -07004901 void setVisibleBehindActivity(ActivityRecord r) {
4902 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004903 }
4904
Jose Lima4b6c6692014-08-12 17:41:12 -07004905 boolean hasVisibleBehindActivity() {
4906 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004907 }
4908
Craig Mautner34b73df2014-01-12 21:11:08 -08004909 @Override
4910 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004911 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4912 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004913
4914 @Override
4915 protected int getChildCount() {
4916 return mStacks.size();
4917 }
4918
4919 @Override
4920 protected ConfigurationContainer getChildAt(int index) {
4921 return mStacks.get(index);
4922 }
4923
4924 @Override
4925 protected ConfigurationContainer getParent() {
4926 return ActivityStackSupervisor.this;
4927 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004928
4929 boolean isPrivate() {
4930 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4931 }
4932
4933 boolean isUidPresent(int uid) {
4934 for (ActivityStack stack : mStacks) {
4935 if (stack.isUidPresent(uid)) {
4936 return true;
4937 }
4938 }
4939 return false;
4940 }
4941
4942 /** Update and get all UIDs that are present on the display and have access to it. */
4943 private IntArray getPresentUIDs() {
4944 mDisplayAccessUIDs.clear();
4945 for (ActivityStack stack : mStacks) {
4946 stack.getPresentUIDs(mDisplayAccessUIDs);
4947 }
4948 return mDisplayAccessUIDs;
4949 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004950
4951 boolean shouldDestroyContentOnRemove() {
4952 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4953 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004954 }
4955
4956 class VirtualActivityDisplay extends ActivityDisplay {
4957 VirtualDisplay mVirtualDisplay;
4958
Craig Mautner6985bad2014-04-21 15:22:06 -07004959 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004960 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004961 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4962 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4963 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4964 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004965
4966 init(mVirtualDisplay.getDisplay());
4967
Bryce Leeaf3a4002017-03-20 10:37:12 -07004968 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004969 }
4970
4971 void setSurface(Surface surface) {
4972 if (mVirtualDisplay != null) {
4973 mVirtualDisplay.setSurface(surface);
4974 }
4975 }
4976
4977 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004978 void detachStack(ActivityStack stack) {
4979 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004980 if (mVirtualDisplay != null) {
4981 mVirtualDisplay.release();
4982 mVirtualDisplay = null;
4983 }
4984 }
4985
4986 @Override
4987 public String toString() {
4988 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004989 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004990 }
Jose Lima58e66d62014-05-27 20:00:27 -07004991
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004992 ActivityStack findStackBehind(ActivityStack stack) {
4993 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004994 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004995 if (display == null) {
4996 return null;
4997 }
4998 final ArrayList<ActivityStack> stacks = display.mStacks;
4999 for (int i = stacks.size() - 1; i >= 0; i--) {
5000 if (stacks.get(i) == stack && i > 0) {
5001 return stacks.get(i - 1);
5002 }
5003 }
5004 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5005 + " in=" + stacks);
5006 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005007
Jorim Jaggic69bd222016-03-15 14:38:37 +01005008 /**
5009 * Puts a task into resizing mode during the next app transition.
5010 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005011 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005012 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005013 private void setResizingDuringAnimation(TaskRecord task) {
5014 mResizingTasksDuringAnimation.add(task.taskId);
5015 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005016 }
5017
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005018 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5019 final TaskRecord task;
5020 final int callingUid;
5021 final String callingPackage;
5022 final Intent intent;
5023 final int userId;
5024 final ActivityOptions activityOptions = (bOptions != null)
5025 ? new ActivityOptions(bOptions) : null;
5026 final int launchStackId = (activityOptions != null)
5027 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005028 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005029 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005030 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005031 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005032
5033 if (launchStackId == DOCKED_STACK_ID) {
5034 mWindowManager.setDockedStackCreateState(
5035 activityOptions.getDockCreateMode(), null /* initialBounds */);
5036
5037 // Defer updating the stack in which recents is until the app transition is done, to
5038 // not run into issues where we still need to draw the task in recents but the
5039 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08005040 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005041 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
5042 }
5043
Winson Chung7900bee2017-03-10 08:59:25 -08005044 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5045 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005046 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08005047 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005048 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005049 throw new IllegalArgumentException(
5050 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5051 }
5052
Jorim Jaggi6afd1562016-06-01 18:57:54 -07005053 // Since we don't have an actual source record here, we assume that the currently focused
5054 // activity was the source.
5055 final ActivityStack focusedStack = getFocusedStack();
5056 final ActivityRecord sourceRecord =
5057 focusedStack != null ? focusedStack.topActivity() : null;
5058
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005059 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005060 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08005061 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5062 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005063 }
5064 }
5065
5066 // If the user must confirm credentials (e.g. when first launching a work app and the
5067 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5068 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5069 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07005070 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07005071 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005072 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07005073 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5074 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005075
5076 // If we are launching the task in the docked stack, put it into resizing mode so
5077 // the window renders full-screen with the background filling the void. Also only
5078 // call this at the end to make sure that tasks exists on the window manager side.
5079 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005080 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005081 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07005082
Bryce Leeaa5e8c32017-03-01 16:01:06 -08005083 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07005084 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07005085 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07005086 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005087 return ActivityManager.START_TASK_TO_FRONT;
5088 }
5089 callingUid = task.mCallingUid;
5090 callingPackage = task.mCallingPackage;
5091 intent = task.intent;
5092 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5093 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005094 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
5095 null, null, 0, 0, bOptions, userId, null, task);
5096 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005097 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005098 }
5099 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005100 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005101
5102 /**
5103 * @return a list of activities which are the top ones in each visible stack. The first
5104 * entry will be the focused activity.
5105 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005106 List<IBinder> getTopVisibleActivities() {
5107 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5108 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005109 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005110 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5111 // Traverse all stacks on a display.
5112 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5113 final ActivityStack stack = display.mStacks.get(j);
5114 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005115 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005116 == ActivityStack.STACK_VISIBLE) {
5117 final ActivityRecord top = stack.topActivity();
5118 if (top != null) {
5119 if (stack == mFocusedStack) {
5120 topActivityTokens.add(0, top.appToken);
5121 } else {
5122 topActivityTokens.add(top.appToken);
5123 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005124 }
5125 }
5126 }
5127 }
5128 return topActivityTokens;
5129 }
Bryce Lee4a194382017-04-04 14:32:48 -07005130
5131 /**
5132 * Internal container to store a match qualifier alongside a WaitResult.
5133 */
5134 static class WaitInfo {
5135 private final ComponentName mTargetComponent;
5136 private final WaitResult mResult;
5137
5138 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5139 this.mTargetComponent = targetComponent;
5140 this.mResult = result;
5141 }
5142
5143 public boolean matches(ActivityRecord record) {
5144 return mTargetComponent == null ||
5145 (TextUtils.equals(mTargetComponent.getPackageName(), record.info.packageName)
5146 && TextUtils.equals(mTargetComponent.getClassName(), record.info.name));
5147 }
5148
5149 public WaitResult getResult() {
5150 return mResult;
5151 }
5152
5153 public ComponentName getComponent() {
5154 return mTargetComponent;
5155 }
5156
5157 public void dump(PrintWriter pw, String prefix) {
5158 pw.println(prefix + "WaitInfo:");
5159 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5160 pw.println(prefix + " mResult=");
5161 mResult.dump(pw, prefix);
5162 }
5163 }
Craig Mautner27084302013-03-25 08:05:25 -07005164}