blob: b4b3465765078320077773a6b44ffd544bc46bcd [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 Ngae1ff4f2016-11-10 15:49:14 -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;
Jorim Jaggife762342016-10-13 14:33:27 +020044
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
74import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
77import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
78import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
81import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
84import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
85import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
86import static com.android.server.am.ActivityStack.STACK_VISIBLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
92import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
93
Svetoslav7008b512015-06-24 18:47:07 -070094import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -070095import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +010096import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080099import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700100import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.app.ActivityOptions;
103import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700106import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400108import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700109import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400110import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700112import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700114import android.content.Intent;
115import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700117import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.pm.PackageManager;
119import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100120import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700122import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800123import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124import android.hardware.display.DisplayManager;
125import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800126import android.hardware.display.DisplayManagerGlobal;
127import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800128import android.hardware.input.InputManager;
129import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100131import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700132import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700133import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700135import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700136import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700138import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700139import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700140import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400141import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700143import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700144import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700145import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100146import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700147import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700148import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400149import android.provider.Settings;
150import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700151import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700152import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700153import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700154import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700155import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700156import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800157import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800158import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800159import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800160import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800161import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800162
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800163import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700164import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400165import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700166import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400167import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800168import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700169import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700171
Craig Mautner8d341ef2013-03-26 09:03:27 -0700172import java.io.FileDescriptor;
173import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700174import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700175import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700176import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700177import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700178import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800179import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700180import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700181
Jorim Jaggife762342016-10-13 14:33:27 +0200182public class ActivityStackSupervisor extends ConfigurationContainer
Andrii Kulian1779e612016-10-12 21:58:25 -0700183 implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700186 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700188 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800195 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800197
Craig Mautnerf3333272013-04-22 10:55:53 -0700198 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700200
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700203
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206
Craig Mautner05d29032013-05-03 13:40:13 -0700207 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
208 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
209 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
213 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
214 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700215 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400216 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
217 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700218 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700219 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700220 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800221 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
222 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700225
Jason Monk62515be2014-05-21 16:06:19 -0400226 private static final String LOCK_TASK_TAG = "Lock-to-App";
227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 // Used to indicate if an object (e.g. stack) that we are trying to get
229 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800230 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700232 // Used to indicate that windows of activities should be preserved during the resize.
233 static final boolean PRESERVE_WINDOWS = true;
234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. task) should be moved/created
236 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700237 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
239 // Used to indicate that an objects (e.g. task) removal from its container
240 // (e.g. stack) is due to it moving to another container.
241 static final boolean MOVING = true;
242
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700243 // Force the focus to change to the stack we are moving a task to..
244 static final boolean FORCE_FOCUS = true;
245
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700246 // Restore task from the saved recents if it can't be found in any live stack.
247 static final boolean RESTORE_FROM_RECENTS = true;
248
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700249 // Don't execute any calls to resume.
250 static final boolean DEFER_RESUME = true;
251
Winson Chung010927a2016-12-15 16:12:35 -0800252 // Used to indicate that a task is removed it should also be removed from recents.
253 static final boolean REMOVE_FROM_RECENTS = true;
254
Svetoslav7008b512015-06-24 18:47:07 -0700255 // Activity actions an app cannot start if it uses a permission which is not granted.
256 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
257 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700258
Svetoslav7008b512015-06-24 18:47:07 -0700259 static {
260 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
261 Manifest.permission.CAMERA);
262 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
263 Manifest.permission.CAMERA);
264 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
265 Manifest.permission.CALL_PHONE);
266 }
267
Svet Ganov99b60432015-06-27 13:15:22 -0700268 /** Action restriction: launching the activity is not restricted. */
269 private static final int ACTIVITY_RESTRICTION_NONE = 0;
270 /** Action restriction: launching the activity is restricted by a permission. */
271 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
272 /** Action restriction: launching the activity is restricted by an app op. */
273 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700274
justinzhang5286d3f2014-05-12 17:06:01 -0400275 /** Status Bar Service **/
276 private IBinder mToken = new Binder();
277 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400278 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400279
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700280 // For debugging to make sure the caller when acquiring/releasing our
281 // wake lock is the system process.
282 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /** The number of distinct task ids that can be assigned to the tasks of a single user */
284 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Craig Mautner27084302013-03-25 08:05:25 -0700286 final ActivityManagerService mService;
287
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290 final ActivityStackSupervisorHandler mHandler;
291
292 /** Short cut */
293 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700296 /** Counter for next free stack ID to use for dynamic activity stacks. */
297 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800299 /**
300 * Maps the task identifier that activities are currently being started in to the userId of the
301 * task. Each time a new task is created, the entry for the userId of the task is incremented
302 */
303 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700304
Craig Mautner2420ead2013-04-01 17:13:20 -0700305 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700307
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 /** The stack containing the launcher app. Assumed to always be attached to
309 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800313 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700314
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
316 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800318 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
320 /** List of activities that are waiting for a new activity to become visible before completing
321 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800322 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700325 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326
327 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700328 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330 /** List of activities that are ready to be stopped, but waiting for the next activity to
331 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700333
Craig Mautnerf3333272013-04-22 10:55:53 -0700334 /** List of activities that are ready to be finished, but waiting for the previous activity to
335 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700337
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800339 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340
Wale Ogunwale22e25262016-02-01 10:32:02 -0800341 /** List of activities whose multi-window mode changed that we need to report to the
342 * application */
343 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
344
345 /** List of activities whose picture-in-picture mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
348
Craig Mautnerf3333272013-04-22 10:55:53 -0700349 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700350 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700351
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
353 * is being brought in front of us. */
354 boolean mUserLeaving = false;
355
Craig Mautner0eea92c2013-05-16 13:35:39 -0700356 /** Set when we have taken too long waiting to go to sleep. */
357 boolean mSleepTimeout = false;
358
359 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360 * We don't want to allow the device to go to sleep while in the process
361 * of launching an activity. This is primarily to allow alarm intent
362 * receivers to launch an activity and get that to run before the device
363 * goes back to sleep.
364 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700365 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700366
367 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368 * Set when the system is going to sleep, until we have
369 * successfully paused the current activity and released our wake lock.
370 * At that point the system is allowed to actually sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700374 /** Stack id of the front stack when user switched, indexed by userId. */
375 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700376
Craig Mautner4504de52013-12-20 09:06:56 -0800377 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700379 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380
381 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383
Jeff Brownca9bc702014-02-11 14:32:56 -0800384 InputManagerInternal mInputManagerInternal;
385
Craig Mautner15df08a2015-04-01 12:17:18 -0700386 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
387 * may be finished until there is only one entry left. If this is empty the system is not
388 * in lockTask mode. */
389 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000390 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700391 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
392 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000393 */
394 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400395 /**
396 * Notifies the user when entering/exiting lock-task.
397 */
398 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800399
Wale Ogunwaled046a012015-12-24 13:05:59 -0800400 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800401 boolean inResumeTopActivity;
402
Andrii Kulian1e32e022016-09-16 15:29:34 -0700403 /**
404 * Temporary rect used during docked stack resize calculation so we don't need to create a new
405 * object each time.
406 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700407 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700408
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409 // The default minimal size that will be used if the activity doesn't specify its minimal size.
410 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700411 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700412
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700413 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
414 private boolean mTaskLayersChanged = true;
415
Jorim Jaggi275561a2016-02-23 10:11:02 -0500416 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800417
Jorim Jaggidc249c42015-12-15 14:57:31 -0800418 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
419
Andrii Kulian1779e612016-10-12 21:58:25 -0700420 @Override
421 protected int getChildCount() {
422 return mActivityDisplays.size();
423 }
424
425 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700426 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700427 return mActivityDisplays.valueAt(index);
428 }
429
430 @Override
431 protected ConfigurationContainer getParent() {
432 return null;
433 }
434
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700435 Configuration getDisplayOverrideConfiguration(int displayId) {
436 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
437 if (activityDisplay == null) {
438 throw new IllegalArgumentException("No display found with id: " + displayId);
439 }
440
441 return activityDisplay.getOverrideConfiguration();
442 }
443
444 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
445 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
446 if (activityDisplay == null) {
447 throw new IllegalArgumentException("No display found with id: " + displayId);
448 }
449
450 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
451 }
452
Wale Ogunwale39381972015-12-17 17:15:29 -0800453 static class FindTaskResult {
454 ActivityRecord r;
455 boolean matchedByRootAffinity;
456 }
457 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
458
Craig Mautneree36c772014-07-16 14:56:05 -0700459 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100460 * Used to keep track whether app visibilities got changed since the last pause. Useful to
461 * determine whether to invoke the task stack change listener after pausing.
462 */
463 boolean mAppVisibilitiesChangedSinceLastPause;
464
465 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100466 * Set of tasks that are in resizing mode during an app transition to fill the "void".
467 */
468 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
469
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700470
471 /**
472 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
473 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
474 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
475 * like the docked stack going empty.
476 */
477 private boolean mAllowDockedStackResize = true;
478
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100479 /**
Tony Mak853304c2016-04-18 15:17:41 +0100480 * Is dock currently minimized.
481 */
482 boolean mIsDockMinimized;
483
Jorim Jaggife762342016-10-13 14:33:27 +0200484 final KeyguardController mKeyguardController;
485
Tony Mak853304c2016-04-18 15:17:41 +0100486 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700487 * Description of a request to start a new activity, which has been held
488 * due to app switches being disabled.
489 */
490 static class PendingActivityLaunch {
491 final ActivityRecord r;
492 final ActivityRecord sourceRecord;
493 final int startFlags;
494 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700495 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700496
497 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700498 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700499 r = _r;
500 sourceRecord = _sourceRecord;
501 startFlags = _startFlags;
502 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700503 callerApp = _callerApp;
504 }
505
506 void sendErrorResult(String message) {
507 try {
508 if (callerApp.thread != null) {
509 callerApp.thread.scheduleCrash(message);
510 }
511 } catch (RemoteException e) {
512 Slog.e(TAG, "Exception scheduling crash of failed "
513 + "activity launcher sourceRecord=" + sourceRecord, e);
514 }
Craig Mautneree36c772014-07-16 14:56:05 -0700515 }
516 }
517
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800518 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700519 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700520 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800521 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800522 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jorim Jaggife762342016-10-13 14:33:27 +0200523 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700524 }
525
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800526 void setRecentTasks(RecentTasks recentTasks) {
527 mRecentTasks = recentTasks;
528 }
529
Jeff Brown2c43c332014-06-12 22:38:59 -0700530 /**
531 * At the time when the constructor runs, the power manager has not yet been
532 * initialized. So we initialize our wakelocks afterwards.
533 */
534 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700536 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700537 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700538 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700539 }
540
justinzhang5286d3f2014-05-12 17:06:01 -0400541 // This function returns a IStatusBarService. The value is from ServiceManager.
542 // getService and is cached.
543 private IStatusBarService getStatusBarService() {
544 synchronized (mService) {
545 if (mStatusBarService == null) {
546 mStatusBarService = IStatusBarService.Stub.asInterface(
547 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
548 if (mStatusBarService == null) {
549 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
550 }
551 }
552 return mStatusBarService;
553 }
554 }
555
Jason Monk35c62a42014-06-17 10:24:47 -0400556 private IDevicePolicyManager getDevicePolicyManager() {
557 synchronized (mService) {
558 if (mDevicePolicyManager == null) {
559 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
560 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
561 if (mDevicePolicyManager == null) {
562 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
563 }
564 }
565 return mDevicePolicyManager;
566 }
567 }
568
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700569 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 synchronized (mService) {
571 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200572 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800573
574 mDisplayManager =
575 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
576 mDisplayManager.registerDisplayListener(this, null);
577
578 Display[] displays = mDisplayManager.getDisplays();
579 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
580 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800581 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700582 if (activityDisplay.mDisplay == null) {
583 throw new IllegalStateException("Default Display does not exist");
584 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800585 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700586 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587 }
588
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800589 mHomeStack = mFocusedStack = mLastFocusedStack =
590 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800591
592 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800593 }
Craig Mautner27084302013-03-25 08:05:25 -0700594 }
595
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700596 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700598 }
599
Craig Mautnerde4ef022013-04-07 19:01:33 -0700600 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700602 }
603
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700604 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700605 if (stack == null) {
606 return false;
607 }
608
Craig Mautnerdf88d732014-01-27 09:21:32 -0800609 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
610 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700611 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800612 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800613 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700614 }
615
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700616 /** The top most stack. */
617 boolean isFrontStack(ActivityStack stack) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800618 return isFrontOfStackList(stack, mHomeStack.mStacks);
619 }
620
621 /** The top most stack on its display. */
622 boolean isFrontStackOnDisplay(ActivityStack stack) {
623 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
624 }
625
626 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700627 if (stack == null) {
628 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700630
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700631 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
632 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700633 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800634 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800635 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700636 }
637
Wale Ogunwaled046a012015-12-24 13:05:59 -0800638 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800639 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
640 if (!focusCandidate.isFocusable()) {
641 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
642 focusCandidate = focusCandidate.getNextFocusableStackLocked();
643 }
644
645 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800647 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800648
Wale Ogunwaled046a012015-12-24 13:05:59 -0800649 EventLogTags.writeAmFocusedStack(
650 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
651 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
652 }
653
654 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800655 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800656 if (r != null && r.idle) {
657 checkFinishBootingLocked();
658 }
659 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700660 }
661
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700662 void moveHomeStackToFront(String reason) {
663 mHomeStack.moveToFront(reason);
664 }
665
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700666 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800667 boolean moveHomeStackTaskToTop(String reason) {
668 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700669
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700670 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700671 if (top == null) {
672 return false;
673 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700674 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700675 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700676 }
677
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800678 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700679 if (!mService.mBooting && !mService.mBooted) {
680 // Not ready yet!
681 return false;
682 }
683
Craig Mautner84984fa2014-06-19 11:19:20 -0700684 if (prev != null) {
685 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
686 }
687
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800688 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700689 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800690 final String myReason = reason + " resumeHomeStackTask";
691
Mark Lua56ea122015-10-08 13:31:01 +0800692 // Only resume home activity if isn't finishing.
693 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700694 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800695 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700696 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800697 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700698 }
699
Craig Mautner8d341ef2013-03-26 09:03:27 -0700700 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700701 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700702 }
703
704 /**
705 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
706 * @param id Id of the task we would like returned.
707 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
708 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700709 * @param stackId The stack to restore the task to (default launch stack will be used if
710 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700711 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700712 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800715 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
717 ActivityStack stack = stacks.get(stackNdx);
718 TaskRecord task = stack.taskForIdLocked(id);
719 if (task != null) {
720 return task;
721 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700722 }
723 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800724
725 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700726 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800727 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800728 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700729 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800730 return null;
731 }
732
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700733 if (!restoreFromRecents) {
734 return task;
735 }
736
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700737 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700738 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
739 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800740 return null;
741 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700742 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800743 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 }
745
Craig Mautner6170f732013-04-02 13:05:23 -0700746 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800747 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800749 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
751 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
752 if (r != null) {
753 return r;
754 }
Craig Mautner6170f732013-04-02 13:05:23 -0700755 }
756 }
757 return null;
758 }
759
Tony Mak853304c2016-04-18 15:17:41 +0100760 /**
761 * TODO: Handle freefom mode.
762 * @return true when credential confirmation is needed for the user and there is any
763 * activity started by the user in any visible stack.
764 */
765 boolean isUserLockedProfile(@UserIdInt int userId) {
766 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
767 return false;
768 }
769 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
770 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
771 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
772 for (final ActivityStack activityStack : activityStacks) {
773 if (activityStack == null) {
774 continue;
775 }
776 if (activityStack.topRunningActivityLocked() == null) {
777 continue;
778 }
779 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
780 continue;
781 }
782 if (activityStack.isDockedStack() && mIsDockMinimized) {
783 continue;
784 }
785 final TaskRecord topTask = activityStack.topTask();
786 if (topTask == null) {
787 continue;
788 }
789 // To handle the case that work app is in the task but just is not the top one.
790 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
791 final ActivityRecord activityRecord = topTask.mActivities.get(i);
792 if (activityRecord.userId == userId) {
793 return true;
794 }
795 }
796 }
797 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000798 }
799
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800800 void setNextTaskIdForUserLocked(int taskId, int userId) {
801 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
802 if (taskId > currentTaskId) {
803 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700804 }
805 }
806
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700807 static int nextTaskIdForUser(int taskId, int userId) {
808 int nextTaskId = taskId + 1;
809 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
810 // Wrap around as there will be smaller task ids that are available now.
811 nextTaskId -= MAX_TASK_IDS_PER_USER;
812 }
813 return nextTaskId;
814 }
815
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800816 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800817 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
818 // for a userId u, a taskId can only be in the range
819 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
820 // 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 -0700821 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800822 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
823 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
824 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700825 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800826 if (candidateTaskId == currentTaskId) {
827 // Something wrong!
828 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
829 throw new IllegalStateException("Cannot get an available task id."
830 + " Reached limit of " + MAX_TASK_IDS_PER_USER
831 + " running tasks per user.");
832 }
833 }
834 mCurTaskIdForUser.put(userId, candidateTaskId);
835 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700836 }
837
Chong Zhang6cda19c2016-06-14 19:07:56 -0700838 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800839 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700840 if (stack == null) {
841 return null;
842 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700843 ActivityRecord resumedActivity = stack.mResumedActivity;
844 if (resumedActivity == null || resumedActivity.app == null) {
845 resumedActivity = stack.mPausingActivity;
846 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700847 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700848 }
849 }
850 return resumedActivity;
851 }
852
Dianne Hackbornff072722014-09-24 10:56:28 -0700853 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700854 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800855 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800856 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
857 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
859 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700860 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 continue;
862 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700863 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800864 if (hr != null) {
865 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
866 && processName.equals(hr.processName)) {
867 try {
George Mount2c92c972014-03-20 09:38:23 -0700868 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 didSomething = true;
870 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700871 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 Slog.w(TAG, "Exception in new application when starting activity "
873 + hr.intent.getComponent().flattenToShortString(), e);
874 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700875 }
Craig Mautner20e72272013-04-01 13:45:53 -0700876 }
Craig Mautner20e72272013-04-01 13:45:53 -0700877 }
878 }
879 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700880 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700881 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700882 }
Craig Mautner20e72272013-04-01 13:45:53 -0700883 return didSomething;
884 }
885
886 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
888 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
890 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700891 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 continue;
893 }
894 final ActivityRecord resumedActivity = stack.mResumedActivity;
895 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700896 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800897 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800898 return false;
899 }
Craig Mautner20e72272013-04-01 13:45:53 -0700900 }
901 }
Wei Wang65c7a152016-06-02 18:51:22 -0700902 // Send launch end powerhint when idle
903 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700904 return true;
905 }
906
Craig Mautnerde4ef022013-04-07 19:01:33 -0700907 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800908 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
909 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
911 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700912 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700914 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 return false;
916 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700917 }
918 }
919 }
920 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700921 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800922 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
923 mLastFocusedStack + " to=" + mFocusedStack);
924 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700925 return true;
926 }
927
928 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800929 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
931 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
933 final ActivityStack stack = stacks.get(stackNdx);
934 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800935 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700936 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800937 return false;
938 }
939 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800940 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700941 }
942 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800943 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700944 }
945
Craig Mautner2acc3892013-09-23 10:28:14 -0700946 /**
947 * Pause all activities in either all of the stacks or just the back stacks.
948 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700949 * @param resuming The resuming activity.
950 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
951 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700952 * @return true if any activity was paused as a result of this call.
953 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700954 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700955 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800956 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
957 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800958 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
959 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700960 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700961 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700963 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
964 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700966 }
967 }
968 return someActivityPaused;
969 }
970
Craig Mautnerde4ef022013-04-07 19:01:33 -0700971 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700972 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
974 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
976 final ActivityStack stack = stacks.get(stackNdx);
977 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700978 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700980 Slog.d(TAG_STATES,
981 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 pausing = false;
983 } else {
984 return false;
985 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700986 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 }
988 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700989 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700990 }
991
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700993 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500994 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800995 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
996 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
998 final ActivityStack stack = stacks.get(stackNdx);
999 if (stack.mResumedActivity != null &&
1000 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001001 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001002 }
1003 }
1004 }
1005 }
1006
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001007 void cancelInitializingActivities() {
1008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1009 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1010 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1011 stacks.get(stackNdx).cancelInitializingActivities();
1012 }
1013 }
1014 }
1015
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001016 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001017 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001018 }
1019
1020 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1021 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001022 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001023 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001024 if (w.who == null) {
1025 changed = true;
1026 w.timeout = false;
1027 if (r != null) {
1028 w.who = new ComponentName(r.info.packageName, r.info.name);
1029 }
1030 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1031 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001032 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001033 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001034 if (changed) {
1035 mService.notifyAll();
1036 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 }
1038
Chong Zhang5022da32016-06-21 16:31:37 -07001039 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1040 boolean changed = false;
1041 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1042 WaitResult w = mWaitingActivityLaunched.remove(i);
1043 if (w.who == null) {
1044 changed = true;
1045 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1046 // the starting activity ends up moving another activity to front, and it should
1047 // wait for this new activity to become visible instead.
1048 // Do not modify other fields.
1049 w.result = START_TASK_TO_FRONT;
1050 }
1051 }
1052 if (changed) {
1053 mService.notifyAll();
1054 }
1055 }
1056
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001057 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1058 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001059 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001060 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001061 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001062 if (w.who == null) {
1063 changed = true;
1064 w.timeout = timeout;
1065 if (r != null) {
1066 w.who = new ComponentName(r.info.packageName, r.info.name);
1067 }
1068 w.thisTime = thisTime;
1069 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001070 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001071 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001072 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001073 if (changed) {
1074 mService.notifyAll();
1075 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001076 }
1077
Craig Mautner29219d92013-04-16 20:19:12 -07001078 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001079 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001080 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001081 if (r != null) {
1082 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001083 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001084
Andrii Kulian7318d632016-07-20 18:59:28 -07001085 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001086 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001087 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1088 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001089 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001090 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001091 if (r != null) {
1092 return r;
1093 }
1094 }
1095 }
1096 return null;
1097 }
1098
Dianne Hackborn09233282014-04-30 11:33:59 -07001099 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001100 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001101 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1102 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001103 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001104 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001105 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001106 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1107 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001108 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001109 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001110 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001111 }
1112 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001113
1114 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1115 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1116 while (maxNum > 0) {
1117 long mostRecentActiveTime = Long.MIN_VALUE;
1118 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001119 final int numTaskLists = runningTaskLists.size();
1120 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1121 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001122 if (!stackTaskList.isEmpty()) {
1123 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1124 if (lastActiveTime > mostRecentActiveTime) {
1125 mostRecentActiveTime = lastActiveTime;
1126 selectedStackList = stackTaskList;
1127 }
1128 }
1129 }
1130 if (selectedStackList != null) {
1131 list.add(selectedStackList.remove(0));
1132 --maxNum;
1133 } else {
1134 break;
1135 }
1136 }
Craig Mautner20e72272013-04-01 13:45:53 -07001137 }
1138
Todd Kennedy7440f172015-12-09 14:31:22 -08001139 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1140 ProfilerInfo profilerInfo) {
1141 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142 if (aInfo != null) {
1143 // Store the found target back into the intent, because now that
1144 // we have it we never want to do this again. For example, if the
1145 // user navigates back to this point in the history, we should
1146 // always restart the exact same activity.
1147 intent.setComponent(new ComponentName(
1148 aInfo.applicationInfo.packageName, aInfo.name));
1149
1150 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001151 if (!aInfo.processName.equals("system")) {
1152 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001153 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001154 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001155
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001156 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1157 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1158 }
1159
Man Caocfa78b22015-06-11 20:14:34 -07001160 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1161 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1162 }
1163
1164 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001165 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166 }
1167 }
1168 }
1169 return aInfo;
1170 }
1171
Todd Kennedy7440f172015-12-09 14:31:22 -08001172 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001173 return resolveIntent(intent, resolvedType, userId, 0);
1174 }
1175
1176 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001177 try {
1178 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001179 PackageManager.MATCH_DEFAULT_ONLY | flags
1180 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001181 } catch (RemoteException e) {
1182 }
1183 return null;
1184 }
1185
1186 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1187 ProfilerInfo profilerInfo, int userId) {
1188 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1189 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1190 }
1191
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001192 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1193 boolean andResume, boolean checkConfig) throws RemoteException {
1194
1195 if (!allPausedActivitiesComplete()) {
1196 // While there are activities pausing we skipping starting any new activities until
1197 // pauses are complete. NOTE: that we also do this for activities that are starting in
1198 // the paused state because they will first be resumed then paused on the client side.
1199 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1200 "realStartActivityLocked: Skipping start of r=" + r
1201 + " some activities pausing...");
1202 return false;
1203 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001204
Craig Mautner2568c3a2015-03-26 14:22:34 -07001205 if (andResume) {
1206 r.startFreezingScreenLocked(app, 0);
1207 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001208
Craig Mautner2568c3a2015-03-26 14:22:34 -07001209 // schedule launch ticks to collect information about slow apps.
1210 r.startLaunchTickingLocked();
1211 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001212
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001213 // Have the window manager re-evaluate the orientation of the screen based on the new
1214 // activity order. Note that as a result of this, it can call back into the activity
1215 // manager with a new orientation. We don't care about that, because the activity is not
1216 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001218 final int displayId = r.getDisplayId();
1219 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1220 getDisplayOverrideConfiguration(displayId),
1221 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001222 // Deferring resume here because we're going to launch new activity shortly.
1223 // We don't want to perform a redundant launch of the same record while ensuring
1224 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001225 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1226 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 }
1228
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001229 if (mKeyguardController.isKeyguardLocked()) {
1230 mWindowManager.notifyUnknownAppVisibilityLaunched(r.appToken);
1231 }
1232
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 r.app = app;
1234 app.waitingToKill = null;
1235 r.launchCount++;
1236 r.lastLaunchTime = SystemClock.uptimeMillis();
1237
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001238 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
1240 int idx = app.activities.indexOf(r);
1241 if (idx < 0) {
1242 app.activities.add(r);
1243 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001244 mService.updateLruProcessLocked(app, true, null);
1245 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001246
Craig Mautner15df08a2015-04-01 12:17:18 -07001247 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001248 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1249 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001250 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001251 }
1252
Andrii Kulian02b7a832016-10-06 23:11:56 -07001253 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 try {
1255 if (app.thread == null) {
1256 throw new RemoteException();
1257 }
1258 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001259 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 if (andResume) {
1261 results = r.results;
1262 newIntents = r.newIntents;
1263 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001264 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1265 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1266 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 if (andResume) {
1268 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1269 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001270 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001272 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001273 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001274 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001276 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1277 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 r.sleeping = false;
1279 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001280 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 mService.showAskCompatModeDialogLocked(r);
1282 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001283 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1285 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1286 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001287 final String profileFile = mService.mProfileFile;
1288 if (profileFile != null) {
1289 ParcelFileDescriptor profileFd = mService.mProfileFd;
1290 if (profileFd != null) {
1291 try {
1292 profileFd = profileFd.dup();
1293 } catch (IOException e) {
1294 if (profileFd != null) {
1295 try {
1296 profileFd.close();
1297 } catch (IOException o) {
1298 }
1299 profileFd = null;
1300 }
1301 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001303
1304 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1305 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 }
1307 }
1308 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001309
Craig Mautner2568c3a2015-03-26 14:22:34 -07001310 if (andResume) {
1311 app.hasShownUi = true;
1312 app.pendingUiClean = true;
1313 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001314 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001315 // Because we could be starting an Activity in the system process this may not go across
1316 // a Binder interface which would create a new Configuration. Consequently we have to
1317 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001318 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001319 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001320 new Configuration(mService.getGlobalConfiguration()),
1321 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1322 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1323 r.persistentState, results, newIntents, !andResume,
1324 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001325
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001326 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 // This may be a heavy-weight process! Note that the package
1328 // manager will ensure that only activity can run in the main
1329 // process of the .apk, which is the only thing that will be
1330 // considered heavy-weight.
1331 if (app.processName.equals(app.info.packageName)) {
1332 if (mService.mHeavyWeightProcess != null
1333 && mService.mHeavyWeightProcess != app) {
1334 Slog.w(TAG, "Starting new heavy weight process " + app
1335 + " when already running "
1336 + mService.mHeavyWeightProcess);
1337 }
1338 mService.mHeavyWeightProcess = app;
1339 Message msg = mService.mHandler.obtainMessage(
1340 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1341 msg.obj = r;
1342 mService.mHandler.sendMessage(msg);
1343 }
1344 }
1345
1346 } catch (RemoteException e) {
1347 if (r.launchFailed) {
1348 // This is the second time we failed -- finish activity
1349 // and give up.
1350 Slog.e(TAG, "Second failure launching "
1351 + r.intent.getComponent().flattenToShortString()
1352 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001353 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001354 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1355 "2nd-crash", false);
1356 return false;
1357 }
1358
1359 // This is the first time we failed -- restart process and
1360 // retry.
1361 app.activities.remove(r);
1362 throw e;
1363 }
1364
1365 r.launchFailed = false;
1366 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001367 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001368 }
1369
1370 if (andResume) {
1371 // As part of the process of launching, ActivityThread also performs
1372 // a resume.
1373 stack.minimalResumeActivityLocked(r);
1374 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001375 // This activity is not starting in the resumed state... which should look like we asked
1376 // it to pause+stop (but remain visible), and it has done so and reported back the
1377 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001378 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001379 "Moving to PAUSED: " + r + " (starting in paused state)");
1380 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001381 }
1382
1383 // Launch the new version setup screen if needed. We do this -after-
1384 // launching the initial activity (that is, home), so that it can have
1385 // a chance to initialize itself while in the background, making the
1386 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001387 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001388 mService.startSetupActivityLocked();
1389 }
1390
Dianne Hackborn465fa392014-09-14 14:21:18 -07001391 // Update any services we are bound to that might care about whether
1392 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001393 if (r.app != null) {
1394 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1395 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001396
Craig Mautner2420ead2013-04-01 17:13:20 -07001397 return true;
1398 }
1399
Craig Mautnere79d42682013-04-01 19:01:53 -07001400 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001401 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001402 // Is this activity's application already running?
1403 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001404 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001405
Andrii Kulian02b7a832016-10-06 23:11:56 -07001406 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001407
1408 if (app != null && app.thread != null) {
1409 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001410 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1411 || !"android".equals(r.info.packageName)) {
1412 // Don't add this if it is a platform component that is marked
1413 // to run in multiple processes, because this is actually
1414 // part of the framework so doesn't make sense to track as a
1415 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001416 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1417 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001418 }
George Mount2c92c972014-03-20 09:38:23 -07001419 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001420 return;
1421 } catch (RemoteException e) {
1422 Slog.w(TAG, "Exception when starting activity "
1423 + r.intent.getComponent().flattenToShortString(), e);
1424 }
1425
1426 // If a dead object exception was thrown -- fall through to
1427 // restart the application.
1428 }
1429
1430 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001431 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001432 }
1433
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001434 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001435 String resultWho, int requestCode, int callingPid, int callingUid,
1436 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001437 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001438 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1439 callingUid);
1440 if (startAnyPerm == PERMISSION_GRANTED) {
1441 return true;
1442 }
1443 final int componentRestriction = getComponentRestrictionForCallingPackage(
1444 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1445 final int actionRestriction = getActionRestrictionForCallingPackage(
1446 intent.getAction(), callingPackage, callingPid, callingUid);
1447 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1448 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1449 if (resultRecord != null) {
1450 resultStack.sendActivityResultLocked(-1,
1451 resultRecord, resultWho, requestCode,
1452 Activity.RESULT_CANCELED, null);
1453 }
1454 final String msg;
1455 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1456 msg = "Permission Denial: starting " + intent.toString()
1457 + " from " + callerApp + " (pid=" + callingPid
1458 + ", uid=" + callingUid + ")" + " with revoked permission "
1459 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1460 } else if (!aInfo.exported) {
1461 msg = "Permission Denial: starting " + intent.toString()
1462 + " from " + callerApp + " (pid=" + callingPid
1463 + ", uid=" + callingUid + ")"
1464 + " not exported from uid " + aInfo.applicationInfo.uid;
1465 } else {
1466 msg = "Permission Denial: starting " + intent.toString()
1467 + " from " + callerApp + " (pid=" + callingPid
1468 + ", uid=" + callingUid + ")"
1469 + " requires " + aInfo.permission;
1470 }
1471 Slog.w(TAG, msg);
1472 throw new SecurityException(msg);
1473 }
1474
1475 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1476 final String message = "Appop Denial: starting " + intent.toString()
1477 + " from " + callerApp + " (pid=" + callingPid
1478 + ", uid=" + callingUid + ")"
1479 + " requires " + AppOpsManager.permissionToOp(
1480 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1481 Slog.w(TAG, message);
1482 return false;
1483 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1484 final String message = "Appop Denial: starting " + intent.toString()
1485 + " from " + callerApp + " (pid=" + callingPid
1486 + ", uid=" + callingUid + ")"
1487 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1488 Slog.w(TAG, message);
1489 return false;
1490 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001491 if (options != null) {
1492 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1493 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1494 callingPid, callingUid);
1495 if (startInTaskPerm != PERMISSION_GRANTED) {
1496 final String msg = "Permission Denial: starting " + intent.toString()
1497 + " from " + callerApp + " (pid=" + callingPid
1498 + ", uid=" + callingUid + ") with launchTaskId="
1499 + options.getLaunchTaskId();
1500 Slog.w(TAG, msg);
1501 throw new SecurityException(msg);
1502 }
1503 }
1504 // Check if someone tries to launch an activity on a private display with a different
1505 // owner.
1506 final int launchDisplayId = options.getLaunchDisplayId();
1507 if (launchDisplayId != INVALID_DISPLAY) {
1508 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1509 if (activityDisplay != null
1510 && (activityDisplay.mDisplay.getFlags() & FLAG_PRIVATE) != 0) {
1511 if (activityDisplay.mDisplay.getOwnerUid() != callingUid) {
1512 final String msg = "Permission Denial: starting " + intent.toString()
1513 + " from " + callerApp + " (pid=" + callingPid
1514 + ", uid=" + callingUid + ") with launchDisplayId="
1515 + launchDisplayId;
1516 Slog.w(TAG, msg);
1517 throw new SecurityException(msg);
1518 }
1519 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001520 }
1521 }
1522
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001523 return true;
1524 }
1525
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001526 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001527 final long identity = Binder.clearCallingIdentity();
1528 try {
1529 return UserManager.get(mService.mContext).getUserInfo(userId);
1530 } finally {
1531 Binder.restoreCallingIdentity(identity);
1532 }
1533 }
1534
Svet Ganov99b60432015-06-27 13:15:22 -07001535 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001536 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001537 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1538 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001539 == PackageManager.PERMISSION_DENIED) {
1540 return ACTIVITY_RESTRICTION_PERMISSION;
1541 }
1542
Christopher Tateff7add02015-08-17 10:23:22 -07001543 if (activityInfo.permission == null) {
1544 return ACTIVITY_RESTRICTION_NONE;
1545 }
1546
Svet Ganov99b60432015-06-27 13:15:22 -07001547 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1548 if (opCode == AppOpsManager.OP_NONE) {
1549 return ACTIVITY_RESTRICTION_NONE;
1550 }
1551
1552 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1553 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001554 if (!ignoreTargetSecurity) {
1555 return ACTIVITY_RESTRICTION_APPOP;
1556 }
Svet Ganov99b60432015-06-27 13:15:22 -07001557 }
1558
1559 return ACTIVITY_RESTRICTION_NONE;
1560 }
1561
Svetoslav7008b512015-06-24 18:47:07 -07001562 private int getActionRestrictionForCallingPackage(String action,
1563 String callingPackage, int callingPid, int callingUid) {
1564 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001565 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001566 }
1567
1568 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1569 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001570 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001571 }
1572
1573 final PackageInfo packageInfo;
1574 try {
1575 packageInfo = mService.mContext.getPackageManager()
1576 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1577 } catch (PackageManager.NameNotFoundException e) {
1578 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001579 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001580 }
1581
1582 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001583 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001584 }
1585
1586 if (mService.checkPermission(permission, callingPid, callingUid) ==
1587 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001588 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001589 }
1590
1591 final int opCode = AppOpsManager.permissionToOpCode(permission);
1592 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001593 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001594 }
1595
1596 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1597 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001598 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001599 }
1600
Svet Ganov99b60432015-06-27 13:15:22 -07001601 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001602 }
1603
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001604 void setLaunchSource(int uid) {
1605 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1606 }
1607
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001608 void acquireLaunchWakelock() {
1609 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1610 throw new IllegalStateException("Calling must be system uid");
1611 }
1612 mLaunchingActivity.acquire();
1613 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1614 // To be safe, don't allow the wake lock to be held for too long.
1615 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1616 }
1617 }
1618
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001619 /**
1620 * Called when the frontmost task is idle.
1621 * @return the state of mService.mBooting before this was called.
1622 */
1623 private boolean checkFinishBootingLocked() {
1624 final boolean booting = mService.mBooting;
1625 boolean enableScreen = false;
1626 mService.mBooting = false;
1627 if (!mService.mBooted) {
1628 mService.mBooted = true;
1629 enableScreen = true;
1630 }
1631 if (booting || enableScreen) {
1632 mService.postFinishBooting(booting, enableScreen);
1633 }
1634 return booting;
1635 }
1636
Craig Mautnerf3333272013-04-22 10:55:53 -07001637 // Checked.
1638 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1639 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001640 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001641
Craig Mautnerf3333272013-04-22 10:55:53 -07001642 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001643 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001644 int NS = 0;
1645 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001646 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001647 boolean activityRemoved = false;
1648
Wale Ogunwale7d701172015-03-11 15:36:30 -07001649 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001650 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001651 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1652 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001653 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1654 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001655 if (fromTimeout) {
1656 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001657 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001658
1659 // This is a hack to semi-deal with a race condition
1660 // in the client where it can be constructed with a
1661 // newer configuration from when we asked it to launch.
1662 // We'll update with whatever configuration it now says
1663 // it used to launch.
1664 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001665 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001666 }
1667
1668 // We are now idle. If someone is waiting for a thumbnail from
1669 // us, we can now deliver.
1670 r.idle = true;
1671
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001672 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001673 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001674 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001675 }
1676 }
1677
1678 if (allResumedActivitiesIdle()) {
1679 if (r != null) {
1680 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001681 }
1682
1683 if (mLaunchingActivity.isHeld()) {
1684 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1685 if (VALIDATE_WAKE_LOCK_CALLER &&
1686 Binder.getCallingUid() != Process.myUid()) {
1687 throw new IllegalStateException("Calling must be system uid");
1688 }
1689 mLaunchingActivity.release();
1690 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001691 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001692 }
1693
1694 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001695 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001696 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001697 if ((NF = mFinishingActivities.size()) > 0) {
1698 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001699 mFinishingActivities.clear();
1700 }
1701
Craig Mautnerf3333272013-04-22 10:55:53 -07001702 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001703 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001704 mStartingUsers.clear();
1705 }
1706
Craig Mautnerf3333272013-04-22 10:55:53 -07001707 // Stop any activities that are scheduled to do so but have been
1708 // waiting for the next one to start.
1709 for (int i = 0; i < NS; i++) {
1710 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001711 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001712 if (stack != null) {
1713 if (r.finishing) {
1714 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1715 } else {
1716 stack.stopActivityLocked(r);
1717 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001718 }
1719 }
1720
1721 // Finish any activities that are scheduled to do so but have been
1722 // waiting for the next one to start.
1723 for (int i = 0; i < NF; i++) {
1724 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001725 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001726 if (stack != null) {
1727 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1728 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001729 }
1730
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001731 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001732 // Complete user switch
1733 if (startingUsers != null) {
1734 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001735 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001736 }
1737 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001738 }
1739
1740 mService.trimApplications();
1741 //dump();
1742 //mWindowManager.dump();
1743
Craig Mautnerf3333272013-04-22 10:55:53 -07001744 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001745 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001746 }
1747
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001748 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001749 }
1750
Craig Mautner8e569572013-10-11 17:36:59 -07001751 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001752 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1756 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1757 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001758 }
Craig Mautner19091252013-10-05 00:03:53 -07001759 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001760 }
1761
1762 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1764 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1766 stacks.get(stackNdx).closeSystemDialogsLocked();
1767 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001768 }
1769 }
1770
Craig Mautner93529a42013-10-04 15:03:13 -07001771 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001772 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001773 }
1774
Craig Mautner8d341ef2013-03-26 09:03:27 -07001775 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001776 * Update the last used stack id for non-current user (current user's last
1777 * used stack is the focused stack)
1778 */
1779 void updateUserStackLocked(int userId, ActivityStack stack) {
1780 if (userId != mCurrentUser) {
1781 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1782 }
1783 }
1784
1785 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001786 * @return true if some activity was finished (or would have finished if doit were true).
1787 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001788 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1789 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001790 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001791 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1792 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001793 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001794 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001795 if (stack.finishDisabledPackageActivitiesLocked(
1796 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001797 didSomething = true;
1798 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001799 }
1800 }
1801 return didSomething;
1802 }
1803
Dianne Hackborna413dc02013-07-12 12:02:55 -07001804 void updatePreviousProcessLocked(ActivityRecord r) {
1805 // Now that this process has stopped, we may want to consider
1806 // it to be the previous app to try to keep around in case
1807 // the user wants to return to it.
1808
1809 // First, found out what is currently the foreground app, so that
1810 // we don't blow away the previous app if this activity is being
1811 // hosted by the process that is actually still the foreground.
1812 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1814 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001815 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1816 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001817 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001818 if (stack.mResumedActivity != null) {
1819 fgApp = stack.mResumedActivity.app;
1820 } else if (stack.mPausingActivity != null) {
1821 fgApp = stack.mPausingActivity.app;
1822 }
1823 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001824 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001825 }
1826 }
1827
1828 // Now set this one as the previous process, only if that really
1829 // makes sense to.
1830 if (r.app != null && fgApp != null && r.app != fgApp
1831 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001832 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001833 mService.mPreviousProcess = r.app;
1834 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1835 }
1836 }
1837
Wale Ogunwaled046a012015-12-24 13:05:59 -08001838 boolean resumeFocusedStackTopActivityLocked() {
1839 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001840 }
1841
Wale Ogunwaled046a012015-12-24 13:05:59 -08001842 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001843 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001844 if (targetStack != null && isFocusedStack(targetStack)) {
1845 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001846 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001847 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1848 if (r == null || r.state != RESUMED) {
1849 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001850 } else if (r.state == RESUMED) {
1851 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1852 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001853 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001854 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001855 }
1856
Todd Kennedy39bfee52016-02-24 10:28:21 -08001857 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1858 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1859 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1860 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1861 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1862 }
1863 }
1864 }
1865
Adrian Roos20d7df32016-01-12 18:59:43 +01001866 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1867 TaskRecord finishedTask = null;
1868 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001869 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1870 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001871 final int numStacks = stacks.size();
1872 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1873 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001874 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1875 if (stack == focusedStack || finishedTask == null) {
1876 finishedTask = t;
1877 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001878 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001879 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001880 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001881 }
1882
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001883 void finishVoiceTask(IVoiceInteractionSession session) {
1884 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1885 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1886 final int numStacks = stacks.size();
1887 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1888 final ActivityStack stack = stacks.get(stackNdx);
1889 stack.finishVoiceTask(session);
1890 }
1891 }
1892 }
1893
Andrii Kulianc27916642016-04-12 17:59:27 -07001894 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1895 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001896 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1897 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001898 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001899 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1900 // Caller wants the home activity moved with it. To accomplish this,
1901 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001902 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001903 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001904 ActivityStack currentStack = task.getStack();
1905 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001906 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1907 + task + " to front. Stack is null");
1908 return;
1909 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001910
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001911 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001912 int stackId = options.getLaunchStackId();
1913 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001914 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001915 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001916 if (stackId == INVALID_STACK_ID) {
1917 stackId = task.getLaunchStackId();
1918 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001919 if (stackId != currentStack.mStackId) {
1920 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1921 !FORCE_FOCUS, reason);
1922 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001923 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1924 // still need moveTaskToFrontLocked() below for any transition settings.
1925 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001926 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1927 resizeStackLocked(stackId, bounds,
1928 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001929 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001930 } else {
1931 // WM resizeTask must be done after the task is moved to the correct stack,
1932 // because Task's setBounds() also updates dim layer's bounds, but that has
1933 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001934 mWindowManager.resizeTask(task.taskId, task.mBounds,
1935 task.getOverrideConfiguration(), false /* relayout */,
1936 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001937 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001938 }
1939 }
1940
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001941 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001942 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001943 r == null ? null : r.appTimeTracker, reason);
1944
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001945 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001946 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001947
Andrii Kulian02b7a832016-10-06 23:11:56 -07001948 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001949 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001950 }
1951
Wale Ogunwale854809c2015-12-27 16:18:19 -08001952 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001953 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001954 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001955 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001956 return false;
1957 }
1958 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1959 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001960 }
1961
Craig Mautner967212c2013-04-13 21:10:58 -07001962 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001963 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001964 }
1965
1966 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001967 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001968 if (activityContainer != null) {
1969 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001970 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001971 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001972 return null;
1973 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001974 // TODO(multi-display): Allow creating stacks on secondary displays.
Jorim Jaggife762342016-10-13 14:33:27 +02001975 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001976 }
1977
Andrii Kulian16802aa2016-11-02 12:21:33 -07001978 /**
1979 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
1980 * If there is no such stack, new dynamic stack can be created.
1981 * @param displayId Target display.
1982 * @param r Activity that should be launched there.
1983 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
1984 */
1985 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
1986 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
1987 if (activityDisplay == null) {
1988 throw new IllegalArgumentException(
1989 "Display with displayId=" + displayId + " not found.");
1990 }
1991
1992 // Return the topmost valid stack on the display.
1993 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
1994 final ActivityStack stack = activityDisplay.mStacks.get(i);
1995 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
1996 return stack;
1997 }
1998 }
1999
2000 // If there is no valid stack on the external display - check if new dynamic stack will do.
2001 if (displayId != Display.DEFAULT_DISPLAY) {
2002 final int newDynamicStackId = getNextStackId();
2003 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2004 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2005 }
2006 }
2007
2008 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2009 return null;
2010 }
2011
Craig Mautner967212c2013-04-13 21:10:58 -07002012 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002013 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002014 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2015 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002016 }
2017 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002018 }
2019
Jorim Jaggife762342016-10-13 14:33:27 +02002020 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2021 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2022 }
2023
Craig Mautneree2e45a2014-06-27 12:10:03 -07002024 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002025 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002026 }
2027
2028 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002029 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2030 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2031 final TaskRecord task = tasks.get(taskNdx);
2032 if (task.isHomeTask()) {
2033 final ArrayList<ActivityRecord> activities = task.mActivities;
2034 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2035 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002036 if (r.isHomeActivity()
2037 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002038 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002039 }
2040 }
2041 }
2042 }
2043 return null;
2044 }
2045
Chong Zhangb15758a2015-11-17 12:12:03 -08002046 /**
2047 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2048 * the docked stack itself, or if it's side-by-side to the docked stack.
2049 */
2050 boolean isStackDockedInEffect(int stackId) {
2051 return stackId == DOCKED_STACK_ID ||
2052 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2053 }
2054
Todd Kennedyca4d8422015-01-15 15:19:22 -08002055 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002056 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002057 ActivityContainer activityContainer =
2058 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002059 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002060 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2061 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002062 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002063 return activityContainer;
2064 }
2065
Craig Mautner34b73df2014-01-12 21:11:08 -08002066 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002067 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2068 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2069 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002070 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2071 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002072 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002073 }
2074 }
2075
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002076 void deleteActivityContainerRecord(int stackId) {
2077 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2078 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2079 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002080 }
2081
Jorim Jaggidc249c42015-12-15 14:57:31 -08002082 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002083 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002084 if (stackId == DOCKED_STACK_ID) {
2085 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002086 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002087 return;
2088 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002089 final ActivityStack stack = getStack(stackId);
2090 if (stack == null) {
2091 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2092 return;
2093 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002094
Jorim Jaggidc249c42015-12-15 14:57:31 -08002095 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002096 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2097 // stack size changing so things don't get out of sync.
2098 return;
2099 }
2100
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002101 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002102 mWindowManager.deferSurfaceLayout();
2103 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002104 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002105 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002106 stack.ensureVisibleActivitiesConfigurationLocked(
2107 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002108 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002109 } finally {
2110 mWindowManager.continueSurfaceLayout();
2111 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002112 }
2113 }
2114
Jorim Jaggi192086e2016-03-11 17:17:03 +01002115 void deferUpdateBounds(int stackId) {
2116 final ActivityStack stack = getStack(stackId);
2117 if (stack != null) {
2118 stack.deferUpdateBounds();
2119 }
2120 }
2121
2122 void continueUpdateBounds(int stackId) {
2123 final ActivityStack stack = getStack(stackId);
2124 if (stack != null) {
2125 stack.continueUpdateBounds();
2126 }
2127 }
2128
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002129 void notifyAppTransitionDone() {
2130 continueUpdateBounds(HOME_STACK_ID);
2131 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2132 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002133 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002134 mWindowManager.setTaskDockedResizing(taskId, false);
2135 }
2136 }
2137 mResizingTasksDuringAnimation.clear();
2138 }
2139
Jorim Jaggi192086e2016-03-11 17:17:03 +01002140 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002141 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002142 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002143
Jorim Jaggi192086e2016-03-11 17:17:03 +01002144 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2145 return;
2146 }
2147
Andrii Kulian1e32e022016-09-16 15:29:34 -07002148 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002149 }
2150
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002151 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2152 final ActivityStack stack = getStack(fromStackId);
2153 if (stack == null) {
2154 return;
2155 }
2156
2157 mWindowManager.deferSurfaceLayout();
2158 try {
2159 if (fromStackId == DOCKED_STACK_ID) {
2160
2161 // We are moving all tasks from the docked stack to the fullscreen stack,
2162 // which is dismissing the docked stack, so resize all other stacks to
2163 // fullscreen here already so we don't end up with resize trashing.
2164 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2165 if (StackId.isResizeableByDockedStack(i)) {
2166 ActivityStack otherStack = getStack(i);
2167 if (otherStack != null) {
2168 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2169 true /* allowResizeInDockedMode */, DEFER_RESUME);
2170 }
2171 }
2172 }
2173
2174 // Also disable docked stack resizing since we have manually adjusted the
2175 // size of other stacks above and we don't want to trigger a docked stack
2176 // resize when we remove task from it below and it is detached from the
2177 // display because it no longer contains any tasks.
2178 mAllowDockedStackResize = false;
2179 }
2180 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2181 final int size = tasks.size();
2182 if (onTop) {
2183 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002184 final TaskRecord task = tasks.get(i);
2185 if (fromStackId == PINNED_STACK_ID) {
2186 // Update the return-to to reflect where the pinned stack task was moved
2187 // from so that we retain the stack that was previously visible if the
2188 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chung010927a2016-12-15 16:12:35 -08002189 final int focusedStackId = getFocusedStack().getStackId();
2190 task.setTaskToReturnTo(focusedStackId == HOME_STACK_ID || !onTop
Winson Chung0c1ca092016-11-10 17:40:34 -08002191 ? HOME_ACTIVITY_TYPE : APPLICATION_ACTIVITY_TYPE);
2192 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002193 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002194 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002195 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002196 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002197
2198 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2199 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002200 } else {
2201 for (int i = size - 1; i >= 0; i--) {
2202 positionTaskInStackLocked(tasks.get(i).taskId,
2203 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2204 }
2205 }
2206 } finally {
2207 mAllowDockedStackResize = true;
2208 mWindowManager.continueSurfaceLayout();
2209 }
2210 }
2211
Jorim Jaggidc249c42015-12-15 14:57:31 -08002212 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002213 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2214 boolean preserveWindows) {
2215 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2216 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2217 false /* deferResume */);
2218 }
2219
2220 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2221 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2222 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002223
2224 if (!mAllowDockedStackResize) {
2225 // Docked stack resize currently disabled.
2226 return;
2227 }
2228
Jorim Jaggidc249c42015-12-15 14:57:31 -08002229 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2230 if (stack == null) {
2231 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2232 return;
2233 }
2234
2235 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2236 mWindowManager.deferSurfaceLayout();
2237 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002238 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2239 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002240 ActivityRecord r = stack.topRunningActivityLocked();
2241 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2242 tempDockedTaskInsetBounds);
2243
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002244 // TODO: Checking for isAttached might not be needed as if the user passes in null
2245 // dockedBounds then they want the docked stack to be dismissed.
2246 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2247 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002248 // In this case we make all other static stacks fullscreen and move all
2249 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002250 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002251
2252 // stack shouldn't contain anymore activities, so nothing to resume.
2253 r = null;
2254 } else {
2255 // Docked stacks occupy a dedicated region on screen so the size of all other
2256 // static stacks need to be adjusted so they don't overlap with the docked stack.
2257 // We get the bounds to use from window manager which has been adjusted for any
2258 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002259 mWindowManager.getStackDockedModeBounds(
2260 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002261 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002262 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2263 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2264 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002265 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002266 }
2267 }
2268 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002269 if (!deferResume) {
2270 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2271 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002272 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002273 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002274 mWindowManager.continueSurfaceLayout();
2275 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2276 }
2277
2278 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2279 tempDockedTaskBounds != null
2280 || tempDockedTaskInsetBounds != null
2281 || tempOtherTaskBounds != null
2282 || tempOtherTaskInsetBounds != null);
2283 }
2284
Robert Carr0d00c2e2016-02-29 17:45:02 -08002285 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2286 final ActivityStack stack = getStack(PINNED_STACK_ID);
2287 if (stack == null) {
2288 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2289 return;
2290 }
2291 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2292 mWindowManager.deferSurfaceLayout();
2293 try {
2294 ActivityRecord r = stack.topRunningActivityLocked();
2295 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2296 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002297 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002298 } finally {
2299 mWindowManager.continueSurfaceLayout();
2300 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2301 }
2302 }
2303
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002304 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2305 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002306 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002307 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002308 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002309 }
2310
Chong Zhang87b21722015-09-21 15:39:51 -07002311 // If this is a forced resize, let it go through even if the bounds is not changing,
2312 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002313 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002314 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002315 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002316 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002317 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002318 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002319
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002320 if (!mWindowManager.isValidTaskId(task.taskId)) {
2321 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002322 // All we can do for now is update the bounds so it can be used when the task is
2323 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002324 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002325 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002326 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002327 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002328 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002329 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002330 }
2331
skuhne@google.com322347b2016-12-02 12:54:03 -08002332 if (!task.canResizeToBounds(bounds)) {
2333 throw new IllegalArgumentException("resizeTaskLocked: Can not resize task=" + task
2334 + " to bounds=" + bounds + " resizeMode=" + task.mResizeMode);
2335 }
2336
Chong Zhang6de2ae82015-09-30 18:25:21 -07002337 // Do not move the task to another stack here.
2338 // This method assumes that the task is already placed in the right stack.
2339 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002340
Chong Zhang6de2ae82015-09-30 18:25:21 -07002341 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002342
Andrii Kulian8072d112016-09-16 11:11:01 -07002343 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002344 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002345 // way and the activity was kept the way it was. If it's false, it means the activity had
2346 // to be relaunched due to configuration change.
2347 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002348 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002349 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002350 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002351 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002352
2353 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002354 // All other activities must be made visible with their correct configuration.
2355 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2356 if (!kept) {
2357 resumeFocusedStackTopActivityLocked();
2358 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002359 }
2360 }
2361 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002362 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2363 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002364
2365 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002366 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002367 }
2368
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002369 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002370 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2371 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002372 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002373 }
2374
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002375 ActivityContainer activityContainer = new ActivityContainer(stackId);
2376 mActivityContainers.put(stackId, activityContainer);
Andrii Kulian839def92016-11-02 10:58:58 -07002377 activityContainer.addToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002378 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002379 }
2380
Winson Chung010927a2016-12-15 16:12:35 -08002381 /**
2382 * Removes the stack associed with the given {@param stackId}. If the {@param stackId} is the
2383 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2384 * instead moved back onto the fullscreen stack.
2385 */
2386 void removeStackLocked(int stackId) {
2387 final ActivityStack stack = getStack(stackId);
2388 if (stack == null) {
2389 return;
2390 }
2391
2392 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2393 if (stack.getStackId() == PINNED_STACK_ID) {
2394 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2395 if (fullscreenStack != null) {
2396 final boolean isFullscreenStackVisible =
2397 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2398 for (int i = 0; i < tasks.size(); i++) {
2399 // Insert the task either at the top of the fullscreen stack if it is hidden,
2400 // or just under the top task if it is currently visible
2401 final int insertPosition = isFullscreenStackVisible
2402 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2403 : fullscreenStack.getChildCount();
2404 positionTaskInStackLocked(tasks.get(i).taskId, FULLSCREEN_WORKSPACE_STACK_ID,
2405 insertPosition);
2406 }
2407 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2408 resumeFocusedStackTopActivityLocked();
2409 } else {
2410 // If there is no fullscreen stack, then create the stack and move all the tasks
2411 // onto the stack
2412 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, false /* onTop */);
2413 }
2414 } else {
2415 for (int i = tasks.size() - 1; i >= 0; i--) {
2416 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2417 REMOVE_FROM_RECENTS);
2418 }
2419 }
2420 }
2421
2422 /**
2423 * Removes the task with the specified task id.
2424 *
2425 * @param taskId Identifier of the task to be removed.
2426 * @param killProcess Kill any process associated with the task if possible.
2427 * @param removeFromRecents Whether to also remove the task from recents.
2428 * @return Returns true if the given task was found and removed.
2429 */
2430 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2431 final TaskRecord tr = anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID);
2432 if (tr != null) {
2433 tr.removeTaskActivitiesLocked();
2434 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2435 if (tr.isPersistable) {
2436 mService.notifyTaskPersisterLocked(null, true);
2437 }
2438 return true;
2439 }
2440 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2441 return false;
2442 }
2443
2444 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2445 if (removeFromRecents) {
2446 mRecentTasks.remove(tr);
2447 tr.removedFromRecents();
2448 }
2449 ComponentName component = tr.getBaseIntent().getComponent();
2450 if (component == null) {
2451 Slog.w(TAG, "No component for base intent of task: " + tr);
2452 return;
2453 }
2454
2455 // Find any running services associated with this app and stop if needed.
2456 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2457
2458 if (!killProcess) {
2459 return;
2460 }
2461
2462 // Determine if the process(es) for this task should be killed.
2463 final String pkg = component.getPackageName();
2464 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2465 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2466 for (int i = 0; i < pmap.size(); i++) {
2467
2468 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2469 for (int j = 0; j < uids.size(); j++) {
2470 ProcessRecord proc = uids.valueAt(j);
2471 if (proc.userId != tr.userId) {
2472 // Don't kill process for a different user.
2473 continue;
2474 }
2475 if (proc == mService.mHomeProcess) {
2476 // Don't kill the home process along with tasks from the same package.
2477 continue;
2478 }
2479 if (!proc.pkgList.containsKey(pkg)) {
2480 // Don't kill process that is not associated with this task.
2481 continue;
2482 }
2483
2484 for (int k = 0; k < proc.activities.size(); k++) {
2485 TaskRecord otherTask = proc.activities.get(k).task;
2486 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2487 // Don't kill process(es) that has an activity in a different task that is
2488 // also in recents.
2489 return;
2490 }
2491 }
2492
2493 if (proc.foregroundServices) {
2494 // Don't kill process(es) with foreground service.
2495 return;
2496 }
2497
2498 // Add process to kill list.
2499 procsToKill.add(proc);
2500 }
2501 }
2502
2503 // Kill the running processes.
2504 for (int i = 0; i < procsToKill.size(); i++) {
2505 ProcessRecord pr = procsToKill.get(i);
2506 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2507 && pr.curReceivers.isEmpty()) {
2508 pr.kill("remove task", true);
2509 } else {
2510 // We delay killing processes that are not in the background or running a receiver.
2511 pr.waitingToKill = "remove task";
2512 }
2513 }
2514 }
2515
Craig Mautner4a1cb222013-12-04 16:14:06 -08002516 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002517 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002518 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2519 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002520 break;
2521 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002522 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002523 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002524 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002525 }
2526
Chong Zhang5dcb2752015-08-18 13:50:26 -07002527 /**
2528 * Restores a recent task to a stack
2529 * @param task The recent task to be restored.
2530 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002531 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2532 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002533 * @return true if the task has been restored successfully.
2534 */
2535 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002536 if (!StackId.isStaticStack(stackId)) {
2537 // If stack is not static (or stack id is invalid) - use the default one.
2538 // This means that tasks that were on external displays will be restored on the
2539 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002540 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002541 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2542 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2543 // Put it in the fullscreen stack.
2544 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002545 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002546
Andrii Kulian02b7a832016-10-06 23:11:56 -07002547 final ActivityStack currentStack = task.getStack();
2548 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002549 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002550 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002551 // Nothing else to do since it is already restored in the right stack.
2552 return true;
2553 }
2554 // Remove current stack association, so we can re-associate the task with the
2555 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002556 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002557 }
2558
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002559 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002560 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002561
2562 if (stack == null) {
2563 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002564 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2565 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002566 return false;
2567 }
2568
Wale Ogunwale72919d22016-12-08 18:58:50 -08002569 stack.addTask(task, false /* toTop */, "restoreRecentTask");
2570 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
2571 mWindowManager.addTask(task.taskId, stack.mStackId, task.userId, bounds,
2572 task.getOverrideConfiguration(), task.mResizeMode, task.isHomeTask(),
2573 task.isOnTopLauncher(), false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002574 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2575 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002576 final ArrayList<ActivityRecord> activities = task.mActivities;
2577 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002578 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002579 }
2580 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002581 }
2582
Wale Ogunwale040b4702015-08-06 18:10:50 -07002583 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002584 * Move stack with all its existing content to specified display.
2585 * @param stackId Id of stack to move.
2586 * @param displayId Id of display to move stack to.
2587 */
2588 void moveStackToDisplayLocked(int stackId, int displayId) {
2589 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2590 if (activityDisplay == null) {
2591 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2592 + displayId);
2593 }
2594 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2595 if (activityContainer != null) {
2596 if (activityContainer.isAttachedLocked()) {
2597 if (activityContainer.getDisplayId() == displayId) {
2598 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2599 + " to its current displayId=" + displayId);
2600 }
2601
2602 activityContainer.moveToDisplayLocked(activityDisplay);
2603 } else {
2604 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2605 + stackId + " is not attached to any display.");
2606 }
2607 } else {
2608 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2609 + stackId);
2610 }
2611
2612 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2613 !PRESERVE_WINDOWS);
2614 // TODO(multi-display): resize stacks properly if moved from split-screen.
2615 }
2616
2617 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002618 * Moves the specified task record to the input stack id.
2619 * WARNING: This method performs an unchecked/raw move of the task and
2620 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002621 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002622 * @param task Task to move.
2623 * @param stackId Id of stack to move task to.
2624 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002625 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002626 * @param reason Reason the task is been moved.
2627 * @return The stack the task was moved to.
2628 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002629 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002630 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002631
2632 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2633 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2634 + "support multi-window task=" + task + " to stackId=" + stackId);
2635 }
2636
Wale Ogunwale06579d62016-04-30 15:29:06 -07002637 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002638 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002639 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002640 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002641 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2642 // Whenever we are moving the top activity from the front stack we want to make sure to move
2643 // the stack to the front.
2644 final boolean wasFront = isFrontStack(prevStack)
2645 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002646
Chong Zhangd545dce2016-02-29 18:09:17 -08002647 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002648 // We don't allow moving a unresizeable task to the docked stack since the docked
2649 // stack is used for split-screen mode and will cause things like the docked divider to
2650 // show up. We instead leave the task in its current stack or move it to the fullscreen
2651 // stack if it isn't currently in a stack.
2652 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002653 Slog.w(TAG, "Can not move unresizeable task=" + task
2654 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2655 }
2656
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002657 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2658 // if a docked stack is created below which will lead to the stack we are moving from and
2659 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002660 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002661 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002662 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002663 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002664 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002665
2666 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002667 // move focus to the new stack by moving the stack to the front.
2668 stack.moveToFrontAndResumeStateIfNeeded(
2669 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002670
Wale Ogunwale040b4702015-08-06 18:10:50 -07002671 return stack;
2672 }
2673
Chong Zhange4fbd322016-03-01 14:44:03 -08002674 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002675 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002676 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2677 false /* deferResume */);
2678 }
2679
2680 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2681 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002682 final TaskRecord task = anyTaskForIdLocked(taskId);
2683 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002684 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002685 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002686 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002687
Andrii Kulian02b7a832016-10-06 23:11:56 -07002688 final ActivityStack currentStack = task.getStack();
2689 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002690 // You are already in the right stack silly...
2691 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002692 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002693 }
2694
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002695 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2696 throw new IllegalArgumentException("moveTaskToStack:"
2697 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2698 }
2699
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002700 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002701 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002702 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002703 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002704 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002705 // We are about to relaunch the activity because its configuration changed due to
2706 // being maximized, i.e. size change. The activity will first remove the old window
2707 // and then add a new one. This call will tell window manager about this, so it can
2708 // preserve the old window until the new one is drawn. This prevents having a gap
2709 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002710 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002711 // Note here we always set the replacing window first, as the flags might be needed
2712 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002713 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002714 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002715
2716 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002717 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002718 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002719 try {
2720 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002721 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002722 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002723
Wale Ogunwale961f4852016-02-01 20:25:54 -08002724 if (!animate) {
2725 stack.mNoAnimActivities.add(topActivity);
2726 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002727
Wale Ogunwale961f4852016-02-01 20:25:54 -08002728 // We might trigger a configuration change. Save the current task bounds for freezing.
2729 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2730
2731 // Make sure the task has the appropriate bounds/size for the stack it is in.
2732 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002733 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2734 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002735 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2736 Rect bounds = task.getLaunchBounds();
2737 if (bounds == null) {
2738 stack.layoutTaskInStack(task, null);
2739 bounds = task.mBounds;
2740 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002741 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2742 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002743 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002744 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2745 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002746 }
2747 } finally {
2748 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002749 }
2750
2751 if (mightReplaceWindow) {
2752 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2753 // window), we need to clear the replace window settings. Otherwise, we schedule a
2754 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002755 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002756 }
2757
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002758 if (!deferResume) {
2759
2760 // The task might have already been running and its visibility needs to be synchronized with
2761 // the visibility of the stack / windows.
2762 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2763 resumeFocusedStackTopActivityLocked();
2764 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002765
Jorim Jaggid53f0922016-04-06 22:16:23 -07002766 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002767
2768 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002769 }
2770
Wale Ogunwale079a0042015-10-24 11:44:07 -07002771 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2772 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2773 if (stack == null) {
2774 throw new IllegalArgumentException(
2775 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2776 }
2777
2778 final ActivityRecord r = stack.topRunningActivityLocked();
2779 if (r == null) {
2780 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2781 + " in stack=" + stack);
2782 return false;
2783 }
2784
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002785 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002786 Slog.w(TAG,
2787 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002788 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002789 return false;
2790 }
2791
Winson Chungb5c41b72016-12-07 15:00:47 -08002792 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds,
2793 true /* moveHomeStackToFront */);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002794 return true;
2795 }
2796
Winson Chungb5c41b72016-12-07 15:00:47 -08002797 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds,
2798 boolean moveHomeStackToFront) {
Wale Ogunwale480dca02016-02-06 13:58:29 -08002799 mWindowManager.deferSurfaceLayout();
2800 try {
2801 final TaskRecord task = r.task;
2802
Andrii Kulian02b7a832016-10-06 23:11:56 -07002803 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002804 // An activity can't be pinned and visible behind at the same time. Go ahead and
2805 // release it from been visible behind before pinning.
2806 requestVisibleBehindLocked(r, false);
2807 }
2808
Wale Ogunwale480dca02016-02-06 13:58:29 -08002809 // Need to make sure the pinned stack exist so we can resize it below...
2810 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2811
2812 // Resize the pinned stack to match the current size of the task the activity we are
2813 // going to be moving is currently contained in. We do this to have the right starting
2814 // animation bounds for the pinned stack to the desired bounds the caller wants.
2815 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2816 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002817 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002818
2819 if (task.mActivities.size() == 1) {
2820 // There is only one activity in the task. So, we can just move the task over to
2821 // the stack without re-parenting the activity in a different task.
Winson Chungb5c41b72016-12-07 15:00:47 -08002822 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002823 // Move the home stack forward if the task we just moved to the pinned stack
2824 // was launched from home so home should be visible behind it.
2825 moveHomeStackToFront(reason);
2826 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002827 moveTaskToStackLocked(
2828 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2829 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002830 // There are multiple activities in the task and moving the top activity should
2831 // reveal/leave the other activities in their original task
Wale Ogunwale480dca02016-02-06 13:58:29 -08002832 stack.moveActivityToStack(r);
2833 }
2834 } finally {
2835 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002836 }
2837
Wale Ogunwale480dca02016-02-06 13:58:29 -08002838 // The task might have already been running and its visibility needs to be synchronized
2839 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002840 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002841 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002842
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002843 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002844 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002845 }
2846
Chong Zhang6cda19c2016-06-14 19:07:56 -07002847 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2848 if (r == null || !r.isFocusable()) {
2849 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2850 "moveActivityStackToFront: unfocusable r=" + r);
2851 return false;
2852 }
2853
2854 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002855 final ActivityStack stack = r.getStack();
2856 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002857 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2858 + r + " task=" + task);
2859 return false;
2860 }
2861
Chong Zhang6cda19c2016-06-14 19:07:56 -07002862 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2863 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2864 "moveActivityStackToFront: already on top, r=" + r);
2865 return false;
2866 }
2867
2868 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2869 "moveActivityStackToFront: r=" + r);
2870
2871 stack.moveToFront(reason, task);
2872 return true;
2873 }
2874
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002875 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2876 final TaskRecord task = anyTaskForIdLocked(taskId);
2877 if (task == null) {
2878 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2879 return;
2880 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002881 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2882
2883 task.updateOverrideConfigurationForStack(stack);
2884
2885 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002886 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002887 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002888 // The task might have already been running and its visibility needs to be synchronized with
2889 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002890 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002891 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002892 }
2893
Craig Mautnerac6f8432013-07-17 13:24:59 -07002894 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002895 mTmpFindTaskResult.r = null;
2896 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002897 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2899 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2901 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002902 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002903 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002904 continue;
2905 }
2906 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002907 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2908 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002909 continue;
2910 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002911 stack.findTaskLocked(r, mTmpFindTaskResult);
2912 // It is possible to have task in multiple stacks with the same root affinity.
2913 // If the match we found was based on root affinity we keep on looking to see if
2914 // there is a better match in another stack. We eventually return the match based
2915 // on root affinity if we don't find a better match.
2916 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2917 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002919 }
2920 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002921 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2922 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002923 }
2924
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002925 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2926 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002930 final ActivityRecord ar = stacks.get(stackNdx)
2931 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002932 if (ar != null) {
2933 return ar;
2934 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002935 }
2936 }
2937 return null;
2938 }
2939
Craig Mautner8d341ef2013-03-26 09:03:27 -07002940 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002941 scheduleSleepTimeout();
2942 if (!mGoingToSleep.isHeld()) {
2943 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002944 if (mLaunchingActivity.isHeld()) {
2945 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2946 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002948 mLaunchingActivity.release();
2949 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002950 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002952 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002953 }
2954
2955 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002956 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002957
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002958 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002959 final long endTime = System.currentTimeMillis() + timeout;
2960 while (true) {
2961 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2963 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002964 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2965 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2966 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002967 }
2968 if (cantShutdown) {
2969 long timeRemaining = endTime - System.currentTimeMillis();
2970 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002972 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 } catch (InterruptedException e) {
2974 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002975 } else {
2976 Slog.w(TAG, "Activity manager shutdown timed out");
2977 timedout = true;
2978 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002979 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002980 } else {
2981 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 }
2983 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002984
2985 // Force checkReadyForSleep to complete.
2986 mSleepTimeout = true;
2987 checkReadyForSleepLocked();
2988
Craig Mautner8d341ef2013-03-26 09:03:27 -07002989 return timedout;
2990 }
2991
2992 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002993 removeSleepTimeouts();
2994 if (mGoingToSleep.isHeld()) {
2995 mGoingToSleep.release();
2996 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002997 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2998 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002999 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3000 final ActivityStack stack = stacks.get(stackNdx);
3001 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003002 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003003 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003004 }
Craig Mautner5314a402013-09-26 12:40:16 -07003005 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003006 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003007 mGoingToSleepActivities.clear();
3008 }
3009
3010 void activitySleptLocked(ActivityRecord r) {
3011 mGoingToSleepActivities.remove(r);
3012 checkReadyForSleepLocked();
3013 }
3014
3015 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003016 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017 // Do not care.
3018 return;
3019 }
3020
3021 if (!mSleepTimeout) {
3022 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003023 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3024 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003025 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3026 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3027 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003028 }
3029
3030 if (mStoppingActivities.size() > 0) {
3031 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003032 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003033 + mStoppingActivities.size() + " activities");
3034 scheduleIdleLocked();
3035 dontSleep = true;
3036 }
3037
3038 if (mGoingToSleepActivities.size() > 0) {
3039 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003040 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003041 + mGoingToSleepActivities.size() + " activities");
3042 dontSleep = true;
3043 }
3044
3045 if (dontSleep) {
3046 return;
3047 }
3048 }
3049
Wei Wang65c7a152016-06-02 18:51:22 -07003050 // Send launch end powerhint before going sleep
3051 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3052
Craig Mautnere0a38842013-12-16 16:14:02 -08003053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3056 stacks.get(stackNdx).goToSleep();
3057 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003058 }
3059
3060 removeSleepTimeouts();
3061
3062 if (mGoingToSleep.isHeld()) {
3063 mGoingToSleep.release();
3064 }
3065 if (mService.mShuttingDown) {
3066 mService.notifyAll();
3067 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003068 }
3069
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003070 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003071 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003072 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003073 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003074 }
3075 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003076 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003077 mWindowManager.executeAppTransition();
3078 return true;
3079 }
3080 return false;
3081 }
3082
Craig Mautner8d341ef2013-03-26 09:03:27 -07003083 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003084 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3085 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003086 int stackNdx = stacks.size() - 1;
3087 while (stackNdx >= 0) {
3088 stacks.get(stackNdx).handleAppCrashLocked(app);
3089 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003090 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 }
3092 }
3093
Jose Lima4b6c6692014-08-12 17:41:12 -07003094 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003095 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003096 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003097 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3098 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003099 return false;
3100 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003101
3102 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3103 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3104 + " visible=" + visible + " stackId=" + stack.mStackId
3105 + " can't contain visible behind activities");
3106 return false;
3107 }
3108
Jose Lima4b6c6692014-08-12 17:41:12 -07003109 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003110 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3111 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003112
3113 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003114 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003115 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003116 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003117 return true;
3118 }
3119
3120 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003121 if (visible && top.fullscreen) {
3122 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003123 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3124 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3125 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3126 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003127 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003128 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3129 // Only the activity set as currently visible behind should actively reset its
3130 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003131 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3132 "requestVisibleBehind: returning visible=" + visible
3133 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3134 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003135 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003136 }
3137
Jose Lima4b6c6692014-08-12 17:41:12 -07003138 stack.setVisibleBehindActivity(visible ? r : null);
3139 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003140 // If there is a translucent home activity, we need to force it stop being translucent,
3141 // because we can't depend on the application to necessarily perform that operation.
3142 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003143 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003144 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003145 mService.convertFromTranslucent(next.appToken);
3146 }
3147 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003148 if (top.app != null && top.app.thread != null) {
3149 // Notify the top app of the change.
3150 try {
3151 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3152 } catch (RemoteException e) {
3153 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003154 }
3155 return true;
3156 }
3157
Craig Mautnerbb742462014-07-07 15:28:55 -07003158 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003159 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003160 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003161 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003162
3163 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003164 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003165 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003166 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Craig Mautnerbb742462014-07-07 15:28:55 -07003167 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07003168
3169 // When launching tasks behind, update the last active time of the top task after the new
3170 // task has been shown briefly
3171 final ActivityRecord top = stack.topActivity();
3172 if (top != null) {
3173 top.task.touchActiveTime();
3174 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003175 }
3176
3177 void scheduleLaunchTaskBehindComplete(IBinder token) {
3178 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3179 }
3180
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003181 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3182 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003183 mKeyguardController.beginActivityVisibilityUpdate();
3184 try {
3185 // First the front stacks. In case any are not fullscreen and are in front of home.
3186 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3187 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3188 final int topStackNdx = stacks.size() - 1;
3189 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3190 final ActivityStack stack = stacks.get(stackNdx);
3191 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3192 }
Craig Mautner580ea812013-04-25 12:58:38 -07003193 }
Jorim Jaggife762342016-10-13 14:33:27 +02003194 } finally {
3195 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003196 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003197 }
3198
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003199 void invalidateTaskLayers() {
3200 mTaskLayersChanged = true;
3201 }
3202
3203 void rankTaskLayersIfNeeded() {
3204 if (!mTaskLayersChanged) {
3205 return;
3206 }
3207 mTaskLayersChanged = false;
3208 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3209 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3210 int baseLayer = 0;
3211 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3212 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3213 }
3214 }
3215 }
3216
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003217 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3218 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3219 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3220 final int topStackNdx = stacks.size() - 1;
3221 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3222 final ActivityStack stack = stacks.get(stackNdx);
3223 stack.clearOtherAppTimeTrackers(except);
3224 }
3225 }
3226 }
3227
Craig Mautner8d341ef2013-03-26 09:03:27 -07003228 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003229 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3230 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 final int numStacks = stacks.size();
3232 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3233 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003234 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003235 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 }
3237 }
3238
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003239 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3240 // Examine all activities currently running in the process.
3241 TaskRecord firstTask = null;
3242 // Tasks is non-null only if two or more tasks are found.
3243 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003244 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3245 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003246 ActivityRecord r = app.activities.get(i);
3247 // First, if we find an activity that is in the process of being destroyed,
3248 // then we just aren't going to do anything for now; we want things to settle
3249 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003250 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003251 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003252 return;
3253 }
3254 // Don't consider any activies that are currently not in a state where they
3255 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003256 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3257 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003258 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003259 continue;
3260 }
3261 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003262 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003263 + " from " + r);
3264 if (firstTask == null) {
3265 firstTask = r.task;
3266 } else if (firstTask != r.task) {
3267 if (tasks == null) {
3268 tasks = new ArraySet<>();
3269 tasks.add(firstTask);
3270 }
3271 tasks.add(r.task);
3272 }
3273 }
3274 }
3275 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003276 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003277 return;
3278 }
3279 // If we have activities in multiple tasks that are in a position to be destroyed,
3280 // let's iterate through the tasks and release the oldest one.
3281 final int numDisplays = mActivityDisplays.size();
3282 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3283 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3284 // Step through all stacks starting from behind, to hit the oldest things first.
3285 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3286 final ActivityStack stack = stacks.get(stackNdx);
3287 // Try to release activities in this stack; if we manage to, we are done.
3288 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3289 return;
3290 }
3291 }
3292 }
3293 }
3294
Amith Yamasani37a40c22015-06-17 13:25:42 -07003295 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003296 final int focusStackId = mFocusedStack.getStackId();
3297 // We dismiss the docked stack whenever we switch users.
3298 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3299
3300 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003301 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003302 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003303
Craig Mautner858d8a62013-04-23 17:08:34 -07003304 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003305 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3306 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003307 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003308 final ActivityStack stack = stacks.get(stackNdx);
3309 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003310 TaskRecord task = stack.topTask();
3311 if (task != null) {
3312 mWindowManager.moveTaskToTop(task.taskId);
3313 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003314 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003315 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003316
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003317 ActivityStack stack = getStack(restoreStackId);
3318 if (stack == null) {
3319 stack = mHomeStack;
3320 }
3321 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003322 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003323 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003324 } else {
3325 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003326 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003327 }
Craig Mautner93529a42013-10-04 15:03:13 -07003328 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003329 }
3330
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003331 /** Checks whether the userid is a profile of the current user. */
3332 boolean isCurrentProfileLocked(int userId) {
3333 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003334 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003335 }
3336
Craig Mautnerde4ef022013-04-07 19:01:33 -07003337 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003338 ArrayList<ActivityRecord> stops = null;
3339
3340 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003341 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3342 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003343 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3344 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003345 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003346 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003347 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3348 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003349 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003350 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003351 if (s.finishing) {
3352 // If this activity is finishing, it is sitting on top of
3353 // everyone else but we now know it is no longer needed...
3354 // so get rid of it. Otherwise, we need to go through the
3355 // normal flow and hide it once we determine that it is
3356 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003357 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003358 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003359 }
3360 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003361 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003362 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003363 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003364 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003365 }
3366 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003367 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003368 }
3369 }
3370
3371 return stops;
3372 }
3373
Craig Mautnercf910b02013-04-23 11:23:27 -07003374 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003375 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3376 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3377 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3378 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003379 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003380 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003381 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003382 if (r == null) Slog.e(TAG,
3383 "validateTop...: null top activity, stack=" + stack);
3384 else {
3385 final ActivityRecord pausing = stack.mPausingActivity;
3386 if (pausing != null && pausing == r) Slog.e(TAG,
3387 "validateTop...: top stack has pausing activity r=" + r
3388 + " state=" + state);
3389 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3390 "validateTop...: activity in front not resumed r=" + r
3391 + " state=" + state);
3392 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003393 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003394 final ActivityRecord resumed = stack.mResumedActivity;
3395 if (resumed != null && resumed == r) Slog.e(TAG,
3396 "validateTop...: back stack has resumed activity r=" + r
3397 + " state=" + state);
3398 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3399 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003400 }
3401 }
3402 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003403 }
3404
Craig Mautnere0570202015-05-13 13:06:11 -07003405 private String lockTaskModeToString() {
3406 switch (mLockTaskModeState) {
3407 case LOCK_TASK_MODE_LOCKED:
3408 return "LOCKED";
3409 case LOCK_TASK_MODE_PINNED:
3410 return "PINNED";
3411 case LOCK_TASK_MODE_NONE:
3412 return "NONE";
3413 default: return "unknown=" + mLockTaskModeState;
3414 }
3415 }
3416
Craig Mautner27084302013-03-25 08:05:25 -07003417 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003418 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003419 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003420 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003421 pw.print(prefix);
3422 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003423 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003424 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003425 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003426 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3427 if (packages.size() > 0) {
3428 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3429 for (int i = 0; i < packages.size(); ++i) {
3430 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3431 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3432 }
3433 }
3434 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3435 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003436 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003437
Winson43d1f262016-06-14 16:05:55 -07003438 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003439 * Dump all connected displays' configurations.
3440 * @param prefix Prefix to apply to each line of the dump.
3441 */
3442 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3443 pw.print(prefix); pw.println("Display override configurations:");
3444 final int displayCount = mActivityDisplays.size();
3445 for (int i = 0; i < displayCount; i++) {
3446 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3447 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3448 pw.println(activityDisplay.getOverrideConfiguration());
3449 }
3450 }
3451
3452 /**
Winson43d1f262016-06-14 16:05:55 -07003453 * Dumps the activities matching the given {@param name} in the either the focused stack
3454 * or all visible stacks if {@param dumpVisibleStacks} is true.
3455 */
3456 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3457 if (dumpVisibleStacks) {
3458 ArrayList<ActivityRecord> activities = new ArrayList<>();
3459 int numDisplays = mActivityDisplays.size();
3460 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3461 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3462 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3463 ActivityStack stack = stacks.get(stackNdx);
3464 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3465 activities.addAll(stack.getDumpActivitiesLocked(name));
3466 }
3467 }
3468 }
3469 return activities;
3470 } else {
3471 return mFocusedStack.getDumpActivitiesLocked(name);
3472 }
Craig Mautner20e72272013-04-01 13:45:53 -07003473 }
3474
Dianne Hackborn390517b2013-05-30 15:03:32 -07003475 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3476 boolean needSep, String prefix) {
3477 if (activity != null) {
3478 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3479 if (needSep) {
3480 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003481 }
3482 pw.print(prefix);
3483 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003484 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003485 }
3486 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003487 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003488 }
3489
Craig Mautner8d341ef2013-03-26 09:03:27 -07003490 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3491 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003492 boolean printed = false;
3493 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003494 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3495 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003496 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003497 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003498 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003499 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003500 final ActivityStack stack = stacks.get(stackNdx);
3501 StringBuilder stackHeader = new StringBuilder(128);
3502 stackHeader.append(" Stack #");
3503 stackHeader.append(stack.mStackId);
3504 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003505 stackHeader.append("\n");
3506 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3507 stackHeader.append("\n");
3508 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003509 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3510 needSep, stackHeader.toString());
3511 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3512 !dumpAll, false, dumpPackage, true,
3513 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003514
Craig Mautner4a1cb222013-12-04 16:14:06 -08003515 needSep = printed;
3516 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3517 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003518 if (pr) {
3519 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003520 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003521 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3523 " mResumedActivity: ");
3524 if (pr) {
3525 printed = true;
3526 needSep = false;
3527 }
3528 if (dumpAll) {
3529 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3530 " mLastPausedActivity: ");
3531 if (pr) {
3532 printed = true;
3533 needSep = true;
3534 }
3535 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3536 needSep, " mLastNoHistoryActivity: ");
3537 }
3538 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539 }
3540 }
3541
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003542 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3543 false, dumpPackage, true, " Activities waiting to finish:", null);
3544 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3545 false, dumpPackage, true, " Activities waiting to stop:", null);
3546 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3547 false, dumpPackage, true, " Activities waiting for another to become visible:",
3548 null);
3549 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3550 false, dumpPackage, true, " Activities waiting to sleep:", null);
3551 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3552 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003553
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003554 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003555 }
3556
Dianne Hackborn390517b2013-05-30 15:03:32 -07003557 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003558 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003559 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003560 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003561 String innerPrefix = null;
3562 String[] args = null;
3563 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003564 for (int i=list.size()-1; i>=0; i--) {
3565 final ActivityRecord r = list.get(i);
3566 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3567 continue;
3568 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003569 if (innerPrefix == null) {
3570 innerPrefix = prefix + " ";
3571 args = new String[0];
3572 }
3573 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003574 final boolean full = !brief && (complete || !r.isInHistory());
3575 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003576 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003577 needNL = false;
3578 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003579 if (header1 != null) {
3580 pw.println(header1);
3581 header1 = null;
3582 }
3583 if (header2 != null) {
3584 pw.println(header2);
3585 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003586 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003587 if (lastTask != r.task) {
3588 lastTask = r.task;
3589 pw.print(prefix);
3590 pw.print(full ? "* " : " ");
3591 pw.println(lastTask);
3592 if (full) {
3593 lastTask.dump(pw, prefix + " ");
3594 } else if (complete) {
3595 // Complete + brief == give a summary. Isn't that obvious?!?
3596 if (lastTask.intent != null) {
3597 pw.print(prefix); pw.print(" ");
3598 pw.println(lastTask.intent.toInsecureStringWithClip());
3599 }
3600 }
3601 }
3602 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3603 pw.print(" #"); pw.print(i); pw.print(": ");
3604 pw.println(r);
3605 if (full) {
3606 r.dump(pw, innerPrefix);
3607 } else if (complete) {
3608 // Complete + brief == give a summary. Isn't that obvious?!?
3609 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3610 if (r.app != null) {
3611 pw.print(innerPrefix); pw.println(r.app);
3612 }
3613 }
3614 if (client && r.app != null && r.app.thread != null) {
3615 // flush anything that is already in the PrintWriter since the thread is going
3616 // to write to the file descriptor directly
3617 pw.flush();
3618 try {
3619 TransferPipe tp = new TransferPipe();
3620 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003621 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003622 // Short timeout, since blocking here can
3623 // deadlock with the application.
3624 tp.go(fd, 2000);
3625 } finally {
3626 tp.kill();
3627 }
3628 } catch (IOException e) {
3629 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3630 } catch (RemoteException e) {
3631 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3632 }
3633 needNL = true;
3634 }
3635 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003636 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003637 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003638
Craig Mautnerf3333272013-04-22 10:55:53 -07003639 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003640 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3641 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003642 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3643 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003644 }
3645
3646 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003647 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003648 }
3649
3650 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003651 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3652 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003653 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3654 }
3655
Craig Mautner05d29032013-05-03 13:40:13 -07003656 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003657 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3658 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3659 }
Craig Mautner05d29032013-05-03 13:40:13 -07003660 }
3661
Craig Mautner0eea92c2013-05-16 13:35:39 -07003662 void removeSleepTimeouts() {
3663 mSleepTimeout = false;
3664 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3665 }
3666
3667 final void scheduleSleepTimeout() {
3668 removeSleepTimeouts();
3669 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3670 }
3671
Craig Mautner4a1cb222013-12-04 16:14:06 -08003672 @Override
3673 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003674 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003675 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3676 }
3677
3678 @Override
3679 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003680 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3682 }
3683
3684 @Override
3685 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003686 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003687 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3688 }
3689
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003690 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003691 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003692 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003693 newDisplay = mActivityDisplays.get(displayId) == null;
3694 if (newDisplay) {
3695 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003696 if (activityDisplay.mDisplay == null) {
3697 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3698 return;
3699 }
Craig Mautner4504de52013-12-20 09:06:56 -08003700 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003701 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003702 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 }
Craig Mautner4504de52013-12-20 09:06:56 -08003704 if (newDisplay) {
3705 mWindowManager.onDisplayAdded(displayId);
3706 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003707 }
3708
Andrii Kulianca007a62016-11-22 16:33:28 -08003709 /** Check if display with specified id is added to the list. */
3710 boolean isDisplayAdded(int displayId) {
3711 return mActivityDisplays.get(displayId) != null;
3712 }
3713
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003714 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003715 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003716 mService.mContext.getResources().getDimensionPixelSize(
3717 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003718 }
3719
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003720 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003722 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3723 if (activityDisplay != null) {
3724 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian03c403d2016-11-11 11:14:12 -08003726 final ActivityStack stack = stacks.get(stackNdx);
3727 // TODO: Implement proper stack removal and ability to choose the behavior -
3728 // remove stack completely or move it to other display.
3729 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003730 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003731 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003732 }
3733 }
3734 mWindowManager.onDisplayRemoved(displayId);
3735 }
3736
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003737 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003739 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3740 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 // TODO: Update the bounds.
3742 }
3743 }
3744 mWindowManager.onDisplayChanged(displayId);
3745 }
3746
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003747 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003748 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003749 StackInfo info = new StackInfo();
3750 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003751 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003753 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003754 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003755 info.position = display != null
3756 ? display.mStacks.indexOf(stack)
3757 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003758
3759 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3760 final int numTasks = tasks.size();
3761 int[] taskIds = new int[numTasks];
3762 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003763 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003764 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003765 for (int i = 0; i < numTasks; ++i) {
3766 final TaskRecord task = tasks.get(i);
3767 taskIds[i] = task.taskId;
3768 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3769 : task.realActivity != null ? task.realActivity.flattenToString()
3770 : task.getTopActivity() != null ? task.getTopActivity().packageName
3771 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003772 taskBounds[i] = new Rect();
3773 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003774 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 }
3776 info.taskIds = taskIds;
3777 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003778 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003779 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003780
3781 final ActivityRecord top = stack.topRunningActivityLocked();
3782 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003783 return info;
3784 }
3785
3786 StackInfo getStackInfoLocked(int stackId) {
3787 ActivityStack stack = getStack(stackId);
3788 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003789 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 }
3791 return null;
3792 }
3793
3794 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003795 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003796 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3797 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003798 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003799 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800 }
3801 }
3802 return list;
3803 }
3804
Craig Mautner15df08a2015-04-01 12:17:18 -07003805 TaskRecord getLockedTaskLocked() {
3806 final int top = mLockTaskModeTasks.size() - 1;
3807 if (top >= 0) {
3808 return mLockTaskModeTasks.get(top);
3809 }
3810 return null;
3811 }
3812
3813 boolean isLockedTask(TaskRecord task) {
3814 return mLockTaskModeTasks.contains(task);
3815 }
3816
3817 boolean isLastLockedTask(TaskRecord task) {
3818 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3819 }
3820
3821 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003822 if (!mLockTaskModeTasks.remove(task)) {
3823 return;
3824 }
3825 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3826 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003827 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003828 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3829 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003830 final Message lockTaskMsg = Message.obtain();
3831 lockTaskMsg.arg1 = task.userId;
3832 lockTaskMsg.what = LOCK_TASK_END_MSG;
3833 mHandler.sendMessage(lockTaskMsg);
3834 }
3835 }
3836
Andrii Kulianc27916642016-04-12 17:59:27 -07003837 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3838 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3839 false /* forceNonResizable */);
3840 }
3841
Jorim Jaggid53f0922016-04-06 22:16:23 -07003842 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003843 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003844 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3845 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003846 return;
3847 }
3848
Jorim Jaggicd13d332016-04-27 15:40:20 -07003849 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003850 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003851 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003852 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003853
Andrii Kulianc27916642016-04-12 17:59:27 -07003854 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3855 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003856 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003857 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3858 && !topActivity.noDisplay) {
3859 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003860 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3861 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003862 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003863 }
3864
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003865 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003866 if (mLockTaskNotify != null) {
3867 mLockTaskNotify.showToast(mLockTaskModeState);
3868 }
Jason Monka8f569c2014-07-07 12:15:21 -04003869 }
3870
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003871 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3872 if (mLockTaskModeTasks.contains(task)) {
3873 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3874 }
3875 }
3876
Craig Mautner432f64e2015-05-20 14:59:57 -07003877 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3878 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003879 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003880 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003881 final TaskRecord lockedTask = getLockedTaskLocked();
3882 if (lockedTask != null) {
3883 removeLockedTaskLocked(lockedTask);
3884 if (!mLockTaskModeTasks.isEmpty()) {
3885 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003886 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3887 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003888 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003889 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003890 return;
3891 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003892 }
Craig Mautnere0570202015-05-13 13:06:11 -07003893 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3894 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003895 return;
3896 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003897
3898 // Should have already been checked, but do it again.
3899 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003900 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3901 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003902 return;
3903 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003904 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003905 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003906 return;
3907 }
3908
3909 if (mLockTaskModeTasks.isEmpty()) {
3910 // First locktask.
3911 final Message lockTaskMsg = Message.obtain();
3912 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3913 lockTaskMsg.arg1 = task.userId;
3914 lockTaskMsg.what = LOCK_TASK_START_MSG;
3915 lockTaskMsg.arg2 = lockTaskModeState;
3916 mHandler.sendMessage(lockTaskMsg);
3917 }
3918 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003919 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3920 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003921 mLockTaskModeTasks.remove(task);
3922 mLockTaskModeTasks.add(task);
3923
3924 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003925 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003926 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003927
3928 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003929 findTaskToMoveToFrontLocked(task, 0, null, reason,
3930 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003931 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003932 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003933 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003934 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003935 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003936 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003937 }
3938
3939 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003940 return isLockTaskModeViolation(task, false);
3941 }
3942
3943 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3944 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003945 return false;
3946 }
3947 final int lockTaskAuth = task.mLockTaskAuth;
3948 switch (lockTaskAuth) {
3949 case LOCK_TASK_AUTH_DONT_LOCK:
3950 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003951 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003952 case LOCK_TASK_AUTH_LAUNCHABLE:
3953 case LOCK_TASK_AUTH_WHITELISTED:
3954 return false;
3955 case LOCK_TASK_AUTH_PINNABLE:
3956 // Pinnable tasks can't be launched on top of locktask tasks.
3957 return !mLockTaskModeTasks.isEmpty();
3958 default:
3959 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3960 return true;
3961 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003962 }
3963
Craig Mautner15df08a2015-04-01 12:17:18 -07003964 void onLockTaskPackagesUpdatedLocked() {
3965 boolean didSomething = false;
3966 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3967 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003968 final boolean wasWhitelisted =
3969 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3970 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003971 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003972 final boolean isWhitelisted =
3973 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3974 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3975 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003976 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003977 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3978 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003979 removeLockedTaskLocked(lockedTask);
3980 lockedTask.performClearTaskLocked();
3981 didSomething = true;
3982 }
3983 }
3984 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3985 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3986 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3987 final ActivityStack stack = stacks.get(stackNdx);
3988 stack.onLockTaskPackagesUpdatedLocked();
3989 }
3990 }
Craig Mautnere0570202015-05-13 13:06:11 -07003991 final ActivityRecord r = topRunningActivityLocked();
3992 final TaskRecord task = r != null ? r.task : null;
3993 if (mLockTaskModeTasks.isEmpty() && task != null
3994 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3995 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003996 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3997 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3998 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3999 false);
4000 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004001 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004002 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004003 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004004 }
4005 }
4006
Benjamin Franz43261142015-02-11 15:59:44 +00004007 int getLockTaskModeState() {
4008 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004009 }
4010
Jorim Jaggife89d122015-12-22 16:28:44 +01004011 void activityRelaunchedLocked(IBinder token) {
4012 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004013 if (mService.isSleepingOrShuttingDownLocked()) {
4014 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4015 if (r != null) {
4016 r.setSleeping(true, true);
4017 }
4018 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004019 }
4020
4021 void activityRelaunchingLocked(ActivityRecord r) {
4022 mWindowManager.notifyAppRelaunching(r.appToken);
4023 }
4024
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004025 void logStackState() {
4026 mActivityMetricsLogger.logWindowState();
4027 }
4028
Andrii Kulian933076d2016-03-29 17:04:42 -07004029 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004030 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4031 final ActivityRecord r = task.mActivities.get(i);
4032 if (r.app != null && r.app.thread != null) {
4033 mMultiWindowModeChangedActivities.add(r);
4034 }
4035 }
4036
4037 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4038 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4039 }
4040 }
4041
Andrii Kulian933076d2016-03-29 17:04:42 -07004042 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004043 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004044 if (prevStack == null || prevStack == stack
4045 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4046 return;
4047 }
4048
4049 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4050 final ActivityRecord r = task.mActivities.get(i);
4051 if (r.app != null && r.app.thread != null) {
4052 mPipModeChangedActivities.add(r);
4053 }
4054 }
4055
4056 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4057 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4058 }
4059 }
4060
Tony Mak853304c2016-04-18 15:17:41 +01004061 void setDockedStackMinimized(boolean minimized) {
4062 mIsDockMinimized = minimized;
4063 if (minimized) {
4064 // Docked stack is not visible, no need to confirm credentials for its top activity.
4065 return;
4066 }
4067 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
4068 if (dockedStack == null) {
4069 return;
4070 }
4071 final ActivityRecord top = dockedStack.topRunningActivityLocked();
4072 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
4073 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
4074 }
4075 }
4076
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004077 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004078
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004079 public ActivityStackSupervisorHandler(Looper looper) {
4080 super(looper);
4081 }
4082
Craig Mautnerf3333272013-04-22 10:55:53 -07004083 void activityIdleInternal(ActivityRecord r) {
4084 synchronized (mService) {
4085 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4086 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004087 }
4088
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004089 @Override
4090 public void handleMessage(Message msg) {
4091 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004092 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4093 synchronized (mService) {
4094 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4095 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004096 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004097 }
4098 }
4099 } break;
4100 case REPORT_PIP_MODE_CHANGED_MSG: {
4101 synchronized (mService) {
4102 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4103 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004104 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004105 }
4106 }
4107 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004108 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004109 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4110 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004111 if (mService.mDidDexOpt) {
4112 mService.mDidDexOpt = false;
4113 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4114 nmsg.obj = msg.obj;
4115 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4116 return;
4117 }
4118 // We don't at this point know if the activity is fullscreen,
4119 // so we need to be conservative and assume it isn't.
4120 activityIdleInternal((ActivityRecord)msg.obj);
4121 } break;
4122 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004123 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004124 activityIdleInternal((ActivityRecord)msg.obj);
4125 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004126 case RESUME_TOP_ACTIVITY_MSG: {
4127 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004128 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004129 }
4130 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004131 case SLEEP_TIMEOUT_MSG: {
4132 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004133 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004134 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4135 mSleepTimeout = true;
4136 checkReadyForSleepLocked();
4137 }
4138 }
4139 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004140 case LAUNCH_TIMEOUT_MSG: {
4141 if (mService.mDidDexOpt) {
4142 mService.mDidDexOpt = false;
4143 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4144 return;
4145 }
4146 synchronized (mService) {
4147 if (mLaunchingActivity.isHeld()) {
4148 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4149 if (VALIDATE_WAKE_LOCK_CALLER
4150 && Binder.getCallingUid() != Process.myUid()) {
4151 throw new IllegalStateException("Calling must be system uid");
4152 }
4153 mLaunchingActivity.release();
4154 }
4155 }
4156 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004158 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 } break;
4160 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004161 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004162 } break;
4163 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004164 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004165 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004166 case CONTAINER_CALLBACK_VISIBILITY: {
4167 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004168 final IActivityContainerCallback callback = container.mCallback;
4169 if (callback != null) {
4170 try {
4171 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4172 } catch (RemoteException e) {
4173 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004174 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004175 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004176 case LOCK_TASK_START_MSG: {
4177 // When lock task starts, we disable the status bars.
4178 try {
Jason Monk62515be2014-05-21 16:06:19 -04004179 if (mLockTaskNotify == null) {
4180 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004181 }
Jason Monk62515be2014-05-21 16:06:19 -04004182 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004183 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004184 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004185 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004186 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004187 flags = StatusBarManager.DISABLE_MASK
4188 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004189 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004190 flags = StatusBarManager.DISABLE_MASK
4191 & (~StatusBarManager.DISABLE_BACK)
4192 & (~StatusBarManager.DISABLE_HOME)
4193 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004194 }
4195 getStatusBarService().disable(flags, mToken,
4196 mService.mContext.getPackageName());
4197 }
4198 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004199 if (getDevicePolicyManager() != null) {
4200 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004201 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004202 }
justinzhang5286d3f2014-05-12 17:06:01 -04004203 } catch (RemoteException ex) {
4204 throw new RuntimeException(ex);
4205 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004206 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004207 case LOCK_TASK_END_MSG: {
4208 // When lock task ends, we enable the status bars.
4209 try {
Jason Monk62515be2014-05-21 16:06:19 -04004210 if (getStatusBarService() != null) {
4211 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4212 mService.mContext.getPackageName());
4213 }
4214 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004215 if (getDevicePolicyManager() != null) {
4216 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4217 msg.arg1);
4218 }
Jason Monk62515be2014-05-21 16:06:19 -04004219 if (mLockTaskNotify == null) {
4220 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4221 }
4222 mLockTaskNotify.show(false);
4223 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004224 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004225 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004226 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004227 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004228 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004229 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004230 new LockPatternUtils(mService.mContext)
4231 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004232 }
4233 } catch (SettingNotFoundException e) {
4234 // No setting, don't lock.
4235 }
justinzhang5286d3f2014-05-12 17:06:01 -04004236 } catch (RemoteException ex) {
4237 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004238 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004239 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004240 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004241 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004242 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4243 if (mLockTaskNotify == null) {
4244 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4245 }
4246 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4247 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004248 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4249 final ActivityContainer container = (ActivityContainer) msg.obj;
4250 final IActivityContainerCallback callback = container.mCallback;
4251 if (callback != null) {
4252 try {
4253 callback.onAllActivitiesComplete(container.asBinder());
4254 } catch (RemoteException e) {
4255 }
4256 }
4257 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004258 case LAUNCH_TASK_BEHIND_COMPLETE: {
4259 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004260 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004261 if (r != null) {
4262 handleLaunchTaskBehindCompleteLocked(r);
4263 }
4264 }
4265 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004266
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004267 }
4268 }
4269 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004270
Ying Wangb081a592014-04-22 15:20:16 -07004271 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004272 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4273 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004275 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004276 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004277 ActivityRecord mParentActivity = null;
4278 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004279
Craig Mautnere3a00d72014-04-16 08:31:19 -07004280 boolean mVisible = true;
4281
Craig Mautner4a1cb222013-12-04 16:14:06 -08004282 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004283 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004284
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004285 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4286 final static int CONTAINER_STATE_NO_SURFACE = 1;
4287 final static int CONTAINER_STATE_FINISHING = 2;
4288 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4289
4290 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004291 synchronized (mService) {
4292 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004293 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004294 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004295 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004296 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004297 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004298
Andrii Kulian839def92016-11-02 10:58:58 -07004299 /**
4300 * Adds the stack to specified display. Also calls WindowManager to do the same from
4301 * {@link ActivityStack#addToDisplay(ActivityDisplay, boolean)}.
4302 * @param activityDisplay The display to add the stack to.
4303 * @param onTop If true the stack will be place at the top of the display, else at the
4304 * bottom.
4305 */
4306 void addToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
4307 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004308 + " to display=" + activityDisplay + " onTop=" + onTop);
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;
Andrii Kulian839def92016-11-02 10:58:58 -07004314 mStack.addToDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004315 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004316 }
4317
4318 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004319 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004320 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004321 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4322 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004323 return;
4324 }
Andrii Kulian839def92016-11-02 10:58:58 -07004325 addToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004326 }
4327 }
4328
4329 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004330 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004331 synchronized (mService) {
4332 if (mActivityDisplay != null) {
4333 return mActivityDisplay.mDisplayId;
4334 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004335 }
4336 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004337 }
4338
Jeff Brownca9bc702014-02-11 14:32:56 -08004339 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004340 public int getStackId() {
4341 synchronized (mService) {
4342 return mStackId;
4343 }
4344 }
4345
4346 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004347 public boolean injectEvent(InputEvent event) {
4348 final long origId = Binder.clearCallingIdentity();
4349 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004350 synchronized (mService) {
4351 if (mActivityDisplay != null) {
4352 return mInputManagerInternal.injectInputEvent(event,
4353 mActivityDisplay.mDisplayId,
4354 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4355 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004356 }
4357 return false;
4358 } finally {
4359 Binder.restoreCallingIdentity(origId);
4360 }
4361 }
4362
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004363 @Override
4364 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004365 synchronized (mService) {
4366 if (mContainerState == CONTAINER_STATE_FINISHING) {
4367 return;
4368 }
4369 mContainerState = CONTAINER_STATE_FINISHING;
4370
Craig Mautnerd163e752014-06-13 17:18:47 -07004371 long origId = Binder.clearCallingIdentity();
4372 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004373 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004374 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004375 } finally {
4376 Binder.restoreCallingIdentity(origId);
4377 }
4378 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004379 }
4380
Andrii Kulian03c403d2016-11-11 11:14:12 -08004381 /**
4382 * Remove the stack completely. Must be called only when there are no tasks left in it,
4383 * as this method does not finish running activities.
4384 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004385 void removeLocked() {
4386 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004387 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004388 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004389 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004390 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004391 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004392 }
4393
Andrii Kulian839def92016-11-02 10:58:58 -07004394 /**
4395 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4396 * completely or re-parented.
4397 */
4398 private void removeFromDisplayLocked() {
4399 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4400 + " current displayId=" + mActivityDisplay.mDisplayId);
4401
4402 mActivityDisplay.detachActivitiesLocked(mStack);
4403 mActivityDisplay = null;
4404 }
4405
4406 /**
4407 * Move the stack to specified display.
4408 * @param activityDisplay Target display to move the stack to.
4409 */
4410 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4411 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;
4418 mStack.moveToDisplay(activityDisplay);
4419 activityDisplay.attachActivities(mStack, ON_TOP);
4420 }
4421
Craig Mautner4a1cb222013-12-04 16:14:06 -08004422 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004423 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004424 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004425 }
4426
4427 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004428 public final int startActivityIntentSender(IIntentSender intentSender)
4429 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004430 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4431
4432 if (!(intentSender instanceof PendingIntentRecord)) {
4433 throw new IllegalArgumentException("Bad PendingIntent object");
4434 }
4435
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004436 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004437 Binder.getCallingUid(), mCurrentUser, false,
4438 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004439
4440 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4441 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4442 pendingIntent.key.requestResolvedType);
4443
4444 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4445 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4446 }
4447
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004448 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004449 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004450 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004451 throw new SecurityException(
4452 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4453 }
4454 }
4455
Craig Mautnerdf88d732014-01-27 09:21:32 -08004456 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004457 public IBinder asBinder() {
4458 return this;
4459 }
4460
Craig Mautner4504de52013-12-20 09:06:56 -08004461 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004462 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004463 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004464 }
4465
Craig Mautner4a1cb222013-12-04 16:14:06 -08004466 ActivityStackSupervisor getOuter() {
4467 return ActivityStackSupervisor.this;
4468 }
4469
Craig Mautnerd163e752014-06-13 17:18:47 -07004470 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004471 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004472 }
4473
Craig Mautner6985bad2014-04-21 15:22:06 -07004474 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004475 void setVisible(boolean visible) {
4476 if (mVisible != visible) {
4477 mVisible = visible;
4478 if (mCallback != null) {
4479 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4480 0 /* unused */, this).sendToTarget();
4481 }
4482 }
4483 }
4484
Craig Mautner6985bad2014-04-21 15:22:06 -07004485 void setDrawn() {
4486 }
4487
Craig Mautner1b4bf852014-05-26 15:06:32 -07004488 // You can always start a new task on a regular ActivityStack.
4489 boolean isEligibleForNewTasks() {
4490 return true;
4491 }
4492
Craig Mautnerd163e752014-06-13 17:18:47 -07004493 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004494 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004495 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004496 }
4497
Craig Mautner34b73df2014-01-12 21:11:08 -08004498 @Override
4499 public String toString() {
4500 return mIdString + (mActivityDisplay == null ? "N" : "A");
4501 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004502 }
4503
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004504 private class VirtualActivityContainer extends ActivityContainer {
4505 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004506 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004507
4508 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4509 super(getNextStackId());
4510 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);
Andrii Kulian839def92016-11-02 10:58:58 -07004541 addToDisplayLocked(virtualActivityDisplay, true);
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;
Winson Chung303c6b72016-10-24 17:12:49 -07004600 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4601 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004602
Craig Mautner4a1cb222013-12-04 16:14:06 -08004603 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4604 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004605 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004606
Jose Lima4b6c6692014-08-12 17:41:12 -07004607 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004608
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004609 ActivityDisplay() {
4610 }
Craig Mautner4504de52013-12-20 09:06:56 -08004611
Craig Mautner1a70a162014-09-13 12:09:31 -07004612 // After instantiation, check that mDisplay is not null before using this. The alternative
4613 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004614 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004615 final Display display = mDisplayManager.getDisplay(displayId);
4616 if (display == null) {
4617 return;
4618 }
4619 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004620 }
4621
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004622 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004623 mDisplay = display;
4624 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004625 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004626
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004627 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004628 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004629 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4630 + " onTop=" + onTop);
4631 if (onTop) {
4632 mStacks.add(stack);
4633 } else {
4634 mStacks.add(0, stack);
4635 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004636 }
4637
Craig Mautnere0a38842013-12-16 16:14:02 -08004638 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004639 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: 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 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004671 }
4672
4673 class VirtualActivityDisplay extends ActivityDisplay {
4674 VirtualDisplay mVirtualDisplay;
4675
Craig Mautner6985bad2014-04-21 15:22:06 -07004676 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004677 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004678 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4679 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4680 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4681 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004682
4683 init(mVirtualDisplay.getDisplay());
4684
4685 mWindowManager.handleDisplayAdded(mDisplayId);
4686 }
4687
4688 void setSurface(Surface surface) {
4689 if (mVirtualDisplay != null) {
4690 mVirtualDisplay.setSurface(surface);
4691 }
4692 }
4693
4694 @Override
4695 void detachActivitiesLocked(ActivityStack stack) {
4696 super.detachActivitiesLocked(stack);
4697 if (mVirtualDisplay != null) {
4698 mVirtualDisplay.release();
4699 mVirtualDisplay = null;
4700 }
4701 }
4702
4703 @Override
4704 public String toString() {
4705 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004706 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004707 }
Jose Lima58e66d62014-05-27 20:00:27 -07004708
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004709 ActivityStack findStackBehind(ActivityStack stack) {
4710 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004711 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004712 if (display == null) {
4713 return null;
4714 }
4715 final ArrayList<ActivityStack> stacks = display.mStacks;
4716 for (int i = stacks.size() - 1; i >= 0; i--) {
4717 if (stacks.get(i) == stack && i > 0) {
4718 return stacks.get(i - 1);
4719 }
4720 }
4721 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4722 + " in=" + stacks);
4723 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004724
Jorim Jaggic69bd222016-03-15 14:38:37 +01004725 /**
4726 * Puts a task into resizing mode during the next app transition.
4727 *
4728 * @param taskId the id of the task to put into resizing mode
4729 */
4730 private void setResizingDuringAnimation(int taskId) {
4731 mResizingTasksDuringAnimation.add(taskId);
4732 mWindowManager.setTaskDockedResizing(taskId, true);
4733 }
4734
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004735 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4736 final TaskRecord task;
4737 final int callingUid;
4738 final String callingPackage;
4739 final Intent intent;
4740 final int userId;
4741 final ActivityOptions activityOptions = (bOptions != null)
4742 ? new ActivityOptions(bOptions) : null;
4743 final int launchStackId = (activityOptions != null)
4744 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004745 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004746 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004747 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004748 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004749
4750 if (launchStackId == DOCKED_STACK_ID) {
4751 mWindowManager.setDockedStackCreateState(
4752 activityOptions.getDockCreateMode(), null /* initialBounds */);
4753
4754 // Defer updating the stack in which recents is until the app transition is done, to
4755 // not run into issues where we still need to draw the task in recents but the
4756 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004757 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004758 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4759 }
4760
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004761 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4762 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004763 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004764 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004765 throw new IllegalArgumentException(
4766 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4767 }
4768
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004769 // Since we don't have an actual source record here, we assume that the currently focused
4770 // activity was the source.
4771 final ActivityStack focusedStack = getFocusedStack();
4772 final ActivityRecord sourceRecord =
4773 focusedStack != null ? focusedStack.topActivity() : null;
4774
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004775 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004776 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004777 moveTaskToStackLocked(
4778 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4779 ANIMATE);
4780 }
4781 }
4782
4783 // If the user must confirm credentials (e.g. when first launching a work app and the
4784 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4785 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4786 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004787 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004788 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004789 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004790 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4791 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004792
4793 // If we are launching the task in the docked stack, put it into resizing mode so
4794 // the window renders full-screen with the background filling the void. Also only
4795 // call this at the end to make sure that tasks exists on the window manager side.
4796 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004797 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004798 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004799
4800 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4801 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004802 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4803 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004804 return ActivityManager.START_TASK_TO_FRONT;
4805 }
4806 callingUid = task.mCallingUid;
4807 callingPackage = task.mCallingPackage;
4808 intent = task.intent;
4809 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4810 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004811 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4812 null, null, 0, 0, bOptions, userId, null, task);
4813 if (launchStackId == DOCKED_STACK_ID) {
4814 setResizingDuringAnimation(task.taskId);
4815 }
4816 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004817 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004818
4819 /**
4820 * @return a list of activities which are the top ones in each visible stack. The first
4821 * entry will be the focused activity.
4822 */
4823 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004824 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4825 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004826 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004827 if (display == null) {
4828 return Collections.EMPTY_LIST;
4829 }
4830 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4831 final ArrayList<ActivityStack> stacks = display.mStacks;
4832 for (int i = stacks.size() - 1; i >= 0; i--) {
4833 ActivityStack stack = stacks.get(i);
4834 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4835 ActivityRecord top = stack.topActivity();
4836 if (top != null) {
4837 if (stack == mFocusedStack) {
4838 topActivityTokens.add(0, top.appToken);
4839 } else {
4840 topActivityTokens.add(top.appToken);
4841 }
4842 }
4843 }
4844 }
4845 return topActivityTokens;
4846 }
Craig Mautner27084302013-03-25 08:05:25 -07004847}