blob: 53afe78f2ea34924f50e86ab26e9f4762add6a43 [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) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700473 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700474 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) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700482 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700483 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 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700510 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700511 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
Andrii Kulian62e6f252017-05-30 22:46:53 -07001675 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001676 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) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002194 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002195 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);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002245 // If a display is registered in WM, it must also be available in AM.
2246 @SuppressWarnings("ConstantConditions")
2247 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002248 for (int j = stacks.size() - 1; j >= 0; --j) {
2249 final ActivityStack stack = stacks.get(j);
2250 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002251 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002252 return stack;
2253 }
2254 }
2255 }
2256
2257 return null;
2258 }
2259
Craig Mautneree2e45a2014-06-27 12:10:03 -07002260 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002261 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002262 }
2263
2264 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002265 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2266 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2267 final TaskRecord task = tasks.get(taskNdx);
2268 if (task.isHomeTask()) {
2269 final ArrayList<ActivityRecord> activities = task.mActivities;
2270 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2271 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002272 if (r.isHomeActivity()
2273 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002274 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002275 }
2276 }
2277 }
2278 }
2279 return null;
2280 }
2281
Chong Zhangb15758a2015-11-17 12:12:03 -08002282 /**
2283 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2284 * the docked stack itself, or if it's side-by-side to the docked stack.
2285 */
2286 boolean isStackDockedInEffect(int stackId) {
2287 return stackId == DOCKED_STACK_ID ||
2288 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2289 }
2290
Todd Kennedyca4d8422015-01-15 15:19:22 -08002291 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002292 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002293 ActivityContainer activityContainer =
2294 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002295 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002296 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2297 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002298 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002299 return activityContainer;
2300 }
2301
Craig Mautner34b73df2014-01-12 21:11:08 -08002302 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002303 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2304 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2305 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002306 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2307 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002308 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002309 }
2310 }
2311
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002312 void deleteActivityContainerRecord(int stackId) {
2313 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2314 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2315 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002316 }
2317
Jorim Jaggidc249c42015-12-15 14:57:31 -08002318 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002319 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002320 if (stackId == DOCKED_STACK_ID) {
2321 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002322 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002323 return;
2324 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002325 final ActivityStack stack = getStack(stackId);
2326 if (stack == null) {
2327 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2328 return;
2329 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002330
Winson Chunga2249ac2017-03-30 18:15:30 -07002331 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2332 getStack(DOCKED_STACK_ID) != null) {
2333 // If the docked stack exists, don't resize non-floating stacks independently of the
2334 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002335 return;
2336 }
2337
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002338 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002339 mWindowManager.deferSurfaceLayout();
2340 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002341 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002342 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002343 stack.ensureVisibleActivitiesConfigurationLocked(
2344 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002345 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002346 } finally {
2347 mWindowManager.continueSurfaceLayout();
2348 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002349 }
2350 }
2351
Jorim Jaggi192086e2016-03-11 17:17:03 +01002352 void deferUpdateBounds(int stackId) {
2353 final ActivityStack stack = getStack(stackId);
2354 if (stack != null) {
2355 stack.deferUpdateBounds();
2356 }
2357 }
2358
2359 void continueUpdateBounds(int stackId) {
2360 final ActivityStack stack = getStack(stackId);
2361 if (stack != null) {
2362 stack.continueUpdateBounds();
2363 }
2364 }
2365
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002366 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002367 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002368 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2369 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002370 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002371 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002372 if (task != null) {
2373 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002374 }
2375 }
2376 mResizingTasksDuringAnimation.clear();
2377 }
2378
Robert Carre7cc44d2017-03-20 19:04:30 -07002379 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2380 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002381
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002382 final ActivityStack stack = getStack(fromStackId);
2383 if (stack == null) {
2384 return;
2385 }
2386
2387 mWindowManager.deferSurfaceLayout();
2388 try {
2389 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002390 // We are moving all tasks from the docked stack to the fullscreen stack,
2391 // which is dismissing the docked stack, so resize all other stacks to
2392 // fullscreen here already so we don't end up with resize trashing.
2393 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2394 if (StackId.isResizeableByDockedStack(i)) {
2395 ActivityStack otherStack = getStack(i);
2396 if (otherStack != null) {
2397 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2398 true /* allowResizeInDockedMode */, DEFER_RESUME);
2399 }
2400 }
2401 }
2402
2403 // Also disable docked stack resizing since we have manually adjusted the
2404 // size of other stacks above and we don't want to trigger a docked stack
2405 // resize when we remove task from it below and it is detached from the
2406 // display because it no longer contains any tasks.
2407 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002408 } else if (fromStackId == PINNED_STACK_ID) {
2409 if (onTop) {
2410 // Log if we are expanding the PiP to fullscreen
2411 MetricsLogger.action(mService.mContext,
2412 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2413 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002414 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002415 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002416 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002417 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002418 // If we are moving from the pinned stack, then the animation takes care of updating
2419 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002420 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002421 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2422 final int size = tasks.size();
2423 if (onTop) {
2424 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002425 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002426 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002427 if (fromStackId == PINNED_STACK_ID) {
2428 // Update the return-to to reflect where the pinned stack task was moved
2429 // from so that we retain the stack that was previously visible if the
2430 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002431 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2432 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002433 }
Winson Chung74666102017-02-22 17:49:24 -08002434 // Defer resume until all the tasks have been moved to the fullscreen stack
2435 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2436 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002437 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002438 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002439 }
2440 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002441 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002442 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002443 // Position the tasks in the fullscreen stack in order at the bottom of the
2444 // stack. Also defer resume until all the tasks have been moved to the
2445 // fullscreen stack.
2446 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002447 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2448 schedulePictureInPictureModeChange,
2449 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002450 }
2451 }
Winson Chung74666102017-02-22 17:49:24 -08002452
2453 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2454 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002455 } finally {
2456 mAllowDockedStackResize = true;
2457 mWindowManager.continueSurfaceLayout();
2458 }
2459 }
2460
Robert Carr6914f082017-03-20 19:04:30 -07002461 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002462 mWindowManager.inSurfaceTransaction(
2463 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002464 }
2465
Jorim Jaggidc249c42015-12-15 14:57:31 -08002466 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002467 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2468 boolean preserveWindows) {
2469 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2470 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2471 false /* deferResume */);
2472 }
2473
2474 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2475 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2476 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002477
2478 if (!mAllowDockedStackResize) {
2479 // Docked stack resize currently disabled.
2480 return;
2481 }
2482
Jorim Jaggidc249c42015-12-15 14:57:31 -08002483 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2484 if (stack == null) {
2485 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2486 return;
2487 }
2488
2489 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2490 mWindowManager.deferSurfaceLayout();
2491 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002492 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2493 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002494 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002495 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002496
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002497 // TODO: Checking for isAttached might not be needed as if the user passes in null
2498 // dockedBounds then they want the docked stack to be dismissed.
2499 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2500 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002501 // In this case we make all other static stacks fullscreen and move all
2502 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002503 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002504
2505 // stack shouldn't contain anymore activities, so nothing to resume.
2506 r = null;
2507 } else {
2508 // Docked stacks occupy a dedicated region on screen so the size of all other
2509 // static stacks need to be adjusted so they don't overlap with the docked stack.
2510 // We get the bounds to use from window manager which has been adjusted for any
2511 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002512 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002513 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002514 final ActivityStack current = getStack(i);
2515 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002516 current.getStackDockedModeBounds(
2517 tempOtherTaskBounds /* currentTempTaskBounds */,
2518 tempRect /* outStackBounds */,
2519 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2520
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002521 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002522 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2523 tempOtherTaskInsetBounds, preserveWindows,
2524 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002525 }
2526 }
2527 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002528 if (!deferResume) {
2529 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2530 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002531 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002532 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002533 mWindowManager.continueSurfaceLayout();
2534 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2535 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002536 }
2537
Robert Carr0d00c2e2016-02-29 17:45:02 -08002538 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002539 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002540 if (stack == null) {
2541 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2542 return;
2543 }
Winson Chung19953ca2017-04-11 11:19:23 -07002544
2545 // It is possible for the bounds animation from the WM to call this but be delayed by
2546 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2547 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2548 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2549 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002550 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002551 return;
2552 }
2553
Robert Carr0d00c2e2016-02-29 17:45:02 -08002554 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2555 mWindowManager.deferSurfaceLayout();
2556 try {
2557 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002558 Rect insetBounds = null;
2559 if (tempPinnedTaskBounds != null) {
2560 // We always use 0,0 as the position for the inset rect because
2561 // if we are getting insets at all in the pinned stack it must mean
2562 // we are headed for fullscreen.
2563 insetBounds = tempRect;
2564 insetBounds.top = 0;
2565 insetBounds.left = 0;
2566 insetBounds.right = tempPinnedTaskBounds.width();
2567 insetBounds.bottom = tempPinnedTaskBounds.height();
2568 }
2569 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002570 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002571 } finally {
2572 mWindowManager.continueSurfaceLayout();
2573 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2574 }
2575 }
2576
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002577 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002578 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002579 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002580 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002581 }
2582
Wale Ogunwale1666e312016-12-16 11:27:18 -08002583 final ActivityContainer activityContainer =
2584 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002585 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002586 }
2587
Robert Carr6914f082017-03-20 19:04:30 -07002588
Robert Carre7cc44d2017-03-20 19:04:30 -07002589 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002590 final ActivityStack stack = getStack(stackId);
2591 if (stack == null) {
2592 return;
2593 }
2594
2595 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2596 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002597 /**
2598 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2599 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2600 * that the client receives onStop() before it is reparented. We do this by detaching
2601 * the stack from the display so that it will be considered invisible when
2602 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2603 * invisible as well and added to the stopping list. After which we process the
2604 * stopping list by handling the idle.
2605 */
2606 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2607 pinnedStack.mForceHidden = true;
2608 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2609 pinnedStack.mForceHidden = false;
2610 activityIdleInternalLocked(null, false /* fromTimeout */,
2611 true /* processPausingActivites */, null /* configuration */);
2612
2613 // Move all the tasks to the bottom of the fullscreen stack
2614 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002615 } else {
2616 for (int i = tasks.size() - 1; i >= 0; i--) {
2617 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2618 REMOVE_FROM_RECENTS);
2619 }
2620 }
2621 }
2622
2623 /**
Robert Carr6914f082017-03-20 19:04:30 -07002624 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2625 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2626 * instead moved back onto the fullscreen stack.
2627 */
2628 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002629 mWindowManager.inSurfaceTransaction(
2630 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002631 }
2632
2633 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002634 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2635 */
2636 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2637 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2638 }
2639
2640 /**
Winson Chung010927a2016-12-15 16:12:35 -08002641 * Removes the task with the specified task id.
2642 *
2643 * @param taskId Identifier of the task to be removed.
2644 * @param killProcess Kill any process associated with the task if possible.
2645 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002646 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2647 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002648 * @return Returns true if the given task was found and removed.
2649 */
Winson Chung6954fc92017-03-24 16:22:12 -07002650 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2651 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002652 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2653 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002654 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002655 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002656 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2657 if (tr.isPersistable) {
2658 mService.notifyTaskPersisterLocked(null, true);
2659 }
2660 return true;
2661 }
2662 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2663 return false;
2664 }
2665
2666 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2667 if (removeFromRecents) {
2668 mRecentTasks.remove(tr);
2669 tr.removedFromRecents();
2670 }
2671 ComponentName component = tr.getBaseIntent().getComponent();
2672 if (component == null) {
2673 Slog.w(TAG, "No component for base intent of task: " + tr);
2674 return;
2675 }
2676
2677 // Find any running services associated with this app and stop if needed.
2678 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2679
2680 if (!killProcess) {
2681 return;
2682 }
2683
2684 // Determine if the process(es) for this task should be killed.
2685 final String pkg = component.getPackageName();
2686 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2687 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2688 for (int i = 0; i < pmap.size(); i++) {
2689
2690 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2691 for (int j = 0; j < uids.size(); j++) {
2692 ProcessRecord proc = uids.valueAt(j);
2693 if (proc.userId != tr.userId) {
2694 // Don't kill process for a different user.
2695 continue;
2696 }
2697 if (proc == mService.mHomeProcess) {
2698 // Don't kill the home process along with tasks from the same package.
2699 continue;
2700 }
2701 if (!proc.pkgList.containsKey(pkg)) {
2702 // Don't kill process that is not associated with this task.
2703 continue;
2704 }
2705
2706 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002707 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002708 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2709 // Don't kill process(es) that has an activity in a different task that is
2710 // also in recents.
2711 return;
2712 }
2713 }
2714
2715 if (proc.foregroundServices) {
2716 // Don't kill process(es) with foreground service.
2717 return;
2718 }
2719
2720 // Add process to kill list.
2721 procsToKill.add(proc);
2722 }
2723 }
2724
2725 // Kill the running processes.
2726 for (int i = 0; i < procsToKill.size(); i++) {
2727 ProcessRecord pr = procsToKill.get(i);
2728 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2729 && pr.curReceivers.isEmpty()) {
2730 pr.kill("remove task", true);
2731 } else {
2732 // We delay killing processes that are not in the background or running a receiver.
2733 pr.waitingToKill = "remove task";
2734 }
2735 }
2736 }
2737
Craig Mautner4a1cb222013-12-04 16:14:06 -08002738 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002739 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002740 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2741 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002742 break;
2743 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002744 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002745 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002746 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002747 }
2748
Chong Zhang5dcb2752015-08-18 13:50:26 -07002749 /**
2750 * Restores a recent task to a stack
2751 * @param task The recent task to be restored.
2752 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002753 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2754 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002755 * @return true if the task has been restored successfully.
2756 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002757 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002758 if (!StackId.isStaticStack(stackId)) {
2759 // If stack is not static (or stack id is invalid) - use the default one.
2760 // This means that tasks that were on external displays will be restored on the
2761 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002762 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002763 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002764 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2765 // Put it in the fullscreen stack.
2766 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002767 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002768
Andrii Kulian02b7a832016-10-06 23:11:56 -07002769 final ActivityStack currentStack = task.getStack();
2770 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002771 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002772 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002773 // Nothing else to do since it is already restored in the right stack.
2774 return true;
2775 }
2776 // Remove current stack association, so we can re-associate the task with the
2777 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002778 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002779 }
2780
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002781 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002782 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002783
2784 if (stack == null) {
2785 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002786 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2787 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002788 return false;
2789 }
2790
Wale Ogunwale72919d22016-12-08 18:58:50 -08002791 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002792 // TODO: move call for creation here and other place into Stack.addTask()
2793 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002794 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2795 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002796 final ArrayList<ActivityRecord> activities = task.mActivities;
2797 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002798 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002799 }
2800 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002801 }
2802
Wale Ogunwale040b4702015-08-06 18:10:50 -07002803 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002804 * Move stack with all its existing content to specified display.
2805 * @param stackId Id of stack to move.
2806 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002807 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002808 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002809 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002810 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002811 if (activityDisplay == null) {
2812 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2813 + displayId);
2814 }
2815 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2816 if (activityContainer != null) {
2817 if (activityContainer.isAttachedLocked()) {
2818 if (activityContainer.getDisplayId() == displayId) {
2819 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2820 + " to its current displayId=" + displayId);
2821 }
2822
Andrii Kulian250d6532017-02-08 23:30:45 -08002823 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002824 } else {
2825 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2826 + stackId + " is not attached to any display.");
2827 }
2828 } else {
2829 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2830 + stackId);
2831 }
Andrii Kulian839def92016-11-02 10:58:58 -07002832 // TODO(multi-display): resize stacks properly if moved from split-screen.
2833 }
2834
2835 /**
Winson Chung74666102017-02-22 17:49:24 -08002836 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2837 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002838 */
Winson Chung74666102017-02-22 17:49:24 -08002839 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002840 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002841
Winson Chung74666102017-02-22 17:49:24 -08002842 // Check that we aren't reparenting to the same stack that the task is already in
2843 if (prevStack != null && prevStack.mStackId == stackId) {
2844 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2845 + " already in stackId=" + stackId);
2846 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002847 }
2848
Winson Chung74666102017-02-22 17:49:24 -08002849 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2850 // support
2851 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2852 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2853 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002854 }
2855
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002856 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2857 // multi-display.
2858 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2859 // TODO: Check ActivityView after fixing b/35349678.
2860 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2861 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2862 + " reparent task=" + task + " to stackId=" + stackId);
2863 }
2864
Winson Chung74666102017-02-22 17:49:24 -08002865 // Ensure that we aren't trying to move into a freeform stack without freeform
2866 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002867 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002868 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2869 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002870 }
2871
Winson Chung74666102017-02-22 17:49:24 -08002872 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2873 // used for split-screen mode and will cause things like the docked divider to show up. We
2874 // instead leave the task in its current stack or move it to the fullscreen stack if it
2875 // isn't currently in a stack.
2876 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2877 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2878 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2879 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002880 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002881
Winson Chung74666102017-02-22 17:49:24 -08002882 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2883 // example, a docked stack is created which will lead to the stack we are moving from being
2884 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002885 try {
Winson Chung74666102017-02-22 17:49:24 -08002886 task.mTemporarilyUnresizable = true;
2887 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002888 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002889 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002890 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
2892
Winson Chung08f81892017-03-02 15:40:51 -08002893 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002894 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2895 if (stack == null) {
2896 throw new IllegalArgumentException(
2897 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2898 }
2899
2900 final ActivityRecord r = stack.topRunningActivityLocked();
2901 if (r == null) {
2902 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2903 + " in stack=" + stack);
2904 return false;
2905 }
2906
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002907 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002908 Slog.w(TAG,
2909 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002910 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002911 return false;
2912 }
2913
Winson Chung3a682872017-04-10 14:38:05 -07002914 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002915 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002916 return true;
2917 }
2918
Winson Chung8bca9e42017-04-16 15:59:43 -07002919 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002920 boolean moveHomeStackToFront, String reason) {
2921
Wale Ogunwale480dca02016-02-06 13:58:29 -08002922 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002923
Bryce Lee04ab3462017-04-10 15:06:33 -07002924 // This will clear the pinned stack by moving an existing task to the full screen stack,
2925 // ensuring only one task is present.
2926 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2927
Wale Ogunwale1666e312016-12-16 11:27:18 -08002928 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002929 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002930
Wale Ogunwale480dca02016-02-06 13:58:29 -08002931 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002932 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002933
Andrii Kulian02b7a832016-10-06 23:11:56 -07002934 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002935 // An activity can't be pinned and visible behind at the same time. Go ahead and
2936 // release it from been visible behind before pinning.
2937 requestVisibleBehindLocked(r, false);
2938 }
2939
Wale Ogunwale480dca02016-02-06 13:58:29 -08002940 // Resize the pinned stack to match the current size of the task the activity we are
2941 // going to be moving is currently contained in. We do this to have the right starting
2942 // animation bounds for the pinned stack to the desired bounds the caller wants.
2943 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2944 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002945 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002946
2947 if (task.mActivities.size() == 1) {
2948 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002949 // the stack without re-parenting the activity in a different task. We don't
2950 // move the home stack forward if we are currently entering picture-in-picture
2951 // while pausing because that changes the focused stack and may prevent the new
2952 // starting activity from resuming.
2953 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002954 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002955 // Move the home stack forward if the task we just moved to the pinned stack
2956 // was launched from home so home should be visible behind it.
2957 moveHomeStackToFront(reason);
2958 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002959 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002960 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002961 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002962 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002963 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002964 // reveal/leave the other activities in their original task.
2965
2966 // Currently, we don't support reparenting activities across tasks in two different
2967 // stacks, so instead, just create a new task in the same stack, reparent the
2968 // activity into that task, and then reparent the whole task to the new stack. This
2969 // ensures that all the necessary work to migrate states in the old and new stacks
2970 // is also done.
2971 final TaskRecord newTask = task.getStack().createTaskRecord(
2972 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2973 r.mActivityType);
2974 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2975
Winson Chung5af42fc2017-03-24 17:11:33 -07002976 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002977 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002978 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002979 }
Winson Chungc2baac02017-01-11 13:34:47 -08002980
2981 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2982 // to the pinned stack
2983 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002984 } finally {
2985 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002986 }
2987
Winson Chunge7ba6862017-05-24 12:13:33 -07002988 // Calculate the default bounds (don't use existing stack bounds as we may have just created
2989 // the stack, and schedule the start of the animation into PiP (the bounds animator that
2990 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07002991 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
2992
Winson Chunge7ba6862017-05-24 12:13:33 -07002993 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
2994 true /* fromFullscreen */);
2995
2996 // Update the visibility of all activities after the they have been reparented to the new
2997 // stack. This MUST run after the animation above is scheduled to ensure that the windows
2998 // drawn signal is scheduled after the bounds animation start call on the bounds animator
2999 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003000 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003001 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003002
Winson Chungb5026902017-05-03 12:45:13 -07003003 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3004 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003005 }
3006
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003007 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003008 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3009 if (r == null || !r.isFocusable()) {
3010 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3011 "moveActivityStackToFront: unfocusable r=" + r);
3012 return false;
3013 }
3014
Bryce Leeaf691c02017-03-20 14:20:22 -07003015 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003016 final ActivityStack stack = r.getStack();
3017 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003018 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3019 + r + " task=" + task);
3020 return false;
3021 }
3022
Chong Zhang6cda19c2016-06-14 19:07:56 -07003023 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3024 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3025 "moveActivityStackToFront: already on top, r=" + r);
3026 return false;
3027 }
3028
3029 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3030 "moveActivityStackToFront: r=" + r);
3031
3032 stack.moveToFront(reason, task);
3033 return true;
3034 }
3035
David Stevensc6b91c62017-02-08 14:23:58 -08003036 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003037 mTmpFindTaskResult.r = null;
3038 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003039 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003040 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003041 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3042 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003043 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3044 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003045 if (!checkActivityBelongsInStack(r, stack)) {
3046 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3047 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003048 continue;
3049 }
3050 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003051 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3052 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003053 continue;
3054 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003055 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003056 // It is possible to have tasks in multiple stacks with the same root affinity, so
3057 // we should keep looking after finding an affinity match to see if there is a
3058 // better match in another stack. Also, task affinity isn't a good enough reason
3059 // to target a display which isn't the source of the intent, so skip any affinity
3060 // matches not on the specified display.
3061 if (mTmpFindTaskResult.r != null) {
3062 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3063 return mTmpFindTaskResult.r;
3064 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003065 // Note: since the traversing through the stacks is top down, the floating
3066 // tasks should always have lower priority than any affinity-matching tasks
3067 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003068 affinityMatch = mTmpFindTaskResult.r;
3069 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003070 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003071 }
3072 }
Winson Chung5b895b72017-05-01 13:46:25 -07003073
David Stevensc6b91c62017-02-08 14:23:58 -08003074 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3075 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003076 }
3077
Winson Chung91e5c882017-04-21 14:44:38 -07003078 /**
3079 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3080 * type.
3081 */
3082 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3083 if (r.isHomeActivity()) {
3084 return stack.isHomeStack();
3085 } else if (r.isRecentsActivity()) {
3086 return stack.isRecentsStack();
3087 } else if (r.isAssistantActivity()) {
3088 return stack.isAssistantStack();
3089 }
3090 return true;
3091 }
3092
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003093 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3094 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003095 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3096 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003097 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003098 final ActivityRecord ar = stacks.get(stackNdx)
3099 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 if (ar != null) {
3101 return ar;
3102 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003103 }
3104 }
3105 return null;
3106 }
3107
Craig Mautner8d341ef2013-03-26 09:03:27 -07003108 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003109 scheduleSleepTimeout();
3110 if (!mGoingToSleep.isHeld()) {
3111 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003112 if (mLaunchingActivity.isHeld()) {
3113 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3114 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003115 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003116 mLaunchingActivity.release();
3117 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003118 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003119 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003120 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003121 }
3122
3123 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003124 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003125
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003126 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003127 final long endTime = System.currentTimeMillis() + timeout;
3128 while (true) {
3129 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003130 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3131 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003132 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3133 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3134 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003135 }
3136 if (cantShutdown) {
3137 long timeRemaining = endTime - System.currentTimeMillis();
3138 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003139 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003140 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003141 } catch (InterruptedException e) {
3142 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003143 } else {
3144 Slog.w(TAG, "Activity manager shutdown timed out");
3145 timedout = true;
3146 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003147 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003148 } else {
3149 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003150 }
3151 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003152
3153 // Force checkReadyForSleep to complete.
3154 mSleepTimeout = true;
3155 checkReadyForSleepLocked();
3156
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 return timedout;
3158 }
3159
3160 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003161 removeSleepTimeouts();
3162 if (mGoingToSleep.isHeld()) {
3163 mGoingToSleep.release();
3164 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003165 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3166 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003167 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3168 final ActivityStack stack = stacks.get(stackNdx);
3169 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003170 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003171 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003172 }
Craig Mautner5314a402013-09-26 12:40:16 -07003173 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003175 mGoingToSleepActivities.clear();
3176 }
3177
3178 void activitySleptLocked(ActivityRecord r) {
3179 mGoingToSleepActivities.remove(r);
3180 checkReadyForSleepLocked();
3181 }
3182
3183 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003184 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003185 // Do not care.
3186 return;
3187 }
3188
3189 if (!mSleepTimeout) {
3190 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003191 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3192 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003193 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3194 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3195 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003196 }
3197
3198 if (mStoppingActivities.size() > 0) {
3199 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003200 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003201 + mStoppingActivities.size() + " activities");
3202 scheduleIdleLocked();
3203 dontSleep = true;
3204 }
3205
3206 if (mGoingToSleepActivities.size() > 0) {
3207 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003208 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003209 + mGoingToSleepActivities.size() + " activities");
3210 dontSleep = true;
3211 }
3212
3213 if (dontSleep) {
3214 return;
3215 }
3216 }
3217
Wei Wang65c7a152016-06-02 18:51:22 -07003218 // Send launch end powerhint before going sleep
3219 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3220
Craig Mautnere0a38842013-12-16 16:14:02 -08003221 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3222 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3224 stacks.get(stackNdx).goToSleep();
3225 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003226 }
3227
3228 removeSleepTimeouts();
3229
3230 if (mGoingToSleep.isHeld()) {
3231 mGoingToSleep.release();
3232 }
3233 if (mService.mShuttingDown) {
3234 mService.notifyAll();
3235 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 }
3237
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003238 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003239 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003240 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003241 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003242 }
3243 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003244 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003245 mWindowManager.executeAppTransition();
3246 return true;
3247 }
3248 return false;
3249 }
3250
Craig Mautner8d341ef2013-03-26 09:03:27 -07003251 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003252 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3253 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003254 int stackNdx = stacks.size() - 1;
3255 while (stackNdx >= 0) {
3256 stacks.get(stackNdx).handleAppCrashLocked(app);
3257 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003258 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003259 }
3260 }
3261
Jose Lima4b6c6692014-08-12 17:41:12 -07003262 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003263 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003264 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003265 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3266 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003267 return false;
3268 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003269
3270 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3271 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3272 + " visible=" + visible + " stackId=" + stack.mStackId
3273 + " can't contain visible behind activities");
3274 return false;
3275 }
3276
Jose Lima4b6c6692014-08-12 17:41:12 -07003277 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003278 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3279 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003280
3281 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003282 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003283 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003284 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003285 return true;
3286 }
3287
3288 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003289 if (visible && top.fullscreen) {
3290 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003291 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3292 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3293 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3294 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003295 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003296 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3297 // Only the activity set as currently visible behind should actively reset its
3298 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003299 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3300 "requestVisibleBehind: returning visible=" + visible
3301 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3302 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003303 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003304 }
3305
Jose Lima4b6c6692014-08-12 17:41:12 -07003306 stack.setVisibleBehindActivity(visible ? r : null);
3307 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003308 // If there is a translucent home activity, we need to force it stop being translucent,
3309 // because we can't depend on the application to necessarily perform that operation.
3310 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003311 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003312 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003313 mService.convertFromTranslucent(next.appToken);
3314 }
3315 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003316 if (top.app != null && top.app.thread != null) {
3317 // Notify the top app of the change.
3318 try {
3319 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3320 } catch (RemoteException e) {
3321 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003322 }
3323 return true;
3324 }
3325
Craig Mautnerbb742462014-07-07 15:28:55 -07003326 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003327 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003328 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003329 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003330
3331 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003332 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003333 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003334 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003335 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003336
3337 // When launching tasks behind, update the last active time of the top task after the new
3338 // task has been shown briefly
3339 final ActivityRecord top = stack.topActivity();
3340 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003341 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003342 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003343 }
3344
3345 void scheduleLaunchTaskBehindComplete(IBinder token) {
3346 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3347 }
3348
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003349 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3350 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003351 mKeyguardController.beginActivityVisibilityUpdate();
3352 try {
3353 // First the front stacks. In case any are not fullscreen and are in front of home.
3354 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3355 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3356 final int topStackNdx = stacks.size() - 1;
3357 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3358 final ActivityStack stack = stacks.get(stackNdx);
3359 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3360 }
Craig Mautner580ea812013-04-25 12:58:38 -07003361 }
Jorim Jaggife762342016-10-13 14:33:27 +02003362 } finally {
3363 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003364 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003365 }
3366
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003367 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3368 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3369 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3370 final int topStackNdx = stacks.size() - 1;
3371 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3372 final ActivityStack stack = stacks.get(stackNdx);
3373 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3374 }
3375 }
3376 }
3377
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003378 void invalidateTaskLayers() {
3379 mTaskLayersChanged = true;
3380 }
3381
3382 void rankTaskLayersIfNeeded() {
3383 if (!mTaskLayersChanged) {
3384 return;
3385 }
3386 mTaskLayersChanged = false;
3387 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3388 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3389 int baseLayer = 0;
3390 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3391 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3392 }
3393 }
3394 }
3395
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003396 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3397 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3398 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3399 final int topStackNdx = stacks.size() - 1;
3400 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3401 final ActivityStack stack = stacks.get(stackNdx);
3402 stack.clearOtherAppTimeTrackers(except);
3403 }
3404 }
3405 }
3406
Craig Mautner8d341ef2013-03-26 09:03:27 -07003407 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003408 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3409 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 final int numStacks = stacks.size();
3411 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3412 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003414 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003415 }
3416 }
3417
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003418 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3419 // Examine all activities currently running in the process.
3420 TaskRecord firstTask = null;
3421 // Tasks is non-null only if two or more tasks are found.
3422 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003423 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3424 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003425 ActivityRecord r = app.activities.get(i);
3426 // First, if we find an activity that is in the process of being destroyed,
3427 // then we just aren't going to do anything for now; we want things to settle
3428 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003429 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003430 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003431 return;
3432 }
3433 // Don't consider any activies that are currently not in a state where they
3434 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003435 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3436 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003437 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003438 continue;
3439 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003440
3441 final TaskRecord task = r.getTask();
3442 if (task != null) {
3443 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003444 + " from " + r);
3445 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003446 firstTask = task;
3447 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003448 if (tasks == null) {
3449 tasks = new ArraySet<>();
3450 tasks.add(firstTask);
3451 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003452 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003453 }
3454 }
3455 }
3456 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003457 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003458 return;
3459 }
3460 // If we have activities in multiple tasks that are in a position to be destroyed,
3461 // let's iterate through the tasks and release the oldest one.
3462 final int numDisplays = mActivityDisplays.size();
3463 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3464 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3465 // Step through all stacks starting from behind, to hit the oldest things first.
3466 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3467 final ActivityStack stack = stacks.get(stackNdx);
3468 // Try to release activities in this stack; if we manage to, we are done.
3469 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3470 return;
3471 }
3472 }
3473 }
3474 }
3475
Amith Yamasani37a40c22015-06-17 13:25:42 -07003476 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003477 final int focusStackId = mFocusedStack.getStackId();
3478 // We dismiss the docked stack whenever we switch users.
3479 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003480 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3481 // also cause all tasks to be moved to the fullscreen stack at a position that is
3482 // appropriate.
3483 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003484
3485 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003486 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003487 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003488
Craig Mautner858d8a62013-04-23 17:08:34 -07003489 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003490 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3491 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003492 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003493 final ActivityStack stack = stacks.get(stackNdx);
3494 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003495 TaskRecord task = stack.topTask();
3496 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003497 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003498 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003500 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003501
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003502 ActivityStack stack = getStack(restoreStackId);
3503 if (stack == null) {
3504 stack = mHomeStack;
3505 }
3506 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003507 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003508 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003509 } else {
3510 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003511 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003512 }
Craig Mautner93529a42013-10-04 15:03:13 -07003513 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003514 }
3515
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003516 /** Checks whether the userid is a profile of the current user. */
3517 boolean isCurrentProfileLocked(int userId) {
3518 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003519 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003520 }
3521
Bryce Leeb7c9b802017-05-02 14:20:24 -07003522 /**
3523 * Returns whether a stopping activity is present that should be stopped after visible, rather
3524 * than idle.
3525 * @return {@code true} if such activity is present. {@code false} otherwise.
3526 */
3527 boolean isStoppingNoHistoryActivity() {
3528 // Activities that are marked as nohistory should be stopped immediately after the resumed
3529 // activity has become visible.
3530 for (ActivityRecord record : mStoppingActivities) {
3531 if (record.isNoHistory()) {
3532 return true;
3533 }
3534 }
3535
3536 return false;
3537 }
3538
Winson Chung4dabf232017-01-25 13:25:22 -08003539 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3540 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003541 ArrayList<ActivityRecord> stops = null;
3542
3543 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003544 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3545 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003546 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003547 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003548 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3549 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003550 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003551 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003552 if (s.finishing) {
3553 // If this activity is finishing, it is sitting on top of
3554 // everyone else but we now know it is no longer needed...
3555 // so get rid of it. Otherwise, we need to go through the
3556 // normal flow and hide it once we determine that it is
3557 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003558 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003559 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003560 }
3561 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003562 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003563 if (!processPausingActivities && s.state == PAUSING) {
3564 // Defer processing pausing activities in this iteration and reschedule
3565 // a delayed idle to reprocess it again
3566 removeTimeoutsForActivityLocked(idleActivity);
3567 scheduleIdleTimeoutLocked(idleActivity);
3568 continue;
3569 }
3570
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003571 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003572 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003573 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003574 }
3575 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003576 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003577 }
3578 }
3579
3580 return stops;
3581 }
3582
Craig Mautnercf910b02013-04-23 11:23:27 -07003583 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003584 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3585 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3586 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3587 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003588 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003589 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003590 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003591 if (r == null) Slog.e(TAG,
3592 "validateTop...: null top activity, stack=" + stack);
3593 else {
3594 final ActivityRecord pausing = stack.mPausingActivity;
3595 if (pausing != null && pausing == r) Slog.e(TAG,
3596 "validateTop...: top stack has pausing activity r=" + r
3597 + " state=" + state);
3598 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3599 "validateTop...: activity in front not resumed r=" + r
3600 + " state=" + state);
3601 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003602 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003603 final ActivityRecord resumed = stack.mResumedActivity;
3604 if (resumed != null && resumed == r) Slog.e(TAG,
3605 "validateTop...: back stack has resumed activity r=" + r
3606 + " state=" + state);
3607 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3608 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003609 }
3610 }
3611 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003612 }
3613
Craig Mautnere0570202015-05-13 13:06:11 -07003614 private String lockTaskModeToString() {
3615 switch (mLockTaskModeState) {
3616 case LOCK_TASK_MODE_LOCKED:
3617 return "LOCKED";
3618 case LOCK_TASK_MODE_PINNED:
3619 return "PINNED";
3620 case LOCK_TASK_MODE_NONE:
3621 return "NONE";
3622 default: return "unknown=" + mLockTaskModeState;
3623 }
3624 }
3625
Craig Mautner27084302013-03-25 08:05:25 -07003626 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003627 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003629 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003630 pw.print(prefix);
3631 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003632 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003633 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003634 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003635 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3636 if (packages.size() > 0) {
3637 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3638 for (int i = 0; i < packages.size(); ++i) {
3639 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3640 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3641 }
3642 }
Bryce Lee4a194382017-04-04 14:32:48 -07003643 if (!mWaitingForActivityVisible.isEmpty()) {
3644 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3645 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3646 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3647 }
3648 }
3649
Jorim Jaggi8d786932016-10-26 19:08:36 -07003650 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3651 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003652 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003653
Winson43d1f262016-06-14 16:05:55 -07003654 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003655 * Dump all connected displays' configurations.
3656 * @param prefix Prefix to apply to each line of the dump.
3657 */
3658 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3659 pw.print(prefix); pw.println("Display override configurations:");
3660 final int displayCount = mActivityDisplays.size();
3661 for (int i = 0; i < displayCount; i++) {
3662 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3663 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3664 pw.println(activityDisplay.getOverrideConfiguration());
3665 }
3666 }
3667
3668 /**
Winson43d1f262016-06-14 16:05:55 -07003669 * Dumps the activities matching the given {@param name} in the either the focused stack
3670 * or all visible stacks if {@param dumpVisibleStacks} is true.
3671 */
Winson Chung6998bc42017-02-28 17:07:05 -08003672 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3673 boolean dumpFocusedStackOnly) {
3674 if (dumpFocusedStackOnly) {
3675 return mFocusedStack.getDumpActivitiesLocked(name);
3676 } else {
Winson43d1f262016-06-14 16:05:55 -07003677 ArrayList<ActivityRecord> activities = new ArrayList<>();
3678 int numDisplays = mActivityDisplays.size();
3679 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3680 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3682 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003683 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003684 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003685 activities.addAll(stack.getDumpActivitiesLocked(name));
3686 }
3687 }
3688 }
3689 return activities;
Winson43d1f262016-06-14 16:05:55 -07003690 }
Craig Mautner20e72272013-04-01 13:45:53 -07003691 }
3692
Dianne Hackborn390517b2013-05-30 15:03:32 -07003693 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3694 boolean needSep, String prefix) {
3695 if (activity != null) {
3696 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3697 if (needSep) {
3698 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003699 }
3700 pw.print(prefix);
3701 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003702 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003703 }
3704 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003705 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003706 }
3707
Craig Mautner8d341ef2013-03-26 09:03:27 -07003708 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3709 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003710 boolean printed = false;
3711 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003712 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3713 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003714 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003715 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003716 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003717 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003718 final ActivityStack stack = stacks.get(stackNdx);
3719 StringBuilder stackHeader = new StringBuilder(128);
3720 stackHeader.append(" Stack #");
3721 stackHeader.append(stack.mStackId);
3722 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003723 stackHeader.append("\n");
3724 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3725 stackHeader.append("\n");
3726 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003727
3728 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3729 dumpClient, dumpPackage, needSep, stackHeader.toString());
3730 printed |= printedStackHeader;
3731 if (!printedStackHeader) {
3732 // Ensure we always dump the stack header even if there are no activities
3733 pw.println();
3734 pw.println(stackHeader);
3735 }
3736
Craig Mautner4a1cb222013-12-04 16:14:06 -08003737 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3738 !dumpAll, false, dumpPackage, true,
3739 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003740
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 needSep = printed;
3742 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3743 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003744 if (pr) {
3745 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003747 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003748 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3749 " mResumedActivity: ");
3750 if (pr) {
3751 printed = true;
3752 needSep = false;
3753 }
3754 if (dumpAll) {
3755 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3756 " mLastPausedActivity: ");
3757 if (pr) {
3758 printed = true;
3759 needSep = true;
3760 }
3761 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3762 needSep, " mLastNoHistoryActivity: ");
3763 }
3764 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003765 }
3766 }
3767
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003768 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3769 false, dumpPackage, true, " Activities waiting to finish:", null);
3770 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3771 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003772 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3773 false, !dumpAll, false, dumpPackage, true,
3774 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003775 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3776 false, dumpPackage, true, " Activities waiting to sleep:", null);
3777 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3778 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003779
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003780 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003781 }
3782
Dianne Hackborn390517b2013-05-30 15:03:32 -07003783 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003784 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003785 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003786 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003787 String innerPrefix = null;
3788 String[] args = null;
3789 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003790 for (int i=list.size()-1; i>=0; i--) {
3791 final ActivityRecord r = list.get(i);
3792 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3793 continue;
3794 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003795 if (innerPrefix == null) {
3796 innerPrefix = prefix + " ";
3797 args = new String[0];
3798 }
3799 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003800 final boolean full = !brief && (complete || !r.isInHistory());
3801 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003802 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003803 needNL = false;
3804 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003805 if (header1 != null) {
3806 pw.println(header1);
3807 header1 = null;
3808 }
3809 if (header2 != null) {
3810 pw.println(header2);
3811 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003812 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003813 if (lastTask != r.getTask()) {
3814 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003815 pw.print(prefix);
3816 pw.print(full ? "* " : " ");
3817 pw.println(lastTask);
3818 if (full) {
3819 lastTask.dump(pw, prefix + " ");
3820 } else if (complete) {
3821 // Complete + brief == give a summary. Isn't that obvious?!?
3822 if (lastTask.intent != null) {
3823 pw.print(prefix); pw.print(" ");
3824 pw.println(lastTask.intent.toInsecureStringWithClip());
3825 }
3826 }
3827 }
3828 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3829 pw.print(" #"); pw.print(i); pw.print(": ");
3830 pw.println(r);
3831 if (full) {
3832 r.dump(pw, innerPrefix);
3833 } else if (complete) {
3834 // Complete + brief == give a summary. Isn't that obvious?!?
3835 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3836 if (r.app != null) {
3837 pw.print(innerPrefix); pw.println(r.app);
3838 }
3839 }
3840 if (client && r.app != null && r.app.thread != null) {
3841 // flush anything that is already in the PrintWriter since the thread is going
3842 // to write to the file descriptor directly
3843 pw.flush();
3844 try {
3845 TransferPipe tp = new TransferPipe();
3846 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003847 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003848 // Short timeout, since blocking here can
3849 // deadlock with the application.
3850 tp.go(fd, 2000);
3851 } finally {
3852 tp.kill();
3853 }
3854 } catch (IOException e) {
3855 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3856 } catch (RemoteException e) {
3857 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3858 }
3859 needNL = true;
3860 }
3861 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003862 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003863 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003864
Craig Mautnerf3333272013-04-22 10:55:53 -07003865 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003866 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3867 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003868 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3869 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003870 }
3871
3872 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003873 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003874 }
3875
3876 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003877 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3878 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003879 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3880 }
3881
Craig Mautner05d29032013-05-03 13:40:13 -07003882 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003883 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3884 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3885 }
Craig Mautner05d29032013-05-03 13:40:13 -07003886 }
3887
Craig Mautner0eea92c2013-05-16 13:35:39 -07003888 void removeSleepTimeouts() {
3889 mSleepTimeout = false;
3890 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3891 }
3892
3893 final void scheduleSleepTimeout() {
3894 removeSleepTimeouts();
3895 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3896 }
3897
Craig Mautner4a1cb222013-12-04 16:14:06 -08003898 @Override
3899 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003900 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3902 }
3903
3904 @Override
3905 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003906 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3908 }
3909
3910 @Override
3911 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003912 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3914 }
3915
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003916 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003917 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003918 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003920 }
3921
Andrii Kulianca007a62016-11-22 16:33:28 -08003922 /** Check if display with specified id is added to the list. */
3923 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003924 return getActivityDisplayOrCreateLocked(displayId) != null;
3925 }
3926
3927 /**
3928 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3929 * corresponding record in display manager.
3930 */
3931 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
3932 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3933 if (activityDisplay != null) {
3934 return activityDisplay;
3935 }
3936 if (mDisplayManager == null) {
3937 // The system isn't fully initialized yet.
3938 return null;
3939 }
3940 final Display display = mDisplayManager.getDisplay(displayId);
3941 if (display == null) {
3942 // The display is not registered in DisplayManager.
3943 return null;
3944 }
3945 // The display hasn't been added to ActivityManager yet, create a new record now.
3946 activityDisplay = new ActivityDisplay(displayId);
3947 if (activityDisplay.mDisplay == null) {
3948 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3949 return null;
3950 }
3951 mActivityDisplays.put(displayId, activityDisplay);
3952 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3953 mWindowManager.onDisplayAdded(displayId);
3954 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003955 }
3956
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003957 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003958 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003959 mService.mContext.getResources().getDimensionPixelSize(
3960 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003961 }
3962
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003963 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003964 if (displayId == DEFAULT_DISPLAY) {
3965 throw new IllegalArgumentException("Can't remove the primary display.");
3966 }
3967
Craig Mautner4a1cb222013-12-04 16:14:06 -08003968 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003969 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3970 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003971 final boolean destroyContentOnRemoval
3972 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003973 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3974 while (!stacks.isEmpty()) {
3975 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003976 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003977 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3978 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003979 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003980 } else {
3981 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3982 // a valid launch stack for all activities. This way the task history from
3983 // external display will be preserved on primary after move.
3984 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003985 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003986 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003987 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003988 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 }
3990 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 }
3992
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003993 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003994 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003995 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3996 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 // TODO: Update the bounds.
3998 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003999 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004000 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004001 }
4002
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004003 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004004 final int displayId = stack.mDisplayId;
4005 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004007 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004008 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004010 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004011 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004012 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004013 info.position = display != null
4014 ? display.mStacks.indexOf(stack)
4015 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004016
4017 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4018 final int numTasks = tasks.size();
4019 int[] taskIds = new int[numTasks];
4020 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004021 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004022 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004023 for (int i = 0; i < numTasks; ++i) {
4024 final TaskRecord task = tasks.get(i);
4025 taskIds[i] = task.taskId;
4026 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4027 : task.realActivity != null ? task.realActivity.flattenToString()
4028 : task.getTopActivity() != null ? task.getTopActivity().packageName
4029 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004030 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004031 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004032 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 }
4034 info.taskIds = taskIds;
4035 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004036 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004037 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004038
4039 final ActivityRecord top = stack.topRunningActivityLocked();
4040 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004041 return info;
4042 }
4043
4044 StackInfo getStackInfoLocked(int stackId) {
4045 ActivityStack stack = getStack(stackId);
4046 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004047 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 }
4049 return null;
4050 }
4051
4052 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004053 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004054 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4055 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004057 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004058 }
4059 }
4060 return list;
4061 }
4062
Craig Mautner15df08a2015-04-01 12:17:18 -07004063 TaskRecord getLockedTaskLocked() {
4064 final int top = mLockTaskModeTasks.size() - 1;
4065 if (top >= 0) {
4066 return mLockTaskModeTasks.get(top);
4067 }
4068 return null;
4069 }
4070
4071 boolean isLockedTask(TaskRecord task) {
4072 return mLockTaskModeTasks.contains(task);
4073 }
4074
4075 boolean isLastLockedTask(TaskRecord task) {
4076 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4077 }
4078
4079 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004080 if (!mLockTaskModeTasks.remove(task)) {
4081 return;
4082 }
4083 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4084 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004085 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004086 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4087 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004088 final Message lockTaskMsg = Message.obtain();
4089 lockTaskMsg.arg1 = task.userId;
4090 lockTaskMsg.what = LOCK_TASK_END_MSG;
4091 mHandler.sendMessage(lockTaskMsg);
4092 }
4093 }
4094
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004095 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4096 int preferredDisplayId, int actualStackId) {
4097 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004098 false /* forceNonResizable */);
4099 }
4100
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004101 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4102 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4103 final boolean isSecondaryDisplayPreferred =
4104 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4105 || StackId.isDynamicStack(preferredStackId);
4106 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4107 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004108 return;
4109 }
4110
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004111 // Handle incorrect launch/move to secondary display if needed.
4112 final boolean launchOnSecondaryDisplayFailed;
4113 if (isSecondaryDisplayPreferred) {
4114 final int actualDisplayId = task.getStack().mDisplayId;
4115 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4116 // The task landed on an inappropriate display somehow, move it to the default
4117 // display.
4118 // TODO(multi-display): Find proper stack for the task on the default display.
4119 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4120 true /* toTop */);
4121 launchOnSecondaryDisplayFailed = true;
4122 } else {
4123 // The task might have landed on a display different from requested.
4124 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4125 || (preferredDisplayId != INVALID_DISPLAY
4126 && preferredDisplayId != actualDisplayId);
4127 }
4128 } else {
4129 // The task wasn't requested to be on a secondary display.
4130 launchOnSecondaryDisplayFailed = false;
4131 }
4132
Jorim Jaggicd13d332016-04-27 15:40:20 -07004133 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004134 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4135 if (launchOnSecondaryDisplayFailed) {
4136 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4137 // display with config different from global config.
4138 mService.mTaskChangeNotificationController
4139 .notifyActivityLaunchOnSecondaryDisplayFailed();
4140 } else {
4141 // Display a warning toast that we tried to put a non-dockable task in the docked
4142 // stack.
4143 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4144 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004145
Andrii Kulianc27916642016-04-12 17:59:27 -07004146 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4147 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004148 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004149 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004150 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004151 final String packageName = topActivity.appInfo.packageName;
4152 final int reason = isSecondaryDisplayPreferred
4153 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4154 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004155 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004156 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004157 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004158 }
4159
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004160 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004161 if (mLockTaskNotify != null) {
4162 mLockTaskNotify.showToast(mLockTaskModeState);
4163 }
Jason Monka8f569c2014-07-07 12:15:21 -04004164 }
4165
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004166 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4167 if (mLockTaskModeTasks.contains(task)) {
4168 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4169 }
4170 }
4171
Craig Mautner432f64e2015-05-20 14:59:57 -07004172 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4173 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004174 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004175 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004176 final TaskRecord lockedTask = getLockedTaskLocked();
4177 if (lockedTask != null) {
4178 removeLockedTaskLocked(lockedTask);
4179 if (!mLockTaskModeTasks.isEmpty()) {
4180 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004181 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4182 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004183 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004184 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004185 return;
4186 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004187 }
Craig Mautnere0570202015-05-13 13:06:11 -07004188 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4189 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004190 return;
4191 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004192
4193 // Should have already been checked, but do it again.
4194 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004195 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4196 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004197 return;
4198 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004199 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004200 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004201 return;
4202 }
4203
4204 if (mLockTaskModeTasks.isEmpty()) {
4205 // First locktask.
4206 final Message lockTaskMsg = Message.obtain();
4207 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4208 lockTaskMsg.arg1 = task.userId;
4209 lockTaskMsg.what = LOCK_TASK_START_MSG;
4210 lockTaskMsg.arg2 = lockTaskModeState;
4211 mHandler.sendMessage(lockTaskMsg);
4212 }
4213 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004214 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4215 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004216 mLockTaskModeTasks.remove(task);
4217 mLockTaskModeTasks.add(task);
4218
4219 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004220 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004221 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004222
4223 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004224 findTaskToMoveToFrontLocked(task, 0, null, reason,
4225 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004226 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004227 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004228 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004229 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4230 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004231 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004232 }
4233
4234 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004235 return isLockTaskModeViolation(task, false);
4236 }
4237
4238 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4239 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004240 return false;
4241 }
4242 final int lockTaskAuth = task.mLockTaskAuth;
4243 switch (lockTaskAuth) {
4244 case LOCK_TASK_AUTH_DONT_LOCK:
4245 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004246 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004247 case LOCK_TASK_AUTH_LAUNCHABLE:
4248 case LOCK_TASK_AUTH_WHITELISTED:
4249 return false;
4250 case LOCK_TASK_AUTH_PINNABLE:
4251 // Pinnable tasks can't be launched on top of locktask tasks.
4252 return !mLockTaskModeTasks.isEmpty();
4253 default:
4254 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4255 return true;
4256 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004257 }
4258
Craig Mautner15df08a2015-04-01 12:17:18 -07004259 void onLockTaskPackagesUpdatedLocked() {
4260 boolean didSomething = false;
4261 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4262 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004263 final boolean wasWhitelisted =
4264 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4265 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004266 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004267 final boolean isWhitelisted =
4268 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4269 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4270 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004271 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004272 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4273 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 removeLockedTaskLocked(lockedTask);
4275 lockedTask.performClearTaskLocked();
4276 didSomething = true;
4277 }
4278 }
4279 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4280 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4281 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4282 final ActivityStack stack = stacks.get(stackNdx);
4283 stack.onLockTaskPackagesUpdatedLocked();
4284 }
4285 }
Craig Mautnere0570202015-05-13 13:06:11 -07004286 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004287 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004288 if (mLockTaskModeTasks.isEmpty() && task != null
4289 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4290 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004291 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4292 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4293 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4294 false);
4295 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004296 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004297 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004298 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004299 }
4300 }
4301
Benjamin Franz43261142015-02-11 15:59:44 +00004302 int getLockTaskModeState() {
4303 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004304 }
4305
Jorim Jaggife89d122015-12-22 16:28:44 +01004306 void activityRelaunchedLocked(IBinder token) {
4307 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004308 if (mService.isSleepingOrShuttingDownLocked()) {
4309 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4310 if (r != null) {
4311 r.setSleeping(true, true);
4312 }
4313 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004314 }
4315
4316 void activityRelaunchingLocked(ActivityRecord r) {
4317 mWindowManager.notifyAppRelaunching(r.appToken);
4318 }
4319
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004320 void logStackState() {
4321 mActivityMetricsLogger.logWindowState();
4322 }
4323
Winson Chung5af42fc2017-03-24 17:11:33 -07004324 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004325 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4326 // end, then ensure that we defer all in between multi-window mode changes
4327 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4328 return;
4329 }
4330
Wale Ogunwale22e25262016-02-01 10:32:02 -08004331 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4332 final ActivityRecord r = task.mActivities.get(i);
4333 if (r.app != null && r.app.thread != null) {
4334 mMultiWindowModeChangedActivities.add(r);
4335 }
4336 }
4337
4338 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4339 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4340 }
4341 }
4342
Winson Chung5af42fc2017-03-24 17:11:33 -07004343 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004344 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004345 if (prevStack == null || prevStack == stack
4346 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4347 return;
4348 }
4349
Winson Chung5af42fc2017-03-24 17:11:33 -07004350 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4351 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004352
Winson Chung5af42fc2017-03-24 17:11:33 -07004353 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4354 boolean immediate) {
4355
4356 if (immediate) {
4357 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4358 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4359 final ActivityRecord r = task.mActivities.get(i);
4360 if (r.app != null && r.app.thread != null) {
4361 r.updatePictureInPictureMode(targetStackBounds);
4362 }
4363 }
4364 } else {
4365 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4366 final ActivityRecord r = task.mActivities.get(i);
4367 if (r.app != null && r.app.thread != null) {
4368 mPipModeChangedActivities.add(r);
4369 }
4370 }
4371 mPipModeChangedTargetStackBounds = targetStackBounds;
4372
4373 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4374 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4375 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004376 }
4377 }
4378
Tony Mak853304c2016-04-18 15:17:41 +01004379 void setDockedStackMinimized(boolean minimized) {
4380 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004381 }
4382
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004383 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004384
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004385 public ActivityStackSupervisorHandler(Looper looper) {
4386 super(looper);
4387 }
4388
Winson Chung4dabf232017-01-25 13:25:22 -08004389 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004390 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004391 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4392 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004393 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004394 }
4395
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004396 @Override
4397 public void handleMessage(Message msg) {
4398 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004399 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4400 synchronized (mService) {
4401 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4402 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004403 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004404 }
4405 }
4406 } break;
4407 case REPORT_PIP_MODE_CHANGED_MSG: {
4408 synchronized (mService) {
4409 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4410 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004411 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004412 }
4413 }
4414 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004415 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004416 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4417 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004418 if (mService.mDidDexOpt) {
4419 mService.mDidDexOpt = false;
4420 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4421 nmsg.obj = msg.obj;
4422 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4423 return;
4424 }
4425 // We don't at this point know if the activity is fullscreen,
4426 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004427 activityIdleInternal((ActivityRecord) msg.obj,
4428 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004429 } break;
4430 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004431 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004432 activityIdleInternal((ActivityRecord) msg.obj,
4433 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004434 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004435 case RESUME_TOP_ACTIVITY_MSG: {
4436 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004437 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004438 }
4439 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004440 case SLEEP_TIMEOUT_MSG: {
4441 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004442 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004443 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4444 mSleepTimeout = true;
4445 checkReadyForSleepLocked();
4446 }
4447 }
4448 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004449 case LAUNCH_TIMEOUT_MSG: {
4450 if (mService.mDidDexOpt) {
4451 mService.mDidDexOpt = false;
4452 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4453 return;
4454 }
4455 synchronized (mService) {
4456 if (mLaunchingActivity.isHeld()) {
4457 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4458 if (VALIDATE_WAKE_LOCK_CALLER
4459 && Binder.getCallingUid() != Process.myUid()) {
4460 throw new IllegalStateException("Calling must be system uid");
4461 }
4462 mLaunchingActivity.release();
4463 }
4464 }
4465 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004466 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004467 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004468 } break;
4469 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004470 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004471 } break;
4472 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004473 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004474 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004475 case CONTAINER_CALLBACK_VISIBILITY: {
4476 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004477 final IActivityContainerCallback callback = container.mCallback;
4478 if (callback != null) {
4479 try {
4480 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4481 } catch (RemoteException e) {
4482 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004483 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004484 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004485 case LOCK_TASK_START_MSG: {
4486 // When lock task starts, we disable the status bars.
4487 try {
Jason Monk62515be2014-05-21 16:06:19 -04004488 if (mLockTaskNotify == null) {
4489 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004490 }
Jason Monk62515be2014-05-21 16:06:19 -04004491 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004492 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004493 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004494 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004495 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004496 flags = StatusBarManager.DISABLE_MASK
4497 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004498 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004499 flags = StatusBarManager.DISABLE_MASK
4500 & (~StatusBarManager.DISABLE_BACK)
4501 & (~StatusBarManager.DISABLE_HOME)
4502 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004503 }
4504 getStatusBarService().disable(flags, mToken,
4505 mService.mContext.getPackageName());
4506 }
4507 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004508 if (getDevicePolicyManager() != null) {
4509 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004510 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004511 }
justinzhang5286d3f2014-05-12 17:06:01 -04004512 } catch (RemoteException ex) {
4513 throw new RuntimeException(ex);
4514 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004515 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004516 case LOCK_TASK_END_MSG: {
4517 // When lock task ends, we enable the status bars.
4518 try {
Jason Monk62515be2014-05-21 16:06:19 -04004519 if (getStatusBarService() != null) {
4520 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4521 mService.mContext.getPackageName());
4522 }
4523 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004524 if (getDevicePolicyManager() != null) {
4525 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4526 msg.arg1);
4527 }
Jason Monk62515be2014-05-21 16:06:19 -04004528 if (mLockTaskNotify == null) {
4529 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4530 }
4531 mLockTaskNotify.show(false);
4532 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004533 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004534 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004535 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004536 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004537 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004538 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004539 new LockPatternUtils(mService.mContext)
4540 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004541 }
4542 } catch (SettingNotFoundException e) {
4543 // No setting, don't lock.
4544 }
justinzhang5286d3f2014-05-12 17:06:01 -04004545 } catch (RemoteException ex) {
4546 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004547 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004548 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004549 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004550 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004551 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4552 if (mLockTaskNotify == null) {
4553 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4554 }
4555 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4556 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004557 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4558 final ActivityContainer container = (ActivityContainer) msg.obj;
4559 final IActivityContainerCallback callback = container.mCallback;
4560 if (callback != null) {
4561 try {
4562 callback.onAllActivitiesComplete(container.asBinder());
4563 } catch (RemoteException e) {
4564 }
4565 }
4566 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004567 case LAUNCH_TASK_BEHIND_COMPLETE: {
4568 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004569 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004570 if (r != null) {
4571 handleLaunchTaskBehindCompleteLocked(r);
4572 }
4573 }
4574 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004575
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004576 }
4577 }
4578 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004579
Ying Wangb081a592014-04-22 15:20:16 -07004580 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004581 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4582 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004583 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004584 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004585 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004586 ActivityRecord mParentActivity = null;
4587 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004588
Craig Mautnere3a00d72014-04-16 08:31:19 -07004589 boolean mVisible = true;
4590
Craig Mautner4a1cb222013-12-04 16:14:06 -08004591 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004592 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004593
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004594 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4595 final static int CONTAINER_STATE_NO_SURFACE = 1;
4596 final static int CONTAINER_STATE_FINISHING = 2;
4597 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4598
Wale Ogunwale1666e312016-12-16 11:27:18 -08004599 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004600 synchronized (mService) {
4601 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004602 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004603 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004604
4605 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004606 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004607 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004608 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004609
Bryce Leeaf691c02017-03-20 14:20:22 -07004610 protected void createStack(int stackId, boolean onTop) {
4611 switch (stackId) {
4612 case PINNED_STACK_ID:
4613 new PinnedActivityStack(this, mRecentTasks, onTop);
4614 break;
4615 default:
4616 new ActivityStack(this, mRecentTasks, onTop);
4617 break;
4618 }
4619 }
4620
Andrii Kulian839def92016-11-02 10:58:58 -07004621 /**
4622 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004623 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004624 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004625 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004626 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004627 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004628 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004629 if (mActivityDisplay != null) {
4630 throw new IllegalStateException("ActivityContainer is already attached, " +
4631 "displayId=" + mActivityDisplay.mDisplayId);
4632 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004633 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004634 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004635 }
4636
4637 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004638 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004639 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004640 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004641 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004642 return;
4643 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004644 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004645 }
4646 }
4647
4648 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004649 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004650 synchronized (mService) {
4651 if (mActivityDisplay != null) {
4652 return mActivityDisplay.mDisplayId;
4653 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004654 }
4655 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004656 }
4657
Jeff Brownca9bc702014-02-11 14:32:56 -08004658 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004659 public int getStackId() {
4660 synchronized (mService) {
4661 return mStackId;
4662 }
4663 }
4664
4665 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004666 public boolean injectEvent(InputEvent event) {
4667 final long origId = Binder.clearCallingIdentity();
4668 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004669 synchronized (mService) {
4670 if (mActivityDisplay != null) {
4671 return mInputManagerInternal.injectInputEvent(event,
4672 mActivityDisplay.mDisplayId,
4673 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4674 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004675 }
4676 return false;
4677 } finally {
4678 Binder.restoreCallingIdentity(origId);
4679 }
4680 }
4681
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004682 @Override
4683 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004684 synchronized (mService) {
4685 if (mContainerState == CONTAINER_STATE_FINISHING) {
4686 return;
4687 }
4688 mContainerState = CONTAINER_STATE_FINISHING;
4689
Craig Mautnerd163e752014-06-13 17:18:47 -07004690 long origId = Binder.clearCallingIdentity();
4691 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004692 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004693 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004694 } finally {
4695 Binder.restoreCallingIdentity(origId);
4696 }
4697 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004698 }
4699
Andrii Kulian03c403d2016-11-11 11:14:12 -08004700 /**
4701 * Remove the stack completely. Must be called only when there are no tasks left in it,
4702 * as this method does not finish running activities.
4703 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004704 void removeLocked() {
4705 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004706 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004707 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004708 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004709 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004710 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004711 }
4712
Andrii Kulian839def92016-11-02 10:58:58 -07004713 /**
4714 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4715 * completely or re-parented.
4716 */
4717 private void removeFromDisplayLocked() {
4718 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4719 + " current displayId=" + mActivityDisplay.mDisplayId);
4720
Wale Ogunwale1666e312016-12-16 11:27:18 -08004721 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004722 mActivityDisplay = null;
4723 }
4724
4725 /**
4726 * Move the stack to specified display.
4727 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004728 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004729 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004730 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004731 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4732 + mActivityDisplay + " to display=" + activityDisplay
4733 + " Callers=" + Debug.getCallers(2));
4734
4735 removeFromDisplayLocked();
4736
4737 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004738 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004739 }
4740
Craig Mautner4a1cb222013-12-04 16:14:06 -08004741 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004742 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004743 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004744 }
4745
4746 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004747 public final int startActivityIntentSender(IIntentSender intentSender)
4748 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004749 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4750
4751 if (!(intentSender instanceof PendingIntentRecord)) {
4752 throw new IllegalArgumentException("Bad PendingIntent object");
4753 }
4754
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004755 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004756 Binder.getCallingUid(), mCurrentUser, false,
4757 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004758
4759 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4760 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4761 pendingIntent.key.requestResolvedType);
4762
Dianne Hackborn98305522017-05-05 17:53:53 -07004763 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004764 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4765 }
4766
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004767 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004768 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004769 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004770 throw new SecurityException(
4771 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4772 }
4773 }
4774
Craig Mautnerdf88d732014-01-27 09:21:32 -08004775 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004776 public IBinder asBinder() {
4777 return this;
4778 }
4779
Craig Mautner4504de52013-12-20 09:06:56 -08004780 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004781 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004782 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004783 }
4784
Craig Mautner4a1cb222013-12-04 16:14:06 -08004785 ActivityStackSupervisor getOuter() {
4786 return ActivityStackSupervisor.this;
4787 }
4788
Craig Mautnerd163e752014-06-13 17:18:47 -07004789 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004790 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004791 }
4792
Craig Mautner6985bad2014-04-21 15:22:06 -07004793 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004794 void setVisible(boolean visible) {
4795 if (mVisible != visible) {
4796 mVisible = visible;
4797 if (mCallback != null) {
4798 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4799 0 /* unused */, this).sendToTarget();
4800 }
4801 }
4802 }
4803
Craig Mautner6985bad2014-04-21 15:22:06 -07004804 void setDrawn() {
4805 }
4806
Craig Mautner1b4bf852014-05-26 15:06:32 -07004807 // You can always start a new task on a regular ActivityStack.
4808 boolean isEligibleForNewTasks() {
4809 return true;
4810 }
4811
Craig Mautnerd163e752014-06-13 17:18:47 -07004812 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004813 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004814 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004815 }
4816
Craig Mautner34b73df2014-01-12 21:11:08 -08004817 @Override
4818 public String toString() {
4819 return mIdString + (mActivityDisplay == null ? "N" : "A");
4820 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004821 }
4822
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004823 private class VirtualActivityContainer extends ActivityContainer {
4824 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004825 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004826
4827 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004828 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4829 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004830 mParentActivity = parent;
4831 mCallback = callback;
4832 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004833 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004834 }
4835
4836 @Override
4837 public void setSurface(Surface surface, int width, int height, int density) {
4838 super.setSurface(surface, width, height, density);
4839
4840 synchronized (mService) {
4841 final long origId = Binder.clearCallingIdentity();
4842 try {
4843 setSurfaceLocked(surface, width, height, density);
4844 } finally {
4845 Binder.restoreCallingIdentity(origId);
4846 }
4847 }
4848 }
4849
4850 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4851 if (mContainerState == CONTAINER_STATE_FINISHING) {
4852 return;
4853 }
4854 VirtualActivityDisplay virtualActivityDisplay =
4855 (VirtualActivityDisplay) mActivityDisplay;
4856 if (virtualActivityDisplay == null) {
4857 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004858 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004859 mActivityDisplay = virtualActivityDisplay;
4860 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004861 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004862 }
4863
4864 if (mSurface != null) {
4865 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004866 }
4867
Craig Mautner6985bad2014-04-21 15:22:06 -07004868 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004869 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004870 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004871 } else {
4872 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004873 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004874 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004875 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004876 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004877 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004878
Craig Mautnerd163e752014-06-13 17:18:47 -07004879 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004880
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004881 if (DEBUG_STACK) Slog.d(TAG_STACK,
4882 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004883 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004884
Craig Mautner6985bad2014-04-21 15:22:06 -07004885 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004886 boolean isAttachedLocked() {
4887 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004888 }
4889
4890 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004891 void setDrawn() {
4892 synchronized (mService) {
4893 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004894 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004895 }
4896 }
4897
Craig Mautner1b4bf852014-05-26 15:06:32 -07004898 // Never start a new task on an ActivityView if it isn't explicitly specified.
4899 @Override
4900 boolean isEligibleForNewTasks() {
4901 return false;
4902 }
4903
Craig Mautnerd163e752014-06-13 17:18:47 -07004904 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004905 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004906 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4907 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4908 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4909 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4910 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004911 }
4912 }
4913
Craig Mautner4a1cb222013-12-04 16:14:06 -08004914 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4915 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004916 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004917 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004918 int mDisplayId;
4919 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004920
Craig Mautner4a1cb222013-12-04 16:14:06 -08004921 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4922 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004923 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004924
Jose Lima4b6c6692014-08-12 17:41:12 -07004925 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004926
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004927 /** Array of all UIDs that are present on the display. */
4928 private IntArray mDisplayAccessUIDs = new IntArray();
4929
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004930 ActivityDisplay() {
4931 }
Craig Mautner4504de52013-12-20 09:06:56 -08004932
Craig Mautner1a70a162014-09-13 12:09:31 -07004933 // After instantiation, check that mDisplay is not null before using this. The alternative
4934 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004935 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004936 final Display display = mDisplayManager.getDisplay(displayId);
4937 if (display == null) {
4938 return;
4939 }
4940 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004941 }
4942
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004943 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004944 mDisplay = display;
4945 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004946 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004947
Winson Chung7c3ede32017-03-14 14:18:34 -07004948 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004949 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004950 + " to displayId=" + mDisplayId + " position=" + position);
4951 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004952 }
4953
Wale Ogunwale1666e312016-12-16 11:27:18 -08004954 void detachStack(ActivityStack stack) {
4955 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004956 + " from displayId=" + mDisplayId);
4957 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004958 }
4959
Jose Lima4b6c6692014-08-12 17:41:12 -07004960 void setVisibleBehindActivity(ActivityRecord r) {
4961 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004962 }
4963
Jose Lima4b6c6692014-08-12 17:41:12 -07004964 boolean hasVisibleBehindActivity() {
4965 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004966 }
4967
Craig Mautner34b73df2014-01-12 21:11:08 -08004968 @Override
4969 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004970 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4971 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004972
4973 @Override
4974 protected int getChildCount() {
4975 return mStacks.size();
4976 }
4977
4978 @Override
4979 protected ConfigurationContainer getChildAt(int index) {
4980 return mStacks.get(index);
4981 }
4982
4983 @Override
4984 protected ConfigurationContainer getParent() {
4985 return ActivityStackSupervisor.this;
4986 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004987
4988 boolean isPrivate() {
4989 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4990 }
4991
4992 boolean isUidPresent(int uid) {
4993 for (ActivityStack stack : mStacks) {
4994 if (stack.isUidPresent(uid)) {
4995 return true;
4996 }
4997 }
4998 return false;
4999 }
5000
5001 /** Update and get all UIDs that are present on the display and have access to it. */
5002 private IntArray getPresentUIDs() {
5003 mDisplayAccessUIDs.clear();
5004 for (ActivityStack stack : mStacks) {
5005 stack.getPresentUIDs(mDisplayAccessUIDs);
5006 }
5007 return mDisplayAccessUIDs;
5008 }
Andrii Kulian250d6532017-02-08 23:30:45 -08005009
5010 boolean shouldDestroyContentOnRemove() {
5011 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
5012 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005013 }
5014
5015 class VirtualActivityDisplay extends ActivityDisplay {
5016 VirtualDisplay mVirtualDisplay;
5017
Craig Mautner6985bad2014-04-21 15:22:06 -07005018 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005019 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonb0608632017-04-05 10:31:15 -07005020 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5021 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005022 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonb0608632017-04-05 10:31:15 -07005023 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5024 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005025
5026 init(mVirtualDisplay.getDisplay());
5027
Bryce Leeaf3a4002017-03-20 10:37:12 -07005028 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005029 }
5030
5031 void setSurface(Surface surface) {
5032 if (mVirtualDisplay != null) {
5033 mVirtualDisplay.setSurface(surface);
5034 }
5035 }
5036
5037 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005038 void detachStack(ActivityStack stack) {
5039 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005040 if (mVirtualDisplay != null) {
5041 mVirtualDisplay.release();
5042 mVirtualDisplay = null;
5043 }
5044 }
5045
5046 @Override
5047 public String toString() {
5048 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005049 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005050 }
Jose Lima58e66d62014-05-27 20:00:27 -07005051
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005052 ActivityStack findStackBehind(ActivityStack stack) {
5053 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005054 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005055 if (display == null) {
5056 return null;
5057 }
5058 final ArrayList<ActivityStack> stacks = display.mStacks;
5059 for (int i = stacks.size() - 1; i >= 0; i--) {
5060 if (stacks.get(i) == stack && i > 0) {
5061 return stacks.get(i - 1);
5062 }
5063 }
5064 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5065 + " in=" + stacks);
5066 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005067
Jorim Jaggic69bd222016-03-15 14:38:37 +01005068 /**
5069 * Puts a task into resizing mode during the next app transition.
5070 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005071 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005072 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005073 private void setResizingDuringAnimation(TaskRecord task) {
5074 mResizingTasksDuringAnimation.add(task.taskId);
5075 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005076 }
5077
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005078 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5079 final TaskRecord task;
5080 final int callingUid;
5081 final String callingPackage;
5082 final Intent intent;
5083 final int userId;
5084 final ActivityOptions activityOptions = (bOptions != null)
5085 ? new ActivityOptions(bOptions) : null;
5086 final int launchStackId = (activityOptions != null)
5087 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005088 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005089 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005090 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005091 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005092
Matthew Ng606dd802017-06-05 14:06:32 -07005093 mWindowManager.deferSurfaceLayout();
5094 try {
5095 if (launchStackId == DOCKED_STACK_ID) {
5096 mWindowManager.setDockedStackCreateState(
5097 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005098
Matthew Ng606dd802017-06-05 14:06:32 -07005099 // Defer updating the stack in which recents is until the app transition is done, to
5100 // not run into issues where we still need to draw the task in recents but the
5101 // docked stack is already created.
5102 deferUpdateBounds(RECENTS_STACK_ID);
5103 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005104 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005105
Matthew Ng606dd802017-06-05 14:06:32 -07005106 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5107 launchStackId);
5108 if (task == null) {
5109 continueUpdateBounds(RECENTS_STACK_ID);
5110 mWindowManager.executeAppTransition();
5111 throw new IllegalArgumentException(
5112 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5113 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005114
Matthew Ng606dd802017-06-05 14:06:32 -07005115 // Since we don't have an actual source record here, we assume that the currently
5116 // focused activity was the source.
5117 final ActivityStack focusedStack = getFocusedStack();
5118 final ActivityRecord sourceRecord =
5119 focusedStack != null ? focusedStack.topActivity() : null;
5120
5121 if (launchStackId != INVALID_STACK_ID) {
5122 if (task.getStackId() != launchStackId) {
5123 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5124 DEFER_RESUME, "startActivityFromRecents");
5125 }
5126 }
5127
5128 // If the user must confirm credentials (e.g. when first launching a work app and the
5129 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5130 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5131 && task.getRootActivity() != null) {
5132 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
5133 mActivityMetricsLogger.notifyActivityLaunching();
5134 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
5135 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5136 task.getTopActivity());
5137
5138 // If we are launching the task in the docked stack, put it into resizing mode so
5139 // the window renders full-screen with the background filling the void. Also only
5140 // call this at the end to make sure that tasks exists on the window manager side.
5141 if (launchStackId == DOCKED_STACK_ID) {
5142 setResizingDuringAnimation(task);
5143 }
5144
5145 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
5146 ActivityManager.START_TASK_TO_FRONT,
5147 sourceRecord != null
5148 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
5149 sourceRecord, task.getStack());
5150 return ActivityManager.START_TASK_TO_FRONT;
5151 }
5152 callingUid = task.mCallingUid;
5153 callingPackage = task.mCallingPackage;
5154 intent = task.intent;
5155 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5156 userId = task.userId;
5157 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
5158 null, null, 0, 0, bOptions, userId, null, task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005159 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005160 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005161 }
Matthew Ng606dd802017-06-05 14:06:32 -07005162 return result;
5163 } finally {
5164 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005165 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005166 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005167
5168 /**
5169 * @return a list of activities which are the top ones in each visible stack. The first
5170 * entry will be the focused activity.
5171 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005172 List<IBinder> getTopVisibleActivities() {
5173 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5174 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005175 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005176 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5177 // Traverse all stacks on a display.
5178 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5179 final ActivityStack stack = display.mStacks.get(j);
5180 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005181 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005182 == ActivityStack.STACK_VISIBLE) {
5183 final ActivityRecord top = stack.topActivity();
5184 if (top != null) {
5185 if (stack == mFocusedStack) {
5186 topActivityTokens.add(0, top.appToken);
5187 } else {
5188 topActivityTokens.add(top.appToken);
5189 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005190 }
5191 }
5192 }
5193 }
5194 return topActivityTokens;
5195 }
Bryce Lee4a194382017-04-04 14:32:48 -07005196
5197 /**
5198 * Internal container to store a match qualifier alongside a WaitResult.
5199 */
5200 static class WaitInfo {
5201 private final ComponentName mTargetComponent;
5202 private final WaitResult mResult;
5203
5204 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5205 this.mTargetComponent = targetComponent;
5206 this.mResult = result;
5207 }
5208
Wale Ogunwale3270f172017-04-26 07:29:42 -07005209 public boolean matches(ComponentName targetComponent) {
5210 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005211 }
5212
5213 public WaitResult getResult() {
5214 return mResult;
5215 }
5216
5217 public ComponentName getComponent() {
5218 return mTargetComponent;
5219 }
5220
5221 public void dump(PrintWriter pw, String prefix) {
5222 pw.println(prefix + "WaitInfo:");
5223 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5224 pw.println(prefix + " mResult=");
5225 mResult.dump(pw, prefix);
5226 }
5227 }
Craig Mautner27084302013-03-25 08:05:25 -07005228}