blob: e828d38ef249f8cd4c1a8716aadac7019bcc31b7 [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;
Jeff Hao1b012d32014-08-20 10:35:34 -0700113import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700114import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400115import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700116import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400117import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700121import android.content.Intent;
122import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700124import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.pm.PackageManager;
126import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100127import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800129import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130import android.hardware.display.DisplayManager;
131import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800132import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800133import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800134import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800135import android.hardware.input.InputManager;
136import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100138import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700139import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700142import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700143import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700145import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700146import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700147import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400148import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700149import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700150import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700151import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700152import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100153import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700154import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700155import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400156import android.provider.Settings;
157import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700158import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700159import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700160import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700161import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800162import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700163import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700164import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800166import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800167import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800168import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800169import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800170
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800171import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800172import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700173import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400174import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700175import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400176import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800177import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700178import com.android.server.am.ActivityStack.ActivityState;
Winson Chung19953ca2017-04-11 11:19:23 -0700179import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700180import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700181
Craig Mautner8d341ef2013-03-26 09:03:27 -0700182import java.io.FileDescriptor;
183import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700184import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800185import java.lang.annotation.Retention;
186import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700187import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700188import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700189import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700190import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700191
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800192public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700195 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700197 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700203 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800204 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800206
Craig Mautnerf3333272013-04-22 10:55:53 -0700207 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700209
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700212
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700214 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700215
Craig Mautner05d29032013-05-03 13:40:13 -0700216 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
217 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
218 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700219 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700220 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800221 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
222 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
223 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700224 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400225 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
226 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700227 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700228 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700229 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800230 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
231 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700234
Jason Monk62515be2014-05-21 16:06:19 -0400235 private static final String LOCK_TASK_TAG = "Lock-to-App";
236
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237 // Used to indicate if an object (e.g. stack) that we are trying to get
238 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800239 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700241 // Used to indicate that windows of activities should be preserved during the resize.
242 static final boolean PRESERVE_WINDOWS = true;
243
Wale Ogunwale040b4702015-08-06 18:10:50 -0700244 // Used to indicate if an object (e.g. task) should be moved/created
245 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700246 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700247
248 // Used to indicate that an objects (e.g. task) removal from its container
249 // (e.g. stack) is due to it moving to another container.
250 static final boolean MOVING = true;
251
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700252 // Force the focus to change to the stack we are moving a task to..
253 static final boolean FORCE_FOCUS = true;
254
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700255 // Don't execute any calls to resume.
256 static final boolean DEFER_RESUME = true;
257
Winson Chung010927a2016-12-15 16:12:35 -0800258 // Used to indicate that a task is removed it should also be removed from recents.
259 static final boolean REMOVE_FROM_RECENTS = true;
260
Winson Chung6954fc92017-03-24 16:22:12 -0700261 // Used to indicate that pausing an activity should occur immediately without waiting for
262 // the activity callback indicating that it has completed pausing
263 static final boolean PAUSE_IMMEDIATELY = true;
264
Winson Chung7900bee2017-03-10 08:59:25 -0800265 /**
266 * The modes which affect which tasks are returned when calling
267 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
268 */
269 @Retention(RetentionPolicy.SOURCE)
270 @IntDef({
271 MATCH_TASK_IN_STACKS_ONLY,
272 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
273 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
274 })
275 public @interface AnyTaskForIdMatchTaskMode {}
276 // Match only tasks in the current stacks
277 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
278 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
279 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
280 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
281 // provided stack id
282 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
283
Svetoslav7008b512015-06-24 18:47:07 -0700284 // Activity actions an app cannot start if it uses a permission which is not granted.
285 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
286 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700287
Svetoslav7008b512015-06-24 18:47:07 -0700288 static {
289 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
290 Manifest.permission.CAMERA);
291 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
292 Manifest.permission.CAMERA);
293 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
294 Manifest.permission.CALL_PHONE);
295 }
296
Svet Ganov99b60432015-06-27 13:15:22 -0700297 /** Action restriction: launching the activity is not restricted. */
298 private static final int ACTIVITY_RESTRICTION_NONE = 0;
299 /** Action restriction: launching the activity is restricted by a permission. */
300 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
301 /** Action restriction: launching the activity is restricted by an app op. */
302 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700303
justinzhang5286d3f2014-05-12 17:06:01 -0400304 /** Status Bar Service **/
305 private IBinder mToken = new Binder();
306 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400307 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400308
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700309 // For debugging to make sure the caller when acquiring/releasing our
310 // wake lock is the system process.
311 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800312 /** The number of distinct task ids that can be assigned to the tasks of a single user */
313 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700314
Craig Mautner27084302013-03-25 08:05:25 -0700315 final ActivityManagerService mService;
316
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800317 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800318
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700319 final ActivityStackSupervisorHandler mHandler;
320
321 /** Short cut */
322 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700325 /** Counter for next free stack ID to use for dynamic activity stacks. */
326 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700327
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800328 /**
329 * Maps the task identifier that activities are currently being started in to the userId of the
330 * task. Each time a new task is created, the entry for the userId of the task is incremented
331 */
332 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700333
Craig Mautner2420ead2013-04-01 17:13:20 -0700334 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800335 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700336
Craig Mautnere0a38842013-12-16 16:14:02 -0800337 /** The stack containing the launcher app. Assumed to always be attached to
338 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800339 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700340
Craig Mautnere0a38842013-12-16 16:14:02 -0800341 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800342 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700343
Craig Mautner4a1cb222013-12-04 16:14:06 -0800344 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
345 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800346 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700348
349 /** List of activities that are waiting for a new activity to become visible before completing
350 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700351 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
352 // mStoppingActivities when something else comes up.
353 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700354
Bryce Lee4a194382017-04-04 14:32:48 -0700355 /** List of processes waiting to find out when a specific activity becomes visible. */
356 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700357
358 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700359 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700360
Craig Mautnerde4ef022013-04-07 19:01:33 -0700361 /** List of activities that are ready to be stopped, but waiting for the next activity to
362 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800363 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700364
Craig Mautnerf3333272013-04-22 10:55:53 -0700365 /** List of activities that are ready to be finished, but waiting for the previous activity to
366 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800367 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700368
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800370 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700371
Wale Ogunwale22e25262016-02-01 10:32:02 -0800372 /** List of activities whose multi-window mode changed that we need to report to the
373 * application */
374 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
375
376 /** List of activities whose picture-in-picture mode changed that we need to report to the
377 * application */
378 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
379
Winson Chung5af42fc2017-03-24 17:11:33 -0700380 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
381 * the application */
382 Rect mPipModeChangedTargetStackBounds;
383
Craig Mautnerf3333272013-04-22 10:55:53 -0700384 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700385 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700386
Craig Mautnerde4ef022013-04-07 19:01:33 -0700387 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
388 * is being brought in front of us. */
389 boolean mUserLeaving = false;
390
Craig Mautner0eea92c2013-05-16 13:35:39 -0700391 /** Set when we have taken too long waiting to go to sleep. */
392 boolean mSleepTimeout = false;
393
394 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700395 * We don't want to allow the device to go to sleep while in the process
396 * of launching an activity. This is primarily to allow alarm intent
397 * receivers to launch an activity and get that to run before the device
398 * goes back to sleep.
399 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700400 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700401
402 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700403 * Set when the system is going to sleep, until we have
404 * successfully paused the current activity and released our wake lock.
405 * At that point the system is allowed to actually sleep.
406 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700407 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700408
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700409 /** Stack id of the front stack when user switched, indexed by userId. */
410 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700411
Craig Mautner4504de52013-12-20 09:06:56 -0800412 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800414 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415
Bryce Leeaf3a4002017-03-20 10:37:12 -0700416 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700418 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800419
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800420 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
421
422 private DisplayManagerInternal mDisplayManagerInternal;
423 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800424
Craig Mautner15df08a2015-04-01 12:17:18 -0700425 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
426 * may be finished until there is only one entry left. If this is empty the system is not
427 * in lockTask mode. */
428 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000429 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700430 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
431 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000432 */
433 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400434 /**
435 * Notifies the user when entering/exiting lock-task.
436 */
437 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800438
Wale Ogunwaled046a012015-12-24 13:05:59 -0800439 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800440 boolean inResumeTopActivity;
441
Andrii Kulian1e32e022016-09-16 15:29:34 -0700442 /**
443 * Temporary rect used during docked stack resize calculation so we don't need to create a new
444 * object each time.
445 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700446 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700447
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700448 // The default minimal size that will be used if the activity doesn't specify its minimal size.
449 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700450 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700451
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700452 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
453 private boolean mTaskLayersChanged = true;
454
Jorim Jaggi275561a2016-02-23 10:11:02 -0500455 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800456
Andrii Kulian1779e612016-10-12 21:58:25 -0700457 @Override
458 protected int getChildCount() {
459 return mActivityDisplays.size();
460 }
461
462 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700463 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700464 return mActivityDisplays.valueAt(index);
465 }
466
467 @Override
468 protected ConfigurationContainer getParent() {
469 return null;
470 }
471
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700472 Configuration getDisplayOverrideConfiguration(int displayId) {
473 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
474 if (activityDisplay == null) {
475 throw new IllegalArgumentException("No display found with id: " + displayId);
476 }
477
478 return activityDisplay.getOverrideConfiguration();
479 }
480
481 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
482 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
483 if (activityDisplay == null) {
484 throw new IllegalArgumentException("No display found with id: " + displayId);
485 }
486
487 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
488 }
489
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700490 /** Check if placing task or activity on specified display is allowed. */
491 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
492 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
493 && (resizeable || displayConfigMatchesGlobal(displayId)));
494 }
495
496 /**
497 * Check if configuration of specified display matches current global config.
498 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
499 * the same config as on the default display.
500 * @param displayId Id of the display to check.
501 * @return {@code true} if configuration matches.
502 */
503 private boolean displayConfigMatchesGlobal(int displayId) {
504 if (displayId == DEFAULT_DISPLAY) {
505 return true;
506 }
507 if (displayId == INVALID_DISPLAY) {
508 return false;
509 }
510 final ActivityDisplay targetDisplay = mActivityDisplays.get(displayId);
511 if (targetDisplay == null) {
512 throw new IllegalArgumentException("No display found with id: " + displayId);
513 }
514 return getConfiguration().equals(targetDisplay.getConfiguration());
515 }
516
Wale Ogunwale39381972015-12-17 17:15:29 -0800517 static class FindTaskResult {
518 ActivityRecord r;
519 boolean matchedByRootAffinity;
520 }
521 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
522
Craig Mautneree36c772014-07-16 14:56:05 -0700523 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800524 * Temp storage for display ids sorted in focus order.
525 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
526 * it's more efficient, as the number of displays is usually small.
527 */
528 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
529
530 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100531 * Used to keep track whether app visibilities got changed since the last pause. Useful to
532 * determine whether to invoke the task stack change listener after pausing.
533 */
534 boolean mAppVisibilitiesChangedSinceLastPause;
535
536 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100537 * Set of tasks that are in resizing mode during an app transition to fill the "void".
538 */
539 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
540
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700541
542 /**
543 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
544 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
545 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
546 * like the docked stack going empty.
547 */
548 private boolean mAllowDockedStackResize = true;
549
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100550 /**
Tony Mak853304c2016-04-18 15:17:41 +0100551 * Is dock currently minimized.
552 */
553 boolean mIsDockMinimized;
554
Jorim Jaggife762342016-10-13 14:33:27 +0200555 final KeyguardController mKeyguardController;
556
Tony Mak853304c2016-04-18 15:17:41 +0100557 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700558 * Description of a request to start a new activity, which has been held
559 * due to app switches being disabled.
560 */
561 static class PendingActivityLaunch {
562 final ActivityRecord r;
563 final ActivityRecord sourceRecord;
564 final int startFlags;
565 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700566 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700567
568 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700569 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700570 r = _r;
571 sourceRecord = _sourceRecord;
572 startFlags = _startFlags;
573 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700574 callerApp = _callerApp;
575 }
576
577 void sendErrorResult(String message) {
578 try {
579 if (callerApp.thread != null) {
580 callerApp.thread.scheduleCrash(message);
581 }
582 } catch (RemoteException e) {
583 Slog.e(TAG, "Exception scheduling crash of failed "
584 + "activity launcher sourceRecord=" + sourceRecord, e);
585 }
Craig Mautneree36c772014-07-16 14:56:05 -0700586 }
587 }
588
Bryce Leeaf691c02017-03-20 14:20:22 -0700589 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700590 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700591 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800592 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200593 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700594 }
595
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800596 void setRecentTasks(RecentTasks recentTasks) {
597 mRecentTasks = recentTasks;
598 }
599
Jeff Brown2c43c332014-06-12 22:38:59 -0700600 /**
601 * At the time when the constructor runs, the power manager has not yet been
602 * initialized. So we initialize our wakelocks afterwards.
603 */
604 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700606 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700607 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700608 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700609 }
610
justinzhang5286d3f2014-05-12 17:06:01 -0400611 // This function returns a IStatusBarService. The value is from ServiceManager.
612 // getService and is cached.
613 private IStatusBarService getStatusBarService() {
614 synchronized (mService) {
615 if (mStatusBarService == null) {
616 mStatusBarService = IStatusBarService.Stub.asInterface(
617 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
618 if (mStatusBarService == null) {
619 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
620 }
621 }
622 return mStatusBarService;
623 }
624 }
625
Jason Monk35c62a42014-06-17 10:24:47 -0400626 private IDevicePolicyManager getDevicePolicyManager() {
627 synchronized (mService) {
628 if (mDevicePolicyManager == null) {
629 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
630 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
631 if (mDevicePolicyManager == null) {
632 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
633 }
634 }
635 return mDevicePolicyManager;
636 }
637 }
638
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700639 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 synchronized (mService) {
641 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200642 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643
644 mDisplayManager =
645 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
646 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800647 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648
649 Display[] displays = mDisplayManager.getDisplays();
650 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
651 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800652 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700653 if (activityDisplay.mDisplay == null) {
654 throw new IllegalStateException("Default Display does not exist");
655 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800656 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700657 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800658 }
659
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800660 mHomeStack = mFocusedStack = mLastFocusedStack =
661 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800662
663 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 }
Craig Mautner27084302013-03-25 08:05:25 -0700665 }
666
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700667 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700669 }
670
Craig Mautnerde4ef022013-04-07 19:01:33 -0700671 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700673 }
674
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700675 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700676 if (stack == null) {
677 return false;
678 }
679
Craig Mautnerdf88d732014-01-27 09:21:32 -0800680 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
681 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700682 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800683 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800684 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700685 }
686
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800687 /** The top most stack on its display. */
688 boolean isFrontStackOnDisplay(ActivityStack stack) {
689 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
690 }
691
692 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700693 if (stack == null) {
694 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700696
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700697 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
698 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700699 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800700 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800701 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700702 }
703
Wale Ogunwaled046a012015-12-24 13:05:59 -0800704 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800705 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
706 if (!focusCandidate.isFocusable()) {
707 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800708 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800709 }
710
711 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800712 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800713 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800714
Wale Ogunwaled046a012015-12-24 13:05:59 -0800715 EventLogTags.writeAmFocusedStack(
716 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
717 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
718 }
719
720 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800721 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800722 if (r != null && r.idle) {
723 checkFinishBootingLocked();
724 }
725 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700726 }
727
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700728 void moveHomeStackToFront(String reason) {
729 mHomeStack.moveToFront(reason);
730 }
731
Matthew Ng330757d2017-02-28 14:19:17 -0800732 void moveRecentsStackToFront(String reason) {
733 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
734 if (recentsStack != null) {
735 recentsStack.moveToFront(reason);
736 }
737 }
738
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700739 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800740 boolean moveHomeStackTaskToTop(String reason) {
741 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700742
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700743 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700744 if (top == null) {
745 return false;
746 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700747 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700748 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700749 }
750
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800751 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700752 if (!mService.mBooting && !mService.mBooted) {
753 // Not ready yet!
754 return false;
755 }
756
Craig Mautner84984fa2014-06-19 11:19:20 -0700757 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700758 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700759 }
760
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800761 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700762 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800763 final String myReason = reason + " resumeHomeStackTask";
764
Mark Lua56ea122015-10-08 13:31:01 +0800765 // Only resume home activity if isn't finishing.
766 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700767 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800768 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700769 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800770 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700771 }
772
Craig Mautner8d341ef2013-03-26 09:03:27 -0700773 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800774 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
775 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700776 }
777
778 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700779 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700780 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800781 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700782 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800783 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
784 * valid if the matchMode is
785 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700786 */
Winson Chung7900bee2017-03-10 08:59:25 -0800787 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
788 // If there is a stack id set, ensure that we are attempting to actually restore a task
789 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
790 stackId != INVALID_STACK_ID) {
791 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
792 }
793
Craig Mautnere0a38842013-12-16 16:14:02 -0800794 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800795 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800796 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
798 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700799 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 if (task != null) {
801 return task;
802 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700803 }
804 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800805
Winson Chung7900bee2017-03-10 08:59:25 -0800806 // If we are matching stack tasks only, return now
807 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800808 return null;
809 }
810
Winson Chung7900bee2017-03-10 08:59:25 -0800811 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
812 // the task from recents
813 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700814 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
815
816 if (task == null) {
817 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800818 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
819 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700820
821 return null;
822 }
823
824 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700825 return task;
826 }
827
Winson Chung7900bee2017-03-10 08:59:25 -0800828 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700829 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700830 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
831 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800832 return null;
833 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700834 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800835 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700836 }
837
Craig Mautner6170f732013-04-02 13:05:23 -0700838 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800839 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800841 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
843 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
844 if (r != null) {
845 return r;
846 }
Craig Mautner6170f732013-04-02 13:05:23 -0700847 }
848 }
849 return null;
850 }
851
Tony Mak853304c2016-04-18 15:17:41 +0100852 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000853 * Detects whether we should show a lock screen in front of this task for a locked user.
854 * <p>
855 * We'll do this if either of the following holds:
856 * <ul>
857 * <li>The top activity explicitly belongs to {@param userId}.</li>
858 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
859 * </ul>
860 *
861 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100862 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000863 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
864 // To handle the case that work app is in the task but just is not the top one.
865 final ActivityRecord activityRecord = task.getTopActivity();
866 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
867
868 return (activityRecord != null && activityRecord.userId == userId)
869 || (resultTo != null && resultTo.userId == userId);
870 }
871
872 /**
873 * Find all visible task stacks containing {@param userId} and intercept them with an activity
874 * to block out the contents and possibly start a credential-confirming intent.
875 *
876 * @param userId user handle for the locked managed profile.
877 */
878 void lockAllProfileTasks(@UserIdInt int userId) {
879 mWindowManager.deferSurfaceLayout();
880 try {
881 final List<ActivityStack> stacks = getStacks();
882 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
883 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
884 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
885 final TaskRecord task = tasks.get(taskNdx);
886
887 // Check the task for a top activity belonging to userId, or returning a result
888 // to an activity belonging to userId. Example case: a document picker for
889 // personal files, opened by a work app, should still get locked.
890 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000891 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
892 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000893 }
Tony Mak853304c2016-04-18 15:17:41 +0100894 }
895 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000896 } finally {
897 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100898 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000899 }
900
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800901 void setNextTaskIdForUserLocked(int taskId, int userId) {
902 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
903 if (taskId > currentTaskId) {
904 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700905 }
906 }
907
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700908 static int nextTaskIdForUser(int taskId, int userId) {
909 int nextTaskId = taskId + 1;
910 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
911 // Wrap around as there will be smaller task ids that are available now.
912 nextTaskId -= MAX_TASK_IDS_PER_USER;
913 }
914 return nextTaskId;
915 }
916
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800917 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800918 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
919 // for a userId u, a taskId can only be in the range
920 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
921 // 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 -0700922 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800923 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800924 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800925 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700926 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800927 if (candidateTaskId == currentTaskId) {
928 // Something wrong!
929 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
930 throw new IllegalStateException("Cannot get an available task id."
931 + " Reached limit of " + MAX_TASK_IDS_PER_USER
932 + " running tasks per user.");
933 }
934 }
935 mCurTaskIdForUser.put(userId, candidateTaskId);
936 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700937 }
938
Chong Zhang6cda19c2016-06-14 19:07:56 -0700939 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800940 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700941 if (stack == null) {
942 return null;
943 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700944 ActivityRecord resumedActivity = stack.mResumedActivity;
945 if (resumedActivity == null || resumedActivity.app == null) {
946 resumedActivity = stack.mPausingActivity;
947 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700948 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700949 }
950 }
951 return resumedActivity;
952 }
953
Dianne Hackbornff072722014-09-24 10:56:28 -0700954 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700955 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800957 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
958 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
960 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700961 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 continue;
963 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700964 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 if (hr != null) {
966 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
967 && processName.equals(hr.processName)) {
968 try {
George Mount2c92c972014-03-20 09:38:23 -0700969 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800970 didSomething = true;
971 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700972 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 Slog.w(TAG, "Exception in new application when starting activity "
974 + hr.intent.getComponent().flattenToShortString(), e);
975 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700976 }
Craig Mautner20e72272013-04-01 13:45:53 -0700977 }
Craig Mautner20e72272013-04-01 13:45:53 -0700978 }
979 }
980 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700981 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700982 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700983 }
Craig Mautner20e72272013-04-01 13:45:53 -0700984 return didSomething;
985 }
986
987 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800988 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
989 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
991 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700992 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 continue;
994 }
995 final ActivityRecord resumedActivity = stack.mResumedActivity;
996 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700997 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800998 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 return false;
1000 }
Craig Mautner20e72272013-04-01 13:45:53 -07001001 }
1002 }
Wei Wang65c7a152016-06-02 18:51:22 -07001003 // Send launch end powerhint when idle
1004 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001005 return true;
1006 }
1007
Craig Mautnerde4ef022013-04-07 19:01:33 -07001008 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001009 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1010 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001011 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1012 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001013 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001015 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 return false;
1017 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001018 }
1019 }
1020 }
1021 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001022 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1024 mLastFocusedStack + " to=" + mFocusedStack);
1025 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001026 return true;
1027 }
1028
1029 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001030 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001031 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1032 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1034 final ActivityStack stack = stacks.get(stackNdx);
1035 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001036 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001037 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001038 return false;
1039 }
1040 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001041 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 }
1043 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001044 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001045 }
1046
Craig Mautner2acc3892013-09-23 10:28:14 -07001047 /**
1048 * Pause all activities in either all of the stacks or just the back stacks.
1049 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001050 * @param resuming The resuming activity.
1051 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1052 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001053 * @return true if any activity was paused as a result of this call.
1054 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001055 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001056 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001057 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1058 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001059 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1060 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001061 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001062 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001063 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001064 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1065 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001066 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001067 }
1068 }
1069 return someActivityPaused;
1070 }
1071
Craig Mautnerde4ef022013-04-07 19:01:33 -07001072 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001073 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001074 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1075 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001076 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1077 final ActivityStack stack = stacks.get(stackNdx);
1078 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001080 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001081 Slog.d(TAG_STATES,
1082 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 pausing = false;
1084 } else {
1085 return false;
1086 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001087 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001088 }
1089 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001090 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001091 }
1092
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001093 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001094 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001095 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001096 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1097 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1098 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1099 final ActivityStack stack = stacks.get(stackNdx);
1100 if (stack.mResumedActivity != null &&
1101 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001102 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001103 }
1104 }
1105 }
1106 }
1107
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001108 void cancelInitializingActivities() {
1109 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1110 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1111 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1112 stacks.get(stackNdx).cancelInitializingActivities();
1113 }
1114 }
1115 }
1116
Bryce Lee4a194382017-04-04 14:32:48 -07001117 void waitActivityVisible(ComponentName name, WaitResult result) {
1118 final WaitInfo waitInfo = new WaitInfo(name, result);
1119 mWaitingForActivityVisible.add(waitInfo);
1120 }
1121
1122 void cleanupActivity(ActivityRecord r) {
1123 // Make sure this record is no longer in the pending finishes list.
1124 // This could happen, for example, if we are trimming activities
1125 // down to the max limit while they are still waiting to finish.
1126 mFinishingActivities.remove(r);
1127 mActivitiesWaitingForVisibleActivity.remove(r);
1128
1129 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001130 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001131 mWaitingForActivityVisible.remove(i);
1132 }
1133 }
1134 }
1135
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001136 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001137 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001138 }
1139
1140 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1141 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001142 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1143 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001144 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001145 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001146 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001147 result.timeout = false;
1148 result.who = w.getComponent();
1149 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1150 result.thisTime = result.totalTime;
1151 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001152 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001153 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001154 if (changed) {
1155 mService.notifyAll();
1156 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001157 }
1158
Chong Zhang5022da32016-06-21 16:31:37 -07001159 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1160 boolean changed = false;
1161 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1162 WaitResult w = mWaitingActivityLaunched.remove(i);
1163 if (w.who == null) {
1164 changed = true;
1165 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1166 // the starting activity ends up moving another activity to front, and it should
1167 // wait for this new activity to become visible instead.
1168 // Do not modify other fields.
1169 w.result = START_TASK_TO_FRONT;
1170 }
1171 }
1172 if (changed) {
1173 mService.notifyAll();
1174 }
1175 }
1176
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001177 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1178 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001179 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001180 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001181 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001182 if (w.who == null) {
1183 changed = true;
1184 w.timeout = timeout;
1185 if (r != null) {
1186 w.who = new ComponentName(r.info.packageName, r.info.name);
1187 }
1188 w.thisTime = thisTime;
1189 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001190 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001191 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001192 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001193 if (changed) {
1194 mService.notifyAll();
1195 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001196 }
1197
Craig Mautner29219d92013-04-16 20:19:12 -07001198 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001199 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001200 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001201 if (r != null) {
1202 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001203 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001204
Andrii Kulian7318d632016-07-20 18:59:28 -07001205 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001206 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1207
1208 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1209 final int displayId = mTmpOrderedDisplayIds.get(i);
1210 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1211 if (stacks == null) {
1212 continue;
1213 }
1214 for (int j = stacks.size() - 1; j >= 0; --j) {
1215 final ActivityStack stack = stacks.get(j);
1216 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1217 r = stack.topRunningActivityLocked();
1218 if (r != null) {
1219 return r;
1220 }
Craig Mautner29219d92013-04-16 20:19:12 -07001221 }
1222 }
1223 }
1224 return null;
1225 }
1226
Dianne Hackborn09233282014-04-30 11:33:59 -07001227 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001228 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001229 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1230 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001231 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001232 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001233 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001234 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1235 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001236 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001237 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001238 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001239 }
1240 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001241
1242 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1243 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1244 while (maxNum > 0) {
1245 long mostRecentActiveTime = Long.MIN_VALUE;
1246 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001247 final int numTaskLists = runningTaskLists.size();
1248 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1249 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001250 if (!stackTaskList.isEmpty()) {
1251 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1252 if (lastActiveTime > mostRecentActiveTime) {
1253 mostRecentActiveTime = lastActiveTime;
1254 selectedStackList = stackTaskList;
1255 }
1256 }
1257 }
1258 if (selectedStackList != null) {
1259 list.add(selectedStackList.remove(0));
1260 --maxNum;
1261 } else {
1262 break;
1263 }
1264 }
Craig Mautner20e72272013-04-01 13:45:53 -07001265 }
1266
Todd Kennedy7440f172015-12-09 14:31:22 -08001267 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1268 ProfilerInfo profilerInfo) {
1269 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001270 if (aInfo != null) {
1271 // Store the found target back into the intent, because now that
1272 // we have it we never want to do this again. For example, if the
1273 // user navigates back to this point in the history, we should
1274 // always restart the exact same activity.
1275 intent.setComponent(new ComponentName(
1276 aInfo.applicationInfo.packageName, aInfo.name));
1277
1278 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001279 if (!aInfo.processName.equals("system")) {
1280 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001281 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001282 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001283
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001284 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1285 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1286 }
1287
Man Caocfa78b22015-06-11 20:14:34 -07001288 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1289 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1290 }
1291
1292 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001293 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001294 }
1295 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001296 final String intentLaunchToken = intent.getLaunchToken();
1297 if (aInfo.launchToken == null && intentLaunchToken != null) {
1298 aInfo.launchToken = intentLaunchToken;
1299 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001300 }
1301 return aInfo;
1302 }
1303
Todd Kennedy7440f172015-12-09 14:31:22 -08001304 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001305 return resolveIntent(intent, resolvedType, userId, 0);
1306 }
1307
1308 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001309 synchronized (mService) {
1310 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001311 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001312 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001313 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001314 }
1315
1316 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1317 ProfilerInfo profilerInfo, int userId) {
1318 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1319 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1320 }
1321
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001322 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1323 boolean andResume, boolean checkConfig) throws RemoteException {
1324
1325 if (!allPausedActivitiesComplete()) {
1326 // While there are activities pausing we skipping starting any new activities until
1327 // pauses are complete. NOTE: that we also do this for activities that are starting in
1328 // the paused state because they will first be resumed then paused on the client side.
1329 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1330 "realStartActivityLocked: Skipping start of r=" + r
1331 + " some activities pausing...");
1332 return false;
1333 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001334
Andrii Kulianada10292017-02-09 23:19:29 -08001335 r.startFreezingScreenLocked(app, 0);
Wale Ogunwaled334a052017-05-12 07:17:36 -07001336 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */, true /* isTop */)) {
1337 // We only set the visibility to true if the activity is allowed to be visible based on
1338 // keyguard state. This avoids setting this into motion in window manager that is later
1339 // cancelled due to later calls to ensure visible activities that set visibility back to
1340 // false.
1341 r.setVisibility(true);
1342 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001343
Andrii Kulianada10292017-02-09 23:19:29 -08001344 // schedule launch ticks to collect information about slow apps.
1345 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001346
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001347 // Have the window manager re-evaluate the orientation of the screen based on the new
1348 // activity order. Note that as a result of this, it can call back into the activity
1349 // manager with a new orientation. We don't care about that, because the activity is not
1350 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001351 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001352 final int displayId = r.getDisplayId();
1353 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1354 getDisplayOverrideConfiguration(displayId),
1355 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001356 // Deferring resume here because we're going to launch new activity shortly.
1357 // We don't want to perform a redundant launch of the same record while ensuring
1358 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001359 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1360 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001361 }
1362
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001363 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001364 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001365 }
Makoto Onuki7a622782017-05-10 16:32:39 -07001366 final int applicationInfoUid =
1367 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1368 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1369 Slog.wtf(TAG,
1370 "User ID for activity changing for " + r
1371 + " appInfo.uid=" + r.appInfo.uid
1372 + " info.ai.uid=" + applicationInfoUid
1373 + " old=" + r.app + " new=" + app);
1374 }
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001375
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 r.app = app;
1377 app.waitingToKill = null;
1378 r.launchCount++;
1379 r.lastLaunchTime = SystemClock.uptimeMillis();
1380
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001381 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001382
1383 int idx = app.activities.indexOf(r);
1384 if (idx < 0) {
1385 app.activities.add(r);
1386 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001387 mService.updateLruProcessLocked(app, true, null);
1388 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001389
Bryce Leeaf691c02017-03-20 14:20:22 -07001390 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001391 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1392 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001393 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001394 }
1395
Andrii Kulian02b7a832016-10-06 23:11:56 -07001396 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001397 try {
1398 if (app.thread == null) {
1399 throw new RemoteException();
1400 }
1401 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001402 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001403 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001404 // We don't need to deliver new intents and/or set results if activity is going
1405 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 results = r.results;
1407 newIntents = r.newIntents;
1408 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001409 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1410 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1411 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001412 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1413 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001414 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001415 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001416 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001417 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001418 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1419 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001420 r.sleeping = false;
1421 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001422 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 mService.showAskCompatModeDialogLocked(r);
1424 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001425 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001426 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1427 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1428 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001429 final String profileFile = mService.mProfileFile;
1430 if (profileFile != null) {
1431 ParcelFileDescriptor profileFd = mService.mProfileFd;
1432 if (profileFd != null) {
1433 try {
1434 profileFd = profileFd.dup();
1435 } catch (IOException e) {
1436 if (profileFd != null) {
1437 try {
1438 profileFd.close();
1439 } catch (IOException o) {
1440 }
1441 profileFd = null;
1442 }
1443 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001444 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001445
1446 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001447 mService.mSamplingInterval, mService.mAutoStopProfiler,
1448 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001449 }
1450 }
1451 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001452
Andrii Kulianada10292017-02-09 23:19:29 -08001453 app.hasShownUi = true;
1454 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001455 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001456 // Because we could be starting an Activity in the system process this may not go across
1457 // a Binder interface which would create a new Configuration. Consequently we have to
1458 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001459
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001460 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1461 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1462 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001463
Craig Mautner2420ead2013-04-01 17:13:20 -07001464 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001465 System.identityHashCode(r), r.info,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001466 // TODO: Have this take the merged configuration instead of separate global and
1467 // override configs.
1468 mergedConfiguration.getGlobalConfiguration(),
1469 mergedConfiguration.getOverrideConfiguration(), r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001470 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1471 r.persistentState, results, newIntents, !andResume,
1472 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001473
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001474 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001475 // This may be a heavy-weight process! Note that the package
1476 // manager will ensure that only activity can run in the main
1477 // process of the .apk, which is the only thing that will be
1478 // considered heavy-weight.
1479 if (app.processName.equals(app.info.packageName)) {
1480 if (mService.mHeavyWeightProcess != null
1481 && mService.mHeavyWeightProcess != app) {
1482 Slog.w(TAG, "Starting new heavy weight process " + app
1483 + " when already running "
1484 + mService.mHeavyWeightProcess);
1485 }
1486 mService.mHeavyWeightProcess = app;
1487 Message msg = mService.mHandler.obtainMessage(
1488 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1489 msg.obj = r;
1490 mService.mHandler.sendMessage(msg);
1491 }
1492 }
1493
1494 } catch (RemoteException e) {
1495 if (r.launchFailed) {
1496 // This is the second time we failed -- finish activity
1497 // and give up.
1498 Slog.e(TAG, "Second failure launching "
1499 + r.intent.getComponent().flattenToShortString()
1500 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001501 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001502 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1503 "2nd-crash", false);
1504 return false;
1505 }
1506
1507 // This is the first time we failed -- restart process and
1508 // retry.
Sudheer Shankae49e7b82017-05-05 16:39:32 -07001509 r.launchFailed = true;
Craig Mautner2420ead2013-04-01 17:13:20 -07001510 app.activities.remove(r);
1511 throw e;
1512 }
1513
1514 r.launchFailed = false;
1515 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001516 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001517 }
1518
1519 if (andResume) {
1520 // As part of the process of launching, ActivityThread also performs
1521 // a resume.
1522 stack.minimalResumeActivityLocked(r);
1523 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001524 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001525 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001526 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001527 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001528 "Moving to PAUSED: " + r + " (starting in paused state)");
1529 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001530 }
1531
1532 // Launch the new version setup screen if needed. We do this -after-
1533 // launching the initial activity (that is, home), so that it can have
1534 // a chance to initialize itself while in the background, making the
1535 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001536 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001537 mService.startSetupActivityLocked();
1538 }
1539
Dianne Hackborn465fa392014-09-14 14:21:18 -07001540 // Update any services we are bound to that might care about whether
1541 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001542 if (r.app != null) {
1543 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1544 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001545
Craig Mautner2420ead2013-04-01 17:13:20 -07001546 return true;
1547 }
1548
Craig Mautnere79d42682013-04-01 19:01:53 -07001549 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001550 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001551 // Is this activity's application already running?
1552 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001553 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001554
Andrii Kulian02b7a832016-10-06 23:11:56 -07001555 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001556
1557 if (app != null && app.thread != null) {
1558 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001559 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1560 || !"android".equals(r.info.packageName)) {
1561 // Don't add this if it is a platform component that is marked
1562 // to run in multiple processes, because this is actually
1563 // part of the framework so doesn't make sense to track as a
1564 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001565 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1566 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001567 }
George Mount2c92c972014-03-20 09:38:23 -07001568 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001569 return;
1570 } catch (RemoteException e) {
1571 Slog.w(TAG, "Exception when starting activity "
1572 + r.intent.getComponent().flattenToShortString(), e);
1573 }
1574
1575 // If a dead object exception was thrown -- fall through to
1576 // restart the application.
1577 }
1578
1579 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001580 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001581 }
1582
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001583 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001584 String resultWho, int requestCode, int callingPid, int callingUid,
1585 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001586 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001587 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1588 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001589 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001590 return true;
1591 }
1592 final int componentRestriction = getComponentRestrictionForCallingPackage(
1593 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1594 final int actionRestriction = getActionRestrictionForCallingPackage(
1595 intent.getAction(), callingPackage, callingPid, callingUid);
1596 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1597 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1598 if (resultRecord != null) {
1599 resultStack.sendActivityResultLocked(-1,
1600 resultRecord, resultWho, requestCode,
1601 Activity.RESULT_CANCELED, null);
1602 }
1603 final String msg;
1604 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1605 msg = "Permission Denial: starting " + intent.toString()
1606 + " from " + callerApp + " (pid=" + callingPid
1607 + ", uid=" + callingUid + ")" + " with revoked permission "
1608 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1609 } else if (!aInfo.exported) {
1610 msg = "Permission Denial: starting " + intent.toString()
1611 + " from " + callerApp + " (pid=" + callingPid
1612 + ", uid=" + callingUid + ")"
1613 + " not exported from uid " + aInfo.applicationInfo.uid;
1614 } else {
1615 msg = "Permission Denial: starting " + intent.toString()
1616 + " from " + callerApp + " (pid=" + callingPid
1617 + ", uid=" + callingUid + ")"
1618 + " requires " + aInfo.permission;
1619 }
1620 Slog.w(TAG, msg);
1621 throw new SecurityException(msg);
1622 }
1623
1624 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1625 final String message = "Appop Denial: starting " + intent.toString()
1626 + " from " + callerApp + " (pid=" + callingPid
1627 + ", uid=" + callingUid + ")"
1628 + " requires " + AppOpsManager.permissionToOp(
1629 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1630 Slog.w(TAG, message);
1631 return false;
1632 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1633 final String message = "Appop Denial: starting " + intent.toString()
1634 + " from " + callerApp + " (pid=" + callingPid
1635 + ", uid=" + callingUid + ")"
1636 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1637 Slog.w(TAG, message);
1638 return false;
1639 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001640 if (options != null) {
1641 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1642 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1643 callingPid, callingUid);
1644 if (startInTaskPerm != PERMISSION_GRANTED) {
1645 final String msg = "Permission Denial: starting " + intent.toString()
1646 + " from " + callerApp + " (pid=" + callingPid
1647 + ", uid=" + callingUid + ") with launchTaskId="
1648 + options.getLaunchTaskId();
1649 Slog.w(TAG, msg);
1650 throw new SecurityException(msg);
1651 }
1652 }
1653 // Check if someone tries to launch an activity on a private display with a different
1654 // owner.
1655 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001656 if (launchDisplayId != INVALID_DISPLAY
1657 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1658 final String msg = "Permission Denial: starting " + intent.toString()
1659 + " from " + callerApp + " (pid=" + callingPid
1660 + ", uid=" + callingUid + ") with launchDisplayId="
1661 + launchDisplayId;
1662 Slog.w(TAG, msg);
1663 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001664 }
1665 }
1666
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001667 return true;
1668 }
1669
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001670 /** Check if caller is allowed to launch activities on specified display. */
1671 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1672 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1673 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1674
1675 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1676 if (activityDisplay == null) {
1677 Slog.w(TAG, "Launch on display check: display not found");
1678 return false;
1679 }
1680
1681 if (!activityDisplay.isPrivate()) {
1682 // Anyone can launch on a public display.
1683 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1684 + " allow launch on public display");
1685 return true;
1686 }
1687
1688 // Check if the caller is the owner of the display.
1689 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1690 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1691 + " allow launch for owner of the display");
1692 return true;
1693 }
1694
1695 // Check if caller is present on display
1696 if (activityDisplay.isUidPresent(callingUid)) {
1697 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1698 + " allow launch for caller present on the display");
1699 return true;
1700 }
1701
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001702 // Check if the caller can manage activity stacks.
1703 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001704 callingUid);
1705 if (startAnyPerm == PERMISSION_GRANTED) {
1706 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1707 + " allow launch any on display");
1708 return true;
1709 }
1710
1711 Slog.w(TAG, "Launch on display check: denied");
1712 return false;
1713 }
1714
1715 /** Update lists of UIDs that are present on displays and have access to them. */
1716 void updateUIDsPresentOnDisplay() {
1717 mDisplayAccessUIDs.clear();
1718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1719 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001720 // Only bother calculating the whitelist for private displays
1721 if (activityDisplay.isPrivate()) {
1722 mDisplayAccessUIDs.append(
1723 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1724 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001725 }
1726 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1727 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1728 }
1729
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001730 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001731 final long identity = Binder.clearCallingIdentity();
1732 try {
1733 return UserManager.get(mService.mContext).getUserInfo(userId);
1734 } finally {
1735 Binder.restoreCallingIdentity(identity);
1736 }
1737 }
1738
Svet Ganov99b60432015-06-27 13:15:22 -07001739 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001740 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001741 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1742 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001743 == PackageManager.PERMISSION_DENIED) {
1744 return ACTIVITY_RESTRICTION_PERMISSION;
1745 }
1746
Christopher Tateff7add02015-08-17 10:23:22 -07001747 if (activityInfo.permission == null) {
1748 return ACTIVITY_RESTRICTION_NONE;
1749 }
1750
Svet Ganov99b60432015-06-27 13:15:22 -07001751 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1752 if (opCode == AppOpsManager.OP_NONE) {
1753 return ACTIVITY_RESTRICTION_NONE;
1754 }
1755
1756 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1757 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001758 if (!ignoreTargetSecurity) {
1759 return ACTIVITY_RESTRICTION_APPOP;
1760 }
Svet Ganov99b60432015-06-27 13:15:22 -07001761 }
1762
1763 return ACTIVITY_RESTRICTION_NONE;
1764 }
1765
Svetoslav7008b512015-06-24 18:47:07 -07001766 private int getActionRestrictionForCallingPackage(String action,
1767 String callingPackage, int callingPid, int callingUid) {
1768 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
1772 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1773 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001774 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001775 }
1776
1777 final PackageInfo packageInfo;
1778 try {
1779 packageInfo = mService.mContext.getPackageManager()
1780 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1781 } catch (PackageManager.NameNotFoundException e) {
1782 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001783 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001784 }
1785
1786 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001787 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001788 }
1789
1790 if (mService.checkPermission(permission, callingPid, callingUid) ==
1791 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001792 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001793 }
1794
1795 final int opCode = AppOpsManager.permissionToOpCode(permission);
1796 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001797 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001798 }
1799
1800 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1801 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001802 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001803 }
1804
Svet Ganov99b60432015-06-27 13:15:22 -07001805 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001806 }
1807
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001808 void setLaunchSource(int uid) {
1809 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1810 }
1811
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001812 void acquireLaunchWakelock() {
1813 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1814 throw new IllegalStateException("Calling must be system uid");
1815 }
1816 mLaunchingActivity.acquire();
1817 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1818 // To be safe, don't allow the wake lock to be held for too long.
1819 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1820 }
1821 }
1822
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001823 /**
1824 * Called when the frontmost task is idle.
1825 * @return the state of mService.mBooting before this was called.
1826 */
1827 private boolean checkFinishBootingLocked() {
1828 final boolean booting = mService.mBooting;
1829 boolean enableScreen = false;
1830 mService.mBooting = false;
1831 if (!mService.mBooted) {
1832 mService.mBooted = true;
1833 enableScreen = true;
1834 }
1835 if (booting || enableScreen) {
1836 mService.postFinishBooting(booting, enableScreen);
1837 }
1838 return booting;
1839 }
1840
Craig Mautnerf3333272013-04-22 10:55:53 -07001841 // Checked.
1842 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001843 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001844 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001845
Craig Mautnerf3333272013-04-22 10:55:53 -07001846 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001847 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001848 int NS = 0;
1849 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001850 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001851 boolean activityRemoved = false;
1852
Wale Ogunwale7d701172015-03-11 15:36:30 -07001853 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001854 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001855 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1856 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001857 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1858 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001859 if (fromTimeout) {
1860 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001861 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001862
1863 // This is a hack to semi-deal with a race condition
1864 // in the client where it can be constructed with a
1865 // newer configuration from when we asked it to launch.
1866 // We'll update with whatever configuration it now says
1867 // it used to launch.
1868 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001869 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001870 }
1871
1872 // We are now idle. If someone is waiting for a thumbnail from
1873 // us, we can now deliver.
1874 r.idle = true;
1875
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001876 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001877 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001878 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001879 }
1880 }
1881
1882 if (allResumedActivitiesIdle()) {
1883 if (r != null) {
1884 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001885 }
1886
1887 if (mLaunchingActivity.isHeld()) {
1888 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1889 if (VALIDATE_WAKE_LOCK_CALLER &&
1890 Binder.getCallingUid() != Process.myUid()) {
1891 throw new IllegalStateException("Calling must be system uid");
1892 }
1893 mLaunchingActivity.release();
1894 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001895 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001896 }
1897
1898 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001899 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1900 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001901 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001902 if ((NF = mFinishingActivities.size()) > 0) {
1903 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001904 mFinishingActivities.clear();
1905 }
1906
Craig Mautnerf3333272013-04-22 10:55:53 -07001907 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001908 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 mStartingUsers.clear();
1910 }
1911
Craig Mautnerf3333272013-04-22 10:55:53 -07001912 // Stop 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 < NS; i++) {
1915 r = stops.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 if (r.finishing) {
1919 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1920 } else {
1921 stack.stopActivityLocked(r);
1922 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001923 }
1924 }
1925
1926 // Finish any activities that are scheduled to do so but have been
1927 // waiting for the next one to start.
1928 for (int i = 0; i < NF; i++) {
1929 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001930 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001931 if (stack != null) {
1932 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1933 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001934 }
1935
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001936 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001937 // Complete user switch
1938 if (startingUsers != null) {
1939 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001940 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001941 }
1942 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001943 }
1944
1945 mService.trimApplications();
1946 //dump();
1947 //mWindowManager.dump();
1948
Craig Mautnerf3333272013-04-22 10:55:53 -07001949 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001950 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001951 }
1952
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001953 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001954 }
1955
Craig Mautner8e569572013-10-11 17:36:59 -07001956 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001957 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001958 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1959 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001960 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1961 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1962 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001963 }
Craig Mautner19091252013-10-05 00:03:53 -07001964 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001965 }
1966
1967 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001968 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1969 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001970 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1971 stacks.get(stackNdx).closeSystemDialogsLocked();
1972 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001973 }
1974 }
1975
Craig Mautner93529a42013-10-04 15:03:13 -07001976 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001977 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001978 }
1979
Craig Mautner8d341ef2013-03-26 09:03:27 -07001980 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001981 * Update the last used stack id for non-current user (current user's last
1982 * used stack is the focused stack)
1983 */
1984 void updateUserStackLocked(int userId, ActivityStack stack) {
1985 if (userId != mCurrentUser) {
1986 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1987 }
1988 }
1989
1990 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001991 * @return true if some activity was finished (or would have finished if doit were true).
1992 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001993 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1994 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001995 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001996 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1997 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001998 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001999 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002000 if (stack.finishDisabledPackageActivitiesLocked(
2001 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002002 didSomething = true;
2003 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002004 }
2005 }
2006 return didSomething;
2007 }
2008
Dianne Hackborna413dc02013-07-12 12:02:55 -07002009 void updatePreviousProcessLocked(ActivityRecord r) {
2010 // Now that this process has stopped, we may want to consider
2011 // it to be the previous app to try to keep around in case
2012 // the user wants to return to it.
2013
2014 // First, found out what is currently the foreground app, so that
2015 // we don't blow away the previous app if this activity is being
2016 // hosted by the process that is actually still the foreground.
2017 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2019 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002020 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2021 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002022 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002023 if (stack.mResumedActivity != null) {
2024 fgApp = stack.mResumedActivity.app;
2025 } else if (stack.mPausingActivity != null) {
2026 fgApp = stack.mPausingActivity.app;
2027 }
2028 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002029 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002030 }
2031 }
2032
2033 // Now set this one as the previous process, only if that really
2034 // makes sense to.
2035 if (r.app != null && fgApp != null && r.app != fgApp
2036 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002037 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002038 mService.mPreviousProcess = r.app;
2039 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2040 }
2041 }
2042
Wale Ogunwaled046a012015-12-24 13:05:59 -08002043 boolean resumeFocusedStackTopActivityLocked() {
2044 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002045 }
2046
Wale Ogunwaled046a012015-12-24 13:05:59 -08002047 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002048 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002049 if (targetStack != null && isFocusedStack(targetStack)) {
2050 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002051 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002052 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2053 if (r == null || r.state != RESUMED) {
2054 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002055 } else if (r.state == RESUMED) {
2056 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2057 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002058 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002059 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002060 }
2061
Todd Kennedy39bfee52016-02-24 10:28:21 -08002062 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2063 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2064 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2065 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2066 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2067 }
2068 }
2069 }
2070
Adrian Roos20d7df32016-01-12 18:59:43 +01002071 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2072 TaskRecord finishedTask = null;
2073 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002074 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2075 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002076 final int numStacks = stacks.size();
2077 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2078 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002079 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2080 if (stack == focusedStack || finishedTask == null) {
2081 finishedTask = t;
2082 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002083 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002084 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002085 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002086 }
2087
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002088 void finishVoiceTask(IVoiceInteractionSession session) {
2089 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2090 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2091 final int numStacks = stacks.size();
2092 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2093 final ActivityStack stack = stacks.get(stackNdx);
2094 stack.finishVoiceTask(session);
2095 }
2096 }
2097 }
2098
Andrii Kulianc27916642016-04-12 17:59:27 -07002099 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2100 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002101 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2102 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002103 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002104 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2105 // Caller wants the home activity moved with it. To accomplish this,
2106 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002107 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002108 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002109 ActivityStack currentStack = task.getStack();
2110 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002111 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2112 + task + " to front. Stack is null");
2113 return;
2114 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002115
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002116 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002117 int stackId = options.getLaunchStackId();
2118 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002119 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002120 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002121 if (stackId == INVALID_STACK_ID) {
2122 stackId = task.getLaunchStackId();
2123 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002124 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002125 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2126 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002127 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002128 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2129 // still need moveTaskToFrontLocked() below for any transition settings.
2130 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002131 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2132 resizeStackLocked(stackId, bounds,
2133 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002134 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002135 } else {
2136 // WM resizeTask must be done after the task is moved to the correct stack,
2137 // because Task's setBounds() also updates dim layer's bounds, but that has
2138 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002139 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002140 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002141 }
2142 }
2143
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002144 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002145 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002146 r == null ? null : r.appTimeTracker, reason);
2147
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002148 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002149 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002150
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002151 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2152 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002153 }
2154
Wale Ogunwale854809c2015-12-27 16:18:19 -08002155 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002156 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002157 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002158 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002159 return false;
2160 }
2161 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2162 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002163 }
2164
Winson Chung55893332017-02-17 17:13:10 -08002165 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002166 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002167 }
2168
Winson Chung55893332017-02-17 17:13:10 -08002169 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2170 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002171 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002172 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002173 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002174 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002175 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002176 return null;
2177 }
Matthew Ng330757d2017-02-28 14:19:17 -08002178 if (stackId == DOCKED_STACK_ID) {
2179 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2180 // the other side of the docked stack and we make visibility decisions based on that.
2181 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2182 }
Winson Chung55893332017-02-17 17:13:10 -08002183 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002184 }
2185
Andrii Kulian16802aa2016-11-02 12:21:33 -07002186 /**
2187 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2188 * If there is no such stack, new dynamic stack can be created.
2189 * @param displayId Target display.
2190 * @param r Activity that should be launched there.
2191 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2192 */
2193 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2194 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2195 if (activityDisplay == null) {
2196 throw new IllegalArgumentException(
2197 "Display with displayId=" + displayId + " not found.");
2198 }
2199
2200 // Return the topmost valid stack on the display.
2201 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2202 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002203 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002204 return stack;
2205 }
2206 }
2207
2208 // If there is no valid stack on the external display - check if new dynamic stack will do.
2209 if (displayId != Display.DEFAULT_DISPLAY) {
2210 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002211 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002212 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2213 }
2214 }
2215
2216 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2217 return null;
2218 }
2219
Craig Mautner967212c2013-04-13 21:10:58 -07002220 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002221 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002222 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2223 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002224 }
2225 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002226 }
2227
Jorim Jaggife762342016-10-13 14:33:27 +02002228 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2229 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2230 }
2231
Andrii Kulian7fc22812016-12-28 13:04:11 -08002232 /**
2233 * Get next focusable stack in the system. This will search across displays and stacks
2234 * in last-focused order for a focusable and visible stack, different from the target stack.
2235 *
2236 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2237 * searching for next candidate.
2238 * @return Next focusable {@link ActivityStack}, null if not found.
2239 */
2240 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2241 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2242
2243 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2244 final int displayId = mTmpOrderedDisplayIds.get(i);
2245 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2246 if (stacks == null) {
2247 continue;
2248 }
2249 for (int j = stacks.size() - 1; j >= 0; --j) {
2250 final ActivityStack stack = stacks.get(j);
2251 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002252 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002253 return stack;
2254 }
2255 }
2256 }
2257
2258 return null;
2259 }
2260
Craig Mautneree2e45a2014-06-27 12:10:03 -07002261 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002262 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002263 }
2264
2265 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002266 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2267 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2268 final TaskRecord task = tasks.get(taskNdx);
2269 if (task.isHomeTask()) {
2270 final ArrayList<ActivityRecord> activities = task.mActivities;
2271 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2272 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002273 if (r.isHomeActivity()
2274 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002275 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002276 }
2277 }
2278 }
2279 }
2280 return null;
2281 }
2282
Chong Zhangb15758a2015-11-17 12:12:03 -08002283 /**
2284 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2285 * the docked stack itself, or if it's side-by-side to the docked stack.
2286 */
2287 boolean isStackDockedInEffect(int stackId) {
2288 return stackId == DOCKED_STACK_ID ||
2289 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2290 }
2291
Todd Kennedyca4d8422015-01-15 15:19:22 -08002292 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002293 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002294 ActivityContainer activityContainer =
2295 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002296 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002297 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2298 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002299 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002300 return activityContainer;
2301 }
2302
Craig Mautner34b73df2014-01-12 21:11:08 -08002303 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002304 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2305 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2306 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002307 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2308 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002309 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002310 }
2311 }
2312
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002313 void deleteActivityContainerRecord(int stackId) {
2314 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2315 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2316 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002317 }
2318
Jorim Jaggidc249c42015-12-15 14:57:31 -08002319 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002320 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002321 if (stackId == DOCKED_STACK_ID) {
2322 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002323 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002324 return;
2325 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002326 final ActivityStack stack = getStack(stackId);
2327 if (stack == null) {
2328 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2329 return;
2330 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002331
Winson Chunga2249ac2017-03-30 18:15:30 -07002332 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2333 getStack(DOCKED_STACK_ID) != null) {
2334 // If the docked stack exists, don't resize non-floating stacks independently of the
2335 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002336 return;
2337 }
2338
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002339 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002340 mWindowManager.deferSurfaceLayout();
2341 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002342 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002343 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002344 stack.ensureVisibleActivitiesConfigurationLocked(
2345 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002346 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002347 } finally {
2348 mWindowManager.continueSurfaceLayout();
2349 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002350 }
2351 }
2352
Jorim Jaggi192086e2016-03-11 17:17:03 +01002353 void deferUpdateBounds(int stackId) {
2354 final ActivityStack stack = getStack(stackId);
2355 if (stack != null) {
2356 stack.deferUpdateBounds();
2357 }
2358 }
2359
2360 void continueUpdateBounds(int stackId) {
2361 final ActivityStack stack = getStack(stackId);
2362 if (stack != null) {
2363 stack.continueUpdateBounds();
2364 }
2365 }
2366
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002367 void notifyAppTransitionDone() {
2368 continueUpdateBounds(HOME_STACK_ID);
2369 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2370 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002371 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002372 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002373 if (task != null) {
2374 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002375 }
2376 }
2377 mResizingTasksDuringAnimation.clear();
2378 }
2379
Robert Carre7cc44d2017-03-20 19:04:30 -07002380 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2381 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002382
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002383 final ActivityStack stack = getStack(fromStackId);
2384 if (stack == null) {
2385 return;
2386 }
2387
2388 mWindowManager.deferSurfaceLayout();
2389 try {
2390 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002391 // We are moving all tasks from the docked stack to the fullscreen stack,
2392 // which is dismissing the docked stack, so resize all other stacks to
2393 // fullscreen here already so we don't end up with resize trashing.
2394 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2395 if (StackId.isResizeableByDockedStack(i)) {
2396 ActivityStack otherStack = getStack(i);
2397 if (otherStack != null) {
2398 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2399 true /* allowResizeInDockedMode */, DEFER_RESUME);
2400 }
2401 }
2402 }
2403
2404 // Also disable docked stack resizing since we have manually adjusted the
2405 // size of other stacks above and we don't want to trigger a docked stack
2406 // resize when we remove task from it below and it is detached from the
2407 // display because it no longer contains any tasks.
2408 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002409 } else if (fromStackId == PINNED_STACK_ID) {
2410 if (onTop) {
2411 // Log if we are expanding the PiP to fullscreen
2412 MetricsLogger.action(mService.mContext,
2413 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2414 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002415 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002416 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002417 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002418 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002419 // If we are moving from the pinned stack, then the animation takes care of updating
2420 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002421 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002422 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2423 final int size = tasks.size();
2424 if (onTop) {
2425 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002426 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002427 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002428 if (fromStackId == PINNED_STACK_ID) {
2429 // Update the return-to to reflect where the pinned stack task was moved
2430 // from so that we retain the stack that was previously visible if the
2431 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002432 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2433 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002434 }
Winson Chung74666102017-02-22 17:49:24 -08002435 // Defer resume until all the tasks have been moved to the fullscreen stack
2436 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2437 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002438 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002439 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002440 }
2441 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002442 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002443 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002444 // Position the tasks in the fullscreen stack in order at the bottom of the
2445 // stack. Also defer resume until all the tasks have been moved to the
2446 // fullscreen stack.
2447 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002448 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2449 schedulePictureInPictureModeChange,
2450 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002451 }
2452 }
Winson Chung74666102017-02-22 17:49:24 -08002453
2454 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2455 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002456 } finally {
2457 mAllowDockedStackResize = true;
2458 mWindowManager.continueSurfaceLayout();
2459 }
2460 }
2461
Robert Carr6914f082017-03-20 19:04:30 -07002462 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002463 mWindowManager.inSurfaceTransaction(
2464 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002465 }
2466
Jorim Jaggidc249c42015-12-15 14:57:31 -08002467 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002468 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2469 boolean preserveWindows) {
2470 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2471 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2472 false /* deferResume */);
2473 }
2474
2475 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2476 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2477 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002478
2479 if (!mAllowDockedStackResize) {
2480 // Docked stack resize currently disabled.
2481 return;
2482 }
2483
Jorim Jaggidc249c42015-12-15 14:57:31 -08002484 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2485 if (stack == null) {
2486 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2487 return;
2488 }
2489
2490 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2491 mWindowManager.deferSurfaceLayout();
2492 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002493 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2494 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002495 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002496 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002497
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002498 // TODO: Checking for isAttached might not be needed as if the user passes in null
2499 // dockedBounds then they want the docked stack to be dismissed.
2500 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2501 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002502 // In this case we make all other static stacks fullscreen and move all
2503 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002504 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002505
2506 // stack shouldn't contain anymore activities, so nothing to resume.
2507 r = null;
2508 } else {
2509 // Docked stacks occupy a dedicated region on screen so the size of all other
2510 // static stacks need to be adjusted so they don't overlap with the docked stack.
2511 // We get the bounds to use from window manager which has been adjusted for any
2512 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002513 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002514 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002515 final ActivityStack current = getStack(i);
2516 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002517 current.getStackDockedModeBounds(
2518 tempOtherTaskBounds /* currentTempTaskBounds */,
2519 tempRect /* outStackBounds */,
2520 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2521
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002522 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002523 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2524 tempOtherTaskInsetBounds, preserveWindows,
2525 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002526 }
2527 }
2528 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002529 if (!deferResume) {
2530 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2531 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002532 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002533 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002534 mWindowManager.continueSurfaceLayout();
2535 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2536 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002537 }
2538
Robert Carr0d00c2e2016-02-29 17:45:02 -08002539 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002540 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002541 if (stack == null) {
2542 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2543 return;
2544 }
Winson Chung19953ca2017-04-11 11:19:23 -07002545
2546 // It is possible for the bounds animation from the WM to call this but be delayed by
2547 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2548 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2549 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2550 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002551 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002552 return;
2553 }
2554
Robert Carr0d00c2e2016-02-29 17:45:02 -08002555 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2556 mWindowManager.deferSurfaceLayout();
2557 try {
2558 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002559 Rect insetBounds = null;
2560 if (tempPinnedTaskBounds != null) {
2561 // We always use 0,0 as the position for the inset rect because
2562 // if we are getting insets at all in the pinned stack it must mean
2563 // we are headed for fullscreen.
2564 insetBounds = tempRect;
2565 insetBounds.top = 0;
2566 insetBounds.left = 0;
2567 insetBounds.right = tempPinnedTaskBounds.width();
2568 insetBounds.bottom = tempPinnedTaskBounds.height();
2569 }
2570 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002571 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002572 } finally {
2573 mWindowManager.continueSurfaceLayout();
2574 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2575 }
2576 }
2577
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002578 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002579 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002580 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002581 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002582 }
2583
Wale Ogunwale1666e312016-12-16 11:27:18 -08002584 final ActivityContainer activityContainer =
2585 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002586 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002587 }
2588
Robert Carr6914f082017-03-20 19:04:30 -07002589
Robert Carre7cc44d2017-03-20 19:04:30 -07002590 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002591 final ActivityStack stack = getStack(stackId);
2592 if (stack == null) {
2593 return;
2594 }
2595
2596 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2597 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002598 /**
2599 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2600 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2601 * that the client receives onStop() before it is reparented. We do this by detaching
2602 * the stack from the display so that it will be considered invisible when
2603 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2604 * invisible as well and added to the stopping list. After which we process the
2605 * stopping list by handling the idle.
2606 */
2607 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2608 pinnedStack.mForceHidden = true;
2609 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2610 pinnedStack.mForceHidden = false;
2611 activityIdleInternalLocked(null, false /* fromTimeout */,
2612 true /* processPausingActivites */, null /* configuration */);
2613
2614 // Move all the tasks to the bottom of the fullscreen stack
2615 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002616 } else {
2617 for (int i = tasks.size() - 1; i >= 0; i--) {
2618 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2619 REMOVE_FROM_RECENTS);
2620 }
2621 }
2622 }
2623
2624 /**
Robert Carr6914f082017-03-20 19:04:30 -07002625 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2626 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2627 * instead moved back onto the fullscreen stack.
2628 */
2629 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002630 mWindowManager.inSurfaceTransaction(
2631 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002632 }
2633
2634 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002635 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2636 */
2637 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2638 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2639 }
2640
2641 /**
Winson Chung010927a2016-12-15 16:12:35 -08002642 * Removes the task with the specified task id.
2643 *
2644 * @param taskId Identifier of the task to be removed.
2645 * @param killProcess Kill any process associated with the task if possible.
2646 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002647 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2648 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002649 * @return Returns true if the given task was found and removed.
2650 */
Winson Chung6954fc92017-03-24 16:22:12 -07002651 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2652 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002653 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2654 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002655 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002656 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002657 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2658 if (tr.isPersistable) {
2659 mService.notifyTaskPersisterLocked(null, true);
2660 }
2661 return true;
2662 }
2663 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2664 return false;
2665 }
2666
2667 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2668 if (removeFromRecents) {
2669 mRecentTasks.remove(tr);
2670 tr.removedFromRecents();
2671 }
2672 ComponentName component = tr.getBaseIntent().getComponent();
2673 if (component == null) {
2674 Slog.w(TAG, "No component for base intent of task: " + tr);
2675 return;
2676 }
2677
2678 // Find any running services associated with this app and stop if needed.
2679 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2680
2681 if (!killProcess) {
2682 return;
2683 }
2684
2685 // Determine if the process(es) for this task should be killed.
2686 final String pkg = component.getPackageName();
2687 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2688 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2689 for (int i = 0; i < pmap.size(); i++) {
2690
2691 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2692 for (int j = 0; j < uids.size(); j++) {
2693 ProcessRecord proc = uids.valueAt(j);
2694 if (proc.userId != tr.userId) {
2695 // Don't kill process for a different user.
2696 continue;
2697 }
2698 if (proc == mService.mHomeProcess) {
2699 // Don't kill the home process along with tasks from the same package.
2700 continue;
2701 }
2702 if (!proc.pkgList.containsKey(pkg)) {
2703 // Don't kill process that is not associated with this task.
2704 continue;
2705 }
2706
2707 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002708 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002709 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2710 // Don't kill process(es) that has an activity in a different task that is
2711 // also in recents.
2712 return;
2713 }
2714 }
2715
2716 if (proc.foregroundServices) {
2717 // Don't kill process(es) with foreground service.
2718 return;
2719 }
2720
2721 // Add process to kill list.
2722 procsToKill.add(proc);
2723 }
2724 }
2725
2726 // Kill the running processes.
2727 for (int i = 0; i < procsToKill.size(); i++) {
2728 ProcessRecord pr = procsToKill.get(i);
2729 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2730 && pr.curReceivers.isEmpty()) {
2731 pr.kill("remove task", true);
2732 } else {
2733 // We delay killing processes that are not in the background or running a receiver.
2734 pr.waitingToKill = "remove task";
2735 }
2736 }
2737 }
2738
Craig Mautner4a1cb222013-12-04 16:14:06 -08002739 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002740 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002741 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2742 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002743 break;
2744 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002745 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002746 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002747 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002748 }
2749
Chong Zhang5dcb2752015-08-18 13:50:26 -07002750 /**
2751 * Restores a recent task to a stack
2752 * @param task The recent task to be restored.
2753 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002754 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2755 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002756 * @return true if the task has been restored successfully.
2757 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002758 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002759 if (!StackId.isStaticStack(stackId)) {
2760 // If stack is not static (or stack id is invalid) - use the default one.
2761 // This means that tasks that were on external displays will be restored on the
2762 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002763 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002764 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002765 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2766 // Put it in the fullscreen stack.
2767 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002768 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002769
Andrii Kulian02b7a832016-10-06 23:11:56 -07002770 final ActivityStack currentStack = task.getStack();
2771 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002772 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002773 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002774 // Nothing else to do since it is already restored in the right stack.
2775 return true;
2776 }
2777 // Remove current stack association, so we can re-associate the task with the
2778 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002779 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002780 }
2781
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002782 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002783 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002784
2785 if (stack == null) {
2786 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002787 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2788 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002789 return false;
2790 }
2791
Wale Ogunwale72919d22016-12-08 18:58:50 -08002792 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002793 // TODO: move call for creation here and other place into Stack.addTask()
2794 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002795 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2796 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002797 final ArrayList<ActivityRecord> activities = task.mActivities;
2798 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002799 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002800 }
2801 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002802 }
2803
Wale Ogunwale040b4702015-08-06 18:10:50 -07002804 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002805 * Move stack with all its existing content to specified display.
2806 * @param stackId Id of stack to move.
2807 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002808 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002809 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002810 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002811 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2812 if (activityDisplay == null) {
2813 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2814 + displayId);
2815 }
2816 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2817 if (activityContainer != null) {
2818 if (activityContainer.isAttachedLocked()) {
2819 if (activityContainer.getDisplayId() == displayId) {
2820 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2821 + " to its current displayId=" + displayId);
2822 }
2823
Andrii Kulian250d6532017-02-08 23:30:45 -08002824 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002825 } else {
2826 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2827 + stackId + " is not attached to any display.");
2828 }
2829 } else {
2830 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2831 + stackId);
2832 }
Andrii Kulian839def92016-11-02 10:58:58 -07002833 // TODO(multi-display): resize stacks properly if moved from split-screen.
2834 }
2835
2836 /**
Winson Chung74666102017-02-22 17:49:24 -08002837 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2838 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002839 */
Winson Chung74666102017-02-22 17:49:24 -08002840 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002841 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002842
Winson Chung74666102017-02-22 17:49:24 -08002843 // Check that we aren't reparenting to the same stack that the task is already in
2844 if (prevStack != null && prevStack.mStackId == stackId) {
2845 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2846 + " already in stackId=" + stackId);
2847 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002848 }
2849
Winson Chung74666102017-02-22 17:49:24 -08002850 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2851 // support
2852 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2853 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2854 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002855 }
2856
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002857 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2858 // multi-display.
2859 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2860 // TODO: Check ActivityView after fixing b/35349678.
2861 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2862 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2863 + " reparent task=" + task + " to stackId=" + stackId);
2864 }
2865
Winson Chung74666102017-02-22 17:49:24 -08002866 // Ensure that we aren't trying to move into a freeform stack without freeform
2867 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002868 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002869 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2870 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002871 }
2872
Winson Chung74666102017-02-22 17:49:24 -08002873 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2874 // used for split-screen mode and will cause things like the docked divider to show up. We
2875 // instead leave the task in its current stack or move it to the fullscreen stack if it
2876 // isn't currently in a stack.
2877 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2878 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2879 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2880 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002881 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002882
Winson Chung74666102017-02-22 17:49:24 -08002883 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2884 // example, a docked stack is created which will lead to the stack we are moving from being
2885 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002886 try {
Winson Chung74666102017-02-22 17:49:24 -08002887 task.mTemporarilyUnresizable = true;
2888 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002889 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002890 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002891 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002892 }
2893
Winson Chung08f81892017-03-02 15:40:51 -08002894 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002895 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2896 if (stack == null) {
2897 throw new IllegalArgumentException(
2898 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2899 }
2900
2901 final ActivityRecord r = stack.topRunningActivityLocked();
2902 if (r == null) {
2903 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2904 + " in stack=" + stack);
2905 return false;
2906 }
2907
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002908 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002909 Slog.w(TAG,
2910 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002911 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002912 return false;
2913 }
2914
Winson Chung3a682872017-04-10 14:38:05 -07002915 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002916 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002917 return true;
2918 }
2919
Winson Chung8bca9e42017-04-16 15:59:43 -07002920 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002921 boolean moveHomeStackToFront, String reason) {
2922
Wale Ogunwale480dca02016-02-06 13:58:29 -08002923 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002924
Bryce Lee04ab3462017-04-10 15:06:33 -07002925 // This will clear the pinned stack by moving an existing task to the full screen stack,
2926 // ensuring only one task is present.
2927 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2928
Wale Ogunwale1666e312016-12-16 11:27:18 -08002929 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002930 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002931
Wale Ogunwale480dca02016-02-06 13:58:29 -08002932 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002933 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002934
Andrii Kulian02b7a832016-10-06 23:11:56 -07002935 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002936 // An activity can't be pinned and visible behind at the same time. Go ahead and
2937 // release it from been visible behind before pinning.
2938 requestVisibleBehindLocked(r, false);
2939 }
2940
Wale Ogunwale480dca02016-02-06 13:58:29 -08002941 // Resize the pinned stack to match the current size of the task the activity we are
2942 // going to be moving is currently contained in. We do this to have the right starting
2943 // animation bounds for the pinned stack to the desired bounds the caller wants.
2944 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2945 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002946 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002947
2948 if (task.mActivities.size() == 1) {
2949 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002950 // the stack without re-parenting the activity in a different task. We don't
2951 // move the home stack forward if we are currently entering picture-in-picture
2952 // while pausing because that changes the focused stack and may prevent the new
2953 // starting activity from resuming.
2954 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002955 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002956 // Move the home stack forward if the task we just moved to the pinned stack
2957 // was launched from home so home should be visible behind it.
2958 moveHomeStackToFront(reason);
2959 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002960 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002961 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002962 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002963 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002964 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002965 // reveal/leave the other activities in their original task.
2966
2967 // Currently, we don't support reparenting activities across tasks in two different
2968 // stacks, so instead, just create a new task in the same stack, reparent the
2969 // activity into that task, and then reparent the whole task to the new stack. This
2970 // ensures that all the necessary work to migrate states in the old and new stacks
2971 // is also done.
2972 final TaskRecord newTask = task.getStack().createTaskRecord(
2973 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2974 r.mActivityType);
2975 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2976
Winson Chung5af42fc2017-03-24 17:11:33 -07002977 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002978 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002979 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002980 }
Winson Chungc2baac02017-01-11 13:34:47 -08002981
2982 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2983 // to the pinned stack
2984 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002985 } finally {
2986 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002987 }
2988
Winson Chunge7ba6862017-05-24 12:13:33 -07002989 // Calculate the default bounds (don't use existing stack bounds as we may have just created
2990 // the stack, and schedule the start of the animation into PiP (the bounds animator that
2991 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07002992 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
2993
Winson Chunge7ba6862017-05-24 12:13:33 -07002994 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
2995 true /* fromFullscreen */);
2996
2997 // Update the visibility of all activities after the they have been reparented to the new
2998 // stack. This MUST run after the animation above is scheduled to ensure that the windows
2999 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3000 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003001 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003002 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003003
Winson Chungb5026902017-05-03 12:45:13 -07003004 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3005 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003006 }
3007
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003008 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003009 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3010 if (r == null || !r.isFocusable()) {
3011 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3012 "moveActivityStackToFront: unfocusable r=" + r);
3013 return false;
3014 }
3015
Bryce Leeaf691c02017-03-20 14:20:22 -07003016 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003017 final ActivityStack stack = r.getStack();
3018 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003019 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3020 + r + " task=" + task);
3021 return false;
3022 }
3023
Chong Zhang6cda19c2016-06-14 19:07:56 -07003024 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3025 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3026 "moveActivityStackToFront: already on top, r=" + r);
3027 return false;
3028 }
3029
3030 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3031 "moveActivityStackToFront: r=" + r);
3032
3033 stack.moveToFront(reason, task);
3034 return true;
3035 }
3036
David Stevensc6b91c62017-02-08 14:23:58 -08003037 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003038 mTmpFindTaskResult.r = null;
3039 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003040 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003041 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003042 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3043 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003044 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3045 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003046 if (!checkActivityBelongsInStack(r, stack)) {
3047 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3048 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003049 continue;
3050 }
3051 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003052 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3053 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 continue;
3055 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003056 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003057 // It is possible to have tasks in multiple stacks with the same root affinity, so
3058 // we should keep looking after finding an affinity match to see if there is a
3059 // better match in another stack. Also, task affinity isn't a good enough reason
3060 // to target a display which isn't the source of the intent, so skip any affinity
3061 // matches not on the specified display.
3062 if (mTmpFindTaskResult.r != null) {
3063 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3064 return mTmpFindTaskResult.r;
3065 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003066 // Note: since the traversing through the stacks is top down, the floating
3067 // tasks should always have lower priority than any affinity-matching tasks
3068 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003069 affinityMatch = mTmpFindTaskResult.r;
3070 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003072 }
3073 }
Winson Chung5b895b72017-05-01 13:46:25 -07003074
David Stevensc6b91c62017-02-08 14:23:58 -08003075 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3076 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003077 }
3078
Winson Chung91e5c882017-04-21 14:44:38 -07003079 /**
3080 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3081 * type.
3082 */
3083 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3084 if (r.isHomeActivity()) {
3085 return stack.isHomeStack();
3086 } else if (r.isRecentsActivity()) {
3087 return stack.isRecentsStack();
3088 } else if (r.isAssistantActivity()) {
3089 return stack.isAssistantStack();
3090 }
3091 return true;
3092 }
3093
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003094 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3095 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3097 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003098 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003099 final ActivityRecord ar = stacks.get(stackNdx)
3100 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 if (ar != null) {
3102 return ar;
3103 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003104 }
3105 }
3106 return null;
3107 }
3108
Craig Mautner8d341ef2013-03-26 09:03:27 -07003109 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003110 scheduleSleepTimeout();
3111 if (!mGoingToSleep.isHeld()) {
3112 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003113 if (mLaunchingActivity.isHeld()) {
3114 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3115 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003116 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003117 mLaunchingActivity.release();
3118 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003121 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003122 }
3123
3124 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003125 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003126
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003127 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003128 final long endTime = System.currentTimeMillis() + timeout;
3129 while (true) {
3130 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3132 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003133 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3134 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3135 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003136 }
3137 if (cantShutdown) {
3138 long timeRemaining = endTime - System.currentTimeMillis();
3139 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003140 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003141 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003142 } catch (InterruptedException e) {
3143 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003144 } else {
3145 Slog.w(TAG, "Activity manager shutdown timed out");
3146 timedout = true;
3147 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003148 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003149 } else {
3150 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003151 }
3152 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003153
3154 // Force checkReadyForSleep to complete.
3155 mSleepTimeout = true;
3156 checkReadyForSleepLocked();
3157
Craig Mautner8d341ef2013-03-26 09:03:27 -07003158 return timedout;
3159 }
3160
3161 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003162 removeSleepTimeouts();
3163 if (mGoingToSleep.isHeld()) {
3164 mGoingToSleep.release();
3165 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003166 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3167 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003168 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3169 final ActivityStack stack = stacks.get(stackNdx);
3170 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003171 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003172 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003173 }
Craig Mautner5314a402013-09-26 12:40:16 -07003174 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003176 mGoingToSleepActivities.clear();
3177 }
3178
3179 void activitySleptLocked(ActivityRecord r) {
3180 mGoingToSleepActivities.remove(r);
3181 checkReadyForSleepLocked();
3182 }
3183
3184 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003185 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003186 // Do not care.
3187 return;
3188 }
3189
3190 if (!mSleepTimeout) {
3191 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003192 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3193 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003194 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3195 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3196 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003197 }
3198
3199 if (mStoppingActivities.size() > 0) {
3200 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003201 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003202 + mStoppingActivities.size() + " activities");
3203 scheduleIdleLocked();
3204 dontSleep = true;
3205 }
3206
3207 if (mGoingToSleepActivities.size() > 0) {
3208 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003209 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003210 + mGoingToSleepActivities.size() + " activities");
3211 dontSleep = true;
3212 }
3213
3214 if (dontSleep) {
3215 return;
3216 }
3217 }
3218
Wei Wang65c7a152016-06-02 18:51:22 -07003219 // Send launch end powerhint before going sleep
3220 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3221
Craig Mautnere0a38842013-12-16 16:14:02 -08003222 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3223 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003224 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3225 stacks.get(stackNdx).goToSleep();
3226 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003227 }
3228
3229 removeSleepTimeouts();
3230
3231 if (mGoingToSleep.isHeld()) {
3232 mGoingToSleep.release();
3233 }
3234 if (mService.mShuttingDown) {
3235 mService.notifyAll();
3236 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003237 }
3238
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003239 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003240 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003241 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003242 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003243 }
3244 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003245 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003246 mWindowManager.executeAppTransition();
3247 return true;
3248 }
3249 return false;
3250 }
3251
Craig Mautner8d341ef2013-03-26 09:03:27 -07003252 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003253 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3254 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003255 int stackNdx = stacks.size() - 1;
3256 while (stackNdx >= 0) {
3257 stacks.get(stackNdx).handleAppCrashLocked(app);
3258 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003259 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003260 }
3261 }
3262
Jose Lima4b6c6692014-08-12 17:41:12 -07003263 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003264 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003265 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003266 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3267 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003268 return false;
3269 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003270
3271 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3272 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3273 + " visible=" + visible + " stackId=" + stack.mStackId
3274 + " can't contain visible behind activities");
3275 return false;
3276 }
3277
Jose Lima4b6c6692014-08-12 17:41:12 -07003278 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003279 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3280 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003281
3282 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003283 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003284 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003285 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003286 return true;
3287 }
3288
3289 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003290 if (visible && top.fullscreen) {
3291 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003292 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3293 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3294 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3295 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003296 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003297 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3298 // Only the activity set as currently visible behind should actively reset its
3299 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003300 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3301 "requestVisibleBehind: returning visible=" + visible
3302 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3303 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003304 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003305 }
3306
Jose Lima4b6c6692014-08-12 17:41:12 -07003307 stack.setVisibleBehindActivity(visible ? r : null);
3308 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003309 // If there is a translucent home activity, we need to force it stop being translucent,
3310 // because we can't depend on the application to necessarily perform that operation.
3311 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003312 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003313 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003314 mService.convertFromTranslucent(next.appToken);
3315 }
3316 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003317 if (top.app != null && top.app.thread != null) {
3318 // Notify the top app of the change.
3319 try {
3320 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3321 } catch (RemoteException e) {
3322 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003323 }
3324 return true;
3325 }
3326
Craig Mautnerbb742462014-07-07 15:28:55 -07003327 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003328 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003329 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003330 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003331
3332 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003333 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003334 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003335 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003336 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003337
3338 // When launching tasks behind, update the last active time of the top task after the new
3339 // task has been shown briefly
3340 final ActivityRecord top = stack.topActivity();
3341 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003342 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003343 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003344 }
3345
3346 void scheduleLaunchTaskBehindComplete(IBinder token) {
3347 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3348 }
3349
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003350 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3351 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003352 mKeyguardController.beginActivityVisibilityUpdate();
3353 try {
3354 // First the front stacks. In case any are not fullscreen and are in front of home.
3355 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3356 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3357 final int topStackNdx = stacks.size() - 1;
3358 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3359 final ActivityStack stack = stacks.get(stackNdx);
3360 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3361 }
Craig Mautner580ea812013-04-25 12:58:38 -07003362 }
Jorim Jaggife762342016-10-13 14:33:27 +02003363 } finally {
3364 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003365 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003366 }
3367
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003368 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3369 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3370 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3371 final int topStackNdx = stacks.size() - 1;
3372 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3373 final ActivityStack stack = stacks.get(stackNdx);
3374 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3375 }
3376 }
3377 }
3378
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003379 void invalidateTaskLayers() {
3380 mTaskLayersChanged = true;
3381 }
3382
3383 void rankTaskLayersIfNeeded() {
3384 if (!mTaskLayersChanged) {
3385 return;
3386 }
3387 mTaskLayersChanged = false;
3388 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3389 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3390 int baseLayer = 0;
3391 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3392 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3393 }
3394 }
3395 }
3396
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003397 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3399 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3400 final int topStackNdx = stacks.size() - 1;
3401 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3402 final ActivityStack stack = stacks.get(stackNdx);
3403 stack.clearOtherAppTimeTrackers(except);
3404 }
3405 }
3406 }
3407
Craig Mautner8d341ef2013-03-26 09:03:27 -07003408 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003409 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3410 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 final int numStacks = stacks.size();
3412 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3413 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003414 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003416 }
3417 }
3418
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003419 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3420 // Examine all activities currently running in the process.
3421 TaskRecord firstTask = null;
3422 // Tasks is non-null only if two or more tasks are found.
3423 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003424 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3425 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003426 ActivityRecord r = app.activities.get(i);
3427 // First, if we find an activity that is in the process of being destroyed,
3428 // then we just aren't going to do anything for now; we want things to settle
3429 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003430 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003431 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003432 return;
3433 }
3434 // Don't consider any activies that are currently not in a state where they
3435 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003436 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3437 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003438 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003439 continue;
3440 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003441
3442 final TaskRecord task = r.getTask();
3443 if (task != null) {
3444 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003445 + " from " + r);
3446 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003447 firstTask = task;
3448 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003449 if (tasks == null) {
3450 tasks = new ArraySet<>();
3451 tasks.add(firstTask);
3452 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003453 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003454 }
3455 }
3456 }
3457 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003458 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003459 return;
3460 }
3461 // If we have activities in multiple tasks that are in a position to be destroyed,
3462 // let's iterate through the tasks and release the oldest one.
3463 final int numDisplays = mActivityDisplays.size();
3464 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3465 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3466 // Step through all stacks starting from behind, to hit the oldest things first.
3467 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3468 final ActivityStack stack = stacks.get(stackNdx);
3469 // Try to release activities in this stack; if we manage to, we are done.
3470 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3471 return;
3472 }
3473 }
3474 }
3475 }
3476
Amith Yamasani37a40c22015-06-17 13:25:42 -07003477 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003478 final int focusStackId = mFocusedStack.getStackId();
3479 // We dismiss the docked stack whenever we switch users.
3480 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003481 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3482 // also cause all tasks to be moved to the fullscreen stack at a position that is
3483 // appropriate.
3484 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003485
3486 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003487 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003488 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003489
Craig Mautner858d8a62013-04-23 17:08:34 -07003490 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003494 final ActivityStack stack = stacks.get(stackNdx);
3495 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003496 TaskRecord task = stack.topTask();
3497 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003498 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003499 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003500 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003501 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003502
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003503 ActivityStack stack = getStack(restoreStackId);
3504 if (stack == null) {
3505 stack = mHomeStack;
3506 }
3507 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003508 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003509 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003510 } else {
3511 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003512 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003513 }
Craig Mautner93529a42013-10-04 15:03:13 -07003514 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003515 }
3516
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003517 /** Checks whether the userid is a profile of the current user. */
3518 boolean isCurrentProfileLocked(int userId) {
3519 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003520 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003521 }
3522
Bryce Leeb7c9b802017-05-02 14:20:24 -07003523 /**
3524 * Returns whether a stopping activity is present that should be stopped after visible, rather
3525 * than idle.
3526 * @return {@code true} if such activity is present. {@code false} otherwise.
3527 */
3528 boolean isStoppingNoHistoryActivity() {
3529 // Activities that are marked as nohistory should be stopped immediately after the resumed
3530 // activity has become visible.
3531 for (ActivityRecord record : mStoppingActivities) {
3532 if (record.isNoHistory()) {
3533 return true;
3534 }
3535 }
3536
3537 return false;
3538 }
3539
Winson Chung4dabf232017-01-25 13:25:22 -08003540 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3541 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003542 ArrayList<ActivityRecord> stops = null;
3543
3544 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003545 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3546 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003547 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003548 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003549 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3550 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003551 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003552 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003553 if (s.finishing) {
3554 // If this activity is finishing, it is sitting on top of
3555 // everyone else but we now know it is no longer needed...
3556 // so get rid of it. Otherwise, we need to go through the
3557 // normal flow and hide it once we determine that it is
3558 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003559 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003560 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003561 }
3562 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003563 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003564 if (!processPausingActivities && s.state == PAUSING) {
3565 // Defer processing pausing activities in this iteration and reschedule
3566 // a delayed idle to reprocess it again
3567 removeTimeoutsForActivityLocked(idleActivity);
3568 scheduleIdleTimeoutLocked(idleActivity);
3569 continue;
3570 }
3571
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003572 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003573 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003574 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003575 }
3576 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003577 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003578 }
3579 }
3580
3581 return stops;
3582 }
3583
Craig Mautnercf910b02013-04-23 11:23:27 -07003584 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003585 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3586 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3587 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3588 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003589 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003590 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003591 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003592 if (r == null) Slog.e(TAG,
3593 "validateTop...: null top activity, stack=" + stack);
3594 else {
3595 final ActivityRecord pausing = stack.mPausingActivity;
3596 if (pausing != null && pausing == r) Slog.e(TAG,
3597 "validateTop...: top stack has pausing activity r=" + r
3598 + " state=" + state);
3599 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3600 "validateTop...: activity in front not resumed r=" + r
3601 + " state=" + state);
3602 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003603 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003604 final ActivityRecord resumed = stack.mResumedActivity;
3605 if (resumed != null && resumed == r) Slog.e(TAG,
3606 "validateTop...: back stack has resumed activity r=" + r
3607 + " state=" + state);
3608 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3609 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003610 }
3611 }
3612 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003613 }
3614
Craig Mautnere0570202015-05-13 13:06:11 -07003615 private String lockTaskModeToString() {
3616 switch (mLockTaskModeState) {
3617 case LOCK_TASK_MODE_LOCKED:
3618 return "LOCKED";
3619 case LOCK_TASK_MODE_PINNED:
3620 return "PINNED";
3621 case LOCK_TASK_MODE_NONE:
3622 return "NONE";
3623 default: return "unknown=" + mLockTaskModeState;
3624 }
3625 }
3626
Craig Mautner27084302013-03-25 08:05:25 -07003627 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003628 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003629 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003630 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003631 pw.print(prefix);
3632 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003633 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003634 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003635 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003636 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3637 if (packages.size() > 0) {
3638 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3639 for (int i = 0; i < packages.size(); ++i) {
3640 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3641 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3642 }
3643 }
Bryce Lee4a194382017-04-04 14:32:48 -07003644 if (!mWaitingForActivityVisible.isEmpty()) {
3645 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3646 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3647 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3648 }
3649 }
3650
Jorim Jaggi8d786932016-10-26 19:08:36 -07003651 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3652 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003653 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654
Winson43d1f262016-06-14 16:05:55 -07003655 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003656 * Dump all connected displays' configurations.
3657 * @param prefix Prefix to apply to each line of the dump.
3658 */
3659 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3660 pw.print(prefix); pw.println("Display override configurations:");
3661 final int displayCount = mActivityDisplays.size();
3662 for (int i = 0; i < displayCount; i++) {
3663 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3664 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3665 pw.println(activityDisplay.getOverrideConfiguration());
3666 }
3667 }
3668
3669 /**
Winson43d1f262016-06-14 16:05:55 -07003670 * Dumps the activities matching the given {@param name} in the either the focused stack
3671 * or all visible stacks if {@param dumpVisibleStacks} is true.
3672 */
Winson Chung6998bc42017-02-28 17:07:05 -08003673 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3674 boolean dumpFocusedStackOnly) {
3675 if (dumpFocusedStackOnly) {
3676 return mFocusedStack.getDumpActivitiesLocked(name);
3677 } else {
Winson43d1f262016-06-14 16:05:55 -07003678 ArrayList<ActivityRecord> activities = new ArrayList<>();
3679 int numDisplays = mActivityDisplays.size();
3680 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3681 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3682 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3683 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003684 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003685 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003686 activities.addAll(stack.getDumpActivitiesLocked(name));
3687 }
3688 }
3689 }
3690 return activities;
Winson43d1f262016-06-14 16:05:55 -07003691 }
Craig Mautner20e72272013-04-01 13:45:53 -07003692 }
3693
Dianne Hackborn390517b2013-05-30 15:03:32 -07003694 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3695 boolean needSep, String prefix) {
3696 if (activity != null) {
3697 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3698 if (needSep) {
3699 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003700 }
3701 pw.print(prefix);
3702 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003703 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003704 }
3705 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003706 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003707 }
3708
Craig Mautner8d341ef2013-03-26 09:03:27 -07003709 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3710 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003711 boolean printed = false;
3712 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003713 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3714 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003715 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003716 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003717 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719 final ActivityStack stack = stacks.get(stackNdx);
3720 StringBuilder stackHeader = new StringBuilder(128);
3721 stackHeader.append(" Stack #");
3722 stackHeader.append(stack.mStackId);
3723 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003724 stackHeader.append("\n");
3725 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3726 stackHeader.append("\n");
3727 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003728
3729 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3730 dumpClient, dumpPackage, needSep, stackHeader.toString());
3731 printed |= printedStackHeader;
3732 if (!printedStackHeader) {
3733 // Ensure we always dump the stack header even if there are no activities
3734 pw.println();
3735 pw.println(stackHeader);
3736 }
3737
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3739 !dumpAll, false, dumpPackage, true,
3740 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003741
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 needSep = printed;
3743 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3744 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003745 if (pr) {
3746 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003747 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003748 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003749 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3750 " mResumedActivity: ");
3751 if (pr) {
3752 printed = true;
3753 needSep = false;
3754 }
3755 if (dumpAll) {
3756 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3757 " mLastPausedActivity: ");
3758 if (pr) {
3759 printed = true;
3760 needSep = true;
3761 }
3762 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3763 needSep, " mLastNoHistoryActivity: ");
3764 }
3765 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003766 }
3767 }
3768
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003769 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3770 false, dumpPackage, true, " Activities waiting to finish:", null);
3771 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3772 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003773 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3774 false, !dumpAll, false, dumpPackage, true,
3775 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003776 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3777 false, dumpPackage, true, " Activities waiting to sleep:", null);
3778 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3779 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003780
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003781 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003782 }
3783
Dianne Hackborn390517b2013-05-30 15:03:32 -07003784 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003785 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003786 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003787 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003788 String innerPrefix = null;
3789 String[] args = null;
3790 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003791 for (int i=list.size()-1; i>=0; i--) {
3792 final ActivityRecord r = list.get(i);
3793 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3794 continue;
3795 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003796 if (innerPrefix == null) {
3797 innerPrefix = prefix + " ";
3798 args = new String[0];
3799 }
3800 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003801 final boolean full = !brief && (complete || !r.isInHistory());
3802 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003803 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003804 needNL = false;
3805 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003806 if (header1 != null) {
3807 pw.println(header1);
3808 header1 = null;
3809 }
3810 if (header2 != null) {
3811 pw.println(header2);
3812 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003813 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003814 if (lastTask != r.getTask()) {
3815 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003816 pw.print(prefix);
3817 pw.print(full ? "* " : " ");
3818 pw.println(lastTask);
3819 if (full) {
3820 lastTask.dump(pw, prefix + " ");
3821 } else if (complete) {
3822 // Complete + brief == give a summary. Isn't that obvious?!?
3823 if (lastTask.intent != null) {
3824 pw.print(prefix); pw.print(" ");
3825 pw.println(lastTask.intent.toInsecureStringWithClip());
3826 }
3827 }
3828 }
3829 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3830 pw.print(" #"); pw.print(i); pw.print(": ");
3831 pw.println(r);
3832 if (full) {
3833 r.dump(pw, innerPrefix);
3834 } else if (complete) {
3835 // Complete + brief == give a summary. Isn't that obvious?!?
3836 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3837 if (r.app != null) {
3838 pw.print(innerPrefix); pw.println(r.app);
3839 }
3840 }
3841 if (client && r.app != null && r.app.thread != null) {
3842 // flush anything that is already in the PrintWriter since the thread is going
3843 // to write to the file descriptor directly
3844 pw.flush();
3845 try {
3846 TransferPipe tp = new TransferPipe();
3847 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003848 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003849 // Short timeout, since blocking here can
3850 // deadlock with the application.
3851 tp.go(fd, 2000);
3852 } finally {
3853 tp.kill();
3854 }
3855 } catch (IOException e) {
3856 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3857 } catch (RemoteException e) {
3858 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3859 }
3860 needNL = true;
3861 }
3862 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003863 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003864 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003865
Craig Mautnerf3333272013-04-22 10:55:53 -07003866 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003867 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3868 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003869 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3870 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003871 }
3872
3873 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003874 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003875 }
3876
3877 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003878 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3879 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003880 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3881 }
3882
Craig Mautner05d29032013-05-03 13:40:13 -07003883 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003884 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3885 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3886 }
Craig Mautner05d29032013-05-03 13:40:13 -07003887 }
3888
Craig Mautner0eea92c2013-05-16 13:35:39 -07003889 void removeSleepTimeouts() {
3890 mSleepTimeout = false;
3891 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3892 }
3893
3894 final void scheduleSleepTimeout() {
3895 removeSleepTimeouts();
3896 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3897 }
3898
Craig Mautner4a1cb222013-12-04 16:14:06 -08003899 @Override
3900 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003901 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003902 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3903 }
3904
3905 @Override
3906 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003907 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003908 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3909 }
3910
3911 @Override
3912 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003913 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3915 }
3916
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003917 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003918 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003920 newDisplay = mActivityDisplays.get(displayId) == null;
3921 if (newDisplay) {
3922 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003923 if (activityDisplay.mDisplay == null) {
3924 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3925 return;
3926 }
Craig Mautner4504de52013-12-20 09:06:56 -08003927 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003928 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003929 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003930 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003932 }
3933
Andrii Kulianca007a62016-11-22 16:33:28 -08003934 /** Check if display with specified id is added to the list. */
3935 boolean isDisplayAdded(int displayId) {
3936 return mActivityDisplays.get(displayId) != null;
3937 }
3938
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003939 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003940 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003941 mService.mContext.getResources().getDimensionPixelSize(
3942 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003943 }
3944
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003945 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003946 if (displayId == DEFAULT_DISPLAY) {
3947 throw new IllegalArgumentException("Can't remove the primary display.");
3948 }
3949
Craig Mautner4a1cb222013-12-04 16:14:06 -08003950 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003951 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3952 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003953 final boolean destroyContentOnRemoval
3954 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003955 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3956 while (!stacks.isEmpty()) {
3957 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003958 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003959 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3960 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003961 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003962 } else {
3963 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3964 // a valid launch stack for all activities. This way the task history from
3965 // external display will be preserved on primary after move.
3966 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003967 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003968 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003969 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003970 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003971 }
3972 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 }
3974
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003975 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003977 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3978 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003979 // TODO: Update the bounds.
3980 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003981 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003983 }
3984
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003985 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07003986 final int displayId = stack.mDisplayId;
3987 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003988 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003989 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07003990 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003992 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003993 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003994 info.position = display != null
3995 ? display.mStacks.indexOf(stack)
3996 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997
3998 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3999 final int numTasks = tasks.size();
4000 int[] taskIds = new int[numTasks];
4001 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004002 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004003 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 for (int i = 0; i < numTasks; ++i) {
4005 final TaskRecord task = tasks.get(i);
4006 taskIds[i] = task.taskId;
4007 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4008 : task.realActivity != null ? task.realActivity.flattenToString()
4009 : task.getTopActivity() != null ? task.getTopActivity().packageName
4010 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004011 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004012 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004013 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 }
4015 info.taskIds = taskIds;
4016 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004017 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004018 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004019
4020 final ActivityRecord top = stack.topRunningActivityLocked();
4021 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004022 return info;
4023 }
4024
4025 StackInfo getStackInfoLocked(int stackId) {
4026 ActivityStack stack = getStack(stackId);
4027 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004028 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029 }
4030 return null;
4031 }
4032
4033 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004034 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004035 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4036 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004037 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004038 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004039 }
4040 }
4041 return list;
4042 }
4043
Craig Mautner15df08a2015-04-01 12:17:18 -07004044 TaskRecord getLockedTaskLocked() {
4045 final int top = mLockTaskModeTasks.size() - 1;
4046 if (top >= 0) {
4047 return mLockTaskModeTasks.get(top);
4048 }
4049 return null;
4050 }
4051
4052 boolean isLockedTask(TaskRecord task) {
4053 return mLockTaskModeTasks.contains(task);
4054 }
4055
4056 boolean isLastLockedTask(TaskRecord task) {
4057 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4058 }
4059
4060 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004061 if (!mLockTaskModeTasks.remove(task)) {
4062 return;
4063 }
4064 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4065 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004066 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004067 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4068 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004069 final Message lockTaskMsg = Message.obtain();
4070 lockTaskMsg.arg1 = task.userId;
4071 lockTaskMsg.what = LOCK_TASK_END_MSG;
4072 mHandler.sendMessage(lockTaskMsg);
4073 }
4074 }
4075
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004076 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4077 int preferredDisplayId, int actualStackId) {
4078 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004079 false /* forceNonResizable */);
4080 }
4081
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004082 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4083 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4084 final boolean isSecondaryDisplayPreferred =
4085 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4086 || StackId.isDynamicStack(preferredStackId);
4087 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4088 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004089 return;
4090 }
4091
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004092 // Handle incorrect launch/move to secondary display if needed.
4093 final boolean launchOnSecondaryDisplayFailed;
4094 if (isSecondaryDisplayPreferred) {
4095 final int actualDisplayId = task.getStack().mDisplayId;
4096 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4097 // The task landed on an inappropriate display somehow, move it to the default
4098 // display.
4099 // TODO(multi-display): Find proper stack for the task on the default display.
4100 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4101 true /* toTop */);
4102 launchOnSecondaryDisplayFailed = true;
4103 } else {
4104 // The task might have landed on a display different from requested.
4105 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4106 || (preferredDisplayId != INVALID_DISPLAY
4107 && preferredDisplayId != actualDisplayId);
4108 }
4109 } else {
4110 // The task wasn't requested to be on a secondary display.
4111 launchOnSecondaryDisplayFailed = false;
4112 }
4113
Jorim Jaggicd13d332016-04-27 15:40:20 -07004114 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004115 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4116 if (launchOnSecondaryDisplayFailed) {
4117 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4118 // display with config different from global config.
4119 mService.mTaskChangeNotificationController
4120 .notifyActivityLaunchOnSecondaryDisplayFailed();
4121 } else {
4122 // Display a warning toast that we tried to put a non-dockable task in the docked
4123 // stack.
4124 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4125 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004126
Andrii Kulianc27916642016-04-12 17:59:27 -07004127 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4128 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004129 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004130 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004131 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004132 final String packageName = topActivity.appInfo.packageName;
4133 final int reason = isSecondaryDisplayPreferred
4134 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4135 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004136 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004137 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004138 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004139 }
4140
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004141 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004142 if (mLockTaskNotify != null) {
4143 mLockTaskNotify.showToast(mLockTaskModeState);
4144 }
Jason Monka8f569c2014-07-07 12:15:21 -04004145 }
4146
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004147 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4148 if (mLockTaskModeTasks.contains(task)) {
4149 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4150 }
4151 }
4152
Craig Mautner432f64e2015-05-20 14:59:57 -07004153 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4154 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004155 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004156 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004157 final TaskRecord lockedTask = getLockedTaskLocked();
4158 if (lockedTask != null) {
4159 removeLockedTaskLocked(lockedTask);
4160 if (!mLockTaskModeTasks.isEmpty()) {
4161 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004162 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4163 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004164 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004165 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004166 return;
4167 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004168 }
Craig Mautnere0570202015-05-13 13:06:11 -07004169 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4170 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004171 return;
4172 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004173
4174 // Should have already been checked, but do it again.
4175 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004176 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4177 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004178 return;
4179 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004180 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004181 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004182 return;
4183 }
4184
4185 if (mLockTaskModeTasks.isEmpty()) {
4186 // First locktask.
4187 final Message lockTaskMsg = Message.obtain();
4188 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4189 lockTaskMsg.arg1 = task.userId;
4190 lockTaskMsg.what = LOCK_TASK_START_MSG;
4191 lockTaskMsg.arg2 = lockTaskModeState;
4192 mHandler.sendMessage(lockTaskMsg);
4193 }
4194 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004195 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4196 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004197 mLockTaskModeTasks.remove(task);
4198 mLockTaskModeTasks.add(task);
4199
4200 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004201 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004202 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004203
4204 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004205 findTaskToMoveToFrontLocked(task, 0, null, reason,
4206 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004207 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004208 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004209 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004210 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4211 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004212 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004213 }
4214
4215 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004216 return isLockTaskModeViolation(task, false);
4217 }
4218
4219 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4220 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004221 return false;
4222 }
4223 final int lockTaskAuth = task.mLockTaskAuth;
4224 switch (lockTaskAuth) {
4225 case LOCK_TASK_AUTH_DONT_LOCK:
4226 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004227 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004228 case LOCK_TASK_AUTH_LAUNCHABLE:
4229 case LOCK_TASK_AUTH_WHITELISTED:
4230 return false;
4231 case LOCK_TASK_AUTH_PINNABLE:
4232 // Pinnable tasks can't be launched on top of locktask tasks.
4233 return !mLockTaskModeTasks.isEmpty();
4234 default:
4235 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4236 return true;
4237 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004238 }
4239
Craig Mautner15df08a2015-04-01 12:17:18 -07004240 void onLockTaskPackagesUpdatedLocked() {
4241 boolean didSomething = false;
4242 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4243 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004244 final boolean wasWhitelisted =
4245 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4246 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004247 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004248 final boolean isWhitelisted =
4249 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4250 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4251 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004252 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004253 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4254 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004255 removeLockedTaskLocked(lockedTask);
4256 lockedTask.performClearTaskLocked();
4257 didSomething = true;
4258 }
4259 }
4260 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4261 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4262 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4263 final ActivityStack stack = stacks.get(stackNdx);
4264 stack.onLockTaskPackagesUpdatedLocked();
4265 }
4266 }
Craig Mautnere0570202015-05-13 13:06:11 -07004267 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004268 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004269 if (mLockTaskModeTasks.isEmpty() && task != null
4270 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4271 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004272 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4273 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4274 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4275 false);
4276 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004277 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004278 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004279 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004280 }
4281 }
4282
Benjamin Franz43261142015-02-11 15:59:44 +00004283 int getLockTaskModeState() {
4284 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004285 }
4286
Jorim Jaggife89d122015-12-22 16:28:44 +01004287 void activityRelaunchedLocked(IBinder token) {
4288 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004289 if (mService.isSleepingOrShuttingDownLocked()) {
4290 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4291 if (r != null) {
4292 r.setSleeping(true, true);
4293 }
4294 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004295 }
4296
4297 void activityRelaunchingLocked(ActivityRecord r) {
4298 mWindowManager.notifyAppRelaunching(r.appToken);
4299 }
4300
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004301 void logStackState() {
4302 mActivityMetricsLogger.logWindowState();
4303 }
4304
Winson Chung5af42fc2017-03-24 17:11:33 -07004305 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004306 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4307 // end, then ensure that we defer all in between multi-window mode changes
4308 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4309 return;
4310 }
4311
Wale Ogunwale22e25262016-02-01 10:32:02 -08004312 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4313 final ActivityRecord r = task.mActivities.get(i);
4314 if (r.app != null && r.app.thread != null) {
4315 mMultiWindowModeChangedActivities.add(r);
4316 }
4317 }
4318
4319 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4320 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4321 }
4322 }
4323
Winson Chung5af42fc2017-03-24 17:11:33 -07004324 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004325 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004326 if (prevStack == null || prevStack == stack
4327 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4328 return;
4329 }
4330
Winson Chung5af42fc2017-03-24 17:11:33 -07004331 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4332 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004333
Winson Chung5af42fc2017-03-24 17:11:33 -07004334 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4335 boolean immediate) {
4336
4337 if (immediate) {
4338 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4339 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4340 final ActivityRecord r = task.mActivities.get(i);
4341 if (r.app != null && r.app.thread != null) {
4342 r.updatePictureInPictureMode(targetStackBounds);
4343 }
4344 }
4345 } else {
4346 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4347 final ActivityRecord r = task.mActivities.get(i);
4348 if (r.app != null && r.app.thread != null) {
4349 mPipModeChangedActivities.add(r);
4350 }
4351 }
4352 mPipModeChangedTargetStackBounds = targetStackBounds;
4353
4354 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4355 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4356 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004357 }
4358 }
4359
Tony Mak853304c2016-04-18 15:17:41 +01004360 void setDockedStackMinimized(boolean minimized) {
4361 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004362 }
4363
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004364 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004365
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004366 public ActivityStackSupervisorHandler(Looper looper) {
4367 super(looper);
4368 }
4369
Winson Chung4dabf232017-01-25 13:25:22 -08004370 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004371 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004372 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4373 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004374 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004375 }
4376
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004377 @Override
4378 public void handleMessage(Message msg) {
4379 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004380 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4381 synchronized (mService) {
4382 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4383 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004384 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004385 }
4386 }
4387 } break;
4388 case REPORT_PIP_MODE_CHANGED_MSG: {
4389 synchronized (mService) {
4390 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4391 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004392 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004393 }
4394 }
4395 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004396 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004397 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4398 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004399 if (mService.mDidDexOpt) {
4400 mService.mDidDexOpt = false;
4401 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4402 nmsg.obj = msg.obj;
4403 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4404 return;
4405 }
4406 // We don't at this point know if the activity is fullscreen,
4407 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004408 activityIdleInternal((ActivityRecord) msg.obj,
4409 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004410 } break;
4411 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004412 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004413 activityIdleInternal((ActivityRecord) msg.obj,
4414 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004415 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004416 case RESUME_TOP_ACTIVITY_MSG: {
4417 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004418 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004419 }
4420 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004421 case SLEEP_TIMEOUT_MSG: {
4422 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004423 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004424 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4425 mSleepTimeout = true;
4426 checkReadyForSleepLocked();
4427 }
4428 }
4429 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004430 case LAUNCH_TIMEOUT_MSG: {
4431 if (mService.mDidDexOpt) {
4432 mService.mDidDexOpt = false;
4433 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4434 return;
4435 }
4436 synchronized (mService) {
4437 if (mLaunchingActivity.isHeld()) {
4438 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4439 if (VALIDATE_WAKE_LOCK_CALLER
4440 && Binder.getCallingUid() != Process.myUid()) {
4441 throw new IllegalStateException("Calling must be system uid");
4442 }
4443 mLaunchingActivity.release();
4444 }
4445 }
4446 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004447 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004448 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004449 } break;
4450 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004451 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004452 } break;
4453 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004454 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004455 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004456 case CONTAINER_CALLBACK_VISIBILITY: {
4457 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004458 final IActivityContainerCallback callback = container.mCallback;
4459 if (callback != null) {
4460 try {
4461 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4462 } catch (RemoteException e) {
4463 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004464 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004465 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004466 case LOCK_TASK_START_MSG: {
4467 // When lock task starts, we disable the status bars.
4468 try {
Jason Monk62515be2014-05-21 16:06:19 -04004469 if (mLockTaskNotify == null) {
4470 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004471 }
Jason Monk62515be2014-05-21 16:06:19 -04004472 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004473 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004474 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004475 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004476 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004477 flags = StatusBarManager.DISABLE_MASK
4478 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004479 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004480 flags = StatusBarManager.DISABLE_MASK
4481 & (~StatusBarManager.DISABLE_BACK)
4482 & (~StatusBarManager.DISABLE_HOME)
4483 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004484 }
4485 getStatusBarService().disable(flags, mToken,
4486 mService.mContext.getPackageName());
4487 }
4488 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004489 if (getDevicePolicyManager() != null) {
4490 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004491 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004492 }
justinzhang5286d3f2014-05-12 17:06:01 -04004493 } catch (RemoteException ex) {
4494 throw new RuntimeException(ex);
4495 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004496 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004497 case LOCK_TASK_END_MSG: {
4498 // When lock task ends, we enable the status bars.
4499 try {
Jason Monk62515be2014-05-21 16:06:19 -04004500 if (getStatusBarService() != null) {
4501 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4502 mService.mContext.getPackageName());
4503 }
4504 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004505 if (getDevicePolicyManager() != null) {
4506 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4507 msg.arg1);
4508 }
Jason Monk62515be2014-05-21 16:06:19 -04004509 if (mLockTaskNotify == null) {
4510 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4511 }
4512 mLockTaskNotify.show(false);
4513 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004514 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004515 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004516 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004517 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004518 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004519 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004520 new LockPatternUtils(mService.mContext)
4521 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004522 }
4523 } catch (SettingNotFoundException e) {
4524 // No setting, don't lock.
4525 }
justinzhang5286d3f2014-05-12 17:06:01 -04004526 } catch (RemoteException ex) {
4527 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004528 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004529 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004530 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004531 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004532 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4533 if (mLockTaskNotify == null) {
4534 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4535 }
4536 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4537 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004538 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4539 final ActivityContainer container = (ActivityContainer) msg.obj;
4540 final IActivityContainerCallback callback = container.mCallback;
4541 if (callback != null) {
4542 try {
4543 callback.onAllActivitiesComplete(container.asBinder());
4544 } catch (RemoteException e) {
4545 }
4546 }
4547 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004548 case LAUNCH_TASK_BEHIND_COMPLETE: {
4549 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004550 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004551 if (r != null) {
4552 handleLaunchTaskBehindCompleteLocked(r);
4553 }
4554 }
4555 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004556
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004557 }
4558 }
4559 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004560
Ying Wangb081a592014-04-22 15:20:16 -07004561 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004562 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4563 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004564 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004565 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004566 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004567 ActivityRecord mParentActivity = null;
4568 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004569
Craig Mautnere3a00d72014-04-16 08:31:19 -07004570 boolean mVisible = true;
4571
Craig Mautner4a1cb222013-12-04 16:14:06 -08004572 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004573 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004574
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004575 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4576 final static int CONTAINER_STATE_NO_SURFACE = 1;
4577 final static int CONTAINER_STATE_FINISHING = 2;
4578 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4579
Wale Ogunwale1666e312016-12-16 11:27:18 -08004580 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004581 synchronized (mService) {
4582 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004583 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004584 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004585
4586 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004587 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004588 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004589 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004590
Bryce Leeaf691c02017-03-20 14:20:22 -07004591 protected void createStack(int stackId, boolean onTop) {
4592 switch (stackId) {
4593 case PINNED_STACK_ID:
4594 new PinnedActivityStack(this, mRecentTasks, onTop);
4595 break;
4596 default:
4597 new ActivityStack(this, mRecentTasks, onTop);
4598 break;
4599 }
4600 }
4601
Andrii Kulian839def92016-11-02 10:58:58 -07004602 /**
4603 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004604 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004605 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004606 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004607 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004608 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004609 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004610 if (mActivityDisplay != null) {
4611 throw new IllegalStateException("ActivityContainer is already attached, " +
4612 "displayId=" + mActivityDisplay.mDisplayId);
4613 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004614 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004615 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004616 }
4617
4618 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004619 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004620 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004621 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4622 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004623 return;
4624 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004625 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004626 }
4627 }
4628
4629 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004630 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004631 synchronized (mService) {
4632 if (mActivityDisplay != null) {
4633 return mActivityDisplay.mDisplayId;
4634 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004635 }
4636 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004637 }
4638
Jeff Brownca9bc702014-02-11 14:32:56 -08004639 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004640 public int getStackId() {
4641 synchronized (mService) {
4642 return mStackId;
4643 }
4644 }
4645
4646 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004647 public boolean injectEvent(InputEvent event) {
4648 final long origId = Binder.clearCallingIdentity();
4649 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004650 synchronized (mService) {
4651 if (mActivityDisplay != null) {
4652 return mInputManagerInternal.injectInputEvent(event,
4653 mActivityDisplay.mDisplayId,
4654 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4655 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004656 }
4657 return false;
4658 } finally {
4659 Binder.restoreCallingIdentity(origId);
4660 }
4661 }
4662
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004663 @Override
4664 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004665 synchronized (mService) {
4666 if (mContainerState == CONTAINER_STATE_FINISHING) {
4667 return;
4668 }
4669 mContainerState = CONTAINER_STATE_FINISHING;
4670
Craig Mautnerd163e752014-06-13 17:18:47 -07004671 long origId = Binder.clearCallingIdentity();
4672 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004673 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004674 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004675 } finally {
4676 Binder.restoreCallingIdentity(origId);
4677 }
4678 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004679 }
4680
Andrii Kulian03c403d2016-11-11 11:14:12 -08004681 /**
4682 * Remove the stack completely. Must be called only when there are no tasks left in it,
4683 * as this method does not finish running activities.
4684 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004685 void removeLocked() {
4686 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004687 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004688 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004689 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004690 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004691 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004692 }
4693
Andrii Kulian839def92016-11-02 10:58:58 -07004694 /**
4695 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4696 * completely or re-parented.
4697 */
4698 private void removeFromDisplayLocked() {
4699 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4700 + " current displayId=" + mActivityDisplay.mDisplayId);
4701
Wale Ogunwale1666e312016-12-16 11:27:18 -08004702 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004703 mActivityDisplay = null;
4704 }
4705
4706 /**
4707 * Move the stack to specified display.
4708 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004709 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004710 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004711 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004712 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4713 + mActivityDisplay + " to display=" + activityDisplay
4714 + " Callers=" + Debug.getCallers(2));
4715
4716 removeFromDisplayLocked();
4717
4718 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004719 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004720 }
4721
Craig Mautner4a1cb222013-12-04 16:14:06 -08004722 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004723 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004724 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004725 }
4726
4727 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004728 public final int startActivityIntentSender(IIntentSender intentSender)
4729 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004730 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4731
4732 if (!(intentSender instanceof PendingIntentRecord)) {
4733 throw new IllegalArgumentException("Bad PendingIntent object");
4734 }
4735
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004736 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004737 Binder.getCallingUid(), mCurrentUser, false,
4738 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004739
4740 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4741 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4742 pendingIntent.key.requestResolvedType);
4743
Dianne Hackborn98305522017-05-05 17:53:53 -07004744 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004745 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4746 }
4747
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004748 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004749 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004750 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004751 throw new SecurityException(
4752 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4753 }
4754 }
4755
Craig Mautnerdf88d732014-01-27 09:21:32 -08004756 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004757 public IBinder asBinder() {
4758 return this;
4759 }
4760
Craig Mautner4504de52013-12-20 09:06:56 -08004761 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004762 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004763 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004764 }
4765
Craig Mautner4a1cb222013-12-04 16:14:06 -08004766 ActivityStackSupervisor getOuter() {
4767 return ActivityStackSupervisor.this;
4768 }
4769
Craig Mautnerd163e752014-06-13 17:18:47 -07004770 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004771 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004772 }
4773
Craig Mautner6985bad2014-04-21 15:22:06 -07004774 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004775 void setVisible(boolean visible) {
4776 if (mVisible != visible) {
4777 mVisible = visible;
4778 if (mCallback != null) {
4779 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4780 0 /* unused */, this).sendToTarget();
4781 }
4782 }
4783 }
4784
Craig Mautner6985bad2014-04-21 15:22:06 -07004785 void setDrawn() {
4786 }
4787
Craig Mautner1b4bf852014-05-26 15:06:32 -07004788 // You can always start a new task on a regular ActivityStack.
4789 boolean isEligibleForNewTasks() {
4790 return true;
4791 }
4792
Craig Mautnerd163e752014-06-13 17:18:47 -07004793 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004794 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004795 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004796 }
4797
Craig Mautner34b73df2014-01-12 21:11:08 -08004798 @Override
4799 public String toString() {
4800 return mIdString + (mActivityDisplay == null ? "N" : "A");
4801 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004802 }
4803
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004804 private class VirtualActivityContainer extends ActivityContainer {
4805 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004806 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004807
4808 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004809 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4810 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004811 mParentActivity = parent;
4812 mCallback = callback;
4813 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004814 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004815 }
4816
4817 @Override
4818 public void setSurface(Surface surface, int width, int height, int density) {
4819 super.setSurface(surface, width, height, density);
4820
4821 synchronized (mService) {
4822 final long origId = Binder.clearCallingIdentity();
4823 try {
4824 setSurfaceLocked(surface, width, height, density);
4825 } finally {
4826 Binder.restoreCallingIdentity(origId);
4827 }
4828 }
4829 }
4830
4831 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4832 if (mContainerState == CONTAINER_STATE_FINISHING) {
4833 return;
4834 }
4835 VirtualActivityDisplay virtualActivityDisplay =
4836 (VirtualActivityDisplay) mActivityDisplay;
4837 if (virtualActivityDisplay == null) {
4838 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004839 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004840 mActivityDisplay = virtualActivityDisplay;
4841 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004842 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004843 }
4844
4845 if (mSurface != null) {
4846 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004847 }
4848
Craig Mautner6985bad2014-04-21 15:22:06 -07004849 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004850 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004851 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004852 } else {
4853 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004854 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004855 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004856 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004857 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004858 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004859
Craig Mautnerd163e752014-06-13 17:18:47 -07004860 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004861
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004862 if (DEBUG_STACK) Slog.d(TAG_STACK,
4863 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004864 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004865
Craig Mautner6985bad2014-04-21 15:22:06 -07004866 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004867 boolean isAttachedLocked() {
4868 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004869 }
4870
4871 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004872 void setDrawn() {
4873 synchronized (mService) {
4874 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004875 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004876 }
4877 }
4878
Craig Mautner1b4bf852014-05-26 15:06:32 -07004879 // Never start a new task on an ActivityView if it isn't explicitly specified.
4880 @Override
4881 boolean isEligibleForNewTasks() {
4882 return false;
4883 }
4884
Craig Mautnerd163e752014-06-13 17:18:47 -07004885 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004886 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004887 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4888 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4889 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4890 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4891 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004892 }
4893 }
4894
Craig Mautner4a1cb222013-12-04 16:14:06 -08004895 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4896 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004897 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004898 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004899 int mDisplayId;
4900 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004901
Craig Mautner4a1cb222013-12-04 16:14:06 -08004902 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4903 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004904 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004905
Jose Lima4b6c6692014-08-12 17:41:12 -07004906 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004907
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004908 /** Array of all UIDs that are present on the display. */
4909 private IntArray mDisplayAccessUIDs = new IntArray();
4910
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004911 ActivityDisplay() {
4912 }
Craig Mautner4504de52013-12-20 09:06:56 -08004913
Craig Mautner1a70a162014-09-13 12:09:31 -07004914 // After instantiation, check that mDisplay is not null before using this. The alternative
4915 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004916 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004917 final Display display = mDisplayManager.getDisplay(displayId);
4918 if (display == null) {
4919 return;
4920 }
4921 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004922 }
4923
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004924 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004925 mDisplay = display;
4926 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004927 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004928
Winson Chung7c3ede32017-03-14 14:18:34 -07004929 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004930 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004931 + " to displayId=" + mDisplayId + " position=" + position);
4932 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004933 }
4934
Wale Ogunwale1666e312016-12-16 11:27:18 -08004935 void detachStack(ActivityStack stack) {
4936 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004937 + " from displayId=" + mDisplayId);
4938 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004939 }
4940
Jose Lima4b6c6692014-08-12 17:41:12 -07004941 void setVisibleBehindActivity(ActivityRecord r) {
4942 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004943 }
4944
Jose Lima4b6c6692014-08-12 17:41:12 -07004945 boolean hasVisibleBehindActivity() {
4946 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004947 }
4948
Craig Mautner34b73df2014-01-12 21:11:08 -08004949 @Override
4950 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004951 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4952 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004953
4954 @Override
4955 protected int getChildCount() {
4956 return mStacks.size();
4957 }
4958
4959 @Override
4960 protected ConfigurationContainer getChildAt(int index) {
4961 return mStacks.get(index);
4962 }
4963
4964 @Override
4965 protected ConfigurationContainer getParent() {
4966 return ActivityStackSupervisor.this;
4967 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004968
4969 boolean isPrivate() {
4970 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4971 }
4972
4973 boolean isUidPresent(int uid) {
4974 for (ActivityStack stack : mStacks) {
4975 if (stack.isUidPresent(uid)) {
4976 return true;
4977 }
4978 }
4979 return false;
4980 }
4981
4982 /** Update and get all UIDs that are present on the display and have access to it. */
4983 private IntArray getPresentUIDs() {
4984 mDisplayAccessUIDs.clear();
4985 for (ActivityStack stack : mStacks) {
4986 stack.getPresentUIDs(mDisplayAccessUIDs);
4987 }
4988 return mDisplayAccessUIDs;
4989 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004990
4991 boolean shouldDestroyContentOnRemove() {
4992 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4993 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004994 }
4995
4996 class VirtualActivityDisplay extends ActivityDisplay {
4997 VirtualDisplay mVirtualDisplay;
4998
Craig Mautner6985bad2014-04-21 15:22:06 -07004999 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005000 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonb0608632017-04-05 10:31:15 -07005001 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5002 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005003 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonb0608632017-04-05 10:31:15 -07005004 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5005 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005006
5007 init(mVirtualDisplay.getDisplay());
5008
Bryce Leeaf3a4002017-03-20 10:37:12 -07005009 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005010 }
5011
5012 void setSurface(Surface surface) {
5013 if (mVirtualDisplay != null) {
5014 mVirtualDisplay.setSurface(surface);
5015 }
5016 }
5017
5018 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005019 void detachStack(ActivityStack stack) {
5020 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005021 if (mVirtualDisplay != null) {
5022 mVirtualDisplay.release();
5023 mVirtualDisplay = null;
5024 }
5025 }
5026
5027 @Override
5028 public String toString() {
5029 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005030 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005031 }
Jose Lima58e66d62014-05-27 20:00:27 -07005032
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005033 ActivityStack findStackBehind(ActivityStack stack) {
5034 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005035 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005036 if (display == null) {
5037 return null;
5038 }
5039 final ArrayList<ActivityStack> stacks = display.mStacks;
5040 for (int i = stacks.size() - 1; i >= 0; i--) {
5041 if (stacks.get(i) == stack && i > 0) {
5042 return stacks.get(i - 1);
5043 }
5044 }
5045 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5046 + " in=" + stacks);
5047 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005048
Jorim Jaggic69bd222016-03-15 14:38:37 +01005049 /**
5050 * Puts a task into resizing mode during the next app transition.
5051 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005052 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005053 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005054 private void setResizingDuringAnimation(TaskRecord task) {
5055 mResizingTasksDuringAnimation.add(task.taskId);
5056 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005057 }
5058
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005059 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5060 final TaskRecord task;
5061 final int callingUid;
5062 final String callingPackage;
5063 final Intent intent;
5064 final int userId;
5065 final ActivityOptions activityOptions = (bOptions != null)
5066 ? new ActivityOptions(bOptions) : null;
5067 final int launchStackId = (activityOptions != null)
5068 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005069 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005070 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005071 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005072 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005073
5074 if (launchStackId == DOCKED_STACK_ID) {
5075 mWindowManager.setDockedStackCreateState(
5076 activityOptions.getDockCreateMode(), null /* initialBounds */);
5077
5078 // Defer updating the stack in which recents is until the app transition is done, to
5079 // not run into issues where we still need to draw the task in recents but the
5080 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08005081 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005082 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
5083 }
5084
Winson Chung7900bee2017-03-10 08:59:25 -08005085 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5086 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005087 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08005088 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005089 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005090 throw new IllegalArgumentException(
5091 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5092 }
5093
Jorim Jaggi6afd1562016-06-01 18:57:54 -07005094 // Since we don't have an actual source record here, we assume that the currently focused
5095 // activity was the source.
5096 final ActivityStack focusedStack = getFocusedStack();
5097 final ActivityRecord sourceRecord =
5098 focusedStack != null ? focusedStack.topActivity() : null;
5099
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005100 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005101 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08005102 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5103 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005104 }
5105 }
5106
5107 // If the user must confirm credentials (e.g. when first launching a work app and the
5108 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5109 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5110 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07005111 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07005112 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005113 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07005114 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5115 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005116
5117 // If we are launching the task in the docked stack, put it into resizing mode so
5118 // the window renders full-screen with the background filling the void. Also only
5119 // call this at the end to make sure that tasks exists on the window manager side.
5120 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005121 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005122 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07005123
Bryce Leeaa5e8c32017-03-01 16:01:06 -08005124 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07005125 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07005126 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07005127 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005128 return ActivityManager.START_TASK_TO_FRONT;
5129 }
5130 callingUid = task.mCallingUid;
5131 callingPackage = task.mCallingPackage;
5132 intent = task.intent;
5133 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5134 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005135 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
5136 null, null, 0, 0, bOptions, userId, null, task);
5137 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005138 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005139 }
5140 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005141 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005142
5143 /**
5144 * @return a list of activities which are the top ones in each visible stack. The first
5145 * entry will be the focused activity.
5146 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005147 List<IBinder> getTopVisibleActivities() {
5148 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5149 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005150 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005151 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5152 // Traverse all stacks on a display.
5153 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5154 final ActivityStack stack = display.mStacks.get(j);
5155 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005156 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005157 == ActivityStack.STACK_VISIBLE) {
5158 final ActivityRecord top = stack.topActivity();
5159 if (top != null) {
5160 if (stack == mFocusedStack) {
5161 topActivityTokens.add(0, top.appToken);
5162 } else {
5163 topActivityTokens.add(top.appToken);
5164 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005165 }
5166 }
5167 }
5168 }
5169 return topActivityTokens;
5170 }
Bryce Lee4a194382017-04-04 14:32:48 -07005171
5172 /**
5173 * Internal container to store a match qualifier alongside a WaitResult.
5174 */
5175 static class WaitInfo {
5176 private final ComponentName mTargetComponent;
5177 private final WaitResult mResult;
5178
5179 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5180 this.mTargetComponent = targetComponent;
5181 this.mResult = result;
5182 }
5183
Wale Ogunwale3270f172017-04-26 07:29:42 -07005184 public boolean matches(ComponentName targetComponent) {
5185 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005186 }
5187
5188 public WaitResult getResult() {
5189 return mResult;
5190 }
5191
5192 public ComponentName getComponent() {
5193 return mTargetComponent;
5194 }
5195
5196 public void dump(PrintWriter pw, String prefix) {
5197 pw.println(prefix + "WaitInfo:");
5198 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5199 pw.println(prefix + " mResult=");
5200 mResult.dump(pw, prefix);
5201 }
5202 }
Craig Mautner27084302013-03-25 08:05:25 -07005203}