blob: 48108fe9e0069e93c86e4a17bc3642b955b1f698 [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;
36import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070041import static android.view.Display.FLAG_PRIVATE;
42import static android.view.Display.INVALID_DISPLAY;
Jorim Jaggife762342016-10-13 14:33:27 +020043
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
69import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
71import static com.android.server.am.ActivityManagerService.ANIMATE;
72import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
73import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
74import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
76import 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
Svetoslav7008b512015-06-24 18:47:07 -0700252 // Activity actions an app cannot start if it uses a permission which is not granted.
253 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
254 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700255
Svetoslav7008b512015-06-24 18:47:07 -0700256 static {
257 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
258 Manifest.permission.CAMERA);
259 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
260 Manifest.permission.CAMERA);
261 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
262 Manifest.permission.CALL_PHONE);
263 }
264
Svet Ganov99b60432015-06-27 13:15:22 -0700265 /** Action restriction: launching the activity is not restricted. */
266 private static final int ACTIVITY_RESTRICTION_NONE = 0;
267 /** Action restriction: launching the activity is restricted by a permission. */
268 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
269 /** Action restriction: launching the activity is restricted by an app op. */
270 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700271
justinzhang5286d3f2014-05-12 17:06:01 -0400272 /** Status Bar Service **/
273 private IBinder mToken = new Binder();
274 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400275 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400276
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700277 // For debugging to make sure the caller when acquiring/releasing our
278 // wake lock is the system process.
279 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800280 /** The number of distinct task ids that can be assigned to the tasks of a single user */
281 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700282
Craig Mautner27084302013-03-25 08:05:25 -0700283 final ActivityManagerService mService;
284
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800285 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800286
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287 final ActivityStackSupervisorHandler mHandler;
288
289 /** Short cut */
290 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800291 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700292
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700293 /** Counter for next free stack ID to use for dynamic activity stacks. */
294 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700295
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800296 /**
297 * Maps the task identifier that activities are currently being started in to the userId of the
298 * task. Each time a new task is created, the entry for the userId of the task is incremented
299 */
300 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700301
Craig Mautner2420ead2013-04-01 17:13:20 -0700302 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800303 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700304
Craig Mautnere0a38842013-12-16 16:14:02 -0800305 /** The stack containing the launcher app. Assumed to always be attached to
306 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800307 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700308
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700311
Craig Mautner4a1cb222013-12-04 16:14:06 -0800312 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
313 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800314 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700316
317 /** List of activities that are waiting for a new activity to become visible before completing
318 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800319 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700322 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700323
324 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700325 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326
Craig Mautnerde4ef022013-04-07 19:01:33 -0700327 /** List of activities that are ready to be stopped, but waiting for the next activity to
328 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800329 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330
Craig Mautnerf3333272013-04-22 10:55:53 -0700331 /** List of activities that are ready to be finished, but waiting for the previous activity to
332 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800333 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700334
Craig Mautner0eea92c2013-05-16 13:35:39 -0700335 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700337
Wale Ogunwale22e25262016-02-01 10:32:02 -0800338 /** List of activities whose multi-window mode changed that we need to report to the
339 * application */
340 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
341
342 /** List of activities whose picture-in-picture mode changed that we need to report to the
343 * application */
344 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
345
Craig Mautnerf3333272013-04-22 10:55:53 -0700346 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700347 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700348
Craig Mautnerde4ef022013-04-07 19:01:33 -0700349 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
350 * is being brought in front of us. */
351 boolean mUserLeaving = false;
352
Craig Mautner0eea92c2013-05-16 13:35:39 -0700353 /** Set when we have taken too long waiting to go to sleep. */
354 boolean mSleepTimeout = false;
355
356 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700357 * We don't want to allow the device to go to sleep while in the process
358 * of launching an activity. This is primarily to allow alarm intent
359 * receivers to launch an activity and get that to run before the device
360 * goes back to sleep.
361 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700362 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700363
364 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700365 * Set when the system is going to sleep, until we have
366 * successfully paused the current activity and released our wake lock.
367 * At that point the system is allowed to actually sleep.
368 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700369 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700370
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700371 /** Stack id of the front stack when user switched, indexed by userId. */
372 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700373
Craig Mautner4504de52013-12-20 09:06:56 -0800374 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700376 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377
378 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700379 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380
Jeff Brownca9bc702014-02-11 14:32:56 -0800381 InputManagerInternal mInputManagerInternal;
382
Craig Mautner15df08a2015-04-01 12:17:18 -0700383 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
384 * may be finished until there is only one entry left. If this is empty the system is not
385 * in lockTask mode. */
386 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000387 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700388 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
389 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000390 */
391 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400392 /**
393 * Notifies the user when entering/exiting lock-task.
394 */
395 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800396
Wale Ogunwaled046a012015-12-24 13:05:59 -0800397 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800398 boolean inResumeTopActivity;
399
Andrii Kulian1e32e022016-09-16 15:29:34 -0700400 /**
401 * Temporary rect used during docked stack resize calculation so we don't need to create a new
402 * object each time.
403 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700404 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700405
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700406 // The default minimal size that will be used if the activity doesn't specify its minimal size.
407 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700408 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700410 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
411 private boolean mTaskLayersChanged = true;
412
Jorim Jaggi275561a2016-02-23 10:11:02 -0500413 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800414
Jorim Jaggidc249c42015-12-15 14:57:31 -0800415 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
416
Andrii Kulian1779e612016-10-12 21:58:25 -0700417 @Override
418 protected int getChildCount() {
419 return mActivityDisplays.size();
420 }
421
422 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700423 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700424 return mActivityDisplays.valueAt(index);
425 }
426
427 @Override
428 protected ConfigurationContainer getParent() {
429 return null;
430 }
431
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700432 Configuration getDisplayOverrideConfiguration(int displayId) {
433 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
434 if (activityDisplay == null) {
435 throw new IllegalArgumentException("No display found with id: " + displayId);
436 }
437
438 return activityDisplay.getOverrideConfiguration();
439 }
440
441 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
442 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
443 if (activityDisplay == null) {
444 throw new IllegalArgumentException("No display found with id: " + displayId);
445 }
446
447 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
448 }
449
Wale Ogunwale39381972015-12-17 17:15:29 -0800450 static class FindTaskResult {
451 ActivityRecord r;
452 boolean matchedByRootAffinity;
453 }
454 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
455
Craig Mautneree36c772014-07-16 14:56:05 -0700456 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100457 * Used to keep track whether app visibilities got changed since the last pause. Useful to
458 * determine whether to invoke the task stack change listener after pausing.
459 */
460 boolean mAppVisibilitiesChangedSinceLastPause;
461
462 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100463 * Set of tasks that are in resizing mode during an app transition to fill the "void".
464 */
465 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
466
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700467
468 /**
469 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
470 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
471 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
472 * like the docked stack going empty.
473 */
474 private boolean mAllowDockedStackResize = true;
475
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100476 /**
Tony Mak853304c2016-04-18 15:17:41 +0100477 * Is dock currently minimized.
478 */
479 boolean mIsDockMinimized;
480
Jorim Jaggife762342016-10-13 14:33:27 +0200481 final KeyguardController mKeyguardController;
482
Tony Mak853304c2016-04-18 15:17:41 +0100483 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700484 * Description of a request to start a new activity, which has been held
485 * due to app switches being disabled.
486 */
487 static class PendingActivityLaunch {
488 final ActivityRecord r;
489 final ActivityRecord sourceRecord;
490 final int startFlags;
491 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700492 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700493
494 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700495 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700496 r = _r;
497 sourceRecord = _sourceRecord;
498 startFlags = _startFlags;
499 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700500 callerApp = _callerApp;
501 }
502
503 void sendErrorResult(String message) {
504 try {
505 if (callerApp.thread != null) {
506 callerApp.thread.scheduleCrash(message);
507 }
508 } catch (RemoteException e) {
509 Slog.e(TAG, "Exception scheduling crash of failed "
510 + "activity launcher sourceRecord=" + sourceRecord, e);
511 }
Craig Mautneree36c772014-07-16 14:56:05 -0700512 }
513 }
514
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800515 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700516 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700517 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800518 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800519 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jorim Jaggife762342016-10-13 14:33:27 +0200520 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700521 }
522
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800523 void setRecentTasks(RecentTasks recentTasks) {
524 mRecentTasks = recentTasks;
525 }
526
Jeff Brown2c43c332014-06-12 22:38:59 -0700527 /**
528 * At the time when the constructor runs, the power manager has not yet been
529 * initialized. So we initialize our wakelocks afterwards.
530 */
531 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800532 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700533 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700534 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700535 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700536 }
537
justinzhang5286d3f2014-05-12 17:06:01 -0400538 // This function returns a IStatusBarService. The value is from ServiceManager.
539 // getService and is cached.
540 private IStatusBarService getStatusBarService() {
541 synchronized (mService) {
542 if (mStatusBarService == null) {
543 mStatusBarService = IStatusBarService.Stub.asInterface(
544 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
545 if (mStatusBarService == null) {
546 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
547 }
548 }
549 return mStatusBarService;
550 }
551 }
552
Jason Monk35c62a42014-06-17 10:24:47 -0400553 private IDevicePolicyManager getDevicePolicyManager() {
554 synchronized (mService) {
555 if (mDevicePolicyManager == null) {
556 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
557 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
558 if (mDevicePolicyManager == null) {
559 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
560 }
561 }
562 return mDevicePolicyManager;
563 }
564 }
565
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700566 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 synchronized (mService) {
568 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200569 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570
571 mDisplayManager =
572 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
573 mDisplayManager.registerDisplayListener(this, null);
574
575 Display[] displays = mDisplayManager.getDisplays();
576 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
577 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800578 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700579 if (activityDisplay.mDisplay == null) {
580 throw new IllegalStateException("Default Display does not exist");
581 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800582 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700583 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584 }
585
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800586 mHomeStack = mFocusedStack = mLastFocusedStack =
587 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800588
589 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 }
Craig Mautner27084302013-03-25 08:05:25 -0700591 }
592
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700593 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700595 }
596
Craig Mautnerde4ef022013-04-07 19:01:33 -0700597 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700599 }
600
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700601 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700602 if (stack == null) {
603 return false;
604 }
605
Craig Mautnerdf88d732014-01-27 09:21:32 -0800606 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
607 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700608 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800609 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800610 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700611 }
612
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700613 /** The top most stack. */
614 boolean isFrontStack(ActivityStack stack) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800615 return isFrontOfStackList(stack, mHomeStack.mStacks);
616 }
617
618 /** The top most stack on its display. */
619 boolean isFrontStackOnDisplay(ActivityStack stack) {
620 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
621 }
622
623 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700624 if (stack == null) {
625 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700627
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700628 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
629 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700630 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800631 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800632 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700633 }
634
Wale Ogunwaled046a012015-12-24 13:05:59 -0800635 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800636 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
637 if (!focusCandidate.isFocusable()) {
638 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
639 focusCandidate = focusCandidate.getNextFocusableStackLocked();
640 }
641
642 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800643 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800644 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800645
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 EventLogTags.writeAmFocusedStack(
647 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
648 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
649 }
650
651 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800652 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800653 if (r != null && r.idle) {
654 checkFinishBootingLocked();
655 }
656 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700657 }
658
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700659 void moveHomeStackToFront(String reason) {
660 mHomeStack.moveToFront(reason);
661 }
662
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700663 /** Returns true if the focus activity was adjusted to the home stack top activity. */
664 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700665 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700666 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700667 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700668 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700669
Craig Mautner84984fa2014-06-19 11:19:20 -0700670 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700671
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700672 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700673 if (top == null) {
674 return false;
675 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700676 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700677 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700678 }
679
Craig Mautner299f9602015-01-26 09:47:33 -0800680 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700681 if (!mService.mBooting && !mService.mBooted) {
682 // Not ready yet!
683 return false;
684 }
685
Craig Mautner84984fa2014-06-19 11:19:20 -0700686 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700687 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700688 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700689 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700690
Craig Mautner84984fa2014-06-19 11:19:20 -0700691 if (prev != null) {
692 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
693 }
694
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700695 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
696 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800697 final String myReason = reason + " resumeHomeStackTask";
698
Mark Lua56ea122015-10-08 13:31:01 +0800699 // Only resume home activity if isn't finishing.
700 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700701 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700703 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800704 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700705 }
706
Craig Mautner8d341ef2013-03-26 09:03:27 -0700707 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700708 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700709 }
710
711 /**
712 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
713 * @param id Id of the task we would like returned.
714 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
715 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700716 * @param stackId The stack to restore the task to (default launch stack will be used if
717 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700718 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700719 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
724 ActivityStack stack = stacks.get(stackNdx);
725 TaskRecord task = stack.taskForIdLocked(id);
726 if (task != null) {
727 return task;
728 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700729 }
730 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800731
732 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700733 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800734 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800735 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700736 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800737 return null;
738 }
739
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700740 if (!restoreFromRecents) {
741 return task;
742 }
743
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700744 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700745 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
746 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800747 return null;
748 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700749 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800750 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700751 }
752
Craig Mautner6170f732013-04-02 13:05:23 -0700753 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800756 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800757 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
758 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
759 if (r != null) {
760 return r;
761 }
Craig Mautner6170f732013-04-02 13:05:23 -0700762 }
763 }
764 return null;
765 }
766
Tony Mak853304c2016-04-18 15:17:41 +0100767 /**
768 * TODO: Handle freefom mode.
769 * @return true when credential confirmation is needed for the user and there is any
770 * activity started by the user in any visible stack.
771 */
772 boolean isUserLockedProfile(@UserIdInt int userId) {
773 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
774 return false;
775 }
776 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
777 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
778 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
779 for (final ActivityStack activityStack : activityStacks) {
780 if (activityStack == null) {
781 continue;
782 }
783 if (activityStack.topRunningActivityLocked() == null) {
784 continue;
785 }
786 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
787 continue;
788 }
789 if (activityStack.isDockedStack() && mIsDockMinimized) {
790 continue;
791 }
792 final TaskRecord topTask = activityStack.topTask();
793 if (topTask == null) {
794 continue;
795 }
796 // To handle the case that work app is in the task but just is not the top one.
797 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
798 final ActivityRecord activityRecord = topTask.mActivities.get(i);
799 if (activityRecord.userId == userId) {
800 return true;
801 }
802 }
803 }
804 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000805 }
806
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800807 void setNextTaskIdForUserLocked(int taskId, int userId) {
808 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
809 if (taskId > currentTaskId) {
810 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700811 }
812 }
813
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700814 static int nextTaskIdForUser(int taskId, int userId) {
815 int nextTaskId = taskId + 1;
816 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
817 // Wrap around as there will be smaller task ids that are available now.
818 nextTaskId -= MAX_TASK_IDS_PER_USER;
819 }
820 return nextTaskId;
821 }
822
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800823 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800824 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
825 // for a userId u, a taskId can only be in the range
826 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
827 // 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 -0700828 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800829 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
830 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
831 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700832 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800833 if (candidateTaskId == currentTaskId) {
834 // Something wrong!
835 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
836 throw new IllegalStateException("Cannot get an available task id."
837 + " Reached limit of " + MAX_TASK_IDS_PER_USER
838 + " running tasks per user.");
839 }
840 }
841 mCurTaskIdForUser.put(userId, candidateTaskId);
842 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700843 }
844
Chong Zhang6cda19c2016-06-14 19:07:56 -0700845 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800846 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700847 if (stack == null) {
848 return null;
849 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700850 ActivityRecord resumedActivity = stack.mResumedActivity;
851 if (resumedActivity == null || resumedActivity.app == null) {
852 resumedActivity = stack.mPausingActivity;
853 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700854 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700855 }
856 }
857 return resumedActivity;
858 }
859
Dianne Hackbornff072722014-09-24 10:56:28 -0700860 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700861 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800863 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
864 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
866 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700867 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 continue;
869 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700870 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 if (hr != null) {
872 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
873 && processName.equals(hr.processName)) {
874 try {
George Mount2c92c972014-03-20 09:38:23 -0700875 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800876 didSomething = true;
877 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700878 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 Slog.w(TAG, "Exception in new application when starting activity "
880 + hr.intent.getComponent().flattenToShortString(), e);
881 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700882 }
Craig Mautner20e72272013-04-01 13:45:53 -0700883 }
Craig Mautner20e72272013-04-01 13:45:53 -0700884 }
885 }
886 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700887 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700888 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700889 }
Craig Mautner20e72272013-04-01 13:45:53 -0700890 return didSomething;
891 }
892
893 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800894 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
895 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800896 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
897 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700898 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800899 continue;
900 }
901 final ActivityRecord resumedActivity = stack.mResumedActivity;
902 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700903 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800904 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800905 return false;
906 }
Craig Mautner20e72272013-04-01 13:45:53 -0700907 }
908 }
Wei Wang65c7a152016-06-02 18:51:22 -0700909 // Send launch end powerhint when idle
910 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700911 return true;
912 }
913
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800915 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
916 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
918 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700919 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700921 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800922 return false;
923 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700924 }
925 }
926 }
927 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700928 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
930 mLastFocusedStack + " to=" + mFocusedStack);
931 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700932 return true;
933 }
934
935 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800936 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800937 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
938 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
940 final ActivityStack stack = stacks.get(stackNdx);
941 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800942 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700943 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800944 return false;
945 }
946 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800947 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700948 }
949 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800950 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 }
952
Craig Mautner2acc3892013-09-23 10:28:14 -0700953 /**
954 * Pause all activities in either all of the stacks or just the back stacks.
955 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700956 * @param resuming The resuming activity.
957 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
958 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700959 * @return true if any activity was paused as a result of this call.
960 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700961 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700962 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
964 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
966 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700967 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700968 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700970 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
971 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700973 }
974 }
975 return someActivityPaused;
976 }
977
Craig Mautnerde4ef022013-04-07 19:01:33 -0700978 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700979 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
981 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
983 final ActivityStack stack = stacks.get(stackNdx);
984 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700985 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700987 Slog.d(TAG_STATES,
988 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800989 pausing = false;
990 } else {
991 return false;
992 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700993 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700994 }
995 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700996 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700997 }
998
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700999 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001000 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001001 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1003 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1004 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1005 final ActivityStack stack = stacks.get(stackNdx);
1006 if (stack.mResumedActivity != null &&
1007 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001008 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001009 }
1010 }
1011 }
1012 }
1013
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001014 void cancelInitializingActivities() {
1015 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1016 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1017 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1018 stacks.get(stackNdx).cancelInitializingActivities();
1019 }
1020 }
1021 }
1022
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001023 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001024 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001025 }
1026
1027 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1028 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001029 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001030 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001031 if (w.who == null) {
1032 changed = true;
1033 w.timeout = false;
1034 if (r != null) {
1035 w.who = new ComponentName(r.info.packageName, r.info.name);
1036 }
1037 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1038 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001039 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001041 if (changed) {
1042 mService.notifyAll();
1043 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001044 }
1045
Chong Zhang5022da32016-06-21 16:31:37 -07001046 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1047 boolean changed = false;
1048 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1049 WaitResult w = mWaitingActivityLaunched.remove(i);
1050 if (w.who == null) {
1051 changed = true;
1052 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1053 // the starting activity ends up moving another activity to front, and it should
1054 // wait for this new activity to become visible instead.
1055 // Do not modify other fields.
1056 w.result = START_TASK_TO_FRONT;
1057 }
1058 }
1059 if (changed) {
1060 mService.notifyAll();
1061 }
1062 }
1063
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001064 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1065 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001066 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001068 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001069 if (w.who == null) {
1070 changed = true;
1071 w.timeout = timeout;
1072 if (r != null) {
1073 w.who = new ComponentName(r.info.packageName, r.info.name);
1074 }
1075 w.thisTime = thisTime;
1076 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001077 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001079 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001080 if (changed) {
1081 mService.notifyAll();
1082 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001083 }
1084
Craig Mautner29219d92013-04-16 20:19:12 -07001085 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001086 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001087 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001088 if (r != null) {
1089 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001090 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001091
Andrii Kulian7318d632016-07-20 18:59:28 -07001092 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001093 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001094 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1095 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001096 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001097 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001098 if (r != null) {
1099 return r;
1100 }
1101 }
1102 }
1103 return null;
1104 }
1105
Dianne Hackborn09233282014-04-30 11:33:59 -07001106 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001107 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001108 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1109 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001110 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001111 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001112 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001113 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1114 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001115 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001116 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001117 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001118 }
1119 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001120
1121 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1122 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1123 while (maxNum > 0) {
1124 long mostRecentActiveTime = Long.MIN_VALUE;
1125 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001126 final int numTaskLists = runningTaskLists.size();
1127 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1128 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001129 if (!stackTaskList.isEmpty()) {
1130 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1131 if (lastActiveTime > mostRecentActiveTime) {
1132 mostRecentActiveTime = lastActiveTime;
1133 selectedStackList = stackTaskList;
1134 }
1135 }
1136 }
1137 if (selectedStackList != null) {
1138 list.add(selectedStackList.remove(0));
1139 --maxNum;
1140 } else {
1141 break;
1142 }
1143 }
Craig Mautner20e72272013-04-01 13:45:53 -07001144 }
1145
Todd Kennedy7440f172015-12-09 14:31:22 -08001146 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1147 ProfilerInfo profilerInfo) {
1148 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001149 if (aInfo != null) {
1150 // Store the found target back into the intent, because now that
1151 // we have it we never want to do this again. For example, if the
1152 // user navigates back to this point in the history, we should
1153 // always restart the exact same activity.
1154 intent.setComponent(new ComponentName(
1155 aInfo.applicationInfo.packageName, aInfo.name));
1156
1157 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001158 if (!aInfo.processName.equals("system")) {
1159 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001160 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001161 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001162
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001163 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1164 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1165 }
1166
Man Caocfa78b22015-06-11 20:14:34 -07001167 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1168 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1169 }
1170
1171 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001172 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001173 }
1174 }
1175 }
1176 return aInfo;
1177 }
1178
Todd Kennedy7440f172015-12-09 14:31:22 -08001179 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001180 return resolveIntent(intent, resolvedType, userId, 0);
1181 }
1182
1183 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001184 try {
1185 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001186 PackageManager.MATCH_DEFAULT_ONLY | flags
1187 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001188 } catch (RemoteException e) {
1189 }
1190 return null;
1191 }
1192
1193 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1194 ProfilerInfo profilerInfo, int userId) {
1195 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1196 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1197 }
1198
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001199 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1200 boolean andResume, boolean checkConfig) throws RemoteException {
1201
1202 if (!allPausedActivitiesComplete()) {
1203 // While there are activities pausing we skipping starting any new activities until
1204 // pauses are complete. NOTE: that we also do this for activities that are starting in
1205 // the paused state because they will first be resumed then paused on the client side.
1206 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1207 "realStartActivityLocked: Skipping start of r=" + r
1208 + " some activities pausing...");
1209 return false;
1210 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001211
Craig Mautner2568c3a2015-03-26 14:22:34 -07001212 if (andResume) {
1213 r.startFreezingScreenLocked(app, 0);
1214 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
Craig Mautner2568c3a2015-03-26 14:22:34 -07001216 // schedule launch ticks to collect information about slow apps.
1217 r.startLaunchTickingLocked();
1218 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001219
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001220 // Have the window manager re-evaluate the orientation of the screen based on the new
1221 // activity order. Note that as a result of this, it can call back into the activity
1222 // manager with a new orientation. We don't care about that, because the activity is not
1223 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001225 final int displayId = r.getDisplayId();
1226 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1227 getDisplayOverrideConfiguration(displayId),
1228 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001229 // Deferring resume here because we're going to launch new activity shortly.
1230 // We don't want to perform a redundant launch of the same record while ensuring
1231 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001232 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1233 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 }
1235
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001236 if (mKeyguardController.isKeyguardLocked()) {
1237 mWindowManager.notifyUnknownAppVisibilityLaunched(r.appToken);
1238 }
1239
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 r.app = app;
1241 app.waitingToKill = null;
1242 r.launchCount++;
1243 r.lastLaunchTime = SystemClock.uptimeMillis();
1244
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001245 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246
1247 int idx = app.activities.indexOf(r);
1248 if (idx < 0) {
1249 app.activities.add(r);
1250 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001251 mService.updateLruProcessLocked(app, true, null);
1252 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001253
Craig Mautner15df08a2015-04-01 12:17:18 -07001254 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001255 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1256 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001257 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001258 }
1259
Andrii Kulian02b7a832016-10-06 23:11:56 -07001260 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001261 try {
1262 if (app.thread == null) {
1263 throw new RemoteException();
1264 }
1265 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001266 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 if (andResume) {
1268 results = r.results;
1269 newIntents = r.newIntents;
1270 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001271 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1272 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1273 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001274 if (andResume) {
1275 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1276 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001277 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001279 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001280 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001281 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001283 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1284 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 r.sleeping = false;
1286 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001287 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 mService.showAskCompatModeDialogLocked(r);
1289 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001290 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1292 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1293 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001294 final String profileFile = mService.mProfileFile;
1295 if (profileFile != null) {
1296 ParcelFileDescriptor profileFd = mService.mProfileFd;
1297 if (profileFd != null) {
1298 try {
1299 profileFd = profileFd.dup();
1300 } catch (IOException e) {
1301 if (profileFd != null) {
1302 try {
1303 profileFd.close();
1304 } catch (IOException o) {
1305 }
1306 profileFd = null;
1307 }
1308 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001310
1311 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1312 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001313 }
1314 }
1315 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001316
Craig Mautner2568c3a2015-03-26 14:22:34 -07001317 if (andResume) {
1318 app.hasShownUi = true;
1319 app.pendingUiClean = true;
1320 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001321 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001322 // Because we could be starting an Activity in the system process this may not go across
1323 // a Binder interface which would create a new Configuration. Consequently we have to
1324 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001325 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001326 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001327 new Configuration(mService.getGlobalConfiguration()),
1328 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1329 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1330 r.persistentState, results, newIntents, !andResume,
1331 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001332
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001333 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001334 // This may be a heavy-weight process! Note that the package
1335 // manager will ensure that only activity can run in the main
1336 // process of the .apk, which is the only thing that will be
1337 // considered heavy-weight.
1338 if (app.processName.equals(app.info.packageName)) {
1339 if (mService.mHeavyWeightProcess != null
1340 && mService.mHeavyWeightProcess != app) {
1341 Slog.w(TAG, "Starting new heavy weight process " + app
1342 + " when already running "
1343 + mService.mHeavyWeightProcess);
1344 }
1345 mService.mHeavyWeightProcess = app;
1346 Message msg = mService.mHandler.obtainMessage(
1347 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1348 msg.obj = r;
1349 mService.mHandler.sendMessage(msg);
1350 }
1351 }
1352
1353 } catch (RemoteException e) {
1354 if (r.launchFailed) {
1355 // This is the second time we failed -- finish activity
1356 // and give up.
1357 Slog.e(TAG, "Second failure launching "
1358 + r.intent.getComponent().flattenToShortString()
1359 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001360 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001361 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1362 "2nd-crash", false);
1363 return false;
1364 }
1365
1366 // This is the first time we failed -- restart process and
1367 // retry.
1368 app.activities.remove(r);
1369 throw e;
1370 }
1371
1372 r.launchFailed = false;
1373 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001374 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001375 }
1376
1377 if (andResume) {
1378 // As part of the process of launching, ActivityThread also performs
1379 // a resume.
1380 stack.minimalResumeActivityLocked(r);
1381 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001382 // This activity is not starting in the resumed state... which should look like we asked
1383 // it to pause+stop (but remain visible), and it has done so and reported back the
1384 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001385 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001386 "Moving to PAUSED: " + r + " (starting in paused state)");
1387 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001388 }
1389
1390 // Launch the new version setup screen if needed. We do this -after-
1391 // launching the initial activity (that is, home), so that it can have
1392 // a chance to initialize itself while in the background, making the
1393 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001394 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 mService.startSetupActivityLocked();
1396 }
1397
Dianne Hackborn465fa392014-09-14 14:21:18 -07001398 // Update any services we are bound to that might care about whether
1399 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001400 if (r.app != null) {
1401 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1402 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001403
Craig Mautner2420ead2013-04-01 17:13:20 -07001404 return true;
1405 }
1406
Craig Mautnere79d42682013-04-01 19:01:53 -07001407 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001408 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001409 // Is this activity's application already running?
1410 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001411 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001412
Andrii Kulian02b7a832016-10-06 23:11:56 -07001413 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001414
1415 if (app != null && app.thread != null) {
1416 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001417 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1418 || !"android".equals(r.info.packageName)) {
1419 // Don't add this if it is a platform component that is marked
1420 // to run in multiple processes, because this is actually
1421 // part of the framework so doesn't make sense to track as a
1422 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001423 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1424 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001425 }
George Mount2c92c972014-03-20 09:38:23 -07001426 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001427 return;
1428 } catch (RemoteException e) {
1429 Slog.w(TAG, "Exception when starting activity "
1430 + r.intent.getComponent().flattenToShortString(), e);
1431 }
1432
1433 // If a dead object exception was thrown -- fall through to
1434 // restart the application.
1435 }
1436
1437 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001438 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001439 }
1440
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001441 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001442 String resultWho, int requestCode, int callingPid, int callingUid,
1443 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001444 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001445 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1446 callingUid);
1447 if (startAnyPerm == PERMISSION_GRANTED) {
1448 return true;
1449 }
1450 final int componentRestriction = getComponentRestrictionForCallingPackage(
1451 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1452 final int actionRestriction = getActionRestrictionForCallingPackage(
1453 intent.getAction(), callingPackage, callingPid, callingUid);
1454 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1455 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1456 if (resultRecord != null) {
1457 resultStack.sendActivityResultLocked(-1,
1458 resultRecord, resultWho, requestCode,
1459 Activity.RESULT_CANCELED, null);
1460 }
1461 final String msg;
1462 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1463 msg = "Permission Denial: starting " + intent.toString()
1464 + " from " + callerApp + " (pid=" + callingPid
1465 + ", uid=" + callingUid + ")" + " with revoked permission "
1466 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1467 } else if (!aInfo.exported) {
1468 msg = "Permission Denial: starting " + intent.toString()
1469 + " from " + callerApp + " (pid=" + callingPid
1470 + ", uid=" + callingUid + ")"
1471 + " not exported from uid " + aInfo.applicationInfo.uid;
1472 } else {
1473 msg = "Permission Denial: starting " + intent.toString()
1474 + " from " + callerApp + " (pid=" + callingPid
1475 + ", uid=" + callingUid + ")"
1476 + " requires " + aInfo.permission;
1477 }
1478 Slog.w(TAG, msg);
1479 throw new SecurityException(msg);
1480 }
1481
1482 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1483 final String message = "Appop Denial: starting " + intent.toString()
1484 + " from " + callerApp + " (pid=" + callingPid
1485 + ", uid=" + callingUid + ")"
1486 + " requires " + AppOpsManager.permissionToOp(
1487 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1488 Slog.w(TAG, message);
1489 return false;
1490 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1491 final String message = "Appop Denial: starting " + intent.toString()
1492 + " from " + callerApp + " (pid=" + callingPid
1493 + ", uid=" + callingUid + ")"
1494 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1495 Slog.w(TAG, message);
1496 return false;
1497 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001498 if (options != null) {
1499 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1500 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1501 callingPid, callingUid);
1502 if (startInTaskPerm != PERMISSION_GRANTED) {
1503 final String msg = "Permission Denial: starting " + intent.toString()
1504 + " from " + callerApp + " (pid=" + callingPid
1505 + ", uid=" + callingUid + ") with launchTaskId="
1506 + options.getLaunchTaskId();
1507 Slog.w(TAG, msg);
1508 throw new SecurityException(msg);
1509 }
1510 }
1511 // Check if someone tries to launch an activity on a private display with a different
1512 // owner.
1513 final int launchDisplayId = options.getLaunchDisplayId();
1514 if (launchDisplayId != INVALID_DISPLAY) {
1515 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1516 if (activityDisplay != null
1517 && (activityDisplay.mDisplay.getFlags() & FLAG_PRIVATE) != 0) {
1518 if (activityDisplay.mDisplay.getOwnerUid() != callingUid) {
1519 final String msg = "Permission Denial: starting " + intent.toString()
1520 + " from " + callerApp + " (pid=" + callingPid
1521 + ", uid=" + callingUid + ") with launchDisplayId="
1522 + launchDisplayId;
1523 Slog.w(TAG, msg);
1524 throw new SecurityException(msg);
1525 }
1526 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001527 }
1528 }
1529
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001530 return true;
1531 }
1532
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001533 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001534 final long identity = Binder.clearCallingIdentity();
1535 try {
1536 return UserManager.get(mService.mContext).getUserInfo(userId);
1537 } finally {
1538 Binder.restoreCallingIdentity(identity);
1539 }
1540 }
1541
Svet Ganov99b60432015-06-27 13:15:22 -07001542 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001543 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001544 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1545 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001546 == PackageManager.PERMISSION_DENIED) {
1547 return ACTIVITY_RESTRICTION_PERMISSION;
1548 }
1549
Christopher Tateff7add02015-08-17 10:23:22 -07001550 if (activityInfo.permission == null) {
1551 return ACTIVITY_RESTRICTION_NONE;
1552 }
1553
Svet Ganov99b60432015-06-27 13:15:22 -07001554 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1555 if (opCode == AppOpsManager.OP_NONE) {
1556 return ACTIVITY_RESTRICTION_NONE;
1557 }
1558
1559 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1560 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001561 if (!ignoreTargetSecurity) {
1562 return ACTIVITY_RESTRICTION_APPOP;
1563 }
Svet Ganov99b60432015-06-27 13:15:22 -07001564 }
1565
1566 return ACTIVITY_RESTRICTION_NONE;
1567 }
1568
Svetoslav7008b512015-06-24 18:47:07 -07001569 private int getActionRestrictionForCallingPackage(String action,
1570 String callingPackage, int callingPid, int callingUid) {
1571 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001572 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001573 }
1574
1575 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1576 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001577 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001578 }
1579
1580 final PackageInfo packageInfo;
1581 try {
1582 packageInfo = mService.mContext.getPackageManager()
1583 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1584 } catch (PackageManager.NameNotFoundException e) {
1585 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001586 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001587 }
1588
1589 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001590 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001591 }
1592
1593 if (mService.checkPermission(permission, callingPid, callingUid) ==
1594 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001595 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001596 }
1597
1598 final int opCode = AppOpsManager.permissionToOpCode(permission);
1599 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001600 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001601 }
1602
1603 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1604 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001605 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001606 }
1607
Svet Ganov99b60432015-06-27 13:15:22 -07001608 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001609 }
1610
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001611 void setLaunchSource(int uid) {
1612 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1613 }
1614
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001615 void acquireLaunchWakelock() {
1616 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1617 throw new IllegalStateException("Calling must be system uid");
1618 }
1619 mLaunchingActivity.acquire();
1620 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1621 // To be safe, don't allow the wake lock to be held for too long.
1622 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1623 }
1624 }
1625
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001626 /**
1627 * Called when the frontmost task is idle.
1628 * @return the state of mService.mBooting before this was called.
1629 */
1630 private boolean checkFinishBootingLocked() {
1631 final boolean booting = mService.mBooting;
1632 boolean enableScreen = false;
1633 mService.mBooting = false;
1634 if (!mService.mBooted) {
1635 mService.mBooted = true;
1636 enableScreen = true;
1637 }
1638 if (booting || enableScreen) {
1639 mService.postFinishBooting(booting, enableScreen);
1640 }
1641 return booting;
1642 }
1643
Craig Mautnerf3333272013-04-22 10:55:53 -07001644 // Checked.
1645 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1646 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001647 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001648
Craig Mautnerf3333272013-04-22 10:55:53 -07001649 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001650 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001651 int NS = 0;
1652 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001653 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001654 boolean activityRemoved = false;
1655
Wale Ogunwale7d701172015-03-11 15:36:30 -07001656 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001657 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001658 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1659 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001660 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1661 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001662 if (fromTimeout) {
1663 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001664 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001665
1666 // This is a hack to semi-deal with a race condition
1667 // in the client where it can be constructed with a
1668 // newer configuration from when we asked it to launch.
1669 // We'll update with whatever configuration it now says
1670 // it used to launch.
1671 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001672 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001673 }
1674
1675 // We are now idle. If someone is waiting for a thumbnail from
1676 // us, we can now deliver.
1677 r.idle = true;
1678
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001679 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001680 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001681 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001682 }
1683 }
1684
1685 if (allResumedActivitiesIdle()) {
1686 if (r != null) {
1687 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001688 }
1689
1690 if (mLaunchingActivity.isHeld()) {
1691 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1692 if (VALIDATE_WAKE_LOCK_CALLER &&
1693 Binder.getCallingUid() != Process.myUid()) {
1694 throw new IllegalStateException("Calling must be system uid");
1695 }
1696 mLaunchingActivity.release();
1697 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001698 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001699 }
1700
1701 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001702 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001703 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001704 if ((NF = mFinishingActivities.size()) > 0) {
1705 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001706 mFinishingActivities.clear();
1707 }
1708
Craig Mautnerf3333272013-04-22 10:55:53 -07001709 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001710 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001711 mStartingUsers.clear();
1712 }
1713
Craig Mautnerf3333272013-04-22 10:55:53 -07001714 // Stop any activities that are scheduled to do so but have been
1715 // waiting for the next one to start.
1716 for (int i = 0; i < NS; i++) {
1717 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001718 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001719 if (stack != null) {
1720 if (r.finishing) {
1721 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1722 } else {
1723 stack.stopActivityLocked(r);
1724 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001725 }
1726 }
1727
1728 // Finish any activities that are scheduled to do so but have been
1729 // waiting for the next one to start.
1730 for (int i = 0; i < NF; i++) {
1731 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001732 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001733 if (stack != null) {
1734 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1735 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001736 }
1737
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001738 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001739 // Complete user switch
1740 if (startingUsers != null) {
1741 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001742 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001743 }
1744 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001745 }
1746
1747 mService.trimApplications();
1748 //dump();
1749 //mWindowManager.dump();
1750
Craig Mautnerf3333272013-04-22 10:55:53 -07001751 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001752 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001753 }
1754
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001755 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001756 }
1757
Craig Mautner8e569572013-10-11 17:36:59 -07001758 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001759 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001760 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1761 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001762 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1763 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1764 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001765 }
Craig Mautner19091252013-10-05 00:03:53 -07001766 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001767 }
1768
1769 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001770 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1771 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001772 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1773 stacks.get(stackNdx).closeSystemDialogsLocked();
1774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001775 }
1776 }
1777
Craig Mautner93529a42013-10-04 15:03:13 -07001778 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001779 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001780 }
1781
Craig Mautner8d341ef2013-03-26 09:03:27 -07001782 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001783 * Update the last used stack id for non-current user (current user's last
1784 * used stack is the focused stack)
1785 */
1786 void updateUserStackLocked(int userId, ActivityStack stack) {
1787 if (userId != mCurrentUser) {
1788 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1789 }
1790 }
1791
1792 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001793 * @return true if some activity was finished (or would have finished if doit were true).
1794 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001795 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1796 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001797 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001798 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1799 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001800 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001801 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001802 if (stack.finishDisabledPackageActivitiesLocked(
1803 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001804 didSomething = true;
1805 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001806 }
1807 }
1808 return didSomething;
1809 }
1810
Dianne Hackborna413dc02013-07-12 12:02:55 -07001811 void updatePreviousProcessLocked(ActivityRecord r) {
1812 // Now that this process has stopped, we may want to consider
1813 // it to be the previous app to try to keep around in case
1814 // the user wants to return to it.
1815
1816 // First, found out what is currently the foreground app, so that
1817 // we don't blow away the previous app if this activity is being
1818 // hosted by the process that is actually still the foreground.
1819 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1821 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1823 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001824 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001825 if (stack.mResumedActivity != null) {
1826 fgApp = stack.mResumedActivity.app;
1827 } else if (stack.mPausingActivity != null) {
1828 fgApp = stack.mPausingActivity.app;
1829 }
1830 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001831 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001832 }
1833 }
1834
1835 // Now set this one as the previous process, only if that really
1836 // makes sense to.
1837 if (r.app != null && fgApp != null && r.app != fgApp
1838 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001839 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001840 mService.mPreviousProcess = r.app;
1841 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1842 }
1843 }
1844
Wale Ogunwaled046a012015-12-24 13:05:59 -08001845 boolean resumeFocusedStackTopActivityLocked() {
1846 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001847 }
1848
Wale Ogunwaled046a012015-12-24 13:05:59 -08001849 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001850 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001851 if (targetStack != null && isFocusedStack(targetStack)) {
1852 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001853 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001854 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1855 if (r == null || r.state != RESUMED) {
1856 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1857 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001858 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001859 }
1860
Todd Kennedy39bfee52016-02-24 10:28:21 -08001861 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1862 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1864 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1865 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1866 }
1867 }
1868 }
1869
Adrian Roos20d7df32016-01-12 18:59:43 +01001870 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1871 TaskRecord finishedTask = null;
1872 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001873 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1874 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001875 final int numStacks = stacks.size();
1876 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1877 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001878 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1879 if (stack == focusedStack || finishedTask == null) {
1880 finishedTask = t;
1881 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001882 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001883 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001884 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001885 }
1886
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001887 void finishVoiceTask(IVoiceInteractionSession session) {
1888 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1889 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1890 final int numStacks = stacks.size();
1891 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1892 final ActivityStack stack = stacks.get(stackNdx);
1893 stack.finishVoiceTask(session);
1894 }
1895 }
1896 }
1897
Andrii Kulianc27916642016-04-12 17:59:27 -07001898 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1899 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001900 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1901 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001902 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001903 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1904 // Caller wants the home activity moved with it. To accomplish this,
1905 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001906 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001907 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001908 ActivityStack currentStack = task.getStack();
1909 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001910 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1911 + task + " to front. Stack is null");
1912 return;
1913 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001914
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001915 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001916 int stackId = options.getLaunchStackId();
1917 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001918 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001919 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001920 if (stackId == INVALID_STACK_ID) {
1921 stackId = task.getLaunchStackId();
1922 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001923 if (stackId != currentStack.mStackId) {
1924 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1925 !FORCE_FOCUS, reason);
1926 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001927 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1928 // still need moveTaskToFrontLocked() below for any transition settings.
1929 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001930 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1931 resizeStackLocked(stackId, bounds,
1932 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001933 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001934 } else {
1935 // WM resizeTask must be done after the task is moved to the correct stack,
1936 // because Task's setBounds() also updates dim layer's bounds, but that has
1937 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001938 mWindowManager.resizeTask(task.taskId, task.mBounds,
1939 task.getOverrideConfiguration(), false /* relayout */,
1940 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001941 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001942 }
1943 }
1944
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001945 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001946 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001947 r == null ? null : r.appTimeTracker, reason);
1948
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001949 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001950 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001951
Andrii Kulian02b7a832016-10-06 23:11:56 -07001952 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001953 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001954 }
1955
Wale Ogunwale854809c2015-12-27 16:18:19 -08001956 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001957 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001958 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001959 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001960 return false;
1961 }
1962 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1963 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001964 }
1965
Craig Mautner967212c2013-04-13 21:10:58 -07001966 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001967 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001968 }
1969
1970 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001971 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001972 if (activityContainer != null) {
1973 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001974 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001975 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001976 return null;
1977 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001978 // TODO(multi-display): Allow creating stacks on secondary displays.
Jorim Jaggife762342016-10-13 14:33:27 +02001979 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001980 }
1981
Andrii Kulian16802aa2016-11-02 12:21:33 -07001982 /**
1983 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
1984 * If there is no such stack, new dynamic stack can be created.
1985 * @param displayId Target display.
1986 * @param r Activity that should be launched there.
1987 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
1988 */
1989 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
1990 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
1991 if (activityDisplay == null) {
1992 throw new IllegalArgumentException(
1993 "Display with displayId=" + displayId + " not found.");
1994 }
1995
1996 // Return the topmost valid stack on the display.
1997 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
1998 final ActivityStack stack = activityDisplay.mStacks.get(i);
1999 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2000 return stack;
2001 }
2002 }
2003
2004 // If there is no valid stack on the external display - check if new dynamic stack will do.
2005 if (displayId != Display.DEFAULT_DISPLAY) {
2006 final int newDynamicStackId = getNextStackId();
2007 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2008 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2009 }
2010 }
2011
2012 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2013 return null;
2014 }
2015
Craig Mautner967212c2013-04-13 21:10:58 -07002016 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002017 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2019 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002020 }
2021 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002022 }
2023
Jorim Jaggife762342016-10-13 14:33:27 +02002024 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2025 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2026 }
2027
Craig Mautneree2e45a2014-06-27 12:10:03 -07002028 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002029 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002030 }
2031
2032 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002033 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2034 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2035 final TaskRecord task = tasks.get(taskNdx);
2036 if (task.isHomeTask()) {
2037 final ArrayList<ActivityRecord> activities = task.mActivities;
2038 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2039 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002040 if (r.isHomeActivity()
2041 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002042 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002043 }
2044 }
2045 }
2046 }
2047 return null;
2048 }
2049
Chong Zhangb15758a2015-11-17 12:12:03 -08002050 /**
2051 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2052 * the docked stack itself, or if it's side-by-side to the docked stack.
2053 */
2054 boolean isStackDockedInEffect(int stackId) {
2055 return stackId == DOCKED_STACK_ID ||
2056 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2057 }
2058
Todd Kennedyca4d8422015-01-15 15:19:22 -08002059 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002060 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002061 ActivityContainer activityContainer =
2062 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002063 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002064 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2065 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002066 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002067 return activityContainer;
2068 }
2069
Craig Mautner34b73df2014-01-12 21:11:08 -08002070 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002071 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2072 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2073 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002074 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2075 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002076 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002077 }
2078 }
2079
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002080 void deleteActivityContainerRecord(int stackId) {
2081 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2082 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2083 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002084 }
2085
Jorim Jaggidc249c42015-12-15 14:57:31 -08002086 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002087 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002088 if (stackId == DOCKED_STACK_ID) {
2089 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002090 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002091 return;
2092 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002093 final ActivityStack stack = getStack(stackId);
2094 if (stack == null) {
2095 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2096 return;
2097 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002098
Jorim Jaggidc249c42015-12-15 14:57:31 -08002099 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002100 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2101 // stack size changing so things don't get out of sync.
2102 return;
2103 }
2104
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002105 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002106 mWindowManager.deferSurfaceLayout();
2107 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002108 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002109 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002110 stack.ensureVisibleActivitiesConfigurationLocked(
2111 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002112 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002113 } finally {
2114 mWindowManager.continueSurfaceLayout();
2115 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002116 }
2117 }
2118
Jorim Jaggi192086e2016-03-11 17:17:03 +01002119 void deferUpdateBounds(int stackId) {
2120 final ActivityStack stack = getStack(stackId);
2121 if (stack != null) {
2122 stack.deferUpdateBounds();
2123 }
2124 }
2125
2126 void continueUpdateBounds(int stackId) {
2127 final ActivityStack stack = getStack(stackId);
2128 if (stack != null) {
2129 stack.continueUpdateBounds();
2130 }
2131 }
2132
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002133 void notifyAppTransitionDone() {
2134 continueUpdateBounds(HOME_STACK_ID);
2135 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2136 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002137 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002138 mWindowManager.setTaskDockedResizing(taskId, false);
2139 }
2140 }
2141 mResizingTasksDuringAnimation.clear();
2142 }
2143
Jorim Jaggi192086e2016-03-11 17:17:03 +01002144 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002145 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002146 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002147
Jorim Jaggi192086e2016-03-11 17:17:03 +01002148 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2149 return;
2150 }
2151
Andrii Kulian1e32e022016-09-16 15:29:34 -07002152 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002153 }
2154
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002155 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2156 final ActivityStack stack = getStack(fromStackId);
2157 if (stack == null) {
2158 return;
2159 }
2160
2161 mWindowManager.deferSurfaceLayout();
2162 try {
2163 if (fromStackId == DOCKED_STACK_ID) {
2164
2165 // We are moving all tasks from the docked stack to the fullscreen stack,
2166 // which is dismissing the docked stack, so resize all other stacks to
2167 // fullscreen here already so we don't end up with resize trashing.
2168 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2169 if (StackId.isResizeableByDockedStack(i)) {
2170 ActivityStack otherStack = getStack(i);
2171 if (otherStack != null) {
2172 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2173 true /* allowResizeInDockedMode */, DEFER_RESUME);
2174 }
2175 }
2176 }
2177
2178 // Also disable docked stack resizing since we have manually adjusted the
2179 // size of other stacks above and we don't want to trigger a docked stack
2180 // resize when we remove task from it below and it is detached from the
2181 // display because it no longer contains any tasks.
2182 mAllowDockedStackResize = false;
2183 }
2184 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2185 final int size = tasks.size();
2186 if (onTop) {
2187 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002188 final TaskRecord task = tasks.get(i);
2189 if (fromStackId == PINNED_STACK_ID) {
2190 // Update the return-to to reflect where the pinned stack task was moved
2191 // from so that we retain the stack that was previously visible if the
2192 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2193 task.setTaskToReturnTo(getFocusedStack().getStackId() == HOME_STACK_ID
2194 ? HOME_ACTIVITY_TYPE : APPLICATION_ACTIVITY_TYPE);
2195 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002196 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002197 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002198 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002199 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002200
2201 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2202 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002203 } else {
2204 for (int i = size - 1; i >= 0; i--) {
2205 positionTaskInStackLocked(tasks.get(i).taskId,
2206 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2207 }
2208 }
2209 } finally {
2210 mAllowDockedStackResize = true;
2211 mWindowManager.continueSurfaceLayout();
2212 }
2213 }
2214
Jorim Jaggidc249c42015-12-15 14:57:31 -08002215 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002216 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2217 boolean preserveWindows) {
2218 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2219 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2220 false /* deferResume */);
2221 }
2222
2223 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2224 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2225 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002226
2227 if (!mAllowDockedStackResize) {
2228 // Docked stack resize currently disabled.
2229 return;
2230 }
2231
Jorim Jaggidc249c42015-12-15 14:57:31 -08002232 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2233 if (stack == null) {
2234 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2235 return;
2236 }
2237
2238 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2239 mWindowManager.deferSurfaceLayout();
2240 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002241 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2242 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002243 ActivityRecord r = stack.topRunningActivityLocked();
2244 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2245 tempDockedTaskInsetBounds);
2246
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002247 // TODO: Checking for isAttached might not be needed as if the user passes in null
2248 // dockedBounds then they want the docked stack to be dismissed.
2249 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2250 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002251 // In this case we make all other static stacks fullscreen and move all
2252 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002253 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002254
2255 // stack shouldn't contain anymore activities, so nothing to resume.
2256 r = null;
2257 } else {
2258 // Docked stacks occupy a dedicated region on screen so the size of all other
2259 // static stacks need to be adjusted so they don't overlap with the docked stack.
2260 // We get the bounds to use from window manager which has been adjusted for any
2261 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002262 mWindowManager.getStackDockedModeBounds(
2263 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002264 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002265 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2266 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2267 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002268 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002269 }
2270 }
2271 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002272 if (!deferResume) {
2273 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2274 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002275 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002276 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002277 mWindowManager.continueSurfaceLayout();
2278 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2279 }
2280
2281 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2282 tempDockedTaskBounds != null
2283 || tempDockedTaskInsetBounds != null
2284 || tempOtherTaskBounds != null
2285 || tempOtherTaskInsetBounds != null);
2286 }
2287
Robert Carr0d00c2e2016-02-29 17:45:02 -08002288 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2289 final ActivityStack stack = getStack(PINNED_STACK_ID);
2290 if (stack == null) {
2291 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2292 return;
2293 }
2294 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2295 mWindowManager.deferSurfaceLayout();
2296 try {
2297 ActivityRecord r = stack.topRunningActivityLocked();
2298 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2299 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002300 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002301 } finally {
2302 mWindowManager.continueSurfaceLayout();
2303 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2304 }
2305 }
2306
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002307 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2308 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002309 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002310 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002311 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002312 }
2313
Chong Zhang87b21722015-09-21 15:39:51 -07002314 // If this is a forced resize, let it go through even if the bounds is not changing,
2315 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002316 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002317 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002318 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002319 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002320 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002321 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002322
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002323 if (!mWindowManager.isValidTaskId(task.taskId)) {
2324 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002325 // All we can do for now is update the bounds so it can be used when the task is
2326 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002327 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002328 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002329 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002330 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002331 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002332 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002333 }
2334
Chong Zhang6de2ae82015-09-30 18:25:21 -07002335 // Do not move the task to another stack here.
2336 // This method assumes that the task is already placed in the right stack.
2337 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002338
Chong Zhang6de2ae82015-09-30 18:25:21 -07002339 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002340
Andrii Kulian8072d112016-09-16 11:11:01 -07002341 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002342 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002343 // way and the activity was kept the way it was. If it's false, it means the activity had
2344 // to be relaunched due to configuration change.
2345 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002346 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002347 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002348 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002349 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002350
2351 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002352 // All other activities must be made visible with their correct configuration.
2353 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2354 if (!kept) {
2355 resumeFocusedStackTopActivityLocked();
2356 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002357 }
2358 }
2359 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002360 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2361 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002362
2363 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002364 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002365 }
2366
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002367 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002368 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2369 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002370 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002371 }
2372
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002373 ActivityContainer activityContainer = new ActivityContainer(stackId);
2374 mActivityContainers.put(stackId, activityContainer);
Andrii Kulian839def92016-11-02 10:58:58 -07002375 activityContainer.addToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002376 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002377 }
2378
2379 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002380 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002381 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2382 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002383 break;
2384 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002385 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002386 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002387 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002388 }
2389
Chong Zhang5dcb2752015-08-18 13:50:26 -07002390 /**
2391 * Restores a recent task to a stack
2392 * @param task The recent task to be restored.
2393 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002394 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2395 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002396 * @return true if the task has been restored successfully.
2397 */
2398 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002399 if (!StackId.isStaticStack(stackId)) {
2400 // If stack is not static (or stack id is invalid) - use the default one.
2401 // This means that tasks that were on external displays will be restored on the
2402 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002403 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002404 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2405 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2406 // Put it in the fullscreen stack.
2407 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002408 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002409
Andrii Kulian02b7a832016-10-06 23:11:56 -07002410 final ActivityStack currentStack = task.getStack();
2411 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002412 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002413 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002414 // Nothing else to do since it is already restored in the right stack.
2415 return true;
2416 }
2417 // Remove current stack association, so we can re-associate the task with the
2418 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002419 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002420 }
2421
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002422 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002423 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002424
2425 if (stack == null) {
2426 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002427 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2428 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002429 return false;
2430 }
2431
Wale Ogunwale5f986092015-12-04 15:35:38 -08002432 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002433 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2434 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002435 final ArrayList<ActivityRecord> activities = task.mActivities;
2436 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002437 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002438 }
2439 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002440 }
2441
Wale Ogunwale040b4702015-08-06 18:10:50 -07002442 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002443 * Move stack with all its existing content to specified display.
2444 * @param stackId Id of stack to move.
2445 * @param displayId Id of display to move stack to.
2446 */
2447 void moveStackToDisplayLocked(int stackId, int displayId) {
2448 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2449 if (activityDisplay == null) {
2450 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2451 + displayId);
2452 }
2453 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2454 if (activityContainer != null) {
2455 if (activityContainer.isAttachedLocked()) {
2456 if (activityContainer.getDisplayId() == displayId) {
2457 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2458 + " to its current displayId=" + displayId);
2459 }
2460
2461 activityContainer.moveToDisplayLocked(activityDisplay);
2462 } else {
2463 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2464 + stackId + " is not attached to any display.");
2465 }
2466 } else {
2467 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2468 + stackId);
2469 }
2470
2471 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2472 !PRESERVE_WINDOWS);
2473 // TODO(multi-display): resize stacks properly if moved from split-screen.
2474 }
2475
2476 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002477 * Moves the specified task record to the input stack id.
2478 * WARNING: This method performs an unchecked/raw move of the task and
2479 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002480 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002481 * @param task Task to move.
2482 * @param stackId Id of stack to move task to.
2483 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002484 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002485 * @param reason Reason the task is been moved.
2486 * @return The stack the task was moved to.
2487 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002488 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002489 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002490
2491 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2492 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2493 + "support multi-window task=" + task + " to stackId=" + stackId);
2494 }
2495
Wale Ogunwale06579d62016-04-30 15:29:06 -07002496 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002497 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002498 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002499 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002500 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2501 // Whenever we are moving the top activity from the front stack we want to make sure to move
2502 // the stack to the front.
2503 final boolean wasFront = isFrontStack(prevStack)
2504 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002505
Chong Zhangd545dce2016-02-29 18:09:17 -08002506 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002507 // We don't allow moving a unresizeable task to the docked stack since the docked
2508 // stack is used for split-screen mode and will cause things like the docked divider to
2509 // show up. We instead leave the task in its current stack or move it to the fullscreen
2510 // stack if it isn't currently in a stack.
2511 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002512 Slog.w(TAG, "Can not move unresizeable task=" + task
2513 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2514 }
2515
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002516 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2517 // if a docked stack is created below which will lead to the stack we are moving from and
2518 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002519 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002520 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002521 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002522 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002523 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002524
2525 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002526 // move focus to the new stack by moving the stack to the front.
2527 stack.moveToFrontAndResumeStateIfNeeded(
2528 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002529
Wale Ogunwale040b4702015-08-06 18:10:50 -07002530 return stack;
2531 }
2532
Chong Zhange4fbd322016-03-01 14:44:03 -08002533 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002534 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002535 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2536 false /* deferResume */);
2537 }
2538
2539 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2540 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002541 final TaskRecord task = anyTaskForIdLocked(taskId);
2542 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002543 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002544 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002545 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002546
Andrii Kulian02b7a832016-10-06 23:11:56 -07002547 final ActivityStack currentStack = task.getStack();
2548 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002549 // You are already in the right stack silly...
2550 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002551 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002552 }
2553
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002554 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2555 throw new IllegalArgumentException("moveTaskToStack:"
2556 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2557 }
2558
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002559 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002560 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002561 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002562 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002563 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002564 // We are about to relaunch the activity because its configuration changed due to
2565 // being maximized, i.e. size change. The activity will first remove the old window
2566 // and then add a new one. This call will tell window manager about this, so it can
2567 // preserve the old window until the new one is drawn. This prevents having a gap
2568 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002569 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002570 // Note here we always set the replacing window first, as the flags might be needed
2571 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002572 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002573 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002574
2575 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002576 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002577 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002578 try {
2579 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002580 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002581 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002582
Wale Ogunwale961f4852016-02-01 20:25:54 -08002583 if (!animate) {
2584 stack.mNoAnimActivities.add(topActivity);
2585 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002586
Wale Ogunwale961f4852016-02-01 20:25:54 -08002587 // We might trigger a configuration change. Save the current task bounds for freezing.
2588 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2589
2590 // Make sure the task has the appropriate bounds/size for the stack it is in.
2591 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002592 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2593 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002594 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2595 Rect bounds = task.getLaunchBounds();
2596 if (bounds == null) {
2597 stack.layoutTaskInStack(task, null);
2598 bounds = task.mBounds;
2599 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002600 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2601 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002602 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002603 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2604 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002605 }
2606 } finally {
2607 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002608 }
2609
2610 if (mightReplaceWindow) {
2611 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2612 // window), we need to clear the replace window settings. Otherwise, we schedule a
2613 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002614 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002615 }
2616
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002617 if (!deferResume) {
2618
2619 // The task might have already been running and its visibility needs to be synchronized with
2620 // the visibility of the stack / windows.
2621 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2622 resumeFocusedStackTopActivityLocked();
2623 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002624
Jorim Jaggid53f0922016-04-06 22:16:23 -07002625 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002626
2627 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002628 }
2629
Wale Ogunwale079a0042015-10-24 11:44:07 -07002630 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2631 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2632 if (stack == null) {
2633 throw new IllegalArgumentException(
2634 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2635 }
2636
2637 final ActivityRecord r = stack.topRunningActivityLocked();
2638 if (r == null) {
2639 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2640 + " in stack=" + stack);
2641 return false;
2642 }
2643
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002644 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002645 Slog.w(TAG,
2646 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002647 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002648 return false;
2649 }
2650
Wale Ogunwale480dca02016-02-06 13:58:29 -08002651 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002652 return true;
2653 }
2654
Wale Ogunwale480dca02016-02-06 13:58:29 -08002655 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2656 mWindowManager.deferSurfaceLayout();
2657 try {
2658 final TaskRecord task = r.task;
2659
Andrii Kulian02b7a832016-10-06 23:11:56 -07002660 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002661 // An activity can't be pinned and visible behind at the same time. Go ahead and
2662 // release it from been visible behind before pinning.
2663 requestVisibleBehindLocked(r, false);
2664 }
2665
Wale Ogunwale480dca02016-02-06 13:58:29 -08002666 // Need to make sure the pinned stack exist so we can resize it below...
2667 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2668
2669 // Resize the pinned stack to match the current size of the task the activity we are
2670 // going to be moving is currently contained in. We do this to have the right starting
2671 // animation bounds for the pinned stack to the desired bounds the caller wants.
2672 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2673 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002674 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002675
2676 if (task.mActivities.size() == 1) {
2677 // There is only one activity in the task. So, we can just move the task over to
2678 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002679 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2680 // Move the home stack forward if the task we just moved to the pinned stack
2681 // was launched from home so home should be visible behind it.
2682 moveHomeStackToFront(reason);
2683 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002684 moveTaskToStackLocked(
2685 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2686 } else {
2687 stack.moveActivityToStack(r);
2688 }
2689 } finally {
2690 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002691 }
2692
Wale Ogunwale480dca02016-02-06 13:58:29 -08002693 // The task might have already been running and its visibility needs to be synchronized
2694 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002695 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002696 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002697
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002698 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002699 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002700 }
2701
Chong Zhang6cda19c2016-06-14 19:07:56 -07002702 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2703 if (r == null || !r.isFocusable()) {
2704 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2705 "moveActivityStackToFront: unfocusable r=" + r);
2706 return false;
2707 }
2708
2709 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002710 final ActivityStack stack = r.getStack();
2711 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002712 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2713 + r + " task=" + task);
2714 return false;
2715 }
2716
Chong Zhang6cda19c2016-06-14 19:07:56 -07002717 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2718 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2719 "moveActivityStackToFront: already on top, r=" + r);
2720 return false;
2721 }
2722
2723 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2724 "moveActivityStackToFront: r=" + r);
2725
2726 stack.moveToFront(reason, task);
2727 return true;
2728 }
2729
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002730 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2731 final TaskRecord task = anyTaskForIdLocked(taskId);
2732 if (task == null) {
2733 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2734 return;
2735 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002736 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2737
2738 task.updateOverrideConfigurationForStack(stack);
2739
2740 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002741 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002742 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002743 // The task might have already been running and its visibility needs to be synchronized with
2744 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002745 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002746 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002747 }
2748
Craig Mautnerac6f8432013-07-17 13:24:59 -07002749 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002750 mTmpFindTaskResult.r = null;
2751 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002752 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2756 final ActivityStack stack = stacks.get(stackNdx);
2757 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002758 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002759 continue;
2760 }
2761 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002762 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2763 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002764 continue;
2765 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002766 stack.findTaskLocked(r, mTmpFindTaskResult);
2767 // It is possible to have task in multiple stacks with the same root affinity.
2768 // If the match we found was based on root affinity we keep on looking to see if
2769 // there is a better match in another stack. We eventually return the match based
2770 // on root affinity if we don't find a better match.
2771 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2772 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002773 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002774 }
2775 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002776 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2777 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002778 }
2779
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002780 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2781 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2783 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002785 final ActivityRecord ar = stacks.get(stackNdx)
2786 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002787 if (ar != null) {
2788 return ar;
2789 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002790 }
2791 }
2792 return null;
2793 }
2794
Craig Mautner8d341ef2013-03-26 09:03:27 -07002795 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002796 scheduleSleepTimeout();
2797 if (!mGoingToSleep.isHeld()) {
2798 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002799 if (mLaunchingActivity.isHeld()) {
2800 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2801 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002802 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002803 mLaunchingActivity.release();
2804 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002805 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002807 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002808 }
2809
2810 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002811 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002812
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002813 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002814 final long endTime = System.currentTimeMillis() + timeout;
2815 while (true) {
2816 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002817 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2818 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002819 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2820 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2821 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002822 }
2823 if (cantShutdown) {
2824 long timeRemaining = endTime - System.currentTimeMillis();
2825 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002826 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002827 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002828 } catch (InterruptedException e) {
2829 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002830 } else {
2831 Slog.w(TAG, "Activity manager shutdown timed out");
2832 timedout = true;
2833 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002834 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002835 } else {
2836 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002837 }
2838 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002839
2840 // Force checkReadyForSleep to complete.
2841 mSleepTimeout = true;
2842 checkReadyForSleepLocked();
2843
Craig Mautner8d341ef2013-03-26 09:03:27 -07002844 return timedout;
2845 }
2846
2847 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002848 removeSleepTimeouts();
2849 if (mGoingToSleep.isHeld()) {
2850 mGoingToSleep.release();
2851 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002852 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2853 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002854 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2855 final ActivityStack stack = stacks.get(stackNdx);
2856 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002857 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002858 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002859 }
Craig Mautner5314a402013-09-26 12:40:16 -07002860 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002861 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002862 mGoingToSleepActivities.clear();
2863 }
2864
2865 void activitySleptLocked(ActivityRecord r) {
2866 mGoingToSleepActivities.remove(r);
2867 checkReadyForSleepLocked();
2868 }
2869
2870 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002871 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002872 // Do not care.
2873 return;
2874 }
2875
2876 if (!mSleepTimeout) {
2877 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2879 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002880 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2881 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2882 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002883 }
2884
2885 if (mStoppingActivities.size() > 0) {
2886 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002887 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002888 + mStoppingActivities.size() + " activities");
2889 scheduleIdleLocked();
2890 dontSleep = true;
2891 }
2892
2893 if (mGoingToSleepActivities.size() > 0) {
2894 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002896 + mGoingToSleepActivities.size() + " activities");
2897 dontSleep = true;
2898 }
2899
2900 if (dontSleep) {
2901 return;
2902 }
2903 }
2904
Wei Wang65c7a152016-06-02 18:51:22 -07002905 // Send launch end powerhint before going sleep
2906 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2907
Craig Mautnere0a38842013-12-16 16:14:02 -08002908 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2909 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002910 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2911 stacks.get(stackNdx).goToSleep();
2912 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002913 }
2914
2915 removeSleepTimeouts();
2916
2917 if (mGoingToSleep.isHeld()) {
2918 mGoingToSleep.release();
2919 }
2920 if (mService.mShuttingDown) {
2921 mService.notifyAll();
2922 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002923 }
2924
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002925 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002926 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002927 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002928 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002929 }
2930 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002931 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002932 mWindowManager.executeAppTransition();
2933 return true;
2934 }
2935 return false;
2936 }
2937
Craig Mautner8d341ef2013-03-26 09:03:27 -07002938 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2940 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002941 int stackNdx = stacks.size() - 1;
2942 while (stackNdx >= 0) {
2943 stacks.get(stackNdx).handleAppCrashLocked(app);
2944 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002945 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
2947 }
2948
Jose Lima4b6c6692014-08-12 17:41:12 -07002949 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002950 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002951 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002952 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2953 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002954 return false;
2955 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002956
2957 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2958 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2959 + " visible=" + visible + " stackId=" + stack.mStackId
2960 + " can't contain visible behind activities");
2961 return false;
2962 }
2963
Jose Lima4b6c6692014-08-12 17:41:12 -07002964 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002965 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2966 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002967
2968 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002969 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002970 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002971 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002972 return true;
2973 }
2974
2975 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002976 if (visible && top.fullscreen) {
2977 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002978 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2979 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2980 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2981 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002982 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002983 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2984 // Only the activity set as currently visible behind should actively reset its
2985 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002986 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2987 "requestVisibleBehind: returning visible=" + visible
2988 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2989 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002990 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002991 }
2992
Jose Lima4b6c6692014-08-12 17:41:12 -07002993 stack.setVisibleBehindActivity(visible ? r : null);
2994 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002995 // If there is a translucent home activity, we need to force it stop being translucent,
2996 // because we can't depend on the application to necessarily perform that operation.
2997 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002998 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002999 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003000 mService.convertFromTranslucent(next.appToken);
3001 }
3002 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003003 if (top.app != null && top.app.thread != null) {
3004 // Notify the top app of the change.
3005 try {
3006 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3007 } catch (RemoteException e) {
3008 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003009 }
3010 return true;
3011 }
3012
Craig Mautnerbb742462014-07-07 15:28:55 -07003013 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003014 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003015 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003016 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003017
3018 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003019 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003020 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003021 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Craig Mautnerbb742462014-07-07 15:28:55 -07003022 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07003023
3024 // When launching tasks behind, update the last active time of the top task after the new
3025 // task has been shown briefly
3026 final ActivityRecord top = stack.topActivity();
3027 if (top != null) {
3028 top.task.touchActiveTime();
3029 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003030 }
3031
3032 void scheduleLaunchTaskBehindComplete(IBinder token) {
3033 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3034 }
3035
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003036 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3037 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003038 mKeyguardController.beginActivityVisibilityUpdate();
3039 try {
3040 // First the front stacks. In case any are not fullscreen and are in front of home.
3041 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3042 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3043 final int topStackNdx = stacks.size() - 1;
3044 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3045 final ActivityStack stack = stacks.get(stackNdx);
3046 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3047 }
Craig Mautner580ea812013-04-25 12:58:38 -07003048 }
Jorim Jaggife762342016-10-13 14:33:27 +02003049 } finally {
3050 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003051 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003052 }
3053
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003054 void invalidateTaskLayers() {
3055 mTaskLayersChanged = true;
3056 }
3057
3058 void rankTaskLayersIfNeeded() {
3059 if (!mTaskLayersChanged) {
3060 return;
3061 }
3062 mTaskLayersChanged = false;
3063 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3064 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3065 int baseLayer = 0;
3066 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3067 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3068 }
3069 }
3070 }
3071
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003072 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3073 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3074 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3075 final int topStackNdx = stacks.size() - 1;
3076 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3077 final ActivityStack stack = stacks.get(stackNdx);
3078 stack.clearOtherAppTimeTrackers(except);
3079 }
3080 }
3081 }
3082
Craig Mautner8d341ef2013-03-26 09:03:27 -07003083 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
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;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003086 final int numStacks = stacks.size();
3087 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3088 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003089 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003090 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 }
3092 }
3093
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003094 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3095 // Examine all activities currently running in the process.
3096 TaskRecord firstTask = null;
3097 // Tasks is non-null only if two or more tasks are found.
3098 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003099 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3100 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003101 ActivityRecord r = app.activities.get(i);
3102 // First, if we find an activity that is in the process of being destroyed,
3103 // then we just aren't going to do anything for now; we want things to settle
3104 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003105 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003106 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003107 return;
3108 }
3109 // Don't consider any activies that are currently not in a state where they
3110 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003111 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3112 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003113 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003114 continue;
3115 }
3116 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003117 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003118 + " from " + r);
3119 if (firstTask == null) {
3120 firstTask = r.task;
3121 } else if (firstTask != r.task) {
3122 if (tasks == null) {
3123 tasks = new ArraySet<>();
3124 tasks.add(firstTask);
3125 }
3126 tasks.add(r.task);
3127 }
3128 }
3129 }
3130 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003131 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003132 return;
3133 }
3134 // If we have activities in multiple tasks that are in a position to be destroyed,
3135 // let's iterate through the tasks and release the oldest one.
3136 final int numDisplays = mActivityDisplays.size();
3137 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3138 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3139 // Step through all stacks starting from behind, to hit the oldest things first.
3140 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3141 final ActivityStack stack = stacks.get(stackNdx);
3142 // Try to release activities in this stack; if we manage to, we are done.
3143 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3144 return;
3145 }
3146 }
3147 }
3148 }
3149
Amith Yamasani37a40c22015-06-17 13:25:42 -07003150 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003151 final int focusStackId = mFocusedStack.getStackId();
3152 // We dismiss the docked stack whenever we switch users.
3153 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3154
3155 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003156 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003157 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003158
Craig Mautner858d8a62013-04-23 17:08:34 -07003159 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003160 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3161 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003162 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003163 final ActivityStack stack = stacks.get(stackNdx);
3164 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003165 TaskRecord task = stack.topTask();
3166 if (task != null) {
3167 mWindowManager.moveTaskToTop(task.taskId);
3168 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003169 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003170 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003171
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003172 ActivityStack stack = getStack(restoreStackId);
3173 if (stack == null) {
3174 stack = mHomeStack;
3175 }
3176 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003177 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003178 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003179 } else {
3180 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003181 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003182 }
Craig Mautner93529a42013-10-04 15:03:13 -07003183 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003184 }
3185
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003186 /** Checks whether the userid is a profile of the current user. */
3187 boolean isCurrentProfileLocked(int userId) {
3188 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003189 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003190 }
3191
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003193 ArrayList<ActivityRecord> stops = null;
3194
3195 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003196 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3197 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003198 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3199 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003200 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003201 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003202 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3203 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003204 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003205 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003206 if (s.finishing) {
3207 // If this activity is finishing, it is sitting on top of
3208 // everyone else but we now know it is no longer needed...
3209 // so get rid of it. Otherwise, we need to go through the
3210 // normal flow and hide it once we determine that it is
3211 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003212 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003213 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003214 }
3215 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003216 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003217 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003218 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003219 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003220 }
3221 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003222 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003223 }
3224 }
3225
3226 return stops;
3227 }
3228
Craig Mautnercf910b02013-04-23 11:23:27 -07003229 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003230 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3231 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3232 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3233 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003234 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003235 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003236 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003237 if (r == null) Slog.e(TAG,
3238 "validateTop...: null top activity, stack=" + stack);
3239 else {
3240 final ActivityRecord pausing = stack.mPausingActivity;
3241 if (pausing != null && pausing == r) Slog.e(TAG,
3242 "validateTop...: top stack has pausing activity r=" + r
3243 + " state=" + state);
3244 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3245 "validateTop...: activity in front not resumed r=" + r
3246 + " state=" + state);
3247 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003248 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003249 final ActivityRecord resumed = stack.mResumedActivity;
3250 if (resumed != null && resumed == r) Slog.e(TAG,
3251 "validateTop...: back stack has resumed activity r=" + r
3252 + " state=" + state);
3253 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3254 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003255 }
3256 }
3257 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003258 }
3259
Craig Mautnere0570202015-05-13 13:06:11 -07003260 private String lockTaskModeToString() {
3261 switch (mLockTaskModeState) {
3262 case LOCK_TASK_MODE_LOCKED:
3263 return "LOCKED";
3264 case LOCK_TASK_MODE_PINNED:
3265 return "PINNED";
3266 case LOCK_TASK_MODE_NONE:
3267 return "NONE";
3268 default: return "unknown=" + mLockTaskModeState;
3269 }
3270 }
3271
Craig Mautner27084302013-03-25 08:05:25 -07003272 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003273 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003274 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003275 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003276 pw.print(prefix);
3277 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003278 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003279 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003280 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003281 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3282 if (packages.size() > 0) {
3283 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3284 for (int i = 0; i < packages.size(); ++i) {
3285 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3286 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3287 }
3288 }
3289 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3290 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003291 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003292
Winson43d1f262016-06-14 16:05:55 -07003293 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003294 * Dump all connected displays' configurations.
3295 * @param prefix Prefix to apply to each line of the dump.
3296 */
3297 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3298 pw.print(prefix); pw.println("Display override configurations:");
3299 final int displayCount = mActivityDisplays.size();
3300 for (int i = 0; i < displayCount; i++) {
3301 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3302 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3303 pw.println(activityDisplay.getOverrideConfiguration());
3304 }
3305 }
3306
3307 /**
Winson43d1f262016-06-14 16:05:55 -07003308 * Dumps the activities matching the given {@param name} in the either the focused stack
3309 * or all visible stacks if {@param dumpVisibleStacks} is true.
3310 */
3311 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3312 if (dumpVisibleStacks) {
3313 ArrayList<ActivityRecord> activities = new ArrayList<>();
3314 int numDisplays = mActivityDisplays.size();
3315 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3316 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3317 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3318 ActivityStack stack = stacks.get(stackNdx);
3319 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3320 activities.addAll(stack.getDumpActivitiesLocked(name));
3321 }
3322 }
3323 }
3324 return activities;
3325 } else {
3326 return mFocusedStack.getDumpActivitiesLocked(name);
3327 }
Craig Mautner20e72272013-04-01 13:45:53 -07003328 }
3329
Dianne Hackborn390517b2013-05-30 15:03:32 -07003330 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3331 boolean needSep, String prefix) {
3332 if (activity != null) {
3333 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3334 if (needSep) {
3335 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003336 }
3337 pw.print(prefix);
3338 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003339 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003340 }
3341 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003342 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003343 }
3344
Craig Mautner8d341ef2013-03-26 09:03:27 -07003345 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3346 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003347 boolean printed = false;
3348 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003349 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3350 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003351 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003352 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003353 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003354 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003355 final ActivityStack stack = stacks.get(stackNdx);
3356 StringBuilder stackHeader = new StringBuilder(128);
3357 stackHeader.append(" Stack #");
3358 stackHeader.append(stack.mStackId);
3359 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003360 stackHeader.append("\n");
3361 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3362 stackHeader.append("\n");
3363 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3365 needSep, stackHeader.toString());
3366 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3367 !dumpAll, false, dumpPackage, true,
3368 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003369
Craig Mautner4a1cb222013-12-04 16:14:06 -08003370 needSep = printed;
3371 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3372 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003373 if (pr) {
3374 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003376 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3378 " mResumedActivity: ");
3379 if (pr) {
3380 printed = true;
3381 needSep = false;
3382 }
3383 if (dumpAll) {
3384 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3385 " mLastPausedActivity: ");
3386 if (pr) {
3387 printed = true;
3388 needSep = true;
3389 }
3390 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3391 needSep, " mLastNoHistoryActivity: ");
3392 }
3393 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003394 }
3395 }
3396
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003397 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3398 false, dumpPackage, true, " Activities waiting to finish:", null);
3399 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3400 false, dumpPackage, true, " Activities waiting to stop:", null);
3401 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3402 false, dumpPackage, true, " Activities waiting for another to become visible:",
3403 null);
3404 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3405 false, dumpPackage, true, " Activities waiting to sleep:", null);
3406 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3407 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003408
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003409 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003410 }
3411
Dianne Hackborn390517b2013-05-30 15:03:32 -07003412 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003413 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003414 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003415 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003416 String innerPrefix = null;
3417 String[] args = null;
3418 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003419 for (int i=list.size()-1; i>=0; i--) {
3420 final ActivityRecord r = list.get(i);
3421 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3422 continue;
3423 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003424 if (innerPrefix == null) {
3425 innerPrefix = prefix + " ";
3426 args = new String[0];
3427 }
3428 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003429 final boolean full = !brief && (complete || !r.isInHistory());
3430 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003431 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003432 needNL = false;
3433 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003434 if (header1 != null) {
3435 pw.println(header1);
3436 header1 = null;
3437 }
3438 if (header2 != null) {
3439 pw.println(header2);
3440 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003441 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003442 if (lastTask != r.task) {
3443 lastTask = r.task;
3444 pw.print(prefix);
3445 pw.print(full ? "* " : " ");
3446 pw.println(lastTask);
3447 if (full) {
3448 lastTask.dump(pw, prefix + " ");
3449 } else if (complete) {
3450 // Complete + brief == give a summary. Isn't that obvious?!?
3451 if (lastTask.intent != null) {
3452 pw.print(prefix); pw.print(" ");
3453 pw.println(lastTask.intent.toInsecureStringWithClip());
3454 }
3455 }
3456 }
3457 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3458 pw.print(" #"); pw.print(i); pw.print(": ");
3459 pw.println(r);
3460 if (full) {
3461 r.dump(pw, innerPrefix);
3462 } else if (complete) {
3463 // Complete + brief == give a summary. Isn't that obvious?!?
3464 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3465 if (r.app != null) {
3466 pw.print(innerPrefix); pw.println(r.app);
3467 }
3468 }
3469 if (client && r.app != null && r.app.thread != null) {
3470 // flush anything that is already in the PrintWriter since the thread is going
3471 // to write to the file descriptor directly
3472 pw.flush();
3473 try {
3474 TransferPipe tp = new TransferPipe();
3475 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003476 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003477 // Short timeout, since blocking here can
3478 // deadlock with the application.
3479 tp.go(fd, 2000);
3480 } finally {
3481 tp.kill();
3482 }
3483 } catch (IOException e) {
3484 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3485 } catch (RemoteException e) {
3486 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3487 }
3488 needNL = true;
3489 }
3490 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003491 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003492 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003493
Craig Mautnerf3333272013-04-22 10:55:53 -07003494 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003495 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3496 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003497 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3498 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003499 }
3500
3501 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003502 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003503 }
3504
3505 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003506 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3507 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003508 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3509 }
3510
Craig Mautner05d29032013-05-03 13:40:13 -07003511 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003512 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3513 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3514 }
Craig Mautner05d29032013-05-03 13:40:13 -07003515 }
3516
Craig Mautner0eea92c2013-05-16 13:35:39 -07003517 void removeSleepTimeouts() {
3518 mSleepTimeout = false;
3519 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3520 }
3521
3522 final void scheduleSleepTimeout() {
3523 removeSleepTimeouts();
3524 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3525 }
3526
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 @Override
3528 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003529 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3531 }
3532
3533 @Override
3534 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003535 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3537 }
3538
3539 @Override
3540 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003541 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003542 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3543 }
3544
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003545 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003546 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003547 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003548 newDisplay = mActivityDisplays.get(displayId) == null;
3549 if (newDisplay) {
3550 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003551 if (activityDisplay.mDisplay == null) {
3552 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3553 return;
3554 }
Craig Mautner4504de52013-12-20 09:06:56 -08003555 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003556 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003557 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003558 }
Craig Mautner4504de52013-12-20 09:06:56 -08003559 if (newDisplay) {
3560 mWindowManager.onDisplayAdded(displayId);
3561 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 }
3563
Andrii Kulianca007a62016-11-22 16:33:28 -08003564 /** Check if display with specified id is added to the list. */
3565 boolean isDisplayAdded(int displayId) {
3566 return mActivityDisplays.get(displayId) != null;
3567 }
3568
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003569 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003570 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003571 mService.mContext.getResources().getDimensionPixelSize(
3572 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003573 }
3574
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003575 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003576 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003577 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3578 if (activityDisplay != null) {
3579 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian03c403d2016-11-11 11:14:12 -08003581 final ActivityStack stack = stacks.get(stackNdx);
3582 // TODO: Implement proper stack removal and ability to choose the behavior -
3583 // remove stack completely or move it to other display.
3584 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003585 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003586 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 }
3588 }
3589 mWindowManager.onDisplayRemoved(displayId);
3590 }
3591
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003592 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003593 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003594 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3595 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 // TODO: Update the bounds.
3597 }
3598 }
3599 mWindowManager.onDisplayChanged(displayId);
3600 }
3601
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003602 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003603 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003604 StackInfo info = new StackInfo();
3605 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003606 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003607 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003608 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003609 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003610 info.position = display != null
3611 ? display.mStacks.indexOf(stack)
3612 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003613
3614 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3615 final int numTasks = tasks.size();
3616 int[] taskIds = new int[numTasks];
3617 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003618 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003619 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 for (int i = 0; i < numTasks; ++i) {
3621 final TaskRecord task = tasks.get(i);
3622 taskIds[i] = task.taskId;
3623 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3624 : task.realActivity != null ? task.realActivity.flattenToString()
3625 : task.getTopActivity() != null ? task.getTopActivity().packageName
3626 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003627 taskBounds[i] = new Rect();
3628 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003629 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 }
3631 info.taskIds = taskIds;
3632 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003633 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003634 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003635
3636 final ActivityRecord top = stack.topRunningActivityLocked();
3637 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003638 return info;
3639 }
3640
3641 StackInfo getStackInfoLocked(int stackId) {
3642 ActivityStack stack = getStack(stackId);
3643 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003644 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003645 }
3646 return null;
3647 }
3648
3649 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003650 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003651 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3652 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003654 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003655 }
3656 }
3657 return list;
3658 }
3659
Craig Mautner15df08a2015-04-01 12:17:18 -07003660 TaskRecord getLockedTaskLocked() {
3661 final int top = mLockTaskModeTasks.size() - 1;
3662 if (top >= 0) {
3663 return mLockTaskModeTasks.get(top);
3664 }
3665 return null;
3666 }
3667
3668 boolean isLockedTask(TaskRecord task) {
3669 return mLockTaskModeTasks.contains(task);
3670 }
3671
3672 boolean isLastLockedTask(TaskRecord task) {
3673 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3674 }
3675
3676 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003677 if (!mLockTaskModeTasks.remove(task)) {
3678 return;
3679 }
3680 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3681 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003682 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003683 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3684 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003685 final Message lockTaskMsg = Message.obtain();
3686 lockTaskMsg.arg1 = task.userId;
3687 lockTaskMsg.what = LOCK_TASK_END_MSG;
3688 mHandler.sendMessage(lockTaskMsg);
3689 }
3690 }
3691
Andrii Kulianc27916642016-04-12 17:59:27 -07003692 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3693 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3694 false /* forceNonResizable */);
3695 }
3696
Jorim Jaggid53f0922016-04-06 22:16:23 -07003697 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003698 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003699 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3700 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003701 return;
3702 }
3703
Jorim Jaggicd13d332016-04-27 15:40:20 -07003704 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003705 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003706 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003707 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003708
Andrii Kulianc27916642016-04-12 17:59:27 -07003709 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3710 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003711 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003712 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3713 && !topActivity.noDisplay) {
3714 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003715 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3716 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003717 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003718 }
3719
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003720 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003721 if (mLockTaskNotify != null) {
3722 mLockTaskNotify.showToast(mLockTaskModeState);
3723 }
Jason Monka8f569c2014-07-07 12:15:21 -04003724 }
3725
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003726 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3727 if (mLockTaskModeTasks.contains(task)) {
3728 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3729 }
3730 }
3731
Craig Mautner432f64e2015-05-20 14:59:57 -07003732 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3733 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003734 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003735 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003736 final TaskRecord lockedTask = getLockedTaskLocked();
3737 if (lockedTask != null) {
3738 removeLockedTaskLocked(lockedTask);
3739 if (!mLockTaskModeTasks.isEmpty()) {
3740 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003741 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3742 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003743 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003744 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003745 return;
3746 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003747 }
Craig Mautnere0570202015-05-13 13:06:11 -07003748 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3749 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003750 return;
3751 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003752
3753 // Should have already been checked, but do it again.
3754 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003755 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3756 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003757 return;
3758 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003759 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003760 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003761 return;
3762 }
3763
3764 if (mLockTaskModeTasks.isEmpty()) {
3765 // First locktask.
3766 final Message lockTaskMsg = Message.obtain();
3767 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3768 lockTaskMsg.arg1 = task.userId;
3769 lockTaskMsg.what = LOCK_TASK_START_MSG;
3770 lockTaskMsg.arg2 = lockTaskModeState;
3771 mHandler.sendMessage(lockTaskMsg);
3772 }
3773 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003774 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3775 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003776 mLockTaskModeTasks.remove(task);
3777 mLockTaskModeTasks.add(task);
3778
3779 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003780 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003781 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003782
3783 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003784 findTaskToMoveToFrontLocked(task, 0, null, reason,
3785 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003786 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003787 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003788 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003789 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003790 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003791 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003792 }
3793
3794 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003795 return isLockTaskModeViolation(task, false);
3796 }
3797
3798 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3799 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003800 return false;
3801 }
3802 final int lockTaskAuth = task.mLockTaskAuth;
3803 switch (lockTaskAuth) {
3804 case LOCK_TASK_AUTH_DONT_LOCK:
3805 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003806 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003807 case LOCK_TASK_AUTH_LAUNCHABLE:
3808 case LOCK_TASK_AUTH_WHITELISTED:
3809 return false;
3810 case LOCK_TASK_AUTH_PINNABLE:
3811 // Pinnable tasks can't be launched on top of locktask tasks.
3812 return !mLockTaskModeTasks.isEmpty();
3813 default:
3814 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3815 return true;
3816 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003817 }
3818
Craig Mautner15df08a2015-04-01 12:17:18 -07003819 void onLockTaskPackagesUpdatedLocked() {
3820 boolean didSomething = false;
3821 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3822 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003823 final boolean wasWhitelisted =
3824 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3825 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003826 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003827 final boolean isWhitelisted =
3828 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3829 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3830 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003831 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003832 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3833 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003834 removeLockedTaskLocked(lockedTask);
3835 lockedTask.performClearTaskLocked();
3836 didSomething = true;
3837 }
3838 }
3839 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3840 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3841 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3842 final ActivityStack stack = stacks.get(stackNdx);
3843 stack.onLockTaskPackagesUpdatedLocked();
3844 }
3845 }
Craig Mautnere0570202015-05-13 13:06:11 -07003846 final ActivityRecord r = topRunningActivityLocked();
3847 final TaskRecord task = r != null ? r.task : null;
3848 if (mLockTaskModeTasks.isEmpty() && task != null
3849 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3850 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003851 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3852 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3853 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3854 false);
3855 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003856 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003857 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003858 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003859 }
3860 }
3861
Benjamin Franz43261142015-02-11 15:59:44 +00003862 int getLockTaskModeState() {
3863 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003864 }
3865
Jorim Jaggife89d122015-12-22 16:28:44 +01003866 void activityRelaunchedLocked(IBinder token) {
3867 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003868 if (mService.isSleepingOrShuttingDownLocked()) {
3869 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3870 if (r != null) {
3871 r.setSleeping(true, true);
3872 }
3873 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003874 }
3875
3876 void activityRelaunchingLocked(ActivityRecord r) {
3877 mWindowManager.notifyAppRelaunching(r.appToken);
3878 }
3879
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003880 void logStackState() {
3881 mActivityMetricsLogger.logWindowState();
3882 }
3883
Andrii Kulian933076d2016-03-29 17:04:42 -07003884 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003885 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3886 final ActivityRecord r = task.mActivities.get(i);
3887 if (r.app != null && r.app.thread != null) {
3888 mMultiWindowModeChangedActivities.add(r);
3889 }
3890 }
3891
3892 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3893 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3894 }
3895 }
3896
Andrii Kulian933076d2016-03-29 17:04:42 -07003897 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003898 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003899 if (prevStack == null || prevStack == stack
3900 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3901 return;
3902 }
3903
3904 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3905 final ActivityRecord r = task.mActivities.get(i);
3906 if (r.app != null && r.app.thread != null) {
3907 mPipModeChangedActivities.add(r);
3908 }
3909 }
3910
3911 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3912 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3913 }
3914 }
3915
Tony Mak853304c2016-04-18 15:17:41 +01003916 void setDockedStackMinimized(boolean minimized) {
3917 mIsDockMinimized = minimized;
3918 if (minimized) {
3919 // Docked stack is not visible, no need to confirm credentials for its top activity.
3920 return;
3921 }
3922 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3923 if (dockedStack == null) {
3924 return;
3925 }
3926 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3927 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3928 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3929 }
3930 }
3931
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003932 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003933
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003934 public ActivityStackSupervisorHandler(Looper looper) {
3935 super(looper);
3936 }
3937
Craig Mautnerf3333272013-04-22 10:55:53 -07003938 void activityIdleInternal(ActivityRecord r) {
3939 synchronized (mService) {
3940 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3941 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003942 }
3943
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003944 @Override
3945 public void handleMessage(Message msg) {
3946 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003947 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3948 synchronized (mService) {
3949 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3950 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003951 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003952 }
3953 }
3954 } break;
3955 case REPORT_PIP_MODE_CHANGED_MSG: {
3956 synchronized (mService) {
3957 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3958 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003959 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003960 }
3961 }
3962 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003963 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003964 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3965 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003966 if (mService.mDidDexOpt) {
3967 mService.mDidDexOpt = false;
3968 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3969 nmsg.obj = msg.obj;
3970 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3971 return;
3972 }
3973 // We don't at this point know if the activity is fullscreen,
3974 // so we need to be conservative and assume it isn't.
3975 activityIdleInternal((ActivityRecord)msg.obj);
3976 } break;
3977 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003978 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003979 activityIdleInternal((ActivityRecord)msg.obj);
3980 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003981 case RESUME_TOP_ACTIVITY_MSG: {
3982 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003983 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003984 }
3985 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003986 case SLEEP_TIMEOUT_MSG: {
3987 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003988 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003989 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3990 mSleepTimeout = true;
3991 checkReadyForSleepLocked();
3992 }
3993 }
3994 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003995 case LAUNCH_TIMEOUT_MSG: {
3996 if (mService.mDidDexOpt) {
3997 mService.mDidDexOpt = false;
3998 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3999 return;
4000 }
4001 synchronized (mService) {
4002 if (mLaunchingActivity.isHeld()) {
4003 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4004 if (VALIDATE_WAKE_LOCK_CALLER
4005 && Binder.getCallingUid() != Process.myUid()) {
4006 throw new IllegalStateException("Calling must be system uid");
4007 }
4008 mLaunchingActivity.release();
4009 }
4010 }
4011 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004013 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 } break;
4015 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004016 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017 } break;
4018 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004019 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004020 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004021 case CONTAINER_CALLBACK_VISIBILITY: {
4022 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004023 final IActivityContainerCallback callback = container.mCallback;
4024 if (callback != null) {
4025 try {
4026 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4027 } catch (RemoteException e) {
4028 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004029 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004030 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004031 case LOCK_TASK_START_MSG: {
4032 // When lock task starts, we disable the status bars.
4033 try {
Jason Monk62515be2014-05-21 16:06:19 -04004034 if (mLockTaskNotify == null) {
4035 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004036 }
Jason Monk62515be2014-05-21 16:06:19 -04004037 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004038 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004039 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004040 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004041 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004042 flags = StatusBarManager.DISABLE_MASK
4043 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004044 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004045 flags = StatusBarManager.DISABLE_MASK
4046 & (~StatusBarManager.DISABLE_BACK)
4047 & (~StatusBarManager.DISABLE_HOME)
4048 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004049 }
4050 getStatusBarService().disable(flags, mToken,
4051 mService.mContext.getPackageName());
4052 }
4053 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004054 if (getDevicePolicyManager() != null) {
4055 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004056 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004057 }
justinzhang5286d3f2014-05-12 17:06:01 -04004058 } catch (RemoteException ex) {
4059 throw new RuntimeException(ex);
4060 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004061 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004062 case LOCK_TASK_END_MSG: {
4063 // When lock task ends, we enable the status bars.
4064 try {
Jason Monk62515be2014-05-21 16:06:19 -04004065 if (getStatusBarService() != null) {
4066 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4067 mService.mContext.getPackageName());
4068 }
4069 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004070 if (getDevicePolicyManager() != null) {
4071 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4072 msg.arg1);
4073 }
Jason Monk62515be2014-05-21 16:06:19 -04004074 if (mLockTaskNotify == null) {
4075 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4076 }
4077 mLockTaskNotify.show(false);
4078 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004079 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004080 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004081 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004082 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004083 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004084 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004085 new LockPatternUtils(mService.mContext)
4086 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004087 }
4088 } catch (SettingNotFoundException e) {
4089 // No setting, don't lock.
4090 }
justinzhang5286d3f2014-05-12 17:06:01 -04004091 } catch (RemoteException ex) {
4092 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004093 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004094 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004095 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004096 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004097 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4098 if (mLockTaskNotify == null) {
4099 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4100 }
4101 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4102 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004103 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4104 final ActivityContainer container = (ActivityContainer) msg.obj;
4105 final IActivityContainerCallback callback = container.mCallback;
4106 if (callback != null) {
4107 try {
4108 callback.onAllActivitiesComplete(container.asBinder());
4109 } catch (RemoteException e) {
4110 }
4111 }
4112 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004113 case LAUNCH_TASK_BEHIND_COMPLETE: {
4114 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004115 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004116 if (r != null) {
4117 handleLaunchTaskBehindCompleteLocked(r);
4118 }
4119 }
4120 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004121
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004122 }
4123 }
4124 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004125
Ying Wangb081a592014-04-22 15:20:16 -07004126 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004127 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4128 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004130 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004131 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004132 ActivityRecord mParentActivity = null;
4133 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004134
Craig Mautnere3a00d72014-04-16 08:31:19 -07004135 boolean mVisible = true;
4136
Craig Mautner4a1cb222013-12-04 16:14:06 -08004137 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004138 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004139
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004140 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4141 final static int CONTAINER_STATE_NO_SURFACE = 1;
4142 final static int CONTAINER_STATE_FINISHING = 2;
4143 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4144
4145 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004146 synchronized (mService) {
4147 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004148 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004149 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004150 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004152 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153
Andrii Kulian839def92016-11-02 10:58:58 -07004154 /**
4155 * Adds the stack to specified display. Also calls WindowManager to do the same from
4156 * {@link ActivityStack#addToDisplay(ActivityDisplay, boolean)}.
4157 * @param activityDisplay The display to add the stack to.
4158 * @param onTop If true the stack will be place at the top of the display, else at the
4159 * bottom.
4160 */
4161 void addToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
4162 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004163 + " to display=" + activityDisplay + " onTop=" + onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004164 if (mActivityDisplay != null) {
4165 throw new IllegalStateException("ActivityContainer is already attached, " +
4166 "displayId=" + mActivityDisplay.mDisplayId);
4167 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004168 mActivityDisplay = activityDisplay;
Andrii Kulian839def92016-11-02 10:58:58 -07004169 mStack.addToDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004170 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 }
4172
4173 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004174 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004175 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004176 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4177 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004178 return;
4179 }
Andrii Kulian839def92016-11-02 10:58:58 -07004180 addToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181 }
4182 }
4183
4184 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004185 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004186 synchronized (mService) {
4187 if (mActivityDisplay != null) {
4188 return mActivityDisplay.mDisplayId;
4189 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004190 }
4191 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 }
4193
Jeff Brownca9bc702014-02-11 14:32:56 -08004194 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004195 public int getStackId() {
4196 synchronized (mService) {
4197 return mStackId;
4198 }
4199 }
4200
4201 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004202 public boolean injectEvent(InputEvent event) {
4203 final long origId = Binder.clearCallingIdentity();
4204 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004205 synchronized (mService) {
4206 if (mActivityDisplay != null) {
4207 return mInputManagerInternal.injectInputEvent(event,
4208 mActivityDisplay.mDisplayId,
4209 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4210 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004211 }
4212 return false;
4213 } finally {
4214 Binder.restoreCallingIdentity(origId);
4215 }
4216 }
4217
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004218 @Override
4219 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004220 synchronized (mService) {
4221 if (mContainerState == CONTAINER_STATE_FINISHING) {
4222 return;
4223 }
4224 mContainerState = CONTAINER_STATE_FINISHING;
4225
Craig Mautnerd163e752014-06-13 17:18:47 -07004226 long origId = Binder.clearCallingIdentity();
4227 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004228 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004229 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004230 } finally {
4231 Binder.restoreCallingIdentity(origId);
4232 }
4233 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004234 }
4235
Andrii Kulian03c403d2016-11-11 11:14:12 -08004236 /**
4237 * Remove the stack completely. Must be called only when there are no tasks left in it,
4238 * as this method does not finish running activities.
4239 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004240 void removeLocked() {
4241 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004242 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004243 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004244 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004246 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 }
4248
Andrii Kulian839def92016-11-02 10:58:58 -07004249 /**
4250 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4251 * completely or re-parented.
4252 */
4253 private void removeFromDisplayLocked() {
4254 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4255 + " current displayId=" + mActivityDisplay.mDisplayId);
4256
4257 mActivityDisplay.detachActivitiesLocked(mStack);
4258 mActivityDisplay = null;
4259 }
4260
4261 /**
4262 * Move the stack to specified display.
4263 * @param activityDisplay Target display to move the stack to.
4264 */
4265 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4266 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4267 + mActivityDisplay + " to display=" + activityDisplay
4268 + " Callers=" + Debug.getCallers(2));
4269
4270 removeFromDisplayLocked();
4271
4272 mActivityDisplay = activityDisplay;
4273 mStack.moveToDisplay(activityDisplay);
4274 activityDisplay.attachActivities(mStack, ON_TOP);
4275 }
4276
Craig Mautner4a1cb222013-12-04 16:14:06 -08004277 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004278 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004279 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004280 }
4281
4282 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004283 public final int startActivityIntentSender(IIntentSender intentSender)
4284 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004285 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4286
4287 if (!(intentSender instanceof PendingIntentRecord)) {
4288 throw new IllegalArgumentException("Bad PendingIntent object");
4289 }
4290
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004291 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004292 Binder.getCallingUid(), mCurrentUser, false,
4293 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004294
4295 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4296 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4297 pendingIntent.key.requestResolvedType);
4298
4299 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4300 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4301 }
4302
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004303 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004304 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004305 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004306 throw new SecurityException(
4307 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4308 }
4309 }
4310
Craig Mautnerdf88d732014-01-27 09:21:32 -08004311 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004312 public IBinder asBinder() {
4313 return this;
4314 }
4315
Craig Mautner4504de52013-12-20 09:06:56 -08004316 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004317 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004318 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004319 }
4320
Craig Mautner4a1cb222013-12-04 16:14:06 -08004321 ActivityStackSupervisor getOuter() {
4322 return ActivityStackSupervisor.this;
4323 }
4324
Craig Mautnerd163e752014-06-13 17:18:47 -07004325 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004326 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004327 }
4328
Craig Mautner6985bad2014-04-21 15:22:06 -07004329 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004330 void setVisible(boolean visible) {
4331 if (mVisible != visible) {
4332 mVisible = visible;
4333 if (mCallback != null) {
4334 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4335 0 /* unused */, this).sendToTarget();
4336 }
4337 }
4338 }
4339
Craig Mautner6985bad2014-04-21 15:22:06 -07004340 void setDrawn() {
4341 }
4342
Craig Mautner1b4bf852014-05-26 15:06:32 -07004343 // You can always start a new task on a regular ActivityStack.
4344 boolean isEligibleForNewTasks() {
4345 return true;
4346 }
4347
Craig Mautnerd163e752014-06-13 17:18:47 -07004348 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004349 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004350 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004351 }
4352
Craig Mautner34b73df2014-01-12 21:11:08 -08004353 @Override
4354 public String toString() {
4355 return mIdString + (mActivityDisplay == null ? "N" : "A");
4356 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004357 }
4358
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004359 private class VirtualActivityContainer extends ActivityContainer {
4360 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004361 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004362
4363 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4364 super(getNextStackId());
4365 mParentActivity = parent;
4366 mCallback = callback;
4367 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004368 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004369 }
4370
4371 @Override
4372 public void setSurface(Surface surface, int width, int height, int density) {
4373 super.setSurface(surface, width, height, density);
4374
4375 synchronized (mService) {
4376 final long origId = Binder.clearCallingIdentity();
4377 try {
4378 setSurfaceLocked(surface, width, height, density);
4379 } finally {
4380 Binder.restoreCallingIdentity(origId);
4381 }
4382 }
4383 }
4384
4385 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4386 if (mContainerState == CONTAINER_STATE_FINISHING) {
4387 return;
4388 }
4389 VirtualActivityDisplay virtualActivityDisplay =
4390 (VirtualActivityDisplay) mActivityDisplay;
4391 if (virtualActivityDisplay == null) {
4392 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004393 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004394 mActivityDisplay = virtualActivityDisplay;
4395 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004396 addToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004397 }
4398
4399 if (mSurface != null) {
4400 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004401 }
4402
Craig Mautner6985bad2014-04-21 15:22:06 -07004403 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004404 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004405 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004406 } else {
4407 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004408 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004409 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004410 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004411 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004412 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004413
Craig Mautnerd163e752014-06-13 17:18:47 -07004414 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004415
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004416 if (DEBUG_STACK) Slog.d(TAG_STACK,
4417 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004418 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004419
Craig Mautner6985bad2014-04-21 15:22:06 -07004420 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004421 boolean isAttachedLocked() {
4422 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004423 }
4424
4425 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004426 void setDrawn() {
4427 synchronized (mService) {
4428 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004429 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004430 }
4431 }
4432
Craig Mautner1b4bf852014-05-26 15:06:32 -07004433 // Never start a new task on an ActivityView if it isn't explicitly specified.
4434 @Override
4435 boolean isEligibleForNewTasks() {
4436 return false;
4437 }
4438
Craig Mautnerd163e752014-06-13 17:18:47 -07004439 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004440 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004441 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4442 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4443 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4444 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4445 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004446 }
4447 }
4448
Craig Mautner4a1cb222013-12-04 16:14:06 -08004449 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4450 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004451 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004452 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004453 int mDisplayId;
4454 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004455 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4456 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004457
Craig Mautner4a1cb222013-12-04 16:14:06 -08004458 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4459 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004460 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004461
Jose Lima4b6c6692014-08-12 17:41:12 -07004462 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004463
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004464 ActivityDisplay() {
4465 }
Craig Mautner4504de52013-12-20 09:06:56 -08004466
Craig Mautner1a70a162014-09-13 12:09:31 -07004467 // After instantiation, check that mDisplay is not null before using this. The alternative
4468 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004469 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004470 final Display display = mDisplayManager.getDisplay(displayId);
4471 if (display == null) {
4472 return;
4473 }
4474 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004475 }
4476
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004477 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004478 mDisplay = display;
4479 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004480 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004481
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004482 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004483 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004484 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4485 + " onTop=" + onTop);
4486 if (onTop) {
4487 mStacks.add(stack);
4488 } else {
4489 mStacks.add(0, stack);
4490 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491 }
4492
Craig Mautnere0a38842013-12-16 16:14:02 -08004493 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004494 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004495 + " from displayId=" + mDisplayId);
4496 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004497 }
4498
Jose Lima4b6c6692014-08-12 17:41:12 -07004499 void setVisibleBehindActivity(ActivityRecord r) {
4500 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004501 }
4502
Jose Lima4b6c6692014-08-12 17:41:12 -07004503 boolean hasVisibleBehindActivity() {
4504 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004505 }
4506
Craig Mautner34b73df2014-01-12 21:11:08 -08004507 @Override
4508 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004509 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4510 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004511
4512 @Override
4513 protected int getChildCount() {
4514 return mStacks.size();
4515 }
4516
4517 @Override
4518 protected ConfigurationContainer getChildAt(int index) {
4519 return mStacks.get(index);
4520 }
4521
4522 @Override
4523 protected ConfigurationContainer getParent() {
4524 return ActivityStackSupervisor.this;
4525 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004526 }
4527
4528 class VirtualActivityDisplay extends ActivityDisplay {
4529 VirtualDisplay mVirtualDisplay;
4530
Craig Mautner6985bad2014-04-21 15:22:06 -07004531 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004532 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004533 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4534 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4535 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4536 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004537
4538 init(mVirtualDisplay.getDisplay());
4539
4540 mWindowManager.handleDisplayAdded(mDisplayId);
4541 }
4542
4543 void setSurface(Surface surface) {
4544 if (mVirtualDisplay != null) {
4545 mVirtualDisplay.setSurface(surface);
4546 }
4547 }
4548
4549 @Override
4550 void detachActivitiesLocked(ActivityStack stack) {
4551 super.detachActivitiesLocked(stack);
4552 if (mVirtualDisplay != null) {
4553 mVirtualDisplay.release();
4554 mVirtualDisplay = null;
4555 }
4556 }
4557
4558 @Override
4559 public String toString() {
4560 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004561 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004562 }
Jose Lima58e66d62014-05-27 20:00:27 -07004563
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004564 ActivityStack findStackBehind(ActivityStack stack) {
4565 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004566 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004567 if (display == null) {
4568 return null;
4569 }
4570 final ArrayList<ActivityStack> stacks = display.mStacks;
4571 for (int i = stacks.size() - 1; i >= 0; i--) {
4572 if (stacks.get(i) == stack && i > 0) {
4573 return stacks.get(i - 1);
4574 }
4575 }
4576 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4577 + " in=" + stacks);
4578 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004579
Jorim Jaggic69bd222016-03-15 14:38:37 +01004580 /**
4581 * Puts a task into resizing mode during the next app transition.
4582 *
4583 * @param taskId the id of the task to put into resizing mode
4584 */
4585 private void setResizingDuringAnimation(int taskId) {
4586 mResizingTasksDuringAnimation.add(taskId);
4587 mWindowManager.setTaskDockedResizing(taskId, true);
4588 }
4589
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004590 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4591 final TaskRecord task;
4592 final int callingUid;
4593 final String callingPackage;
4594 final Intent intent;
4595 final int userId;
4596 final ActivityOptions activityOptions = (bOptions != null)
4597 ? new ActivityOptions(bOptions) : null;
4598 final int launchStackId = (activityOptions != null)
4599 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004600 if (launchStackId == HOME_STACK_ID) {
4601 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4602 + taskId + " can't be launch in the home stack.");
4603 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004604
4605 if (launchStackId == DOCKED_STACK_ID) {
4606 mWindowManager.setDockedStackCreateState(
4607 activityOptions.getDockCreateMode(), null /* initialBounds */);
4608
4609 // Defer updating the stack in which recents is until the app transition is done, to
4610 // not run into issues where we still need to draw the task in recents but the
4611 // docked stack is already created.
4612 deferUpdateBounds(HOME_STACK_ID);
4613 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4614 }
4615
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004616 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4617 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004618 continueUpdateBounds(HOME_STACK_ID);
4619 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004620 throw new IllegalArgumentException(
4621 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4622 }
4623
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004624 // Since we don't have an actual source record here, we assume that the currently focused
4625 // activity was the source.
4626 final ActivityStack focusedStack = getFocusedStack();
4627 final ActivityRecord sourceRecord =
4628 focusedStack != null ? focusedStack.topActivity() : null;
4629
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004630 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004631 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004632 moveTaskToStackLocked(
4633 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4634 ANIMATE);
4635 }
4636 }
4637
4638 // If the user must confirm credentials (e.g. when first launching a work app and the
4639 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4640 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4641 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004642 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004643 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004644 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004645 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4646 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004647
4648 // If we are launching the task in the docked stack, put it into resizing mode so
4649 // the window renders full-screen with the background filling the void. Also only
4650 // call this at the end to make sure that tasks exists on the window manager side.
4651 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004652 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004653 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004654
4655 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4656 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004657 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4658 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004659 return ActivityManager.START_TASK_TO_FRONT;
4660 }
4661 callingUid = task.mCallingUid;
4662 callingPackage = task.mCallingPackage;
4663 intent = task.intent;
4664 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4665 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004666 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4667 null, null, 0, 0, bOptions, userId, null, task);
4668 if (launchStackId == DOCKED_STACK_ID) {
4669 setResizingDuringAnimation(task.taskId);
4670 }
4671 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004672 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004673
4674 /**
4675 * @return a list of activities which are the top ones in each visible stack. The first
4676 * entry will be the focused activity.
4677 */
4678 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004679 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4680 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004681 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004682 if (display == null) {
4683 return Collections.EMPTY_LIST;
4684 }
4685 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4686 final ArrayList<ActivityStack> stacks = display.mStacks;
4687 for (int i = stacks.size() - 1; i >= 0; i--) {
4688 ActivityStack stack = stacks.get(i);
4689 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4690 ActivityRecord top = stack.topActivity();
4691 if (top != null) {
4692 if (stack == mFocusedStack) {
4693 topActivityTokens.add(0, top.appToken);
4694 } else {
4695 topActivityTokens.add(top.appToken);
4696 }
4697 }
4698 }
4699 }
4700 return topActivityTokens;
4701 }
Craig Mautner27084302013-03-25 08:05:25 -07004702}