blob: 62dbd84b0e3e0eca7ba166e3e18675f8b545a640 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Jorim Jaggife762342016-10-13 14:33:27 +020019import static android.Manifest.permission.START_ANY_ACTIVITY;
20import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
21import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
22import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
23import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
24import static android.app.ActivityManager.RESIZE_MODE_FORCED;
25import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
26import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080036import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020037import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
38import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
39import static android.content.pm.PackageManager.PERMISSION_GRANTED;
40import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
41import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070042import static android.view.Display.FLAG_PRIVATE;
43import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080044import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020045
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
72import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
73import static com.android.server.am.ActivityManagerService.ANIMATE;
74import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
75import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
76import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
86import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
87import static com.android.server.am.ActivityStack.STACK_VISIBLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080093import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
94import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
95import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020096import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080097import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020098
Svetoslav7008b512015-06-24 18:47:07 -070099import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700100import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100101import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.app.ActivityOptions;
108import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800110import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700111import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700114import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400115import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import android.content.Intent;
120import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700122import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.PackageManager;
124import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100125import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800127import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128import android.hardware.display.DisplayManager;
129import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800130import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800131import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800132import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800133import android.hardware.input.InputManager;
134import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100136import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700137import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700140import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700141import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700143import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700145import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400146import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700148import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700149import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700150import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100151import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700152import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700153import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400154import android.provider.Settings;
155import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700156import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700157import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700158import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700159import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800160import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700161import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800164import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800165import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800166import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800167
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800168import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800169import com.android.internal.logging.MetricsLogger;
170import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700171import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400172import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700173import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400174import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800175import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700176import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700177import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700178
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179import java.io.FileDescriptor;
180import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700181import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700182import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700183import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700184import java.util.List;
Andrii Kulian0864bbb2017-02-16 15:45:58 -0800185import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700186import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700187
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800188public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800189 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700191 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700193 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800200 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800202
Craig Mautnerf3333272013-04-22 10:55:53 -0700203 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700205
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700210 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211
Craig Mautner05d29032013-05-03 13:40:13 -0700212 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
213 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
214 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800217 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
218 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
219 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700220 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400221 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
222 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700223 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700224 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700225 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800226 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
227 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800228
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700230
Jason Monk62515be2014-05-21 16:06:19 -0400231 private static final String LOCK_TASK_TAG = "Lock-to-App";
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. stack) that we are trying to get
234 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800235 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700237 // Used to indicate that windows of activities should be preserved during the resize.
238 static final boolean PRESERVE_WINDOWS = true;
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. task) should be moved/created
241 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700242 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
244 // Used to indicate that an objects (e.g. task) removal from its container
245 // (e.g. stack) is due to it moving to another container.
246 static final boolean MOVING = true;
247
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700248 // Force the focus to change to the stack we are moving a task to..
249 static final boolean FORCE_FOCUS = true;
250
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700251 // Restore task from the saved recents if it can't be found in any live stack.
252 static final boolean RESTORE_FROM_RECENTS = true;
253
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700254 // Don't execute any calls to resume.
255 static final boolean DEFER_RESUME = true;
256
Winson Chung010927a2016-12-15 16:12:35 -0800257 // Used to indicate that a task is removed it should also be removed from recents.
258 static final boolean REMOVE_FROM_RECENTS = true;
259
Svetoslav7008b512015-06-24 18:47:07 -0700260 // Activity actions an app cannot start if it uses a permission which is not granted.
261 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
262 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700263
Svetoslav7008b512015-06-24 18:47:07 -0700264 static {
265 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
266 Manifest.permission.CAMERA);
267 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
268 Manifest.permission.CAMERA);
269 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
270 Manifest.permission.CALL_PHONE);
271 }
272
Svet Ganov99b60432015-06-27 13:15:22 -0700273 /** Action restriction: launching the activity is not restricted. */
274 private static final int ACTIVITY_RESTRICTION_NONE = 0;
275 /** Action restriction: launching the activity is restricted by a permission. */
276 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
277 /** Action restriction: launching the activity is restricted by an app op. */
278 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700279
justinzhang5286d3f2014-05-12 17:06:01 -0400280 /** Status Bar Service **/
281 private IBinder mToken = new Binder();
282 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400283 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400284
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700285 // For debugging to make sure the caller when acquiring/releasing our
286 // wake lock is the system process.
287 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 /** The number of distinct task ids that can be assigned to the tasks of a single user */
289 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700290
Craig Mautner27084302013-03-25 08:05:25 -0700291 final ActivityManagerService mService;
292
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800293 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800294
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295 final ActivityStackSupervisorHandler mHandler;
296
297 /** Short cut */
298 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800299 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700300
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700301 /** Counter for next free stack ID to use for dynamic activity stacks. */
302 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700303
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800304 /**
305 * Maps the task identifier that activities are currently being started in to the userId of the
306 * task. Each time a new task is created, the entry for the userId of the task is incremented
307 */
308 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700309
Craig Mautner2420ead2013-04-01 17:13:20 -0700310 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack containing the launcher app. Assumed to always be attached to
314 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700316
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800318 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700319
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
321 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800322 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
325 /** List of activities that are waiting for a new activity to become visible before completing
326 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700330 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331
332 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700333 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700334
Craig Mautnerde4ef022013-04-07 19:01:33 -0700335 /** List of activities that are ready to be stopped, but waiting for the next activity to
336 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700338
Craig Mautnerf3333272013-04-22 10:55:53 -0700339 /** List of activities that are ready to be finished, but waiting for the previous activity to
340 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800341 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700342
Craig Mautner0eea92c2013-05-16 13:35:39 -0700343 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800344 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700345
Wale Ogunwale22e25262016-02-01 10:32:02 -0800346 /** List of activities whose multi-window mode changed that we need to report to the
347 * application */
348 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
349
350 /** List of activities whose picture-in-picture mode changed that we need to report to the
351 * application */
352 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
353
Craig Mautnerf3333272013-04-22 10:55:53 -0700354 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700355 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700356
Craig Mautnerde4ef022013-04-07 19:01:33 -0700357 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
358 * is being brought in front of us. */
359 boolean mUserLeaving = false;
360
Craig Mautner0eea92c2013-05-16 13:35:39 -0700361 /** Set when we have taken too long waiting to go to sleep. */
362 boolean mSleepTimeout = false;
363
364 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700365 * We don't want to allow the device to go to sleep while in the process
366 * of launching an activity. This is primarily to allow alarm intent
367 * receivers to launch an activity and get that to run before the device
368 * goes back to sleep.
369 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700370 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700371
372 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373 * Set when the system is going to sleep, until we have
374 * successfully paused the current activity and released our wake lock.
375 * At that point the system is allowed to actually sleep.
376 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700377 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700378
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700379 /** Stack id of the front stack when user switched, indexed by userId. */
380 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700381
Craig Mautner4504de52013-12-20 09:06:56 -0800382 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800384 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385
386 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700387 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800389 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
390
391 private DisplayManagerInternal mDisplayManagerInternal;
392 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800393
Craig Mautner15df08a2015-04-01 12:17:18 -0700394 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
395 * may be finished until there is only one entry left. If this is empty the system is not
396 * in lockTask mode. */
397 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000398 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700399 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
400 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000401 */
402 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400403 /**
404 * Notifies the user when entering/exiting lock-task.
405 */
406 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800407
Wale Ogunwaled046a012015-12-24 13:05:59 -0800408 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800409 boolean inResumeTopActivity;
410
Andrii Kulian1e32e022016-09-16 15:29:34 -0700411 /**
412 * Temporary rect used during docked stack resize calculation so we don't need to create a new
413 * object each time.
414 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700415 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700416
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700417 // The default minimal size that will be used if the activity doesn't specify its minimal size.
418 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700419 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700420
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700421 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
422 private boolean mTaskLayersChanged = true;
423
Jorim Jaggi275561a2016-02-23 10:11:02 -0500424 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800425
Andrii Kulian1779e612016-10-12 21:58:25 -0700426 @Override
427 protected int getChildCount() {
428 return mActivityDisplays.size();
429 }
430
431 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700432 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700433 return mActivityDisplays.valueAt(index);
434 }
435
436 @Override
437 protected ConfigurationContainer getParent() {
438 return null;
439 }
440
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700441 Configuration getDisplayOverrideConfiguration(int displayId) {
442 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
443 if (activityDisplay == null) {
444 throw new IllegalArgumentException("No display found with id: " + displayId);
445 }
446
447 return activityDisplay.getOverrideConfiguration();
448 }
449
450 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
451 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
452 if (activityDisplay == null) {
453 throw new IllegalArgumentException("No display found with id: " + displayId);
454 }
455
456 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
457 }
458
Wale Ogunwale39381972015-12-17 17:15:29 -0800459 static class FindTaskResult {
460 ActivityRecord r;
461 boolean matchedByRootAffinity;
462 }
463 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
464
Craig Mautneree36c772014-07-16 14:56:05 -0700465 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800466 * Temp storage for display ids sorted in focus order.
467 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
468 * it's more efficient, as the number of displays is usually small.
469 */
470 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
471
472 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100473 * Used to keep track whether app visibilities got changed since the last pause. Useful to
474 * determine whether to invoke the task stack change listener after pausing.
475 */
476 boolean mAppVisibilitiesChangedSinceLastPause;
477
478 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100479 * Set of tasks that are in resizing mode during an app transition to fill the "void".
480 */
481 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
482
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700483
484 /**
485 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
486 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
487 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
488 * like the docked stack going empty.
489 */
490 private boolean mAllowDockedStackResize = true;
491
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100492 /**
Tony Mak853304c2016-04-18 15:17:41 +0100493 * Is dock currently minimized.
494 */
495 boolean mIsDockMinimized;
496
Jorim Jaggife762342016-10-13 14:33:27 +0200497 final KeyguardController mKeyguardController;
498
Tony Mak853304c2016-04-18 15:17:41 +0100499 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700500 * Description of a request to start a new activity, which has been held
501 * due to app switches being disabled.
502 */
503 static class PendingActivityLaunch {
504 final ActivityRecord r;
505 final ActivityRecord sourceRecord;
506 final int startFlags;
507 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700508 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700509
510 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700511 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700512 r = _r;
513 sourceRecord = _sourceRecord;
514 startFlags = _startFlags;
515 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700516 callerApp = _callerApp;
517 }
518
519 void sendErrorResult(String message) {
520 try {
521 if (callerApp.thread != null) {
522 callerApp.thread.scheduleCrash(message);
523 }
524 } catch (RemoteException e) {
525 Slog.e(TAG, "Exception scheduling crash of failed "
526 + "activity launcher sourceRecord=" + sourceRecord, e);
527 }
Craig Mautneree36c772014-07-16 14:56:05 -0700528 }
529 }
530
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800531 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700532 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700533 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800534 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200535 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700536 }
537
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800538 void setRecentTasks(RecentTasks recentTasks) {
539 mRecentTasks = recentTasks;
540 }
541
Jeff Brown2c43c332014-06-12 22:38:59 -0700542 /**
543 * At the time when the constructor runs, the power manager has not yet been
544 * initialized. So we initialize our wakelocks afterwards.
545 */
546 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700548 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700549 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700550 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700551 }
552
justinzhang5286d3f2014-05-12 17:06:01 -0400553 // This function returns a IStatusBarService. The value is from ServiceManager.
554 // getService and is cached.
555 private IStatusBarService getStatusBarService() {
556 synchronized (mService) {
557 if (mStatusBarService == null) {
558 mStatusBarService = IStatusBarService.Stub.asInterface(
559 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
560 if (mStatusBarService == null) {
561 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
562 }
563 }
564 return mStatusBarService;
565 }
566 }
567
Jason Monk35c62a42014-06-17 10:24:47 -0400568 private IDevicePolicyManager getDevicePolicyManager() {
569 synchronized (mService) {
570 if (mDevicePolicyManager == null) {
571 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
572 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
573 if (mDevicePolicyManager == null) {
574 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
575 }
576 }
577 return mDevicePolicyManager;
578 }
579 }
580
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700581 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 synchronized (mService) {
583 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200584 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800585
586 mDisplayManager =
587 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
588 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800589 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590
591 Display[] displays = mDisplayManager.getDisplays();
592 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
593 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700595 if (activityDisplay.mDisplay == null) {
596 throw new IllegalStateException("Default Display does not exist");
597 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800598 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700599 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600 }
601
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800602 mHomeStack = mFocusedStack = mLastFocusedStack =
603 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800604
605 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606 }
Craig Mautner27084302013-03-25 08:05:25 -0700607 }
608
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700609 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700611 }
612
Craig Mautnerde4ef022013-04-07 19:01:33 -0700613 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700615 }
616
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700617 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700618 if (stack == null) {
619 return false;
620 }
621
Craig Mautnerdf88d732014-01-27 09:21:32 -0800622 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
623 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700624 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800625 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800626 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700627 }
628
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800629 /** The top most stack on its display. */
630 boolean isFrontStackOnDisplay(ActivityStack stack) {
631 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
632 }
633
634 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700635 if (stack == null) {
636 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700638
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700639 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
640 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700641 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800642 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800643 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700644 }
645
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800647 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
648 if (!focusCandidate.isFocusable()) {
649 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800650 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 }
652
653 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800655 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800656
Wale Ogunwaled046a012015-12-24 13:05:59 -0800657 EventLogTags.writeAmFocusedStack(
658 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
659 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
660 }
661
662 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800663 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800664 if (r != null && r.idle) {
665 checkFinishBootingLocked();
666 }
667 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700668 }
669
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700670 void moveHomeStackToFront(String reason) {
671 mHomeStack.moveToFront(reason);
672 }
673
Matthew Ng330757d2017-02-28 14:19:17 -0800674 void moveRecentsStackToFront(String reason) {
675 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
676 if (recentsStack != null) {
677 recentsStack.moveToFront(reason);
678 }
679 }
680
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800682 boolean moveHomeStackTaskToTop(String reason) {
683 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700684
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700685 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700686 if (top == null) {
687 return false;
688 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700689 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700690 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700691 }
692
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800693 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700694 if (!mService.mBooting && !mService.mBooted) {
695 // Not ready yet!
696 return false;
697 }
698
Craig Mautner84984fa2014-06-19 11:19:20 -0700699 if (prev != null) {
700 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
701 }
702
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800703 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700704 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 final String myReason = reason + " resumeHomeStackTask";
706
Mark Lua56ea122015-10-08 13:31:01 +0800707 // Only resume home activity if isn't finishing.
708 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700709 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800710 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700711 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800712 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700713 }
714
Craig Mautner8d341ef2013-03-26 09:03:27 -0700715 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700716 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700717 }
718
719 /**
720 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
721 * @param id Id of the task we would like returned.
722 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
723 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700724 * @param stackId The stack to restore the task to (default launch stack will be used if
725 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700726 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700727 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800728 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
732 ActivityStack stack = stacks.get(stackNdx);
733 TaskRecord task = stack.taskForIdLocked(id);
734 if (task != null) {
735 return task;
736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700737 }
738 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800739
740 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700741 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800742 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800743 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700744 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800745 return null;
746 }
747
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700748 if (!restoreFromRecents) {
749 return task;
750 }
751
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700752 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700753 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
754 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800755 return null;
756 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700757 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800758 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700759 }
760
Craig Mautner6170f732013-04-02 13:05:23 -0700761 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800762 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800763 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800764 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
766 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
767 if (r != null) {
768 return r;
769 }
Craig Mautner6170f732013-04-02 13:05:23 -0700770 }
771 }
772 return null;
773 }
774
Tony Mak853304c2016-04-18 15:17:41 +0100775 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000776 * Detects whether we should show a lock screen in front of this task for a locked user.
777 * <p>
778 * We'll do this if either of the following holds:
779 * <ul>
780 * <li>The top activity explicitly belongs to {@param userId}.</li>
781 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
782 * </ul>
783 *
784 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100785 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000786 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
787 // To handle the case that work app is in the task but just is not the top one.
788 final ActivityRecord activityRecord = task.getTopActivity();
789 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
790
791 return (activityRecord != null && activityRecord.userId == userId)
792 || (resultTo != null && resultTo.userId == userId);
793 }
794
795 /**
796 * Find all visible task stacks containing {@param userId} and intercept them with an activity
797 * to block out the contents and possibly start a credential-confirming intent.
798 *
799 * @param userId user handle for the locked managed profile.
800 */
801 void lockAllProfileTasks(@UserIdInt int userId) {
802 mWindowManager.deferSurfaceLayout();
803 try {
804 final List<ActivityStack> stacks = getStacks();
805 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
806 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
807 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
808 final TaskRecord task = tasks.get(taskNdx);
809
810 // Check the task for a top activity belonging to userId, or returning a result
811 // to an activity belonging to userId. Example case: a document picker for
812 // personal files, opened by a work app, should still get locked.
813 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000814 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
815 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000816 }
Tony Mak853304c2016-04-18 15:17:41 +0100817 }
818 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000819 } finally {
820 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100821 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000822 }
823
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800824 void setNextTaskIdForUserLocked(int taskId, int userId) {
825 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
826 if (taskId > currentTaskId) {
827 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700828 }
829 }
830
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700831 static int nextTaskIdForUser(int taskId, int userId) {
832 int nextTaskId = taskId + 1;
833 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
834 // Wrap around as there will be smaller task ids that are available now.
835 nextTaskId -= MAX_TASK_IDS_PER_USER;
836 }
837 return nextTaskId;
838 }
839
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800840 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800841 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
842 // for a userId u, a taskId can only be in the range
843 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
844 // 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 -0700845 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800846 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
847 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
848 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700849 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800850 if (candidateTaskId == currentTaskId) {
851 // Something wrong!
852 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
853 throw new IllegalStateException("Cannot get an available task id."
854 + " Reached limit of " + MAX_TASK_IDS_PER_USER
855 + " running tasks per user.");
856 }
857 }
858 mCurTaskIdForUser.put(userId, candidateTaskId);
859 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700860 }
861
Chong Zhang6cda19c2016-06-14 19:07:56 -0700862 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800863 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700864 if (stack == null) {
865 return null;
866 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700867 ActivityRecord resumedActivity = stack.mResumedActivity;
868 if (resumedActivity == null || resumedActivity.app == null) {
869 resumedActivity = stack.mPausingActivity;
870 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700871 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700872 }
873 }
874 return resumedActivity;
875 }
876
Dianne Hackbornff072722014-09-24 10:56:28 -0700877 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700878 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800880 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
881 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
883 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700884 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 continue;
886 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700887 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800888 if (hr != null) {
889 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
890 && processName.equals(hr.processName)) {
891 try {
George Mount2c92c972014-03-20 09:38:23 -0700892 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 didSomething = true;
894 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700895 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800896 Slog.w(TAG, "Exception in new application when starting activity "
897 + hr.intent.getComponent().flattenToShortString(), e);
898 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700899 }
Craig Mautner20e72272013-04-01 13:45:53 -0700900 }
Craig Mautner20e72272013-04-01 13:45:53 -0700901 }
902 }
903 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700904 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700905 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700906 }
Craig Mautner20e72272013-04-01 13:45:53 -0700907 return didSomething;
908 }
909
910 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800911 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
912 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
914 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700915 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800916 continue;
917 }
918 final ActivityRecord resumedActivity = stack.mResumedActivity;
919 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700920 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800921 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800922 return false;
923 }
Craig Mautner20e72272013-04-01 13:45:53 -0700924 }
925 }
Wei Wang65c7a152016-06-02 18:51:22 -0700926 // Send launch end powerhint when idle
927 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700928 return true;
929 }
930
Craig Mautnerde4ef022013-04-07 19:01:33 -0700931 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800932 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
933 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
935 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700936 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800937 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700938 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 return false;
940 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700941 }
942 }
943 }
944 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700945 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
947 mLastFocusedStack + " to=" + mFocusedStack);
948 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700949 return true;
950 }
951
952 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800953 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
955 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
957 final ActivityStack stack = stacks.get(stackNdx);
958 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800959 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700960 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800961 return false;
962 }
963 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800964 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700965 }
966 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800967 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 }
969
Craig Mautner2acc3892013-09-23 10:28:14 -0700970 /**
971 * Pause all activities in either all of the stacks or just the back stacks.
972 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700973 * @param resuming The resuming activity.
974 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
975 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700976 * @return true if any activity was paused as a result of this call.
977 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700978 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700979 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
981 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
983 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700984 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700985 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700987 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
988 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800989 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700990 }
991 }
992 return someActivityPaused;
993 }
994
Craig Mautnerde4ef022013-04-07 19:01:33 -0700995 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700996 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800997 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
998 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1000 final ActivityStack stack = stacks.get(stackNdx);
1001 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001002 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001003 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001004 Slog.d(TAG_STATES,
1005 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001006 pausing = false;
1007 } else {
1008 return false;
1009 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001010 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 }
1012 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001013 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001014 }
1015
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001016 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001017 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001018 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001019 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1020 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1021 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1022 final ActivityStack stack = stacks.get(stackNdx);
1023 if (stack.mResumedActivity != null &&
1024 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001025 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001026 }
1027 }
1028 }
1029 }
1030
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001031 void cancelInitializingActivities() {
1032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1033 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1035 stacks.get(stackNdx).cancelInitializingActivities();
1036 }
1037 }
1038 }
1039
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001041 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001042 }
1043
1044 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1045 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001046 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001047 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001048 if (w.who == null) {
1049 changed = true;
1050 w.timeout = false;
1051 if (r != null) {
1052 w.who = new ComponentName(r.info.packageName, r.info.name);
1053 }
1054 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1055 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001056 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001057 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001058 if (changed) {
1059 mService.notifyAll();
1060 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001061 }
1062
Chong Zhang5022da32016-06-21 16:31:37 -07001063 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1064 boolean changed = false;
1065 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1066 WaitResult w = mWaitingActivityLaunched.remove(i);
1067 if (w.who == null) {
1068 changed = true;
1069 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1070 // the starting activity ends up moving another activity to front, and it should
1071 // wait for this new activity to become visible instead.
1072 // Do not modify other fields.
1073 w.result = START_TASK_TO_FRONT;
1074 }
1075 }
1076 if (changed) {
1077 mService.notifyAll();
1078 }
1079 }
1080
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001081 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1082 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001083 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001085 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001086 if (w.who == null) {
1087 changed = true;
1088 w.timeout = timeout;
1089 if (r != null) {
1090 w.who = new ComponentName(r.info.packageName, r.info.name);
1091 }
1092 w.thisTime = thisTime;
1093 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001094 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001096 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001097 if (changed) {
1098 mService.notifyAll();
1099 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001100 }
1101
Craig Mautner29219d92013-04-16 20:19:12 -07001102 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001103 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001104 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001105 if (r != null) {
1106 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001107 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001108
Andrii Kulian7318d632016-07-20 18:59:28 -07001109 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001110 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1111
1112 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1113 final int displayId = mTmpOrderedDisplayIds.get(i);
1114 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1115 if (stacks == null) {
1116 continue;
1117 }
1118 for (int j = stacks.size() - 1; j >= 0; --j) {
1119 final ActivityStack stack = stacks.get(j);
1120 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1121 r = stack.topRunningActivityLocked();
1122 if (r != null) {
1123 return r;
1124 }
Craig Mautner29219d92013-04-16 20:19:12 -07001125 }
1126 }
1127 }
1128 return null;
1129 }
1130
Dianne Hackborn09233282014-04-30 11:33:59 -07001131 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001132 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001133 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1134 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001135 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001136 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001137 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001138 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1139 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001140 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001141 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001142 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001143 }
1144 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001145
1146 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1147 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1148 while (maxNum > 0) {
1149 long mostRecentActiveTime = Long.MIN_VALUE;
1150 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001151 final int numTaskLists = runningTaskLists.size();
1152 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1153 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001154 if (!stackTaskList.isEmpty()) {
1155 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1156 if (lastActiveTime > mostRecentActiveTime) {
1157 mostRecentActiveTime = lastActiveTime;
1158 selectedStackList = stackTaskList;
1159 }
1160 }
1161 }
1162 if (selectedStackList != null) {
1163 list.add(selectedStackList.remove(0));
1164 --maxNum;
1165 } else {
1166 break;
1167 }
1168 }
Craig Mautner20e72272013-04-01 13:45:53 -07001169 }
1170
Todd Kennedy7440f172015-12-09 14:31:22 -08001171 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1172 ProfilerInfo profilerInfo) {
1173 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001174 if (aInfo != null) {
1175 // Store the found target back into the intent, because now that
1176 // we have it we never want to do this again. For example, if the
1177 // user navigates back to this point in the history, we should
1178 // always restart the exact same activity.
1179 intent.setComponent(new ComponentName(
1180 aInfo.applicationInfo.packageName, aInfo.name));
1181
1182 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001183 if (!aInfo.processName.equals("system")) {
1184 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001185 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001186 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001187
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001188 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1189 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1190 }
1191
Man Caocfa78b22015-06-11 20:14:34 -07001192 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1193 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1194 }
1195
1196 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001197 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001198 }
1199 }
1200 }
1201 return aInfo;
1202 }
1203
Todd Kennedy7440f172015-12-09 14:31:22 -08001204 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001205 return resolveIntent(intent, resolvedType, userId, 0);
1206 }
1207
1208 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001209 synchronized (mService) {
1210 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001211 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001212 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001213 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001214 }
1215
1216 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1217 ProfilerInfo profilerInfo, int userId) {
1218 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1219 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1220 }
1221
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001222 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1223 boolean andResume, boolean checkConfig) throws RemoteException {
1224
1225 if (!allPausedActivitiesComplete()) {
1226 // While there are activities pausing we skipping starting any new activities until
1227 // pauses are complete. NOTE: that we also do this for activities that are starting in
1228 // the paused state because they will first be resumed then paused on the client side.
1229 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1230 "realStartActivityLocked: Skipping start of r=" + r
1231 + " some activities pausing...");
1232 return false;
1233 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001234
Andrii Kulianada10292017-02-09 23:19:29 -08001235 r.startFreezingScreenLocked(app, 0);
1236 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001237
Andrii Kulianada10292017-02-09 23:19:29 -08001238 // schedule launch ticks to collect information about slow apps.
1239 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001240
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001241 // Have the window manager re-evaluate the orientation of the screen based on the new
1242 // activity order. Note that as a result of this, it can call back into the activity
1243 // manager with a new orientation. We don't care about that, because the activity is not
1244 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001245 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001246 final int displayId = r.getDisplayId();
1247 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1248 getDisplayOverrideConfiguration(displayId),
1249 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001250 // Deferring resume here because we're going to launch new activity shortly.
1251 // We don't want to perform a redundant launch of the same record while ensuring
1252 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001253 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1254 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 }
1256
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001257 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001258 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001259 }
1260
Craig Mautner2420ead2013-04-01 17:13:20 -07001261 r.app = app;
1262 app.waitingToKill = null;
1263 r.launchCount++;
1264 r.lastLaunchTime = SystemClock.uptimeMillis();
1265
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001266 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267
1268 int idx = app.activities.indexOf(r);
1269 if (idx < 0) {
1270 app.activities.add(r);
1271 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001272 mService.updateLruProcessLocked(app, true, null);
1273 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001274
Craig Mautner15df08a2015-04-01 12:17:18 -07001275 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001276 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1277 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001278 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001279 }
1280
Andrii Kulian02b7a832016-10-06 23:11:56 -07001281 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 try {
1283 if (app.thread == null) {
1284 throw new RemoteException();
1285 }
1286 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001287 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001289 // We don't need to deliver new intents and/or set results if activity is going
1290 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 results = r.results;
1292 newIntents = r.newIntents;
1293 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001294 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1295 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1296 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001297 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1298 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001299 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001300 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001301 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001303 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1304 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 r.sleeping = false;
1306 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001307 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 mService.showAskCompatModeDialogLocked(r);
1309 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001310 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1312 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1313 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001314 final String profileFile = mService.mProfileFile;
1315 if (profileFile != null) {
1316 ParcelFileDescriptor profileFd = mService.mProfileFd;
1317 if (profileFd != null) {
1318 try {
1319 profileFd = profileFd.dup();
1320 } catch (IOException e) {
1321 if (profileFd != null) {
1322 try {
1323 profileFd.close();
1324 } catch (IOException o) {
1325 }
1326 profileFd = null;
1327 }
1328 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001329 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001330
1331 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001332 mService.mSamplingInterval, mService.mAutoStopProfiler,
1333 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001334 }
1335 }
1336 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001337
Andrii Kulianada10292017-02-09 23:19:29 -08001338 app.hasShownUi = true;
1339 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001340 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001341 // Because we could be starting an Activity in the system process this may not go across
1342 // a Binder interface which would create a new Configuration. Consequently we have to
1343 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001344
1345 final Configuration globalConfiguration =
1346 new Configuration(mService.getGlobalConfiguration());
1347 r.setLastReportedGlobalConfiguration(globalConfiguration);
1348 final Configuration mergedOverrideConfiguration =
1349 new Configuration(task.getMergedOverrideConfiguration());
1350 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1351
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001353 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001354 globalConfiguration,
1355 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001356 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1357 r.persistentState, results, newIntents, !andResume,
1358 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001359
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001360 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001361 // This may be a heavy-weight process! Note that the package
1362 // manager will ensure that only activity can run in the main
1363 // process of the .apk, which is the only thing that will be
1364 // considered heavy-weight.
1365 if (app.processName.equals(app.info.packageName)) {
1366 if (mService.mHeavyWeightProcess != null
1367 && mService.mHeavyWeightProcess != app) {
1368 Slog.w(TAG, "Starting new heavy weight process " + app
1369 + " when already running "
1370 + mService.mHeavyWeightProcess);
1371 }
1372 mService.mHeavyWeightProcess = app;
1373 Message msg = mService.mHandler.obtainMessage(
1374 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1375 msg.obj = r;
1376 mService.mHandler.sendMessage(msg);
1377 }
1378 }
1379
1380 } catch (RemoteException e) {
1381 if (r.launchFailed) {
1382 // This is the second time we failed -- finish activity
1383 // and give up.
1384 Slog.e(TAG, "Second failure launching "
1385 + r.intent.getComponent().flattenToShortString()
1386 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001387 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001388 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1389 "2nd-crash", false);
1390 return false;
1391 }
1392
1393 // This is the first time we failed -- restart process and
1394 // retry.
1395 app.activities.remove(r);
1396 throw e;
1397 }
1398
1399 r.launchFailed = false;
1400 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001401 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001402 }
1403
1404 if (andResume) {
1405 // As part of the process of launching, ActivityThread also performs
1406 // a resume.
1407 stack.minimalResumeActivityLocked(r);
1408 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001409 // This activity is not starting in the resumed state... which should look like we asked
1410 // it to pause+stop (but remain visible), and it has done so and reported back the
1411 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001412 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001413 "Moving to PAUSED: " + r + " (starting in paused state)");
1414 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001415 }
1416
1417 // Launch the new version setup screen if needed. We do this -after-
1418 // launching the initial activity (that is, home), so that it can have
1419 // a chance to initialize itself while in the background, making the
1420 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001421 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001422 mService.startSetupActivityLocked();
1423 }
1424
Dianne Hackborn465fa392014-09-14 14:21:18 -07001425 // Update any services we are bound to that might care about whether
1426 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001427 if (r.app != null) {
1428 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1429 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001430
Craig Mautner2420ead2013-04-01 17:13:20 -07001431 return true;
1432 }
1433
Craig Mautnere79d42682013-04-01 19:01:53 -07001434 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001435 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001436 // Is this activity's application already running?
1437 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001438 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001439
Andrii Kulian02b7a832016-10-06 23:11:56 -07001440 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001441
1442 if (app != null && app.thread != null) {
1443 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001444 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1445 || !"android".equals(r.info.packageName)) {
1446 // Don't add this if it is a platform component that is marked
1447 // to run in multiple processes, because this is actually
1448 // part of the framework so doesn't make sense to track as a
1449 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001450 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1451 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001452 }
George Mount2c92c972014-03-20 09:38:23 -07001453 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001454 return;
1455 } catch (RemoteException e) {
1456 Slog.w(TAG, "Exception when starting activity "
1457 + r.intent.getComponent().flattenToShortString(), e);
1458 }
1459
1460 // If a dead object exception was thrown -- fall through to
1461 // restart the application.
1462 }
1463
1464 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001465 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001466 }
1467
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001468 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001469 String resultWho, int requestCode, int callingPid, int callingUid,
1470 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001471 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001472 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1473 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001474 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001475 return true;
1476 }
1477 final int componentRestriction = getComponentRestrictionForCallingPackage(
1478 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1479 final int actionRestriction = getActionRestrictionForCallingPackage(
1480 intent.getAction(), callingPackage, callingPid, callingUid);
1481 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1482 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1483 if (resultRecord != null) {
1484 resultStack.sendActivityResultLocked(-1,
1485 resultRecord, resultWho, requestCode,
1486 Activity.RESULT_CANCELED, null);
1487 }
1488 final String msg;
1489 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1490 msg = "Permission Denial: starting " + intent.toString()
1491 + " from " + callerApp + " (pid=" + callingPid
1492 + ", uid=" + callingUid + ")" + " with revoked permission "
1493 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1494 } else if (!aInfo.exported) {
1495 msg = "Permission Denial: starting " + intent.toString()
1496 + " from " + callerApp + " (pid=" + callingPid
1497 + ", uid=" + callingUid + ")"
1498 + " not exported from uid " + aInfo.applicationInfo.uid;
1499 } else {
1500 msg = "Permission Denial: starting " + intent.toString()
1501 + " from " + callerApp + " (pid=" + callingPid
1502 + ", uid=" + callingUid + ")"
1503 + " requires " + aInfo.permission;
1504 }
1505 Slog.w(TAG, msg);
1506 throw new SecurityException(msg);
1507 }
1508
1509 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1510 final String message = "Appop Denial: starting " + intent.toString()
1511 + " from " + callerApp + " (pid=" + callingPid
1512 + ", uid=" + callingUid + ")"
1513 + " requires " + AppOpsManager.permissionToOp(
1514 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1515 Slog.w(TAG, message);
1516 return false;
1517 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1518 final String message = "Appop Denial: starting " + intent.toString()
1519 + " from " + callerApp + " (pid=" + callingPid
1520 + ", uid=" + callingUid + ")"
1521 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1522 Slog.w(TAG, message);
1523 return false;
1524 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001525 if (options != null) {
1526 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1527 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1528 callingPid, callingUid);
1529 if (startInTaskPerm != PERMISSION_GRANTED) {
1530 final String msg = "Permission Denial: starting " + intent.toString()
1531 + " from " + callerApp + " (pid=" + callingPid
1532 + ", uid=" + callingUid + ") with launchTaskId="
1533 + options.getLaunchTaskId();
1534 Slog.w(TAG, msg);
1535 throw new SecurityException(msg);
1536 }
1537 }
1538 // Check if someone tries to launch an activity on a private display with a different
1539 // owner.
1540 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001541 if (launchDisplayId != INVALID_DISPLAY
1542 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1543 final String msg = "Permission Denial: starting " + intent.toString()
1544 + " from " + callerApp + " (pid=" + callingPid
1545 + ", uid=" + callingUid + ") with launchDisplayId="
1546 + launchDisplayId;
1547 Slog.w(TAG, msg);
1548 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001549 }
1550 }
1551
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001552 return true;
1553 }
1554
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001555 /** Check if caller is allowed to launch activities on specified display. */
1556 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1557 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1558 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1559
1560 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1561 if (activityDisplay == null) {
1562 Slog.w(TAG, "Launch on display check: display not found");
1563 return false;
1564 }
1565
1566 if (!activityDisplay.isPrivate()) {
1567 // Anyone can launch on a public display.
1568 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1569 + " allow launch on public display");
1570 return true;
1571 }
1572
1573 // Check if the caller is the owner of the display.
1574 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1575 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1576 + " allow launch for owner of the display");
1577 return true;
1578 }
1579
1580 // Check if caller is present on display
1581 if (activityDisplay.isUidPresent(callingUid)) {
1582 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1583 + " allow launch for caller present on the display");
1584 return true;
1585 }
1586
1587 // Check if the caller can launch anything.
1588 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1589 callingUid);
1590 if (startAnyPerm == PERMISSION_GRANTED) {
1591 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1592 + " allow launch any on display");
1593 return true;
1594 }
1595
1596 Slog.w(TAG, "Launch on display check: denied");
1597 return false;
1598 }
1599
1600 /** Update lists of UIDs that are present on displays and have access to them. */
1601 void updateUIDsPresentOnDisplay() {
1602 mDisplayAccessUIDs.clear();
1603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1604 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
1605 mDisplayAccessUIDs.append(activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1606 }
1607 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1608 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1609 }
1610
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001611 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001612 final long identity = Binder.clearCallingIdentity();
1613 try {
1614 return UserManager.get(mService.mContext).getUserInfo(userId);
1615 } finally {
1616 Binder.restoreCallingIdentity(identity);
1617 }
1618 }
1619
Svet Ganov99b60432015-06-27 13:15:22 -07001620 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001621 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001622 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1623 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001624 == PackageManager.PERMISSION_DENIED) {
1625 return ACTIVITY_RESTRICTION_PERMISSION;
1626 }
1627
Christopher Tateff7add02015-08-17 10:23:22 -07001628 if (activityInfo.permission == null) {
1629 return ACTIVITY_RESTRICTION_NONE;
1630 }
1631
Svet Ganov99b60432015-06-27 13:15:22 -07001632 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1633 if (opCode == AppOpsManager.OP_NONE) {
1634 return ACTIVITY_RESTRICTION_NONE;
1635 }
1636
1637 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1638 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001639 if (!ignoreTargetSecurity) {
1640 return ACTIVITY_RESTRICTION_APPOP;
1641 }
Svet Ganov99b60432015-06-27 13:15:22 -07001642 }
1643
1644 return ACTIVITY_RESTRICTION_NONE;
1645 }
1646
Svetoslav7008b512015-06-24 18:47:07 -07001647 private int getActionRestrictionForCallingPackage(String action,
1648 String callingPackage, int callingPid, int callingUid) {
1649 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001650 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001651 }
1652
1653 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1654 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001655 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001656 }
1657
1658 final PackageInfo packageInfo;
1659 try {
1660 packageInfo = mService.mContext.getPackageManager()
1661 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1662 } catch (PackageManager.NameNotFoundException e) {
1663 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001664 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001665 }
1666
1667 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001668 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001669 }
1670
1671 if (mService.checkPermission(permission, callingPid, callingUid) ==
1672 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001673 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001674 }
1675
1676 final int opCode = AppOpsManager.permissionToOpCode(permission);
1677 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001678 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001679 }
1680
1681 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1682 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001683 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001684 }
1685
Svet Ganov99b60432015-06-27 13:15:22 -07001686 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001687 }
1688
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001689 void setLaunchSource(int uid) {
1690 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1691 }
1692
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001693 void acquireLaunchWakelock() {
1694 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1695 throw new IllegalStateException("Calling must be system uid");
1696 }
1697 mLaunchingActivity.acquire();
1698 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1699 // To be safe, don't allow the wake lock to be held for too long.
1700 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1701 }
1702 }
1703
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001704 /**
1705 * Called when the frontmost task is idle.
1706 * @return the state of mService.mBooting before this was called.
1707 */
1708 private boolean checkFinishBootingLocked() {
1709 final boolean booting = mService.mBooting;
1710 boolean enableScreen = false;
1711 mService.mBooting = false;
1712 if (!mService.mBooted) {
1713 mService.mBooted = true;
1714 enableScreen = true;
1715 }
1716 if (booting || enableScreen) {
1717 mService.postFinishBooting(booting, enableScreen);
1718 }
1719 return booting;
1720 }
1721
Craig Mautnerf3333272013-04-22 10:55:53 -07001722 // Checked.
1723 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001724 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001725 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001726
Craig Mautnerf3333272013-04-22 10:55:53 -07001727 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001728 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001729 int NS = 0;
1730 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001731 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001732 boolean activityRemoved = false;
1733
Wale Ogunwale7d701172015-03-11 15:36:30 -07001734 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001735 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001736 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1737 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001738 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1739 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001740 if (fromTimeout) {
1741 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001742 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001743
1744 // This is a hack to semi-deal with a race condition
1745 // in the client where it can be constructed with a
1746 // newer configuration from when we asked it to launch.
1747 // We'll update with whatever configuration it now says
1748 // it used to launch.
1749 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001750 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001751 }
1752
1753 // We are now idle. If someone is waiting for a thumbnail from
1754 // us, we can now deliver.
1755 r.idle = true;
1756
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001757 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001758 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001759 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001760 }
1761 }
1762
1763 if (allResumedActivitiesIdle()) {
1764 if (r != null) {
1765 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001766 }
1767
1768 if (mLaunchingActivity.isHeld()) {
1769 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1770 if (VALIDATE_WAKE_LOCK_CALLER &&
1771 Binder.getCallingUid() != Process.myUid()) {
1772 throw new IllegalStateException("Calling must be system uid");
1773 }
1774 mLaunchingActivity.release();
1775 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001776 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001777 }
1778
1779 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001780 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1781 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001782 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001783 if ((NF = mFinishingActivities.size()) > 0) {
1784 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001785 mFinishingActivities.clear();
1786 }
1787
Craig Mautnerf3333272013-04-22 10:55:53 -07001788 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001789 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001790 mStartingUsers.clear();
1791 }
1792
Craig Mautnerf3333272013-04-22 10:55:53 -07001793 // Stop any activities that are scheduled to do so but have been
1794 // waiting for the next one to start.
1795 for (int i = 0; i < NS; i++) {
1796 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001797 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001798 if (stack != null) {
1799 if (r.finishing) {
1800 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1801 } else {
1802 stack.stopActivityLocked(r);
1803 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001804 }
1805 }
1806
1807 // Finish any activities that are scheduled to do so but have been
1808 // waiting for the next one to start.
1809 for (int i = 0; i < NF; i++) {
1810 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001811 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001812 if (stack != null) {
1813 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1814 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001815 }
1816
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001817 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001818 // Complete user switch
1819 if (startingUsers != null) {
1820 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001821 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001822 }
1823 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 }
1825
1826 mService.trimApplications();
1827 //dump();
1828 //mWindowManager.dump();
1829
Craig Mautnerf3333272013-04-22 10:55:53 -07001830 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001831 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 }
1833
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001835 }
1836
Craig Mautner8e569572013-10-11 17:36:59 -07001837 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001838 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001839 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1840 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001841 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1842 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1843 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001844 }
Craig Mautner19091252013-10-05 00:03:53 -07001845 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001846 }
1847
1848 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001849 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1850 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001851 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1852 stacks.get(stackNdx).closeSystemDialogsLocked();
1853 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001854 }
1855 }
1856
Craig Mautner93529a42013-10-04 15:03:13 -07001857 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001858 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001859 }
1860
Craig Mautner8d341ef2013-03-26 09:03:27 -07001861 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001862 * Update the last used stack id for non-current user (current user's last
1863 * used stack is the focused stack)
1864 */
1865 void updateUserStackLocked(int userId, ActivityStack stack) {
1866 if (userId != mCurrentUser) {
1867 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1868 }
1869 }
1870
1871 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001872 * @return true if some activity was finished (or would have finished if doit were true).
1873 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001874 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1875 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001876 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001877 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1878 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001879 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001880 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001881 if (stack.finishDisabledPackageActivitiesLocked(
1882 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001883 didSomething = true;
1884 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001885 }
1886 }
1887 return didSomething;
1888 }
1889
Dianne Hackborna413dc02013-07-12 12:02:55 -07001890 void updatePreviousProcessLocked(ActivityRecord r) {
1891 // Now that this process has stopped, we may want to consider
1892 // it to be the previous app to try to keep around in case
1893 // the user wants to return to it.
1894
1895 // First, found out what is currently the foreground app, so that
1896 // we don't blow away the previous app if this activity is being
1897 // hosted by the process that is actually still the foreground.
1898 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001899 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1900 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001901 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1902 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001903 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001904 if (stack.mResumedActivity != null) {
1905 fgApp = stack.mResumedActivity.app;
1906 } else if (stack.mPausingActivity != null) {
1907 fgApp = stack.mPausingActivity.app;
1908 }
1909 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001910 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001911 }
1912 }
1913
1914 // Now set this one as the previous process, only if that really
1915 // makes sense to.
1916 if (r.app != null && fgApp != null && r.app != fgApp
1917 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001918 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001919 mService.mPreviousProcess = r.app;
1920 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1921 }
1922 }
1923
Wale Ogunwaled046a012015-12-24 13:05:59 -08001924 boolean resumeFocusedStackTopActivityLocked() {
1925 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001926 }
1927
Wale Ogunwaled046a012015-12-24 13:05:59 -08001928 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001929 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001930 if (targetStack != null && isFocusedStack(targetStack)) {
1931 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001932 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001933 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1934 if (r == null || r.state != RESUMED) {
1935 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001936 } else if (r.state == RESUMED) {
1937 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1938 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001939 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001940 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001941 }
1942
Todd Kennedy39bfee52016-02-24 10:28:21 -08001943 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1947 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1948 }
1949 }
1950 }
1951
Adrian Roos20d7df32016-01-12 18:59:43 +01001952 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1953 TaskRecord finishedTask = null;
1954 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001955 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1956 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001957 final int numStacks = stacks.size();
1958 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1959 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001960 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1961 if (stack == focusedStack || finishedTask == null) {
1962 finishedTask = t;
1963 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001964 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001965 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001966 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001967 }
1968
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001969 void finishVoiceTask(IVoiceInteractionSession session) {
1970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1971 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1972 final int numStacks = stacks.size();
1973 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1974 final ActivityStack stack = stacks.get(stackNdx);
1975 stack.finishVoiceTask(session);
1976 }
1977 }
1978 }
1979
Andrii Kulianc27916642016-04-12 17:59:27 -07001980 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1981 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001982 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1983 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001984 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001985 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1986 // Caller wants the home activity moved with it. To accomplish this,
1987 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001988 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001989 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001990 ActivityStack currentStack = task.getStack();
1991 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001992 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1993 + task + " to front. Stack is null");
1994 return;
1995 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001996
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001997 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001998 int stackId = options.getLaunchStackId();
1999 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002000 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002001 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002002 if (stackId == INVALID_STACK_ID) {
2003 stackId = task.getLaunchStackId();
2004 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002005 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002006 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2007 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002008 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002009 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2010 // still need moveTaskToFrontLocked() below for any transition settings.
2011 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002012 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2013 resizeStackLocked(stackId, bounds,
2014 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002015 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002016 } else {
2017 // WM resizeTask must be done after the task is moved to the correct stack,
2018 // because Task's setBounds() also updates dim layer's bounds, but that has
2019 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002020 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002021 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002022 }
2023 }
2024
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002025 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002026 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002027 r == null ? null : r.appTimeTracker, reason);
2028
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002029 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002030 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002031
Andrii Kulian02b7a832016-10-06 23:11:56 -07002032 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002033 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002034 }
2035
Wale Ogunwale854809c2015-12-27 16:18:19 -08002036 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002037 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002038 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002039 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002040 return false;
2041 }
2042 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2043 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002044 }
2045
Winson Chung55893332017-02-17 17:13:10 -08002046 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002047 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002048 }
2049
Winson Chung55893332017-02-17 17:13:10 -08002050 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2051 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002052 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002053 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002054 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002055 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002056 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002057 return null;
2058 }
Matthew Ng330757d2017-02-28 14:19:17 -08002059 if (stackId == DOCKED_STACK_ID) {
2060 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2061 // the other side of the docked stack and we make visibility decisions based on that.
2062 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2063 }
Winson Chung55893332017-02-17 17:13:10 -08002064 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002065 }
2066
Andrii Kulian16802aa2016-11-02 12:21:33 -07002067 /**
2068 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2069 * If there is no such stack, new dynamic stack can be created.
2070 * @param displayId Target display.
2071 * @param r Activity that should be launched there.
2072 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2073 */
2074 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2075 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2076 if (activityDisplay == null) {
2077 throw new IllegalArgumentException(
2078 "Display with displayId=" + displayId + " not found.");
2079 }
2080
2081 // Return the topmost valid stack on the display.
2082 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2083 final ActivityStack stack = activityDisplay.mStacks.get(i);
2084 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2085 return stack;
2086 }
2087 }
2088
2089 // If there is no valid stack on the external display - check if new dynamic stack will do.
2090 if (displayId != Display.DEFAULT_DISPLAY) {
2091 final int newDynamicStackId = getNextStackId();
2092 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2093 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2094 }
2095 }
2096
2097 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2098 return null;
2099 }
2100
Craig Mautner967212c2013-04-13 21:10:58 -07002101 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002102 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002103 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2104 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002105 }
2106 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002107 }
2108
Jorim Jaggife762342016-10-13 14:33:27 +02002109 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2110 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2111 }
2112
Andrii Kulian7fc22812016-12-28 13:04:11 -08002113 /**
2114 * Get next focusable stack in the system. This will search across displays and stacks
2115 * in last-focused order for a focusable and visible stack, different from the target stack.
2116 *
2117 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2118 * searching for next candidate.
2119 * @return Next focusable {@link ActivityStack}, null if not found.
2120 */
2121 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2122 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2123
2124 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2125 final int displayId = mTmpOrderedDisplayIds.get(i);
2126 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2127 if (stacks == null) {
2128 continue;
2129 }
2130 for (int j = stacks.size() - 1; j >= 0; --j) {
2131 final ActivityStack stack = stacks.get(j);
2132 if (stack != currentFocus && stack.isFocusable()
2133 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2134 return stack;
2135 }
2136 }
2137 }
2138
2139 return null;
2140 }
2141
Craig Mautneree2e45a2014-06-27 12:10:03 -07002142 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002143 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002144 }
2145
2146 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002147 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2148 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2149 final TaskRecord task = tasks.get(taskNdx);
2150 if (task.isHomeTask()) {
2151 final ArrayList<ActivityRecord> activities = task.mActivities;
2152 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2153 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002154 if (r.isHomeActivity()
2155 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002156 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002157 }
2158 }
2159 }
2160 }
2161 return null;
2162 }
2163
Chong Zhangb15758a2015-11-17 12:12:03 -08002164 /**
2165 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2166 * the docked stack itself, or if it's side-by-side to the docked stack.
2167 */
2168 boolean isStackDockedInEffect(int stackId) {
2169 return stackId == DOCKED_STACK_ID ||
2170 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2171 }
2172
Todd Kennedyca4d8422015-01-15 15:19:22 -08002173 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002174 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002175 ActivityContainer activityContainer =
2176 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002177 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002178 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2179 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002180 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002181 return activityContainer;
2182 }
2183
Craig Mautner34b73df2014-01-12 21:11:08 -08002184 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002185 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2186 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2187 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002188 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2189 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002190 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002191 }
2192 }
2193
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002194 void deleteActivityContainerRecord(int stackId) {
2195 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2196 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2197 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002198 }
2199
Jorim Jaggidc249c42015-12-15 14:57:31 -08002200 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002201 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002202 if (stackId == DOCKED_STACK_ID) {
2203 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002204 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002205 return;
2206 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002207 final ActivityStack stack = getStack(stackId);
2208 if (stack == null) {
2209 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2210 return;
2211 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002212
Jorim Jaggidc249c42015-12-15 14:57:31 -08002213 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002214 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2215 // stack size changing so things don't get out of sync.
2216 return;
2217 }
2218
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002219 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002220 mWindowManager.deferSurfaceLayout();
2221 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002222 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002223 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002224 stack.ensureVisibleActivitiesConfigurationLocked(
2225 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002226 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002227 } finally {
2228 mWindowManager.continueSurfaceLayout();
2229 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002230 }
2231 }
2232
Jorim Jaggi192086e2016-03-11 17:17:03 +01002233 void deferUpdateBounds(int stackId) {
2234 final ActivityStack stack = getStack(stackId);
2235 if (stack != null) {
2236 stack.deferUpdateBounds();
2237 }
2238 }
2239
2240 void continueUpdateBounds(int stackId) {
2241 final ActivityStack stack = getStack(stackId);
2242 if (stack != null) {
2243 stack.continueUpdateBounds();
2244 }
2245 }
2246
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002247 void notifyAppTransitionDone() {
2248 continueUpdateBounds(HOME_STACK_ID);
2249 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2250 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002251 final TaskRecord task =
2252 anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2253 if (task != null) {
2254 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002255 }
2256 }
2257 mResizingTasksDuringAnimation.clear();
2258 }
2259
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002260 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2261 final ActivityStack stack = getStack(fromStackId);
2262 if (stack == null) {
2263 return;
2264 }
2265
2266 mWindowManager.deferSurfaceLayout();
2267 try {
2268 if (fromStackId == DOCKED_STACK_ID) {
2269
2270 // We are moving all tasks from the docked stack to the fullscreen stack,
2271 // which is dismissing the docked stack, so resize all other stacks to
2272 // fullscreen here already so we don't end up with resize trashing.
2273 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2274 if (StackId.isResizeableByDockedStack(i)) {
2275 ActivityStack otherStack = getStack(i);
2276 if (otherStack != null) {
2277 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2278 true /* allowResizeInDockedMode */, DEFER_RESUME);
2279 }
2280 }
2281 }
2282
2283 // Also disable docked stack resizing since we have manually adjusted the
2284 // size of other stacks above and we don't want to trigger a docked stack
2285 // resize when we remove task from it below and it is detached from the
2286 // display because it no longer contains any tasks.
2287 mAllowDockedStackResize = false;
2288 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002289 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002290 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2291 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002292 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2293 final int size = tasks.size();
2294 if (onTop) {
2295 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002296 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002297 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002298 if (fromStackId == PINNED_STACK_ID) {
2299 // Update the return-to to reflect where the pinned stack task was moved
2300 // from so that we retain the stack that was previously visible if the
2301 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002302 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2303 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002304 MetricsLogger.action(mService.mContext,
2305 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002306 }
Winson Chung74666102017-02-22 17:49:24 -08002307 // Defer resume until all the tasks have been moved to the fullscreen stack
2308 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2309 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
2310 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002311 }
2312 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002313 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002314 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002315 final int position = fullscreenStack != null
2316 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2317 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002318 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung74666102017-02-22 17:49:24 -08002319 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
2320 DEFER_RESUME, "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002321 }
2322 }
Winson Chung74666102017-02-22 17:49:24 -08002323
2324 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2325 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002326 } finally {
2327 mAllowDockedStackResize = true;
2328 mWindowManager.continueSurfaceLayout();
2329 }
2330 }
2331
Jorim Jaggidc249c42015-12-15 14:57:31 -08002332 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002333 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2334 boolean preserveWindows) {
2335 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2336 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2337 false /* deferResume */);
2338 }
2339
2340 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2341 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2342 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002343
2344 if (!mAllowDockedStackResize) {
2345 // Docked stack resize currently disabled.
2346 return;
2347 }
2348
Jorim Jaggidc249c42015-12-15 14:57:31 -08002349 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2350 if (stack == null) {
2351 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2352 return;
2353 }
2354
2355 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2356 mWindowManager.deferSurfaceLayout();
2357 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002358 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2359 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002360 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002361 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002362
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002363 // TODO: Checking for isAttached might not be needed as if the user passes in null
2364 // dockedBounds then they want the docked stack to be dismissed.
2365 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2366 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002367 // In this case we make all other static stacks fullscreen and move all
2368 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002369 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002370
2371 // stack shouldn't contain anymore activities, so nothing to resume.
2372 r = null;
2373 } else {
2374 // Docked stacks occupy a dedicated region on screen so the size of all other
2375 // static stacks need to be adjusted so they don't overlap with the docked stack.
2376 // We get the bounds to use from window manager which has been adjusted for any
2377 // screen controls and is also the same for all stacks.
Matthew Nge15352e2016-12-20 15:36:29 -08002378 final Rect tempOtherTaskRect = new Rect();
2379 final Rect tempOtherTaskInsetRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002380 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002381 final ActivityStack current = getStack(i);
2382 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Nge15352e2016-12-20 15:36:29 -08002383 current.getStackDockedModeBounds(tempRect, tempOtherTaskRect,
2384 tempOtherTaskInsetRect, true /* ignoreVisibility */);
2385 resizeStackLocked(i, tempRect,
2386 !tempOtherTaskRect.isEmpty() ? tempOtherTaskRect :
2387 tempOtherTaskBounds,
2388 !tempOtherTaskInsetRect.isEmpty() ? tempOtherTaskInsetRect :
2389 tempOtherTaskInsetBounds,
2390 preserveWindows, true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002391 }
2392 }
2393 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002394 if (!deferResume) {
2395 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2396 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002397 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002398 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002399 mWindowManager.continueSurfaceLayout();
2400 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2401 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002402 }
2403
Robert Carr0d00c2e2016-02-29 17:45:02 -08002404 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2405 final ActivityStack stack = getStack(PINNED_STACK_ID);
2406 if (stack == null) {
2407 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2408 return;
2409 }
2410 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2411 mWindowManager.deferSurfaceLayout();
2412 try {
2413 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002414 Rect insetBounds = null;
2415 if (tempPinnedTaskBounds != null) {
2416 // We always use 0,0 as the position for the inset rect because
2417 // if we are getting insets at all in the pinned stack it must mean
2418 // we are headed for fullscreen.
2419 insetBounds = tempRect;
2420 insetBounds.top = 0;
2421 insetBounds.left = 0;
2422 insetBounds.right = tempPinnedTaskBounds.width();
2423 insetBounds.bottom = tempPinnedTaskBounds.height();
2424 }
2425 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002426 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002427 } finally {
2428 mWindowManager.continueSurfaceLayout();
2429 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2430 }
2431 }
2432
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002433 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002434 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002435 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002436 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002437 }
2438
Wale Ogunwale1666e312016-12-16 11:27:18 -08002439 final ActivityContainer activityContainer =
2440 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002441 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002442 }
2443
Winson Chung010927a2016-12-15 16:12:35 -08002444 /**
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002445 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
Winson Chung010927a2016-12-15 16:12:35 -08002446 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2447 * instead moved back onto the fullscreen stack.
2448 */
2449 void removeStackLocked(int stackId) {
2450 final ActivityStack stack = getStack(stackId);
2451 if (stack == null) {
2452 return;
2453 }
2454
2455 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2456 if (stack.getStackId() == PINNED_STACK_ID) {
2457 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2458 if (fullscreenStack != null) {
2459 final boolean isFullscreenStackVisible =
2460 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2461 for (int i = 0; i < tasks.size(); i++) {
2462 // Insert the task either at the top of the fullscreen stack if it is hidden,
2463 // or just under the top task if it is currently visible
2464 final int insertPosition = isFullscreenStackVisible
2465 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2466 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002467 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002468 // Defer resume until we remove all the tasks
2469 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2470 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002471 }
2472 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2473 resumeFocusedStackTopActivityLocked();
2474 } else {
2475 // If there is no fullscreen stack, then create the stack and move all the tasks
2476 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002477 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002478 }
2479 } else {
2480 for (int i = tasks.size() - 1; i >= 0; i--) {
2481 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2482 REMOVE_FROM_RECENTS);
2483 }
2484 }
2485 }
2486
2487 /**
2488 * Removes the task with the specified task id.
2489 *
2490 * @param taskId Identifier of the task to be removed.
2491 * @param killProcess Kill any process associated with the task if possible.
2492 * @param removeFromRecents Whether to also remove the task from recents.
2493 * @return Returns true if the given task was found and removed.
2494 */
2495 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2496 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2497 if (tr != null) {
2498 tr.removeTaskActivitiesLocked();
2499 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2500 if (tr.isPersistable) {
2501 mService.notifyTaskPersisterLocked(null, true);
2502 }
2503 return true;
2504 }
2505 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2506 return false;
2507 }
2508
2509 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2510 if (removeFromRecents) {
2511 mRecentTasks.remove(tr);
2512 tr.removedFromRecents();
2513 }
2514 ComponentName component = tr.getBaseIntent().getComponent();
2515 if (component == null) {
2516 Slog.w(TAG, "No component for base intent of task: " + tr);
2517 return;
2518 }
2519
2520 // Find any running services associated with this app and stop if needed.
2521 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2522
2523 if (!killProcess) {
2524 return;
2525 }
2526
2527 // Determine if the process(es) for this task should be killed.
2528 final String pkg = component.getPackageName();
2529 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2530 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2531 for (int i = 0; i < pmap.size(); i++) {
2532
2533 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2534 for (int j = 0; j < uids.size(); j++) {
2535 ProcessRecord proc = uids.valueAt(j);
2536 if (proc.userId != tr.userId) {
2537 // Don't kill process for a different user.
2538 continue;
2539 }
2540 if (proc == mService.mHomeProcess) {
2541 // Don't kill the home process along with tasks from the same package.
2542 continue;
2543 }
2544 if (!proc.pkgList.containsKey(pkg)) {
2545 // Don't kill process that is not associated with this task.
2546 continue;
2547 }
2548
2549 for (int k = 0; k < proc.activities.size(); k++) {
2550 TaskRecord otherTask = proc.activities.get(k).task;
2551 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2552 // Don't kill process(es) that has an activity in a different task that is
2553 // also in recents.
2554 return;
2555 }
2556 }
2557
2558 if (proc.foregroundServices) {
2559 // Don't kill process(es) with foreground service.
2560 return;
2561 }
2562
2563 // Add process to kill list.
2564 procsToKill.add(proc);
2565 }
2566 }
2567
2568 // Kill the running processes.
2569 for (int i = 0; i < procsToKill.size(); i++) {
2570 ProcessRecord pr = procsToKill.get(i);
2571 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2572 && pr.curReceivers.isEmpty()) {
2573 pr.kill("remove task", true);
2574 } else {
2575 // We delay killing processes that are not in the background or running a receiver.
2576 pr.waitingToKill = "remove task";
2577 }
2578 }
2579 }
2580
Craig Mautner4a1cb222013-12-04 16:14:06 -08002581 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002582 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002583 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2584 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002585 break;
2586 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002587 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002588 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002589 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002590 }
2591
Chong Zhang5dcb2752015-08-18 13:50:26 -07002592 /**
2593 * Restores a recent task to a stack
2594 * @param task The recent task to be restored.
2595 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002596 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2597 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002598 * @return true if the task has been restored successfully.
2599 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002600 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002601 if (!StackId.isStaticStack(stackId)) {
2602 // If stack is not static (or stack id is invalid) - use the default one.
2603 // This means that tasks that were on external displays will be restored on the
2604 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002605 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002606 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002607 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2608 // Put it in the fullscreen stack.
2609 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002610 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002611
Andrii Kulian02b7a832016-10-06 23:11:56 -07002612 final ActivityStack currentStack = task.getStack();
2613 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002614 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002615 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002616 // Nothing else to do since it is already restored in the right stack.
2617 return true;
2618 }
2619 // Remove current stack association, so we can re-associate the task with the
2620 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002621 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002622 }
2623
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002624 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002625 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002626
2627 if (stack == null) {
2628 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002629 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2630 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002631 return false;
2632 }
2633
Wale Ogunwale72919d22016-12-08 18:58:50 -08002634 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002635 // TODO: move call for creation here and other place into Stack.addTask()
2636 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002637 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2638 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002639 final ArrayList<ActivityRecord> activities = task.mActivities;
2640 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002641 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002642 }
2643 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002644 }
2645
Wale Ogunwale040b4702015-08-06 18:10:50 -07002646 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002647 * Move stack with all its existing content to specified display.
2648 * @param stackId Id of stack to move.
2649 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002650 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002651 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002652 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002653 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2654 if (activityDisplay == null) {
2655 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2656 + displayId);
2657 }
2658 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2659 if (activityContainer != null) {
2660 if (activityContainer.isAttachedLocked()) {
2661 if (activityContainer.getDisplayId() == displayId) {
2662 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2663 + " to its current displayId=" + displayId);
2664 }
2665
Andrii Kulian250d6532017-02-08 23:30:45 -08002666 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002667 } else {
2668 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2669 + stackId + " is not attached to any display.");
2670 }
2671 } else {
2672 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2673 + stackId);
2674 }
2675
2676 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2677 !PRESERVE_WINDOWS);
2678 // TODO(multi-display): resize stacks properly if moved from split-screen.
2679 }
2680
2681 /**
Winson Chung74666102017-02-22 17:49:24 -08002682 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2683 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002684 */
Winson Chung74666102017-02-22 17:49:24 -08002685 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002686 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002687
Winson Chung74666102017-02-22 17:49:24 -08002688 // Check that we aren't reparenting to the same stack that the task is already in
2689 if (prevStack != null && prevStack.mStackId == stackId) {
2690 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2691 + " already in stackId=" + stackId);
2692 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002693 }
2694
Winson Chung74666102017-02-22 17:49:24 -08002695 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2696 // support
2697 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2698 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2699 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002700 }
2701
Winson Chung74666102017-02-22 17:49:24 -08002702 // Ensure that we aren't trying to move into a freeform stack without freeform
2703 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002704 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002705 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2706 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002707 }
2708
Winson Chung74666102017-02-22 17:49:24 -08002709 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2710 // used for split-screen mode and will cause things like the docked divider to show up. We
2711 // instead leave the task in its current stack or move it to the fullscreen stack if it
2712 // isn't currently in a stack.
2713 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2714 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2715 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2716 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002717 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002718
Winson Chung74666102017-02-22 17:49:24 -08002719 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2720 // example, a docked stack is created which will lead to the stack we are moving from being
2721 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002722 try {
Winson Chung74666102017-02-22 17:49:24 -08002723 task.mTemporarilyUnresizable = true;
2724 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002725 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002726 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002727 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002728 }
2729
Wale Ogunwale079a0042015-10-24 11:44:07 -07002730 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2731 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2732 if (stack == null) {
2733 throw new IllegalArgumentException(
2734 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2735 }
2736
2737 final ActivityRecord r = stack.topRunningActivityLocked();
2738 if (r == null) {
2739 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2740 + " in stack=" + stack);
2741 return false;
2742 }
2743
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002744 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002745 Slog.w(TAG,
2746 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002747 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002748 return false;
2749 }
2750
Winson Chungb5c41b72016-12-07 15:00:47 -08002751 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2752 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002753 return true;
2754 }
2755
Winson Chungb5c41b72016-12-07 15:00:47 -08002756 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2757 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002758 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002759
Wale Ogunwale1666e312016-12-16 11:27:18 -08002760 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002761 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002762
Wale Ogunwale480dca02016-02-06 13:58:29 -08002763 try {
2764 final TaskRecord task = r.task;
2765
Andrii Kulian02b7a832016-10-06 23:11:56 -07002766 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002767 // An activity can't be pinned and visible behind at the same time. Go ahead and
2768 // release it from been visible behind before pinning.
2769 requestVisibleBehindLocked(r, false);
2770 }
2771
Wale Ogunwale480dca02016-02-06 13:58:29 -08002772 // Resize the pinned stack to match the current size of the task the activity we are
2773 // going to be moving is currently contained in. We do this to have the right starting
2774 // animation bounds for the pinned stack to the desired bounds the caller wants.
2775 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2776 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002777 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002778
2779 if (task.mActivities.size() == 1) {
2780 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002781 // the stack without re-parenting the activity in a different task. We don't
2782 // move the home stack forward if we are currently entering picture-in-picture
2783 // while pausing because that changes the focused stack and may prevent the new
2784 // starting activity from resuming.
2785 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
2786 && !r.supportsPictureInPictureWhilePausing) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002787 // Move the home stack forward if the task we just moved to the pinned stack
2788 // was launched from home so home should be visible behind it.
2789 moveHomeStackToFront(reason);
2790 }
Winson Chung74666102017-02-22 17:49:24 -08002791 // Defer resume until below
2792 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2793 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002794 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002795 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002796 // reveal/leave the other activities in their original task.
2797
2798 // Currently, we don't support reparenting activities across tasks in two different
2799 // stacks, so instead, just create a new task in the same stack, reparent the
2800 // activity into that task, and then reparent the whole task to the new stack. This
2801 // ensures that all the necessary work to migrate states in the old and new stacks
2802 // is also done.
2803 final TaskRecord newTask = task.getStack().createTaskRecord(
2804 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2805 r.mActivityType);
2806 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2807
2808 // Defer resume until below
2809 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2810 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002811 }
Winson Chungc2baac02017-01-11 13:34:47 -08002812
2813 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2814 // to the pinned stack
2815 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002816 } finally {
2817 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002818 }
2819
Wale Ogunwale480dca02016-02-06 13:58:29 -08002820 // The task might have already been running and its visibility needs to be synchronized
2821 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002822 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002823 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002824
Winson Chung55893332017-02-17 17:13:10 -08002825 stack.animateResizePinnedStack(bounds, -1 /* animationDuration */);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002826 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002827 }
2828
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002829 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002830 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2831 if (r == null || !r.isFocusable()) {
2832 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2833 "moveActivityStackToFront: unfocusable r=" + r);
2834 return false;
2835 }
2836
2837 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002838 final ActivityStack stack = r.getStack();
2839 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002840 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2841 + r + " task=" + task);
2842 return false;
2843 }
2844
Chong Zhang6cda19c2016-06-14 19:07:56 -07002845 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2846 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2847 "moveActivityStackToFront: already on top, r=" + r);
2848 return false;
2849 }
2850
2851 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2852 "moveActivityStackToFront: r=" + r);
2853
2854 stack.moveToFront(reason, task);
2855 return true;
2856 }
2857
Craig Mautnerac6f8432013-07-17 13:24:59 -07002858 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002859 mTmpFindTaskResult.r = null;
2860 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002861 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002862 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002864 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2865 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002866 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002867 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002868 continue;
2869 }
2870 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002871 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2872 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 continue;
2874 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002875 stack.findTaskLocked(r, mTmpFindTaskResult);
2876 // It is possible to have task in multiple stacks with the same root affinity.
2877 // If the match we found was based on root affinity we keep on looking to see if
2878 // there is a better match in another stack. We eventually return the match based
2879 // on root affinity if we don't find a better match.
2880 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2881 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002882 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002883 }
2884 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002885 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2886 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002887 }
2888
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002889 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2890 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002891 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2892 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002893 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002894 final ActivityRecord ar = stacks.get(stackNdx)
2895 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002896 if (ar != null) {
2897 return ar;
2898 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002899 }
2900 }
2901 return null;
2902 }
2903
Craig Mautner8d341ef2013-03-26 09:03:27 -07002904 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002905 scheduleSleepTimeout();
2906 if (!mGoingToSleep.isHeld()) {
2907 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002908 if (mLaunchingActivity.isHeld()) {
2909 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2910 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002911 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002912 mLaunchingActivity.release();
2913 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002914 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002916 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002917 }
2918
2919 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002920 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002921
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002922 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002923 final long endTime = System.currentTimeMillis() + timeout;
2924 while (true) {
2925 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002926 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2927 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002928 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2929 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2930 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002931 }
2932 if (cantShutdown) {
2933 long timeRemaining = endTime - System.currentTimeMillis();
2934 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002935 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002936 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 } catch (InterruptedException e) {
2938 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002939 } else {
2940 Slog.w(TAG, "Activity manager shutdown timed out");
2941 timedout = true;
2942 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002943 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002944 } else {
2945 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
2947 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002948
2949 // Force checkReadyForSleep to complete.
2950 mSleepTimeout = true;
2951 checkReadyForSleepLocked();
2952
Craig Mautner8d341ef2013-03-26 09:03:27 -07002953 return timedout;
2954 }
2955
2956 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002957 removeSleepTimeouts();
2958 if (mGoingToSleep.isHeld()) {
2959 mGoingToSleep.release();
2960 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002961 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2962 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002963 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2964 final ActivityStack stack = stacks.get(stackNdx);
2965 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002966 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002967 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002968 }
Craig Mautner5314a402013-09-26 12:40:16 -07002969 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002971 mGoingToSleepActivities.clear();
2972 }
2973
2974 void activitySleptLocked(ActivityRecord r) {
2975 mGoingToSleepActivities.remove(r);
2976 checkReadyForSleepLocked();
2977 }
2978
2979 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002980 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002981 // Do not care.
2982 return;
2983 }
2984
2985 if (!mSleepTimeout) {
2986 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002987 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2988 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002989 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2990 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2991 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002992 }
2993
2994 if (mStoppingActivities.size() > 0) {
2995 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002996 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002997 + mStoppingActivities.size() + " activities");
2998 scheduleIdleLocked();
2999 dontSleep = true;
3000 }
3001
3002 if (mGoingToSleepActivities.size() > 0) {
3003 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003004 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005 + mGoingToSleepActivities.size() + " activities");
3006 dontSleep = true;
3007 }
3008
3009 if (dontSleep) {
3010 return;
3011 }
3012 }
3013
Wei Wang65c7a152016-06-02 18:51:22 -07003014 // Send launch end powerhint before going sleep
3015 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3016
Craig Mautnere0a38842013-12-16 16:14:02 -08003017 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3018 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003019 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3020 stacks.get(stackNdx).goToSleep();
3021 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003022 }
3023
3024 removeSleepTimeouts();
3025
3026 if (mGoingToSleep.isHeld()) {
3027 mGoingToSleep.release();
3028 }
3029 if (mService.mShuttingDown) {
3030 mService.notifyAll();
3031 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003032 }
3033
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003034 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003035 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003036 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003037 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003038 }
3039 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003040 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003041 mWindowManager.executeAppTransition();
3042 return true;
3043 }
3044 return false;
3045 }
3046
Craig Mautner8d341ef2013-03-26 09:03:27 -07003047 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003048 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3049 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003050 int stackNdx = stacks.size() - 1;
3051 while (stackNdx >= 0) {
3052 stacks.get(stackNdx).handleAppCrashLocked(app);
3053 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003055 }
3056 }
3057
Jose Lima4b6c6692014-08-12 17:41:12 -07003058 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003059 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003060 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003061 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3062 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003063 return false;
3064 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003065
3066 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3067 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3068 + " visible=" + visible + " stackId=" + stack.mStackId
3069 + " can't contain visible behind activities");
3070 return false;
3071 }
3072
Jose Lima4b6c6692014-08-12 17:41:12 -07003073 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003074 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3075 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003076
3077 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003078 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003079 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003080 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003081 return true;
3082 }
3083
3084 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003085 if (visible && top.fullscreen) {
3086 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003087 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3088 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3089 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3090 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003091 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003092 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3093 // Only the activity set as currently visible behind should actively reset its
3094 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003095 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3096 "requestVisibleBehind: returning visible=" + visible
3097 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3098 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003099 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003100 }
3101
Jose Lima4b6c6692014-08-12 17:41:12 -07003102 stack.setVisibleBehindActivity(visible ? r : null);
3103 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003104 // If there is a translucent home activity, we need to force it stop being translucent,
3105 // because we can't depend on the application to necessarily perform that operation.
3106 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003107 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003108 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003109 mService.convertFromTranslucent(next.appToken);
3110 }
3111 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003112 if (top.app != null && top.app.thread != null) {
3113 // Notify the top app of the change.
3114 try {
3115 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3116 } catch (RemoteException e) {
3117 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003118 }
3119 return true;
3120 }
3121
Craig Mautnerbb742462014-07-07 15:28:55 -07003122 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003123 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003124 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003125 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003126
3127 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003128 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003129 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003130 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003131 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003132
3133 // When launching tasks behind, update the last active time of the top task after the new
3134 // task has been shown briefly
3135 final ActivityRecord top = stack.topActivity();
3136 if (top != null) {
3137 top.task.touchActiveTime();
3138 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003139 }
3140
3141 void scheduleLaunchTaskBehindComplete(IBinder token) {
3142 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3143 }
3144
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003145 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3146 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003147 mKeyguardController.beginActivityVisibilityUpdate();
3148 try {
3149 // First the front stacks. In case any are not fullscreen and are in front of home.
3150 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3151 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3152 final int topStackNdx = stacks.size() - 1;
3153 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3154 final ActivityStack stack = stacks.get(stackNdx);
3155 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3156 }
Craig Mautner580ea812013-04-25 12:58:38 -07003157 }
Jorim Jaggife762342016-10-13 14:33:27 +02003158 } finally {
3159 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003160 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003161 }
3162
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003163 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3164 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3165 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3166 final int topStackNdx = stacks.size() - 1;
3167 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3168 final ActivityStack stack = stacks.get(stackNdx);
3169 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3170 }
3171 }
3172 }
3173
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003174 void invalidateTaskLayers() {
3175 mTaskLayersChanged = true;
3176 }
3177
3178 void rankTaskLayersIfNeeded() {
3179 if (!mTaskLayersChanged) {
3180 return;
3181 }
3182 mTaskLayersChanged = false;
3183 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3184 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3185 int baseLayer = 0;
3186 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3187 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3188 }
3189 }
3190 }
3191
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003192 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3193 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3194 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3195 final int topStackNdx = stacks.size() - 1;
3196 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3197 final ActivityStack stack = stacks.get(stackNdx);
3198 stack.clearOtherAppTimeTrackers(except);
3199 }
3200 }
3201 }
3202
Craig Mautner8d341ef2013-03-26 09:03:27 -07003203 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3205 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 final int numStacks = stacks.size();
3207 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3208 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003209 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003210 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003211 }
3212 }
3213
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003214 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3215 // Examine all activities currently running in the process.
3216 TaskRecord firstTask = null;
3217 // Tasks is non-null only if two or more tasks are found.
3218 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003219 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3220 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003221 ActivityRecord r = app.activities.get(i);
3222 // First, if we find an activity that is in the process of being destroyed,
3223 // then we just aren't going to do anything for now; we want things to settle
3224 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003225 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003226 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003227 return;
3228 }
3229 // Don't consider any activies that are currently not in a state where they
3230 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003231 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3232 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003233 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003234 continue;
3235 }
3236 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003237 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003238 + " from " + r);
3239 if (firstTask == null) {
3240 firstTask = r.task;
3241 } else if (firstTask != r.task) {
3242 if (tasks == null) {
3243 tasks = new ArraySet<>();
3244 tasks.add(firstTask);
3245 }
3246 tasks.add(r.task);
3247 }
3248 }
3249 }
3250 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003251 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003252 return;
3253 }
3254 // If we have activities in multiple tasks that are in a position to be destroyed,
3255 // let's iterate through the tasks and release the oldest one.
3256 final int numDisplays = mActivityDisplays.size();
3257 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3258 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3259 // Step through all stacks starting from behind, to hit the oldest things first.
3260 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3261 final ActivityStack stack = stacks.get(stackNdx);
3262 // Try to release activities in this stack; if we manage to, we are done.
3263 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3264 return;
3265 }
3266 }
3267 }
3268 }
3269
Amith Yamasani37a40c22015-06-17 13:25:42 -07003270 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003271 final int focusStackId = mFocusedStack.getStackId();
3272 // We dismiss the docked stack whenever we switch users.
3273 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003274 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3275 // also cause all tasks to be moved to the fullscreen stack at a position that is
3276 // appropriate.
3277 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003278
3279 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003280 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003281 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003282
Craig Mautner858d8a62013-04-23 17:08:34 -07003283 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003284 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3285 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003287 final ActivityStack stack = stacks.get(stackNdx);
3288 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003289 TaskRecord task = stack.topTask();
3290 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003291 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003292 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003293 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003294 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003295
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003296 ActivityStack stack = getStack(restoreStackId);
3297 if (stack == null) {
3298 stack = mHomeStack;
3299 }
3300 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003301 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003302 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003303 } else {
3304 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003305 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003306 }
Craig Mautner93529a42013-10-04 15:03:13 -07003307 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003308 }
3309
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003310 /** Checks whether the userid is a profile of the current user. */
3311 boolean isCurrentProfileLocked(int userId) {
3312 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003313 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003314 }
3315
Winson Chung4dabf232017-01-25 13:25:22 -08003316 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3317 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003318 ArrayList<ActivityRecord> stops = null;
3319
3320 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003321 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3322 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003323 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3324 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003325 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003326 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003327 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3328 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003329 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003330 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003331 if (s.finishing) {
3332 // If this activity is finishing, it is sitting on top of
3333 // everyone else but we now know it is no longer needed...
3334 // so get rid of it. Otherwise, we need to go through the
3335 // normal flow and hide it once we determine that it is
3336 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003337 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003338 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003339 }
3340 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003341 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003342 if (!processPausingActivities && s.state == PAUSING) {
3343 // Defer processing pausing activities in this iteration and reschedule
3344 // a delayed idle to reprocess it again
3345 removeTimeoutsForActivityLocked(idleActivity);
3346 scheduleIdleTimeoutLocked(idleActivity);
3347 continue;
3348 }
3349
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003350 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003351 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003352 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003353 }
3354 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003355 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003356 }
3357 }
3358
3359 return stops;
3360 }
3361
Craig Mautnercf910b02013-04-23 11:23:27 -07003362 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003363 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3364 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3365 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3366 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003367 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003368 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003369 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003370 if (r == null) Slog.e(TAG,
3371 "validateTop...: null top activity, stack=" + stack);
3372 else {
3373 final ActivityRecord pausing = stack.mPausingActivity;
3374 if (pausing != null && pausing == r) Slog.e(TAG,
3375 "validateTop...: top stack has pausing activity r=" + r
3376 + " state=" + state);
3377 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3378 "validateTop...: activity in front not resumed r=" + r
3379 + " state=" + state);
3380 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003381 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003382 final ActivityRecord resumed = stack.mResumedActivity;
3383 if (resumed != null && resumed == r) Slog.e(TAG,
3384 "validateTop...: back stack has resumed activity r=" + r
3385 + " state=" + state);
3386 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3387 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003388 }
3389 }
3390 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003391 }
3392
Craig Mautnere0570202015-05-13 13:06:11 -07003393 private String lockTaskModeToString() {
3394 switch (mLockTaskModeState) {
3395 case LOCK_TASK_MODE_LOCKED:
3396 return "LOCKED";
3397 case LOCK_TASK_MODE_PINNED:
3398 return "PINNED";
3399 case LOCK_TASK_MODE_NONE:
3400 return "NONE";
3401 default: return "unknown=" + mLockTaskModeState;
3402 }
3403 }
3404
Craig Mautner27084302013-03-25 08:05:25 -07003405 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003406 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003408 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003409 pw.print(prefix);
3410 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003411 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003412 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003413 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003414 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3415 if (packages.size() > 0) {
3416 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3417 for (int i = 0; i < packages.size(); ++i) {
3418 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3419 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3420 }
3421 }
3422 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3423 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003424 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003425
Winson43d1f262016-06-14 16:05:55 -07003426 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003427 * Dump all connected displays' configurations.
3428 * @param prefix Prefix to apply to each line of the dump.
3429 */
3430 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3431 pw.print(prefix); pw.println("Display override configurations:");
3432 final int displayCount = mActivityDisplays.size();
3433 for (int i = 0; i < displayCount; i++) {
3434 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3435 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3436 pw.println(activityDisplay.getOverrideConfiguration());
3437 }
3438 }
3439
3440 /**
Winson43d1f262016-06-14 16:05:55 -07003441 * Dumps the activities matching the given {@param name} in the either the focused stack
3442 * or all visible stacks if {@param dumpVisibleStacks} is true.
3443 */
Winson Chung6998bc42017-02-28 17:07:05 -08003444 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3445 boolean dumpFocusedStackOnly) {
3446 if (dumpFocusedStackOnly) {
3447 return mFocusedStack.getDumpActivitiesLocked(name);
3448 } else {
Winson43d1f262016-06-14 16:05:55 -07003449 ArrayList<ActivityRecord> activities = new ArrayList<>();
3450 int numDisplays = mActivityDisplays.size();
3451 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3452 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3453 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3454 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003455 if (!dumpVisibleStacksOnly ||
3456 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003457 activities.addAll(stack.getDumpActivitiesLocked(name));
3458 }
3459 }
3460 }
3461 return activities;
Winson43d1f262016-06-14 16:05:55 -07003462 }
Craig Mautner20e72272013-04-01 13:45:53 -07003463 }
3464
Dianne Hackborn390517b2013-05-30 15:03:32 -07003465 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3466 boolean needSep, String prefix) {
3467 if (activity != null) {
3468 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3469 if (needSep) {
3470 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003471 }
3472 pw.print(prefix);
3473 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003474 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003475 }
3476 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003477 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003478 }
3479
Craig Mautner8d341ef2013-03-26 09:03:27 -07003480 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3481 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003482 boolean printed = false;
3483 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003484 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3485 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003486 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003487 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003488 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003489 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003490 final ActivityStack stack = stacks.get(stackNdx);
3491 StringBuilder stackHeader = new StringBuilder(128);
3492 stackHeader.append(" Stack #");
3493 stackHeader.append(stack.mStackId);
3494 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003495 stackHeader.append("\n");
3496 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3497 stackHeader.append("\n");
3498 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3500 needSep, stackHeader.toString());
3501 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3502 !dumpAll, false, dumpPackage, true,
3503 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003504
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 needSep = printed;
3506 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3507 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003508 if (pr) {
3509 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003511 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003512 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3513 " mResumedActivity: ");
3514 if (pr) {
3515 printed = true;
3516 needSep = false;
3517 }
3518 if (dumpAll) {
3519 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3520 " mLastPausedActivity: ");
3521 if (pr) {
3522 printed = true;
3523 needSep = true;
3524 }
3525 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3526 needSep, " mLastNoHistoryActivity: ");
3527 }
3528 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003529 }
3530 }
3531
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003532 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3533 false, dumpPackage, true, " Activities waiting to finish:", null);
3534 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3535 false, dumpPackage, true, " Activities waiting to stop:", null);
3536 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3537 false, dumpPackage, true, " Activities waiting for another to become visible:",
3538 null);
3539 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3540 false, dumpPackage, true, " Activities waiting to sleep:", null);
3541 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3542 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003543
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003544 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003545 }
3546
Dianne Hackborn390517b2013-05-30 15:03:32 -07003547 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003548 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003549 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003550 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003551 String innerPrefix = null;
3552 String[] args = null;
3553 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003554 for (int i=list.size()-1; i>=0; i--) {
3555 final ActivityRecord r = list.get(i);
3556 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3557 continue;
3558 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003559 if (innerPrefix == null) {
3560 innerPrefix = prefix + " ";
3561 args = new String[0];
3562 }
3563 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003564 final boolean full = !brief && (complete || !r.isInHistory());
3565 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003566 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003567 needNL = false;
3568 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003569 if (header1 != null) {
3570 pw.println(header1);
3571 header1 = null;
3572 }
3573 if (header2 != null) {
3574 pw.println(header2);
3575 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003576 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003577 if (lastTask != r.task) {
3578 lastTask = r.task;
3579 pw.print(prefix);
3580 pw.print(full ? "* " : " ");
3581 pw.println(lastTask);
3582 if (full) {
3583 lastTask.dump(pw, prefix + " ");
3584 } else if (complete) {
3585 // Complete + brief == give a summary. Isn't that obvious?!?
3586 if (lastTask.intent != null) {
3587 pw.print(prefix); pw.print(" ");
3588 pw.println(lastTask.intent.toInsecureStringWithClip());
3589 }
3590 }
3591 }
3592 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3593 pw.print(" #"); pw.print(i); pw.print(": ");
3594 pw.println(r);
3595 if (full) {
3596 r.dump(pw, innerPrefix);
3597 } else if (complete) {
3598 // Complete + brief == give a summary. Isn't that obvious?!?
3599 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3600 if (r.app != null) {
3601 pw.print(innerPrefix); pw.println(r.app);
3602 }
3603 }
3604 if (client && r.app != null && r.app.thread != null) {
3605 // flush anything that is already in the PrintWriter since the thread is going
3606 // to write to the file descriptor directly
3607 pw.flush();
3608 try {
3609 TransferPipe tp = new TransferPipe();
3610 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003611 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003612 // Short timeout, since blocking here can
3613 // deadlock with the application.
3614 tp.go(fd, 2000);
3615 } finally {
3616 tp.kill();
3617 }
3618 } catch (IOException e) {
3619 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3620 } catch (RemoteException e) {
3621 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3622 }
3623 needNL = true;
3624 }
3625 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003626 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003627 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003628
Craig Mautnerf3333272013-04-22 10:55:53 -07003629 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003630 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3631 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003632 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3633 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003634 }
3635
3636 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003637 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003638 }
3639
3640 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003641 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3642 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003643 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3644 }
3645
Craig Mautner05d29032013-05-03 13:40:13 -07003646 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003647 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3648 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3649 }
Craig Mautner05d29032013-05-03 13:40:13 -07003650 }
3651
Craig Mautner0eea92c2013-05-16 13:35:39 -07003652 void removeSleepTimeouts() {
3653 mSleepTimeout = false;
3654 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3655 }
3656
3657 final void scheduleSleepTimeout() {
3658 removeSleepTimeouts();
3659 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3660 }
3661
Craig Mautner4a1cb222013-12-04 16:14:06 -08003662 @Override
3663 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003664 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003665 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3666 }
3667
3668 @Override
3669 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003670 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003671 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3672 }
3673
3674 @Override
3675 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003676 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003677 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3678 }
3679
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003680 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003681 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003682 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003683 newDisplay = mActivityDisplays.get(displayId) == null;
3684 if (newDisplay) {
3685 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003686 if (activityDisplay.mDisplay == null) {
3687 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3688 return;
3689 }
Craig Mautner4504de52013-12-20 09:06:56 -08003690 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003691 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003692 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003693 }
Craig Mautner4504de52013-12-20 09:06:56 -08003694 if (newDisplay) {
3695 mWindowManager.onDisplayAdded(displayId);
3696 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003697 }
3698
Andrii Kulianca007a62016-11-22 16:33:28 -08003699 /** Check if display with specified id is added to the list. */
3700 boolean isDisplayAdded(int displayId) {
3701 return mActivityDisplays.get(displayId) != null;
3702 }
3703
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003704 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003705 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003706 mService.mContext.getResources().getDimensionPixelSize(
3707 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003708 }
3709
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003710 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003711 if (displayId == DEFAULT_DISPLAY) {
3712 throw new IllegalArgumentException("Can't remove the primary display.");
3713 }
3714
Craig Mautner4a1cb222013-12-04 16:14:06 -08003715 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003716 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3717 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003718 final boolean destroyContentOnRemoval
3719 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003720 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3721 while (!stacks.isEmpty()) {
3722 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003723 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003724 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3725 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003726 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003727 } else {
3728 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3729 // a valid launch stack for all activities. This way the task history from
3730 // external display will be preserved on primary after move.
3731 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003732 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003734 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 }
3736 }
3737 mWindowManager.onDisplayRemoved(displayId);
3738 }
3739
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003740 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003742 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3743 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003744 // TODO: Update the bounds.
3745 }
3746 }
3747 mWindowManager.onDisplayChanged(displayId);
3748 }
3749
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003750 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003751 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003753 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003754 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003756 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003757 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003758 info.position = display != null
3759 ? display.mStacks.indexOf(stack)
3760 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761
3762 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3763 final int numTasks = tasks.size();
3764 int[] taskIds = new int[numTasks];
3765 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003766 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003767 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 for (int i = 0; i < numTasks; ++i) {
3769 final TaskRecord task = tasks.get(i);
3770 taskIds[i] = task.taskId;
3771 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3772 : task.realActivity != null ? task.realActivity.flattenToString()
3773 : task.getTopActivity() != null ? task.getTopActivity().packageName
3774 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003775 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003776 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003777 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778 }
3779 info.taskIds = taskIds;
3780 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003781 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003782 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003783
3784 final ActivityRecord top = stack.topRunningActivityLocked();
3785 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 return info;
3787 }
3788
3789 StackInfo getStackInfoLocked(int stackId) {
3790 ActivityStack stack = getStack(stackId);
3791 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003792 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 }
3794 return null;
3795 }
3796
3797 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003798 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003799 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3800 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003802 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003803 }
3804 }
3805 return list;
3806 }
3807
Craig Mautner15df08a2015-04-01 12:17:18 -07003808 TaskRecord getLockedTaskLocked() {
3809 final int top = mLockTaskModeTasks.size() - 1;
3810 if (top >= 0) {
3811 return mLockTaskModeTasks.get(top);
3812 }
3813 return null;
3814 }
3815
3816 boolean isLockedTask(TaskRecord task) {
3817 return mLockTaskModeTasks.contains(task);
3818 }
3819
3820 boolean isLastLockedTask(TaskRecord task) {
3821 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3822 }
3823
3824 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003825 if (!mLockTaskModeTasks.remove(task)) {
3826 return;
3827 }
3828 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3829 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003830 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003831 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3832 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003833 final Message lockTaskMsg = Message.obtain();
3834 lockTaskMsg.arg1 = task.userId;
3835 lockTaskMsg.what = LOCK_TASK_END_MSG;
3836 mHandler.sendMessage(lockTaskMsg);
3837 }
3838 }
3839
Andrii Kulianc27916642016-04-12 17:59:27 -07003840 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3841 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3842 false /* forceNonResizable */);
3843 }
3844
Jorim Jaggid53f0922016-04-06 22:16:23 -07003845 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003846 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003847 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3848 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003849 return;
3850 }
3851
Jorim Jaggicd13d332016-04-27 15:40:20 -07003852 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003853 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003854 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003855 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003856
Andrii Kulianc27916642016-04-12 17:59:27 -07003857 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3858 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003859 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003860 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003861 && !topActivity.noDisplay) {
3862 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003863 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3864 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003865 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003866 }
3867
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003868 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003869 if (mLockTaskNotify != null) {
3870 mLockTaskNotify.showToast(mLockTaskModeState);
3871 }
Jason Monka8f569c2014-07-07 12:15:21 -04003872 }
3873
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003874 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3875 if (mLockTaskModeTasks.contains(task)) {
3876 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3877 }
3878 }
3879
Craig Mautner432f64e2015-05-20 14:59:57 -07003880 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3881 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003882 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003883 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003884 final TaskRecord lockedTask = getLockedTaskLocked();
3885 if (lockedTask != null) {
3886 removeLockedTaskLocked(lockedTask);
3887 if (!mLockTaskModeTasks.isEmpty()) {
3888 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003889 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3890 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003891 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003892 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003893 return;
3894 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003895 }
Craig Mautnere0570202015-05-13 13:06:11 -07003896 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3897 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003898 return;
3899 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003900
3901 // Should have already been checked, but do it again.
3902 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003903 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3904 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003905 return;
3906 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003907 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003908 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003909 return;
3910 }
3911
3912 if (mLockTaskModeTasks.isEmpty()) {
3913 // First locktask.
3914 final Message lockTaskMsg = Message.obtain();
3915 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3916 lockTaskMsg.arg1 = task.userId;
3917 lockTaskMsg.what = LOCK_TASK_START_MSG;
3918 lockTaskMsg.arg2 = lockTaskModeState;
3919 mHandler.sendMessage(lockTaskMsg);
3920 }
3921 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003922 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3923 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003924 mLockTaskModeTasks.remove(task);
3925 mLockTaskModeTasks.add(task);
3926
3927 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003928 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003929 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003930
3931 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003932 findTaskToMoveToFrontLocked(task, 0, null, reason,
3933 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003934 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003935 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003936 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003937 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003938 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003939 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003940 }
3941
3942 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003943 return isLockTaskModeViolation(task, false);
3944 }
3945
3946 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3947 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003948 return false;
3949 }
3950 final int lockTaskAuth = task.mLockTaskAuth;
3951 switch (lockTaskAuth) {
3952 case LOCK_TASK_AUTH_DONT_LOCK:
3953 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003954 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003955 case LOCK_TASK_AUTH_LAUNCHABLE:
3956 case LOCK_TASK_AUTH_WHITELISTED:
3957 return false;
3958 case LOCK_TASK_AUTH_PINNABLE:
3959 // Pinnable tasks can't be launched on top of locktask tasks.
3960 return !mLockTaskModeTasks.isEmpty();
3961 default:
3962 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3963 return true;
3964 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003965 }
3966
Craig Mautner15df08a2015-04-01 12:17:18 -07003967 void onLockTaskPackagesUpdatedLocked() {
3968 boolean didSomething = false;
3969 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3970 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003971 final boolean wasWhitelisted =
3972 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3973 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003974 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003975 final boolean isWhitelisted =
3976 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3977 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3978 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003979 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003980 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3981 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003982 removeLockedTaskLocked(lockedTask);
3983 lockedTask.performClearTaskLocked();
3984 didSomething = true;
3985 }
3986 }
3987 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3988 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3989 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3990 final ActivityStack stack = stacks.get(stackNdx);
3991 stack.onLockTaskPackagesUpdatedLocked();
3992 }
3993 }
Craig Mautnere0570202015-05-13 13:06:11 -07003994 final ActivityRecord r = topRunningActivityLocked();
3995 final TaskRecord task = r != null ? r.task : null;
3996 if (mLockTaskModeTasks.isEmpty() && task != null
3997 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3998 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003999 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4000 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4001 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4002 false);
4003 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004004 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004005 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004006 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004007 }
4008 }
4009
Benjamin Franz43261142015-02-11 15:59:44 +00004010 int getLockTaskModeState() {
4011 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004012 }
4013
Jorim Jaggife89d122015-12-22 16:28:44 +01004014 void activityRelaunchedLocked(IBinder token) {
4015 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004016 if (mService.isSleepingOrShuttingDownLocked()) {
4017 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4018 if (r != null) {
4019 r.setSleeping(true, true);
4020 }
4021 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004022 }
4023
4024 void activityRelaunchingLocked(ActivityRecord r) {
4025 mWindowManager.notifyAppRelaunching(r.appToken);
4026 }
4027
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004028 void logStackState() {
4029 mActivityMetricsLogger.logWindowState();
4030 }
4031
Andrii Kulian933076d2016-03-29 17:04:42 -07004032 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004033 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4034 final ActivityRecord r = task.mActivities.get(i);
4035 if (r.app != null && r.app.thread != null) {
4036 mMultiWindowModeChangedActivities.add(r);
4037 }
4038 }
4039
4040 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4041 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4042 }
4043 }
4044
Andrii Kulian933076d2016-03-29 17:04:42 -07004045 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004046 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004047 if (prevStack == null || prevStack == stack
4048 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4049 return;
4050 }
4051
4052 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4053 final ActivityRecord r = task.mActivities.get(i);
4054 if (r.app != null && r.app.thread != null) {
4055 mPipModeChangedActivities.add(r);
4056 }
4057 }
4058
4059 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4060 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4061 }
4062 }
4063
Tony Mak853304c2016-04-18 15:17:41 +01004064 void setDockedStackMinimized(boolean minimized) {
4065 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004066 }
4067
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004068 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004069
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004070 public ActivityStackSupervisorHandler(Looper looper) {
4071 super(looper);
4072 }
4073
Winson Chung4dabf232017-01-25 13:25:22 -08004074 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004075 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004076 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4077 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004078 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004079 }
4080
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004081 @Override
4082 public void handleMessage(Message msg) {
4083 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004084 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4085 synchronized (mService) {
4086 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4087 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004088 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004089 }
4090 }
4091 } break;
4092 case REPORT_PIP_MODE_CHANGED_MSG: {
4093 synchronized (mService) {
4094 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4095 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004096 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004097 }
4098 }
4099 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004100 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004101 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4102 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004103 if (mService.mDidDexOpt) {
4104 mService.mDidDexOpt = false;
4105 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4106 nmsg.obj = msg.obj;
4107 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4108 return;
4109 }
4110 // We don't at this point know if the activity is fullscreen,
4111 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004112 activityIdleInternal((ActivityRecord) msg.obj,
4113 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004114 } break;
4115 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004116 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004117 activityIdleInternal((ActivityRecord) msg.obj,
4118 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004119 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004120 case RESUME_TOP_ACTIVITY_MSG: {
4121 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004122 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004123 }
4124 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004125 case SLEEP_TIMEOUT_MSG: {
4126 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004127 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004128 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4129 mSleepTimeout = true;
4130 checkReadyForSleepLocked();
4131 }
4132 }
4133 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004134 case LAUNCH_TIMEOUT_MSG: {
4135 if (mService.mDidDexOpt) {
4136 mService.mDidDexOpt = false;
4137 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4138 return;
4139 }
4140 synchronized (mService) {
4141 if (mLaunchingActivity.isHeld()) {
4142 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4143 if (VALIDATE_WAKE_LOCK_CALLER
4144 && Binder.getCallingUid() != Process.myUid()) {
4145 throw new IllegalStateException("Calling must be system uid");
4146 }
4147 mLaunchingActivity.release();
4148 }
4149 }
4150 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004152 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153 } break;
4154 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004155 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004156 } break;
4157 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004158 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004160 case CONTAINER_CALLBACK_VISIBILITY: {
4161 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004162 final IActivityContainerCallback callback = container.mCallback;
4163 if (callback != null) {
4164 try {
4165 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4166 } catch (RemoteException e) {
4167 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004168 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004169 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004170 case LOCK_TASK_START_MSG: {
4171 // When lock task starts, we disable the status bars.
4172 try {
Jason Monk62515be2014-05-21 16:06:19 -04004173 if (mLockTaskNotify == null) {
4174 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004175 }
Jason Monk62515be2014-05-21 16:06:19 -04004176 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004177 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004178 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004179 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004180 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004181 flags = StatusBarManager.DISABLE_MASK
4182 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004183 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004184 flags = StatusBarManager.DISABLE_MASK
4185 & (~StatusBarManager.DISABLE_BACK)
4186 & (~StatusBarManager.DISABLE_HOME)
4187 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004188 }
4189 getStatusBarService().disable(flags, mToken,
4190 mService.mContext.getPackageName());
4191 }
4192 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004193 if (getDevicePolicyManager() != null) {
4194 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004195 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004196 }
justinzhang5286d3f2014-05-12 17:06:01 -04004197 } catch (RemoteException ex) {
4198 throw new RuntimeException(ex);
4199 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004200 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004201 case LOCK_TASK_END_MSG: {
4202 // When lock task ends, we enable the status bars.
4203 try {
Jason Monk62515be2014-05-21 16:06:19 -04004204 if (getStatusBarService() != null) {
4205 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4206 mService.mContext.getPackageName());
4207 }
4208 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004209 if (getDevicePolicyManager() != null) {
4210 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4211 msg.arg1);
4212 }
Jason Monk62515be2014-05-21 16:06:19 -04004213 if (mLockTaskNotify == null) {
4214 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4215 }
4216 mLockTaskNotify.show(false);
4217 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004218 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004219 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004220 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004221 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004222 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004223 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004224 new LockPatternUtils(mService.mContext)
4225 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004226 }
4227 } catch (SettingNotFoundException e) {
4228 // No setting, don't lock.
4229 }
justinzhang5286d3f2014-05-12 17:06:01 -04004230 } catch (RemoteException ex) {
4231 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004232 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004233 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004234 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004235 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004236 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4237 if (mLockTaskNotify == null) {
4238 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4239 }
4240 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4241 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004242 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4243 final ActivityContainer container = (ActivityContainer) msg.obj;
4244 final IActivityContainerCallback callback = container.mCallback;
4245 if (callback != null) {
4246 try {
4247 callback.onAllActivitiesComplete(container.asBinder());
4248 } catch (RemoteException e) {
4249 }
4250 }
4251 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004252 case LAUNCH_TASK_BEHIND_COMPLETE: {
4253 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004254 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004255 if (r != null) {
4256 handleLaunchTaskBehindCompleteLocked(r);
4257 }
4258 }
4259 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004260
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004261 }
4262 }
4263 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004264
Ying Wangb081a592014-04-22 15:20:16 -07004265 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004266 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4267 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004268 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004269 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004270 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004271 ActivityRecord mParentActivity = null;
4272 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004273
Craig Mautnere3a00d72014-04-16 08:31:19 -07004274 boolean mVisible = true;
4275
Craig Mautner4a1cb222013-12-04 16:14:06 -08004276 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004277 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004278
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004279 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4280 final static int CONTAINER_STATE_NO_SURFACE = 1;
4281 final static int CONTAINER_STATE_FINISHING = 2;
4282 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4283
Wale Ogunwale1666e312016-12-16 11:27:18 -08004284 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004285 synchronized (mService) {
4286 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004287 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004288 switch (mStackId) {
4289 case PINNED_STACK_ID:
4290 new PinnedActivityStack(this, mRecentTasks, onTop);
4291 break;
4292 default:
4293 new ActivityStack(this, mRecentTasks, onTop);
4294 break;
4295 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004296 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004297 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004298 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004299 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004300
Andrii Kulian839def92016-11-02 10:58:58 -07004301 /**
4302 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004303 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004304 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004305 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004306 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004307 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004308 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004309 if (mActivityDisplay != null) {
4310 throw new IllegalStateException("ActivityContainer is already attached, " +
4311 "displayId=" + mActivityDisplay.mDisplayId);
4312 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004313 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004314 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004315 }
4316
4317 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004318 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004319 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004320 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4321 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004322 return;
4323 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004324 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004325 }
4326 }
4327
4328 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004329 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004330 synchronized (mService) {
4331 if (mActivityDisplay != null) {
4332 return mActivityDisplay.mDisplayId;
4333 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004334 }
4335 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004336 }
4337
Jeff Brownca9bc702014-02-11 14:32:56 -08004338 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004339 public int getStackId() {
4340 synchronized (mService) {
4341 return mStackId;
4342 }
4343 }
4344
4345 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004346 public boolean injectEvent(InputEvent event) {
4347 final long origId = Binder.clearCallingIdentity();
4348 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004349 synchronized (mService) {
4350 if (mActivityDisplay != null) {
4351 return mInputManagerInternal.injectInputEvent(event,
4352 mActivityDisplay.mDisplayId,
4353 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4354 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004355 }
4356 return false;
4357 } finally {
4358 Binder.restoreCallingIdentity(origId);
4359 }
4360 }
4361
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004362 @Override
4363 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004364 synchronized (mService) {
4365 if (mContainerState == CONTAINER_STATE_FINISHING) {
4366 return;
4367 }
4368 mContainerState = CONTAINER_STATE_FINISHING;
4369
Craig Mautnerd163e752014-06-13 17:18:47 -07004370 long origId = Binder.clearCallingIdentity();
4371 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004372 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004373 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004374 } finally {
4375 Binder.restoreCallingIdentity(origId);
4376 }
4377 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004378 }
4379
Andrii Kulian03c403d2016-11-11 11:14:12 -08004380 /**
4381 * Remove the stack completely. Must be called only when there are no tasks left in it,
4382 * as this method does not finish running activities.
4383 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004384 void removeLocked() {
4385 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004386 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004387 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004388 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004389 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004390 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004391 }
4392
Andrii Kulian839def92016-11-02 10:58:58 -07004393 /**
4394 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4395 * completely or re-parented.
4396 */
4397 private void removeFromDisplayLocked() {
4398 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4399 + " current displayId=" + mActivityDisplay.mDisplayId);
4400
Wale Ogunwale1666e312016-12-16 11:27:18 -08004401 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004402 mActivityDisplay = null;
4403 }
4404
4405 /**
4406 * Move the stack to specified display.
4407 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004408 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004409 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004410 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004411 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4412 + mActivityDisplay + " to display=" + activityDisplay
4413 + " Callers=" + Debug.getCallers(2));
4414
4415 removeFromDisplayLocked();
4416
4417 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004418 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004419 }
4420
Craig Mautner4a1cb222013-12-04 16:14:06 -08004421 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004422 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004423 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004424 }
4425
4426 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004427 public final int startActivityIntentSender(IIntentSender intentSender)
4428 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004429 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4430
4431 if (!(intentSender instanceof PendingIntentRecord)) {
4432 throw new IllegalArgumentException("Bad PendingIntent object");
4433 }
4434
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004435 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004436 Binder.getCallingUid(), mCurrentUser, false,
4437 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004438
4439 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4440 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4441 pendingIntent.key.requestResolvedType);
4442
4443 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4444 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4445 }
4446
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004447 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004448 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004449 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004450 throw new SecurityException(
4451 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4452 }
4453 }
4454
Craig Mautnerdf88d732014-01-27 09:21:32 -08004455 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 public IBinder asBinder() {
4457 return this;
4458 }
4459
Craig Mautner4504de52013-12-20 09:06:56 -08004460 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004461 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004462 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004463 }
4464
Craig Mautner4a1cb222013-12-04 16:14:06 -08004465 ActivityStackSupervisor getOuter() {
4466 return ActivityStackSupervisor.this;
4467 }
4468
Craig Mautnerd163e752014-06-13 17:18:47 -07004469 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004470 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004471 }
4472
Craig Mautner6985bad2014-04-21 15:22:06 -07004473 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004474 void setVisible(boolean visible) {
4475 if (mVisible != visible) {
4476 mVisible = visible;
4477 if (mCallback != null) {
4478 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4479 0 /* unused */, this).sendToTarget();
4480 }
4481 }
4482 }
4483
Craig Mautner6985bad2014-04-21 15:22:06 -07004484 void setDrawn() {
4485 }
4486
Craig Mautner1b4bf852014-05-26 15:06:32 -07004487 // You can always start a new task on a regular ActivityStack.
4488 boolean isEligibleForNewTasks() {
4489 return true;
4490 }
4491
Craig Mautnerd163e752014-06-13 17:18:47 -07004492 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004493 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004494 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004495 }
4496
Craig Mautner34b73df2014-01-12 21:11:08 -08004497 @Override
4498 public String toString() {
4499 return mIdString + (mActivityDisplay == null ? "N" : "A");
4500 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004501 }
4502
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004503 private class VirtualActivityContainer extends ActivityContainer {
4504 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004505 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004506
4507 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004508 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4509 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004510 mParentActivity = parent;
4511 mCallback = callback;
4512 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004513 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004514 }
4515
4516 @Override
4517 public void setSurface(Surface surface, int width, int height, int density) {
4518 super.setSurface(surface, width, height, density);
4519
4520 synchronized (mService) {
4521 final long origId = Binder.clearCallingIdentity();
4522 try {
4523 setSurfaceLocked(surface, width, height, density);
4524 } finally {
4525 Binder.restoreCallingIdentity(origId);
4526 }
4527 }
4528 }
4529
4530 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4531 if (mContainerState == CONTAINER_STATE_FINISHING) {
4532 return;
4533 }
4534 VirtualActivityDisplay virtualActivityDisplay =
4535 (VirtualActivityDisplay) mActivityDisplay;
4536 if (virtualActivityDisplay == null) {
4537 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004538 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004539 mActivityDisplay = virtualActivityDisplay;
4540 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004541 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004542 }
4543
4544 if (mSurface != null) {
4545 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004546 }
4547
Craig Mautner6985bad2014-04-21 15:22:06 -07004548 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004549 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004550 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004551 } else {
4552 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004553 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004554 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004555 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004556 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004557 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004558
Craig Mautnerd163e752014-06-13 17:18:47 -07004559 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004560
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004561 if (DEBUG_STACK) Slog.d(TAG_STACK,
4562 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004563 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004564
Craig Mautner6985bad2014-04-21 15:22:06 -07004565 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004566 boolean isAttachedLocked() {
4567 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004568 }
4569
4570 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004571 void setDrawn() {
4572 synchronized (mService) {
4573 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004574 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004575 }
4576 }
4577
Craig Mautner1b4bf852014-05-26 15:06:32 -07004578 // Never start a new task on an ActivityView if it isn't explicitly specified.
4579 @Override
4580 boolean isEligibleForNewTasks() {
4581 return false;
4582 }
4583
Craig Mautnerd163e752014-06-13 17:18:47 -07004584 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004585 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004586 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4587 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4588 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4589 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4590 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004591 }
4592 }
4593
Craig Mautner4a1cb222013-12-04 16:14:06 -08004594 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4595 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004596 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004597 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004598 int mDisplayId;
4599 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004600
Craig Mautner4a1cb222013-12-04 16:14:06 -08004601 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4602 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004603 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004604
Jose Lima4b6c6692014-08-12 17:41:12 -07004605 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004606
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004607 /** Array of all UIDs that are present on the display. */
4608 private IntArray mDisplayAccessUIDs = new IntArray();
4609
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004610 ActivityDisplay() {
4611 }
Craig Mautner4504de52013-12-20 09:06:56 -08004612
Craig Mautner1a70a162014-09-13 12:09:31 -07004613 // After instantiation, check that mDisplay is not null before using this. The alternative
4614 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004615 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004616 final Display display = mDisplayManager.getDisplay(displayId);
4617 if (display == null) {
4618 return;
4619 }
4620 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004621 }
4622
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004623 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004624 mDisplay = display;
4625 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004626 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004627
Wale Ogunwale1666e312016-12-16 11:27:18 -08004628 void attachStack(ActivityStack stack, boolean onTop) {
4629 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
4630 + " to displayId=" + mDisplayId + " onTop=" + onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004631 if (onTop) {
4632 mStacks.add(stack);
4633 } else {
4634 mStacks.add(0, stack);
4635 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004636 }
4637
Wale Ogunwale1666e312016-12-16 11:27:18 -08004638 void detachStack(ActivityStack stack) {
4639 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004640 + " from displayId=" + mDisplayId);
4641 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004642 }
4643
Jose Lima4b6c6692014-08-12 17:41:12 -07004644 void setVisibleBehindActivity(ActivityRecord r) {
4645 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004646 }
4647
Jose Lima4b6c6692014-08-12 17:41:12 -07004648 boolean hasVisibleBehindActivity() {
4649 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004650 }
4651
Craig Mautner34b73df2014-01-12 21:11:08 -08004652 @Override
4653 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004654 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4655 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004656
4657 @Override
4658 protected int getChildCount() {
4659 return mStacks.size();
4660 }
4661
4662 @Override
4663 protected ConfigurationContainer getChildAt(int index) {
4664 return mStacks.get(index);
4665 }
4666
4667 @Override
4668 protected ConfigurationContainer getParent() {
4669 return ActivityStackSupervisor.this;
4670 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004671
4672 boolean isPrivate() {
4673 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4674 }
4675
4676 boolean isUidPresent(int uid) {
4677 for (ActivityStack stack : mStacks) {
4678 if (stack.isUidPresent(uid)) {
4679 return true;
4680 }
4681 }
4682 return false;
4683 }
4684
4685 /** Update and get all UIDs that are present on the display and have access to it. */
4686 private IntArray getPresentUIDs() {
4687 mDisplayAccessUIDs.clear();
4688 for (ActivityStack stack : mStacks) {
4689 stack.getPresentUIDs(mDisplayAccessUIDs);
4690 }
4691 return mDisplayAccessUIDs;
4692 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004693
4694 boolean shouldDestroyContentOnRemove() {
4695 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4696 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004697 }
4698
4699 class VirtualActivityDisplay extends ActivityDisplay {
4700 VirtualDisplay mVirtualDisplay;
4701
Craig Mautner6985bad2014-04-21 15:22:06 -07004702 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004703 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004704 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4705 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4706 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4707 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004708
4709 init(mVirtualDisplay.getDisplay());
4710
4711 mWindowManager.handleDisplayAdded(mDisplayId);
4712 }
4713
4714 void setSurface(Surface surface) {
4715 if (mVirtualDisplay != null) {
4716 mVirtualDisplay.setSurface(surface);
4717 }
4718 }
4719
4720 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004721 void detachStack(ActivityStack stack) {
4722 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004723 if (mVirtualDisplay != null) {
4724 mVirtualDisplay.release();
4725 mVirtualDisplay = null;
4726 }
4727 }
4728
4729 @Override
4730 public String toString() {
4731 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004732 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004733 }
Jose Lima58e66d62014-05-27 20:00:27 -07004734
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004735 ActivityStack findStackBehind(ActivityStack stack) {
4736 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004737 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004738 if (display == null) {
4739 return null;
4740 }
4741 final ArrayList<ActivityStack> stacks = display.mStacks;
4742 for (int i = stacks.size() - 1; i >= 0; i--) {
4743 if (stacks.get(i) == stack && i > 0) {
4744 return stacks.get(i - 1);
4745 }
4746 }
4747 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4748 + " in=" + stacks);
4749 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004750
Jorim Jaggic69bd222016-03-15 14:38:37 +01004751 /**
4752 * Puts a task into resizing mode during the next app transition.
4753 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004754 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004755 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004756 private void setResizingDuringAnimation(TaskRecord task) {
4757 mResizingTasksDuringAnimation.add(task.taskId);
4758 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004759 }
4760
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004761 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4762 final TaskRecord task;
4763 final int callingUid;
4764 final String callingPackage;
4765 final Intent intent;
4766 final int userId;
4767 final ActivityOptions activityOptions = (bOptions != null)
4768 ? new ActivityOptions(bOptions) : null;
4769 final int launchStackId = (activityOptions != null)
4770 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004771 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004772 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004773 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004774 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004775
4776 if (launchStackId == DOCKED_STACK_ID) {
4777 mWindowManager.setDockedStackCreateState(
4778 activityOptions.getDockCreateMode(), null /* initialBounds */);
4779
4780 // Defer updating the stack in which recents is until the app transition is done, to
4781 // not run into issues where we still need to draw the task in recents but the
4782 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004783 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004784 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4785 }
4786
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004787 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4788 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004789 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004790 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004791 throw new IllegalArgumentException(
4792 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4793 }
4794
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004795 // Since we don't have an actual source record here, we assume that the currently focused
4796 // activity was the source.
4797 final ActivityStack focusedStack = getFocusedStack();
4798 final ActivityRecord sourceRecord =
4799 focusedStack != null ? focusedStack.topActivity() : null;
4800
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004801 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004802 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004803 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4804 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004805 }
4806 }
4807
4808 // If the user must confirm credentials (e.g. when first launching a work app and the
4809 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4810 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4811 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004812 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004813 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004814 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004815 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4816 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004817
4818 // If we are launching the task in the docked stack, put it into resizing mode so
4819 // the window renders full-screen with the background filling the void. Also only
4820 // call this at the end to make sure that tasks exists on the window manager side.
4821 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004822 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004823 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004824
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004825 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004826 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004827 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4828 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004829 return ActivityManager.START_TASK_TO_FRONT;
4830 }
4831 callingUid = task.mCallingUid;
4832 callingPackage = task.mCallingPackage;
4833 intent = task.intent;
4834 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4835 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004836 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4837 null, null, 0, 0, bOptions, userId, null, task);
4838 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004839 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004840 }
4841 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004842 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004843
4844 /**
4845 * @return a list of activities which are the top ones in each visible stack. The first
4846 * entry will be the focused activity.
4847 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004848 List<IBinder> getTopVisibleActivities() {
4849 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4850 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004851 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004852 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4853 // Traverse all stacks on a display.
4854 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4855 final ActivityStack stack = display.mStacks.get(j);
4856 // Get top activity from a visible stack and add it to the list.
4857 if (stack.getStackVisibilityLocked(null /* starting */)
4858 == ActivityStack.STACK_VISIBLE) {
4859 final ActivityRecord top = stack.topActivity();
4860 if (top != null) {
4861 if (stack == mFocusedStack) {
4862 topActivityTokens.add(0, top.appToken);
4863 } else {
4864 topActivityTokens.add(top.appToken);
4865 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004866 }
4867 }
4868 }
4869 }
4870 return topActivityTokens;
4871 }
Craig Mautner27084302013-03-25 08:05:25 -07004872}