blob: dde948f2d6f94d5ba52cb6d6f4668d9f4e0eae3b [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Jorim Jaggife762342016-10-13 14:33:27 +020019import static android.Manifest.permission.START_ANY_ACTIVITY;
20import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
21import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
22import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
23import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
24import static android.app.ActivityManager.RESIZE_MODE_FORCED;
25import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
26import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -080036import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020037import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
38import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
39import static android.content.pm.PackageManager.PERMISSION_GRANTED;
40import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
41import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070042import static android.view.Display.FLAG_PRIVATE;
43import static android.view.Display.INVALID_DISPLAY;
Jorim Jaggife762342016-10-13 14:33:27 +020044
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
74import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
77import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
78import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
81import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
84import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
85import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
86import static com.android.server.am.ActivityStack.STACK_VISIBLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
92import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
93
Svetoslav7008b512015-06-24 18:47:07 -070094import android.Manifest;
Andrii Kulian16802aa2016-11-02 12:21:33 -070095import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +010096import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080099import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700100import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.app.ActivityOptions;
103import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700106import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400108import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700109import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400110import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700112import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700114import android.content.Intent;
115import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700117import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.pm.PackageManager;
119import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100120import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700122import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800123import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124import android.hardware.display.DisplayManager;
125import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800126import android.hardware.display.DisplayManagerGlobal;
127import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800128import android.hardware.input.InputManager;
129import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100131import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700132import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700133import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700135import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700136import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700138import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700139import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700140import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400141import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700143import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700144import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700145import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100146import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700147import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700148import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400149import android.provider.Settings;
150import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700151import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700152import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700153import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700154import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700155import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700156import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800157import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800158import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800159import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800160import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800161import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800162
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800163import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700164import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400165import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700166import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400167import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800168import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700169import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700171
Craig Mautner8d341ef2013-03-26 09:03:27 -0700172import java.io.FileDescriptor;
173import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700174import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700175import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700176import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700177import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700178import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800179import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700180import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700181
Jorim Jaggife762342016-10-13 14:33:27 +0200182public class ActivityStackSupervisor extends ConfigurationContainer
Andrii Kulian1779e612016-10-12 21:58:25 -0700183 implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700186 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700188 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800195 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800197
Craig Mautnerf3333272013-04-22 10:55:53 -0700198 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700200
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700203
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206
Craig Mautner05d29032013-05-03 13:40:13 -0700207 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
208 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
209 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
213 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
214 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700215 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400216 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
217 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700218 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700219 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700220 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800221 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
222 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700225
Jason Monk62515be2014-05-21 16:06:19 -0400226 private static final String LOCK_TASK_TAG = "Lock-to-App";
227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 // Used to indicate if an object (e.g. stack) that we are trying to get
229 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800230 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700232 // Used to indicate that windows of activities should be preserved during the resize.
233 static final boolean PRESERVE_WINDOWS = true;
234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. task) should be moved/created
236 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700237 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
239 // Used to indicate that an objects (e.g. task) removal from its container
240 // (e.g. stack) is due to it moving to another container.
241 static final boolean MOVING = true;
242
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700243 // Force the focus to change to the stack we are moving a task to..
244 static final boolean FORCE_FOCUS = true;
245
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700246 // Restore task from the saved recents if it can't be found in any live stack.
247 static final boolean RESTORE_FROM_RECENTS = true;
248
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700249 // Don't execute any calls to resume.
250 static final boolean DEFER_RESUME = true;
251
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. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800664 boolean moveHomeStackTaskToTop(String reason) {
665 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700666
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700667 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700668 if (top == null) {
669 return false;
670 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700671 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700672 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700673 }
674
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800675 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700676 if (!mService.mBooting && !mService.mBooted) {
677 // Not ready yet!
678 return false;
679 }
680
Craig Mautner84984fa2014-06-19 11:19:20 -0700681 if (prev != null) {
682 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
683 }
684
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800685 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700686 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800687 final String myReason = reason + " resumeHomeStackTask";
688
Mark Lua56ea122015-10-08 13:31:01 +0800689 // Only resume home activity if isn't finishing.
690 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700691 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800692 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700693 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800694 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700695 }
696
Craig Mautner8d341ef2013-03-26 09:03:27 -0700697 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700698 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700699 }
700
701 /**
702 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
703 * @param id Id of the task we would like returned.
704 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
705 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700706 * @param stackId The stack to restore the task to (default launch stack will be used if
707 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700708 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700709 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800710 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800712 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
714 ActivityStack stack = stacks.get(stackNdx);
715 TaskRecord task = stack.taskForIdLocked(id);
716 if (task != null) {
717 return task;
718 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700719 }
720 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800721
722 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700723 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800724 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800725 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700726 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800727 return null;
728 }
729
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700730 if (!restoreFromRecents) {
731 return task;
732 }
733
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700734 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700735 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
736 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800737 return null;
738 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700739 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800740 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700741 }
742
Craig Mautner6170f732013-04-02 13:05:23 -0700743 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800746 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800747 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
748 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
749 if (r != null) {
750 return r;
751 }
Craig Mautner6170f732013-04-02 13:05:23 -0700752 }
753 }
754 return null;
755 }
756
Tony Mak853304c2016-04-18 15:17:41 +0100757 /**
758 * TODO: Handle freefom mode.
759 * @return true when credential confirmation is needed for the user and there is any
760 * activity started by the user in any visible stack.
761 */
762 boolean isUserLockedProfile(@UserIdInt int userId) {
763 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
764 return false;
765 }
766 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
767 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
768 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
769 for (final ActivityStack activityStack : activityStacks) {
770 if (activityStack == null) {
771 continue;
772 }
773 if (activityStack.topRunningActivityLocked() == null) {
774 continue;
775 }
776 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
777 continue;
778 }
779 if (activityStack.isDockedStack() && mIsDockMinimized) {
780 continue;
781 }
782 final TaskRecord topTask = activityStack.topTask();
783 if (topTask == null) {
784 continue;
785 }
786 // To handle the case that work app is in the task but just is not the top one.
787 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
788 final ActivityRecord activityRecord = topTask.mActivities.get(i);
789 if (activityRecord.userId == userId) {
790 return true;
791 }
792 }
793 }
794 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000795 }
796
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800797 void setNextTaskIdForUserLocked(int taskId, int userId) {
798 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
799 if (taskId > currentTaskId) {
800 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700801 }
802 }
803
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700804 static int nextTaskIdForUser(int taskId, int userId) {
805 int nextTaskId = taskId + 1;
806 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
807 // Wrap around as there will be smaller task ids that are available now.
808 nextTaskId -= MAX_TASK_IDS_PER_USER;
809 }
810 return nextTaskId;
811 }
812
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800813 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800814 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
815 // for a userId u, a taskId can only be in the range
816 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
817 // 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 -0700818 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800819 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
820 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
821 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700822 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800823 if (candidateTaskId == currentTaskId) {
824 // Something wrong!
825 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
826 throw new IllegalStateException("Cannot get an available task id."
827 + " Reached limit of " + MAX_TASK_IDS_PER_USER
828 + " running tasks per user.");
829 }
830 }
831 mCurTaskIdForUser.put(userId, candidateTaskId);
832 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700833 }
834
Chong Zhang6cda19c2016-06-14 19:07:56 -0700835 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800836 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700837 if (stack == null) {
838 return null;
839 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700840 ActivityRecord resumedActivity = stack.mResumedActivity;
841 if (resumedActivity == null || resumedActivity.app == null) {
842 resumedActivity = stack.mPausingActivity;
843 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700844 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700845 }
846 }
847 return resumedActivity;
848 }
849
Dianne Hackbornff072722014-09-24 10:56:28 -0700850 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700851 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800852 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800853 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
854 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800855 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
856 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700857 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 continue;
859 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700860 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 if (hr != null) {
862 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
863 && processName.equals(hr.processName)) {
864 try {
George Mount2c92c972014-03-20 09:38:23 -0700865 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 didSomething = true;
867 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700868 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 Slog.w(TAG, "Exception in new application when starting activity "
870 + hr.intent.getComponent().flattenToShortString(), e);
871 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700872 }
Craig Mautner20e72272013-04-01 13:45:53 -0700873 }
Craig Mautner20e72272013-04-01 13:45:53 -0700874 }
875 }
876 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700877 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700878 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700879 }
Craig Mautner20e72272013-04-01 13:45:53 -0700880 return didSomething;
881 }
882
883 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800884 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
885 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
887 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700888 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 continue;
890 }
891 final ActivityRecord resumedActivity = stack.mResumedActivity;
892 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700893 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800894 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800895 return false;
896 }
Craig Mautner20e72272013-04-01 13:45:53 -0700897 }
898 }
Wei Wang65c7a152016-06-02 18:51:22 -0700899 // Send launch end powerhint when idle
900 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700901 return true;
902 }
903
Craig Mautnerde4ef022013-04-07 19:01:33 -0700904 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
906 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800907 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
908 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700909 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700911 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 return false;
913 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 }
915 }
916 }
917 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700918 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800919 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
920 mLastFocusedStack + " to=" + mFocusedStack);
921 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700922 return true;
923 }
924
925 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800926 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
928 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
930 final ActivityStack stack = stacks.get(stackNdx);
931 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800932 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700933 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800934 return false;
935 }
936 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800937 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 }
939 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800940 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700941 }
942
Craig Mautner2acc3892013-09-23 10:28:14 -0700943 /**
944 * Pause all activities in either all of the stacks or just the back stacks.
945 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700946 * @param resuming The resuming activity.
947 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
948 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700949 * @return true if any activity was paused as a result of this call.
950 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700951 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700952 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
954 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800955 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
956 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700957 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700958 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700960 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
961 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700963 }
964 }
965 return someActivityPaused;
966 }
967
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700969 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
971 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
973 final ActivityStack stack = stacks.get(stackNdx);
974 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700975 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700977 Slog.d(TAG_STATES,
978 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 pausing = false;
980 } else {
981 return false;
982 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700983 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700984 }
985 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700986 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 }
988
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700989 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700990 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500991 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
993 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
994 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
995 final ActivityStack stack = stacks.get(stackNdx);
996 if (stack.mResumedActivity != null &&
997 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700998 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800999 }
1000 }
1001 }
1002 }
1003
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001004 void cancelInitializingActivities() {
1005 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1006 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1007 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1008 stacks.get(stackNdx).cancelInitializingActivities();
1009 }
1010 }
1011 }
1012
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001013 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001014 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001015 }
1016
1017 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1018 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001019 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001020 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001021 if (w.who == null) {
1022 changed = true;
1023 w.timeout = false;
1024 if (r != null) {
1025 w.who = new ComponentName(r.info.packageName, r.info.name);
1026 }
1027 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1028 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001029 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001030 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001031 if (changed) {
1032 mService.notifyAll();
1033 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001034 }
1035
Chong Zhang5022da32016-06-21 16:31:37 -07001036 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1037 boolean changed = false;
1038 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1039 WaitResult w = mWaitingActivityLaunched.remove(i);
1040 if (w.who == null) {
1041 changed = true;
1042 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1043 // the starting activity ends up moving another activity to front, and it should
1044 // wait for this new activity to become visible instead.
1045 // Do not modify other fields.
1046 w.result = START_TASK_TO_FRONT;
1047 }
1048 }
1049 if (changed) {
1050 mService.notifyAll();
1051 }
1052 }
1053
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001054 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1055 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001056 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001057 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001058 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001059 if (w.who == null) {
1060 changed = true;
1061 w.timeout = timeout;
1062 if (r != null) {
1063 w.who = new ComponentName(r.info.packageName, r.info.name);
1064 }
1065 w.thisTime = thisTime;
1066 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001067 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001069 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001070 if (changed) {
1071 mService.notifyAll();
1072 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001073 }
1074
Craig Mautner29219d92013-04-16 20:19:12 -07001075 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001076 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001077 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001078 if (r != null) {
1079 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001080 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001081
Andrii Kulian7318d632016-07-20 18:59:28 -07001082 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001083 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001084 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1085 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001086 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001087 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001088 if (r != null) {
1089 return r;
1090 }
1091 }
1092 }
1093 return null;
1094 }
1095
Dianne Hackborn09233282014-04-30 11:33:59 -07001096 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001097 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001098 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1099 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001100 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001101 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001102 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001103 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1104 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001105 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001106 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001107 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001108 }
1109 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001110
1111 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1112 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1113 while (maxNum > 0) {
1114 long mostRecentActiveTime = Long.MIN_VALUE;
1115 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001116 final int numTaskLists = runningTaskLists.size();
1117 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1118 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001119 if (!stackTaskList.isEmpty()) {
1120 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1121 if (lastActiveTime > mostRecentActiveTime) {
1122 mostRecentActiveTime = lastActiveTime;
1123 selectedStackList = stackTaskList;
1124 }
1125 }
1126 }
1127 if (selectedStackList != null) {
1128 list.add(selectedStackList.remove(0));
1129 --maxNum;
1130 } else {
1131 break;
1132 }
1133 }
Craig Mautner20e72272013-04-01 13:45:53 -07001134 }
1135
Todd Kennedy7440f172015-12-09 14:31:22 -08001136 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1137 ProfilerInfo profilerInfo) {
1138 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001139 if (aInfo != null) {
1140 // Store the found target back into the intent, because now that
1141 // we have it we never want to do this again. For example, if the
1142 // user navigates back to this point in the history, we should
1143 // always restart the exact same activity.
1144 intent.setComponent(new ComponentName(
1145 aInfo.applicationInfo.packageName, aInfo.name));
1146
1147 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001148 if (!aInfo.processName.equals("system")) {
1149 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001150 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001151 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001152
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001153 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1154 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1155 }
1156
Man Caocfa78b22015-06-11 20:14:34 -07001157 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1158 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1159 }
1160
1161 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001162 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001163 }
1164 }
1165 }
1166 return aInfo;
1167 }
1168
Todd Kennedy7440f172015-12-09 14:31:22 -08001169 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001170 return resolveIntent(intent, resolvedType, userId, 0);
1171 }
1172
1173 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001174 try {
1175 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001176 PackageManager.MATCH_DEFAULT_ONLY | flags
1177 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001178 } catch (RemoteException e) {
1179 }
1180 return null;
1181 }
1182
1183 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1184 ProfilerInfo profilerInfo, int userId) {
1185 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1186 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1187 }
1188
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001189 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1190 boolean andResume, boolean checkConfig) throws RemoteException {
1191
1192 if (!allPausedActivitiesComplete()) {
1193 // While there are activities pausing we skipping starting any new activities until
1194 // pauses are complete. NOTE: that we also do this for activities that are starting in
1195 // the paused state because they will first be resumed then paused on the client side.
1196 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1197 "realStartActivityLocked: Skipping start of r=" + r
1198 + " some activities pausing...");
1199 return false;
1200 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001201
Craig Mautner2568c3a2015-03-26 14:22:34 -07001202 if (andResume) {
1203 r.startFreezingScreenLocked(app, 0);
1204 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001205
Craig Mautner2568c3a2015-03-26 14:22:34 -07001206 // schedule launch ticks to collect information about slow apps.
1207 r.startLaunchTickingLocked();
1208 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001209
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001210 // Have the window manager re-evaluate the orientation of the screen based on the new
1211 // activity order. Note that as a result of this, it can call back into the activity
1212 // manager with a new orientation. We don't care about that, because the activity is not
1213 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001215 final int displayId = r.getDisplayId();
1216 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1217 getDisplayOverrideConfiguration(displayId),
1218 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001219 // Deferring resume here because we're going to launch new activity shortly.
1220 // We don't want to perform a redundant launch of the same record while ensuring
1221 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001222 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1223 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 }
1225
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001226 if (mKeyguardController.isKeyguardLocked()) {
1227 mWindowManager.notifyUnknownAppVisibilityLaunched(r.appToken);
1228 }
1229
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 r.app = app;
1231 app.waitingToKill = null;
1232 r.launchCount++;
1233 r.lastLaunchTime = SystemClock.uptimeMillis();
1234
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001235 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236
1237 int idx = app.activities.indexOf(r);
1238 if (idx < 0) {
1239 app.activities.add(r);
1240 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001241 mService.updateLruProcessLocked(app, true, null);
1242 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001243
Craig Mautner15df08a2015-04-01 12:17:18 -07001244 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001245 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1246 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001247 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001248 }
1249
Andrii Kulian02b7a832016-10-06 23:11:56 -07001250 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 try {
1252 if (app.thread == null) {
1253 throw new RemoteException();
1254 }
1255 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001256 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001257 if (andResume) {
1258 results = r.results;
1259 newIntents = r.newIntents;
1260 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001261 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1262 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1263 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001264 if (andResume) {
1265 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1266 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001267 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001269 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001270 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001273 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1274 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 r.sleeping = false;
1276 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001277 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 mService.showAskCompatModeDialogLocked(r);
1279 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001280 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1282 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1283 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001284 final String profileFile = mService.mProfileFile;
1285 if (profileFile != null) {
1286 ParcelFileDescriptor profileFd = mService.mProfileFd;
1287 if (profileFd != null) {
1288 try {
1289 profileFd = profileFd.dup();
1290 } catch (IOException e) {
1291 if (profileFd != null) {
1292 try {
1293 profileFd.close();
1294 } catch (IOException o) {
1295 }
1296 profileFd = null;
1297 }
1298 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001300
1301 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1302 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 }
1304 }
1305 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001306
Craig Mautner2568c3a2015-03-26 14:22:34 -07001307 if (andResume) {
1308 app.hasShownUi = true;
1309 app.pendingUiClean = true;
1310 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001311 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001312 // Because we could be starting an Activity in the system process this may not go across
1313 // a Binder interface which would create a new Configuration. Consequently we have to
1314 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001316 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001317 new Configuration(mService.getGlobalConfiguration()),
1318 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1319 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1320 r.persistentState, results, newIntents, !andResume,
1321 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001322
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001323 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 // This may be a heavy-weight process! Note that the package
1325 // manager will ensure that only activity can run in the main
1326 // process of the .apk, which is the only thing that will be
1327 // considered heavy-weight.
1328 if (app.processName.equals(app.info.packageName)) {
1329 if (mService.mHeavyWeightProcess != null
1330 && mService.mHeavyWeightProcess != app) {
1331 Slog.w(TAG, "Starting new heavy weight process " + app
1332 + " when already running "
1333 + mService.mHeavyWeightProcess);
1334 }
1335 mService.mHeavyWeightProcess = app;
1336 Message msg = mService.mHandler.obtainMessage(
1337 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1338 msg.obj = r;
1339 mService.mHandler.sendMessage(msg);
1340 }
1341 }
1342
1343 } catch (RemoteException e) {
1344 if (r.launchFailed) {
1345 // This is the second time we failed -- finish activity
1346 // and give up.
1347 Slog.e(TAG, "Second failure launching "
1348 + r.intent.getComponent().flattenToShortString()
1349 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001350 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001351 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1352 "2nd-crash", false);
1353 return false;
1354 }
1355
1356 // This is the first time we failed -- restart process and
1357 // retry.
1358 app.activities.remove(r);
1359 throw e;
1360 }
1361
1362 r.launchFailed = false;
1363 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001364 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001365 }
1366
1367 if (andResume) {
1368 // As part of the process of launching, ActivityThread also performs
1369 // a resume.
1370 stack.minimalResumeActivityLocked(r);
1371 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001372 // This activity is not starting in the resumed state... which should look like we asked
1373 // it to pause+stop (but remain visible), and it has done so and reported back the
1374 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001375 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001376 "Moving to PAUSED: " + r + " (starting in paused state)");
1377 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 }
1379
1380 // Launch the new version setup screen if needed. We do this -after-
1381 // launching the initial activity (that is, home), so that it can have
1382 // a chance to initialize itself while in the background, making the
1383 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001384 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 mService.startSetupActivityLocked();
1386 }
1387
Dianne Hackborn465fa392014-09-14 14:21:18 -07001388 // Update any services we are bound to that might care about whether
1389 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001390 if (r.app != null) {
1391 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1392 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001393
Craig Mautner2420ead2013-04-01 17:13:20 -07001394 return true;
1395 }
1396
Craig Mautnere79d42682013-04-01 19:01:53 -07001397 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001398 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001399 // Is this activity's application already running?
1400 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001401 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001402
Andrii Kulian02b7a832016-10-06 23:11:56 -07001403 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001404
1405 if (app != null && app.thread != null) {
1406 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001407 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1408 || !"android".equals(r.info.packageName)) {
1409 // Don't add this if it is a platform component that is marked
1410 // to run in multiple processes, because this is actually
1411 // part of the framework so doesn't make sense to track as a
1412 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001413 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1414 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001415 }
George Mount2c92c972014-03-20 09:38:23 -07001416 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001417 return;
1418 } catch (RemoteException e) {
1419 Slog.w(TAG, "Exception when starting activity "
1420 + r.intent.getComponent().flattenToShortString(), e);
1421 }
1422
1423 // If a dead object exception was thrown -- fall through to
1424 // restart the application.
1425 }
1426
1427 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001428 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001429 }
1430
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001431 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001432 String resultWho, int requestCode, int callingPid, int callingUid,
1433 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001434 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001435 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1436 callingUid);
1437 if (startAnyPerm == PERMISSION_GRANTED) {
1438 return true;
1439 }
1440 final int componentRestriction = getComponentRestrictionForCallingPackage(
1441 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1442 final int actionRestriction = getActionRestrictionForCallingPackage(
1443 intent.getAction(), callingPackage, callingPid, callingUid);
1444 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1445 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1446 if (resultRecord != null) {
1447 resultStack.sendActivityResultLocked(-1,
1448 resultRecord, resultWho, requestCode,
1449 Activity.RESULT_CANCELED, null);
1450 }
1451 final String msg;
1452 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1453 msg = "Permission Denial: starting " + intent.toString()
1454 + " from " + callerApp + " (pid=" + callingPid
1455 + ", uid=" + callingUid + ")" + " with revoked permission "
1456 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1457 } else if (!aInfo.exported) {
1458 msg = "Permission Denial: starting " + intent.toString()
1459 + " from " + callerApp + " (pid=" + callingPid
1460 + ", uid=" + callingUid + ")"
1461 + " not exported from uid " + aInfo.applicationInfo.uid;
1462 } else {
1463 msg = "Permission Denial: starting " + intent.toString()
1464 + " from " + callerApp + " (pid=" + callingPid
1465 + ", uid=" + callingUid + ")"
1466 + " requires " + aInfo.permission;
1467 }
1468 Slog.w(TAG, msg);
1469 throw new SecurityException(msg);
1470 }
1471
1472 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1473 final String message = "Appop Denial: starting " + intent.toString()
1474 + " from " + callerApp + " (pid=" + callingPid
1475 + ", uid=" + callingUid + ")"
1476 + " requires " + AppOpsManager.permissionToOp(
1477 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1478 Slog.w(TAG, message);
1479 return false;
1480 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1481 final String message = "Appop Denial: starting " + intent.toString()
1482 + " from " + callerApp + " (pid=" + callingPid
1483 + ", uid=" + callingUid + ")"
1484 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1485 Slog.w(TAG, message);
1486 return false;
1487 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001488 if (options != null) {
1489 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1490 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1491 callingPid, callingUid);
1492 if (startInTaskPerm != PERMISSION_GRANTED) {
1493 final String msg = "Permission Denial: starting " + intent.toString()
1494 + " from " + callerApp + " (pid=" + callingPid
1495 + ", uid=" + callingUid + ") with launchTaskId="
1496 + options.getLaunchTaskId();
1497 Slog.w(TAG, msg);
1498 throw new SecurityException(msg);
1499 }
1500 }
1501 // Check if someone tries to launch an activity on a private display with a different
1502 // owner.
1503 final int launchDisplayId = options.getLaunchDisplayId();
1504 if (launchDisplayId != INVALID_DISPLAY) {
1505 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1506 if (activityDisplay != null
1507 && (activityDisplay.mDisplay.getFlags() & FLAG_PRIVATE) != 0) {
1508 if (activityDisplay.mDisplay.getOwnerUid() != callingUid) {
1509 final String msg = "Permission Denial: starting " + intent.toString()
1510 + " from " + callerApp + " (pid=" + callingPid
1511 + ", uid=" + callingUid + ") with launchDisplayId="
1512 + launchDisplayId;
1513 Slog.w(TAG, msg);
1514 throw new SecurityException(msg);
1515 }
1516 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001517 }
1518 }
1519
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001520 return true;
1521 }
1522
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001523 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001524 final long identity = Binder.clearCallingIdentity();
1525 try {
1526 return UserManager.get(mService.mContext).getUserInfo(userId);
1527 } finally {
1528 Binder.restoreCallingIdentity(identity);
1529 }
1530 }
1531
Svet Ganov99b60432015-06-27 13:15:22 -07001532 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001533 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001534 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1535 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001536 == PackageManager.PERMISSION_DENIED) {
1537 return ACTIVITY_RESTRICTION_PERMISSION;
1538 }
1539
Christopher Tateff7add02015-08-17 10:23:22 -07001540 if (activityInfo.permission == null) {
1541 return ACTIVITY_RESTRICTION_NONE;
1542 }
1543
Svet Ganov99b60432015-06-27 13:15:22 -07001544 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1545 if (opCode == AppOpsManager.OP_NONE) {
1546 return ACTIVITY_RESTRICTION_NONE;
1547 }
1548
1549 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1550 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001551 if (!ignoreTargetSecurity) {
1552 return ACTIVITY_RESTRICTION_APPOP;
1553 }
Svet Ganov99b60432015-06-27 13:15:22 -07001554 }
1555
1556 return ACTIVITY_RESTRICTION_NONE;
1557 }
1558
Svetoslav7008b512015-06-24 18:47:07 -07001559 private int getActionRestrictionForCallingPackage(String action,
1560 String callingPackage, int callingPid, int callingUid) {
1561 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001562 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001563 }
1564
1565 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1566 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001567 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001568 }
1569
1570 final PackageInfo packageInfo;
1571 try {
1572 packageInfo = mService.mContext.getPackageManager()
1573 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1574 } catch (PackageManager.NameNotFoundException e) {
1575 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001576 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001577 }
1578
1579 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001580 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001581 }
1582
1583 if (mService.checkPermission(permission, callingPid, callingUid) ==
1584 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001585 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001586 }
1587
1588 final int opCode = AppOpsManager.permissionToOpCode(permission);
1589 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001590 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001591 }
1592
1593 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1594 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001595 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001596 }
1597
Svet Ganov99b60432015-06-27 13:15:22 -07001598 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001599 }
1600
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001601 void setLaunchSource(int uid) {
1602 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1603 }
1604
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001605 void acquireLaunchWakelock() {
1606 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1607 throw new IllegalStateException("Calling must be system uid");
1608 }
1609 mLaunchingActivity.acquire();
1610 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1611 // To be safe, don't allow the wake lock to be held for too long.
1612 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1613 }
1614 }
1615
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001616 /**
1617 * Called when the frontmost task is idle.
1618 * @return the state of mService.mBooting before this was called.
1619 */
1620 private boolean checkFinishBootingLocked() {
1621 final boolean booting = mService.mBooting;
1622 boolean enableScreen = false;
1623 mService.mBooting = false;
1624 if (!mService.mBooted) {
1625 mService.mBooted = true;
1626 enableScreen = true;
1627 }
1628 if (booting || enableScreen) {
1629 mService.postFinishBooting(booting, enableScreen);
1630 }
1631 return booting;
1632 }
1633
Craig Mautnerf3333272013-04-22 10:55:53 -07001634 // Checked.
1635 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1636 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001637 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001638
Craig Mautnerf3333272013-04-22 10:55:53 -07001639 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001640 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001641 int NS = 0;
1642 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001643 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001644 boolean activityRemoved = false;
1645
Wale Ogunwale7d701172015-03-11 15:36:30 -07001646 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001647 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001648 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1649 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001650 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1651 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001652 if (fromTimeout) {
1653 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001654 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001655
1656 // This is a hack to semi-deal with a race condition
1657 // in the client where it can be constructed with a
1658 // newer configuration from when we asked it to launch.
1659 // We'll update with whatever configuration it now says
1660 // it used to launch.
1661 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001662 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001663 }
1664
1665 // We are now idle. If someone is waiting for a thumbnail from
1666 // us, we can now deliver.
1667 r.idle = true;
1668
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001669 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001670 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001671 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001672 }
1673 }
1674
1675 if (allResumedActivitiesIdle()) {
1676 if (r != null) {
1677 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001678 }
1679
1680 if (mLaunchingActivity.isHeld()) {
1681 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1682 if (VALIDATE_WAKE_LOCK_CALLER &&
1683 Binder.getCallingUid() != Process.myUid()) {
1684 throw new IllegalStateException("Calling must be system uid");
1685 }
1686 mLaunchingActivity.release();
1687 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001688 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001689 }
1690
1691 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001692 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001693 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001694 if ((NF = mFinishingActivities.size()) > 0) {
1695 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001696 mFinishingActivities.clear();
1697 }
1698
Craig Mautnerf3333272013-04-22 10:55:53 -07001699 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001700 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001701 mStartingUsers.clear();
1702 }
1703
Craig Mautnerf3333272013-04-22 10:55:53 -07001704 // Stop any activities that are scheduled to do so but have been
1705 // waiting for the next one to start.
1706 for (int i = 0; i < NS; i++) {
1707 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001708 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001709 if (stack != null) {
1710 if (r.finishing) {
1711 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1712 } else {
1713 stack.stopActivityLocked(r);
1714 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001715 }
1716 }
1717
1718 // Finish any activities that are scheduled to do so but have been
1719 // waiting for the next one to start.
1720 for (int i = 0; i < NF; i++) {
1721 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001722 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001723 if (stack != null) {
1724 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1725 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001726 }
1727
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001728 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001729 // Complete user switch
1730 if (startingUsers != null) {
1731 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001732 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001733 }
1734 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001735 }
1736
1737 mService.trimApplications();
1738 //dump();
1739 //mWindowManager.dump();
1740
Craig Mautnerf3333272013-04-22 10:55:53 -07001741 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001742 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001743 }
1744
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001745 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001746 }
1747
Craig Mautner8e569572013-10-11 17:36:59 -07001748 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001749 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001752 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1753 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1754 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001755 }
Craig Mautner19091252013-10-05 00:03:53 -07001756 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001757 }
1758
1759 void closeSystemDialogsLocked() {
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 stacks.get(stackNdx).closeSystemDialogsLocked();
1764 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001765 }
1766 }
1767
Craig Mautner93529a42013-10-04 15:03:13 -07001768 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001769 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001770 }
1771
Craig Mautner8d341ef2013-03-26 09:03:27 -07001772 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001773 * Update the last used stack id for non-current user (current user's last
1774 * used stack is the focused stack)
1775 */
1776 void updateUserStackLocked(int userId, ActivityStack stack) {
1777 if (userId != mCurrentUser) {
1778 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1779 }
1780 }
1781
1782 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001783 * @return true if some activity was finished (or would have finished if doit were true).
1784 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001785 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1786 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001787 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001788 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1789 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001791 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001792 if (stack.finishDisabledPackageActivitiesLocked(
1793 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001794 didSomething = true;
1795 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001796 }
1797 }
1798 return didSomething;
1799 }
1800
Dianne Hackborna413dc02013-07-12 12:02:55 -07001801 void updatePreviousProcessLocked(ActivityRecord r) {
1802 // Now that this process has stopped, we may want to consider
1803 // it to be the previous app to try to keep around in case
1804 // the user wants to return to it.
1805
1806 // First, found out what is currently the foreground app, so that
1807 // we don't blow away the previous app if this activity is being
1808 // hosted by the process that is actually still the foreground.
1809 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001810 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1811 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1813 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001814 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001815 if (stack.mResumedActivity != null) {
1816 fgApp = stack.mResumedActivity.app;
1817 } else if (stack.mPausingActivity != null) {
1818 fgApp = stack.mPausingActivity.app;
1819 }
1820 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001821 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001822 }
1823 }
1824
1825 // Now set this one as the previous process, only if that really
1826 // makes sense to.
1827 if (r.app != null && fgApp != null && r.app != fgApp
1828 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001829 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001830 mService.mPreviousProcess = r.app;
1831 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1832 }
1833 }
1834
Wale Ogunwaled046a012015-12-24 13:05:59 -08001835 boolean resumeFocusedStackTopActivityLocked() {
1836 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001837 }
1838
Wale Ogunwaled046a012015-12-24 13:05:59 -08001839 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001840 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001841 if (targetStack != null && isFocusedStack(targetStack)) {
1842 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001843 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001844 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1845 if (r == null || r.state != RESUMED) {
1846 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1847 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001848 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001849 }
1850
Todd Kennedy39bfee52016-02-24 10:28:21 -08001851 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1852 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1853 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1854 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1855 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1856 }
1857 }
1858 }
1859
Adrian Roos20d7df32016-01-12 18:59:43 +01001860 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1861 TaskRecord finishedTask = null;
1862 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001863 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1864 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001865 final int numStacks = stacks.size();
1866 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1867 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001868 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1869 if (stack == focusedStack || finishedTask == null) {
1870 finishedTask = t;
1871 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001872 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001873 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001874 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001875 }
1876
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001877 void finishVoiceTask(IVoiceInteractionSession session) {
1878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1879 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1880 final int numStacks = stacks.size();
1881 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1882 final ActivityStack stack = stacks.get(stackNdx);
1883 stack.finishVoiceTask(session);
1884 }
1885 }
1886 }
1887
Andrii Kulianc27916642016-04-12 17:59:27 -07001888 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1889 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001890 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1891 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001892 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001893 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1894 // Caller wants the home activity moved with it. To accomplish this,
1895 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001896 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001897 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001898 ActivityStack currentStack = task.getStack();
1899 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001900 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1901 + task + " to front. Stack is null");
1902 return;
1903 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001904
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001905 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001906 int stackId = options.getLaunchStackId();
1907 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001908 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001909 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001910 if (stackId == INVALID_STACK_ID) {
1911 stackId = task.getLaunchStackId();
1912 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001913 if (stackId != currentStack.mStackId) {
1914 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1915 !FORCE_FOCUS, reason);
1916 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001917 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1918 // still need moveTaskToFrontLocked() below for any transition settings.
1919 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001920 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1921 resizeStackLocked(stackId, bounds,
1922 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001923 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001924 } else {
1925 // WM resizeTask must be done after the task is moved to the correct stack,
1926 // because Task's setBounds() also updates dim layer's bounds, but that has
1927 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001928 mWindowManager.resizeTask(task.taskId, task.mBounds,
1929 task.getOverrideConfiguration(), false /* relayout */,
1930 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001931 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001932 }
1933 }
1934
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001935 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001936 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001937 r == null ? null : r.appTimeTracker, reason);
1938
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001939 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001940 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001941
Andrii Kulian02b7a832016-10-06 23:11:56 -07001942 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001943 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001944 }
1945
Wale Ogunwale854809c2015-12-27 16:18:19 -08001946 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001947 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001948 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001949 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001950 return false;
1951 }
1952 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1953 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001954 }
1955
Craig Mautner967212c2013-04-13 21:10:58 -07001956 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001957 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001958 }
1959
1960 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001961 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001962 if (activityContainer != null) {
1963 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001964 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001965 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001966 return null;
1967 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001968 // TODO(multi-display): Allow creating stacks on secondary displays.
Jorim Jaggife762342016-10-13 14:33:27 +02001969 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001970 }
1971
Andrii Kulian16802aa2016-11-02 12:21:33 -07001972 /**
1973 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
1974 * If there is no such stack, new dynamic stack can be created.
1975 * @param displayId Target display.
1976 * @param r Activity that should be launched there.
1977 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
1978 */
1979 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
1980 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
1981 if (activityDisplay == null) {
1982 throw new IllegalArgumentException(
1983 "Display with displayId=" + displayId + " not found.");
1984 }
1985
1986 // Return the topmost valid stack on the display.
1987 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
1988 final ActivityStack stack = activityDisplay.mStacks.get(i);
1989 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
1990 return stack;
1991 }
1992 }
1993
1994 // If there is no valid stack on the external display - check if new dynamic stack will do.
1995 if (displayId != Display.DEFAULT_DISPLAY) {
1996 final int newDynamicStackId = getNextStackId();
1997 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
1998 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
1999 }
2000 }
2001
2002 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2003 return null;
2004 }
2005
Craig Mautner967212c2013-04-13 21:10:58 -07002006 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002007 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2009 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002010 }
2011 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002012 }
2013
Jorim Jaggife762342016-10-13 14:33:27 +02002014 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2015 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2016 }
2017
Craig Mautneree2e45a2014-06-27 12:10:03 -07002018 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002019 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002020 }
2021
2022 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002023 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2024 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2025 final TaskRecord task = tasks.get(taskNdx);
2026 if (task.isHomeTask()) {
2027 final ArrayList<ActivityRecord> activities = task.mActivities;
2028 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2029 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002030 if (r.isHomeActivity()
2031 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002032 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002033 }
2034 }
2035 }
2036 }
2037 return null;
2038 }
2039
Chong Zhangb15758a2015-11-17 12:12:03 -08002040 /**
2041 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2042 * the docked stack itself, or if it's side-by-side to the docked stack.
2043 */
2044 boolean isStackDockedInEffect(int stackId) {
2045 return stackId == DOCKED_STACK_ID ||
2046 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2047 }
2048
Todd Kennedyca4d8422015-01-15 15:19:22 -08002049 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002050 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002051 ActivityContainer activityContainer =
2052 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002053 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002054 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2055 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002056 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002057 return activityContainer;
2058 }
2059
Craig Mautner34b73df2014-01-12 21:11:08 -08002060 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002061 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2062 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2063 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002064 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2065 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002066 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002067 }
2068 }
2069
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002070 void deleteActivityContainerRecord(int stackId) {
2071 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2072 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2073 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002074 }
2075
Jorim Jaggidc249c42015-12-15 14:57:31 -08002076 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002077 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002078 if (stackId == DOCKED_STACK_ID) {
2079 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002080 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002081 return;
2082 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002083 final ActivityStack stack = getStack(stackId);
2084 if (stack == null) {
2085 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2086 return;
2087 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002088
Jorim Jaggidc249c42015-12-15 14:57:31 -08002089 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002090 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2091 // stack size changing so things don't get out of sync.
2092 return;
2093 }
2094
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002095 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002096 mWindowManager.deferSurfaceLayout();
2097 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002098 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002099 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002100 stack.ensureVisibleActivitiesConfigurationLocked(
2101 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002102 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002103 } finally {
2104 mWindowManager.continueSurfaceLayout();
2105 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002106 }
2107 }
2108
Jorim Jaggi192086e2016-03-11 17:17:03 +01002109 void deferUpdateBounds(int stackId) {
2110 final ActivityStack stack = getStack(stackId);
2111 if (stack != null) {
2112 stack.deferUpdateBounds();
2113 }
2114 }
2115
2116 void continueUpdateBounds(int stackId) {
2117 final ActivityStack stack = getStack(stackId);
2118 if (stack != null) {
2119 stack.continueUpdateBounds();
2120 }
2121 }
2122
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002123 void notifyAppTransitionDone() {
2124 continueUpdateBounds(HOME_STACK_ID);
2125 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2126 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002127 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002128 mWindowManager.setTaskDockedResizing(taskId, false);
2129 }
2130 }
2131 mResizingTasksDuringAnimation.clear();
2132 }
2133
Jorim Jaggi192086e2016-03-11 17:17:03 +01002134 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002135 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002136 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002137
Jorim Jaggi192086e2016-03-11 17:17:03 +01002138 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2139 return;
2140 }
2141
Andrii Kulian1e32e022016-09-16 15:29:34 -07002142 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002143 }
2144
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002145 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2146 final ActivityStack stack = getStack(fromStackId);
2147 if (stack == null) {
2148 return;
2149 }
2150
2151 mWindowManager.deferSurfaceLayout();
2152 try {
2153 if (fromStackId == DOCKED_STACK_ID) {
2154
2155 // We are moving all tasks from the docked stack to the fullscreen stack,
2156 // which is dismissing the docked stack, so resize all other stacks to
2157 // fullscreen here already so we don't end up with resize trashing.
2158 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2159 if (StackId.isResizeableByDockedStack(i)) {
2160 ActivityStack otherStack = getStack(i);
2161 if (otherStack != null) {
2162 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2163 true /* allowResizeInDockedMode */, DEFER_RESUME);
2164 }
2165 }
2166 }
2167
2168 // Also disable docked stack resizing since we have manually adjusted the
2169 // size of other stacks above and we don't want to trigger a docked stack
2170 // resize when we remove task from it below and it is detached from the
2171 // display because it no longer contains any tasks.
2172 mAllowDockedStackResize = false;
2173 }
2174 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2175 final int size = tasks.size();
2176 if (onTop) {
2177 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002178 final TaskRecord task = tasks.get(i);
2179 if (fromStackId == PINNED_STACK_ID) {
2180 // Update the return-to to reflect where the pinned stack task was moved
2181 // from so that we retain the stack that was previously visible if the
2182 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2183 task.setTaskToReturnTo(getFocusedStack().getStackId() == HOME_STACK_ID
2184 ? HOME_ACTIVITY_TYPE : APPLICATION_ACTIVITY_TYPE);
2185 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002186 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002187 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002188 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002189 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002190
2191 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2192 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002193 } else {
2194 for (int i = size - 1; i >= 0; i--) {
2195 positionTaskInStackLocked(tasks.get(i).taskId,
2196 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2197 }
2198 }
2199 } finally {
2200 mAllowDockedStackResize = true;
2201 mWindowManager.continueSurfaceLayout();
2202 }
2203 }
2204
Jorim Jaggidc249c42015-12-15 14:57:31 -08002205 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002206 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2207 boolean preserveWindows) {
2208 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2209 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2210 false /* deferResume */);
2211 }
2212
2213 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2214 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2215 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002216
2217 if (!mAllowDockedStackResize) {
2218 // Docked stack resize currently disabled.
2219 return;
2220 }
2221
Jorim Jaggidc249c42015-12-15 14:57:31 -08002222 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2223 if (stack == null) {
2224 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2225 return;
2226 }
2227
2228 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2229 mWindowManager.deferSurfaceLayout();
2230 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002231 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2232 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002233 ActivityRecord r = stack.topRunningActivityLocked();
2234 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2235 tempDockedTaskInsetBounds);
2236
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002237 // TODO: Checking for isAttached might not be needed as if the user passes in null
2238 // dockedBounds then they want the docked stack to be dismissed.
2239 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2240 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002241 // In this case we make all other static stacks fullscreen and move all
2242 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002243 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002244
2245 // stack shouldn't contain anymore activities, so nothing to resume.
2246 r = null;
2247 } else {
2248 // Docked stacks occupy a dedicated region on screen so the size of all other
2249 // static stacks need to be adjusted so they don't overlap with the docked stack.
2250 // We get the bounds to use from window manager which has been adjusted for any
2251 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002252 mWindowManager.getStackDockedModeBounds(
2253 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002254 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002255 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2256 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2257 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002258 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002259 }
2260 }
2261 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002262 if (!deferResume) {
2263 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2264 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002265 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002266 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002267 mWindowManager.continueSurfaceLayout();
2268 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2269 }
2270
2271 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2272 tempDockedTaskBounds != null
2273 || tempDockedTaskInsetBounds != null
2274 || tempOtherTaskBounds != null
2275 || tempOtherTaskInsetBounds != null);
2276 }
2277
Robert Carr0d00c2e2016-02-29 17:45:02 -08002278 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2279 final ActivityStack stack = getStack(PINNED_STACK_ID);
2280 if (stack == null) {
2281 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2282 return;
2283 }
2284 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2285 mWindowManager.deferSurfaceLayout();
2286 try {
2287 ActivityRecord r = stack.topRunningActivityLocked();
2288 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2289 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002290 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002291 } finally {
2292 mWindowManager.continueSurfaceLayout();
2293 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2294 }
2295 }
2296
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002297 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2298 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002299 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002300 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002301 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002302 }
2303
Chong Zhang87b21722015-09-21 15:39:51 -07002304 // If this is a forced resize, let it go through even if the bounds is not changing,
2305 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002306 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002307 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002308 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002309 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002310 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002311 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002312
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002313 if (!mWindowManager.isValidTaskId(task.taskId)) {
2314 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002315 // All we can do for now is update the bounds so it can be used when the task is
2316 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002317 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002318 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002319 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002320 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002321 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002322 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002323 }
2324
Chong Zhang6de2ae82015-09-30 18:25:21 -07002325 // Do not move the task to another stack here.
2326 // This method assumes that the task is already placed in the right stack.
2327 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002328
Chong Zhang6de2ae82015-09-30 18:25:21 -07002329 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002330
Andrii Kulian8072d112016-09-16 11:11:01 -07002331 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002332 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002333 // way and the activity was kept the way it was. If it's false, it means the activity had
2334 // to be relaunched due to configuration change.
2335 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002336 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002337 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002338 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002339 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002340
2341 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002342 // All other activities must be made visible with their correct configuration.
2343 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2344 if (!kept) {
2345 resumeFocusedStackTopActivityLocked();
2346 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002347 }
2348 }
2349 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002350 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2351 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002352
2353 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002354 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002355 }
2356
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002357 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002358 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2359 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002360 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002361 }
2362
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002363 ActivityContainer activityContainer = new ActivityContainer(stackId);
2364 mActivityContainers.put(stackId, activityContainer);
Andrii Kulian839def92016-11-02 10:58:58 -07002365 activityContainer.addToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002366 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002367 }
2368
2369 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002370 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002371 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2372 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002373 break;
2374 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002375 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002376 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002377 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002378 }
2379
Chong Zhang5dcb2752015-08-18 13:50:26 -07002380 /**
2381 * Restores a recent task to a stack
2382 * @param task The recent task to be restored.
2383 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002384 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2385 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002386 * @return true if the task has been restored successfully.
2387 */
2388 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002389 if (!StackId.isStaticStack(stackId)) {
2390 // If stack is not static (or stack id is invalid) - use the default one.
2391 // This means that tasks that were on external displays will be restored on the
2392 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002393 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002394 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2395 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2396 // Put it in the fullscreen stack.
2397 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002398 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002399
Andrii Kulian02b7a832016-10-06 23:11:56 -07002400 final ActivityStack currentStack = task.getStack();
2401 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002402 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002403 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002404 // Nothing else to do since it is already restored in the right stack.
2405 return true;
2406 }
2407 // Remove current stack association, so we can re-associate the task with the
2408 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002409 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002410 }
2411
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002412 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002413 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002414
2415 if (stack == null) {
2416 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002417 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2418 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002419 return false;
2420 }
2421
Wale Ogunwale5f986092015-12-04 15:35:38 -08002422 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002423 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2424 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002425 final ArrayList<ActivityRecord> activities = task.mActivities;
2426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002427 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002428 }
2429 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002430 }
2431
Wale Ogunwale040b4702015-08-06 18:10:50 -07002432 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002433 * Move stack with all its existing content to specified display.
2434 * @param stackId Id of stack to move.
2435 * @param displayId Id of display to move stack to.
2436 */
2437 void moveStackToDisplayLocked(int stackId, int displayId) {
2438 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2439 if (activityDisplay == null) {
2440 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2441 + displayId);
2442 }
2443 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2444 if (activityContainer != null) {
2445 if (activityContainer.isAttachedLocked()) {
2446 if (activityContainer.getDisplayId() == displayId) {
2447 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2448 + " to its current displayId=" + displayId);
2449 }
2450
2451 activityContainer.moveToDisplayLocked(activityDisplay);
2452 } else {
2453 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2454 + stackId + " is not attached to any display.");
2455 }
2456 } else {
2457 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2458 + stackId);
2459 }
2460
2461 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2462 !PRESERVE_WINDOWS);
2463 // TODO(multi-display): resize stacks properly if moved from split-screen.
2464 }
2465
2466 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002467 * Moves the specified task record to the input stack id.
2468 * WARNING: This method performs an unchecked/raw move of the task and
2469 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002470 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002471 * @param task Task to move.
2472 * @param stackId Id of stack to move task to.
2473 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002474 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002475 * @param reason Reason the task is been moved.
2476 * @return The stack the task was moved to.
2477 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002478 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002479 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002480
2481 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2482 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2483 + "support multi-window task=" + task + " to stackId=" + stackId);
2484 }
2485
Wale Ogunwale06579d62016-04-30 15:29:06 -07002486 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002487 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002488 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002489 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002490 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2491 // Whenever we are moving the top activity from the front stack we want to make sure to move
2492 // the stack to the front.
2493 final boolean wasFront = isFrontStack(prevStack)
2494 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002495
Chong Zhangd545dce2016-02-29 18:09:17 -08002496 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002497 // We don't allow moving a unresizeable task to the docked stack since the docked
2498 // stack is used for split-screen mode and will cause things like the docked divider to
2499 // show up. We instead leave the task in its current stack or move it to the fullscreen
2500 // stack if it isn't currently in a stack.
2501 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002502 Slog.w(TAG, "Can not move unresizeable task=" + task
2503 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2504 }
2505
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002506 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2507 // if a docked stack is created below which will lead to the stack we are moving from and
2508 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002509 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002510 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002511 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002512 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002513 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002514
2515 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002516 // move focus to the new stack by moving the stack to the front.
2517 stack.moveToFrontAndResumeStateIfNeeded(
2518 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002519
Wale Ogunwale040b4702015-08-06 18:10:50 -07002520 return stack;
2521 }
2522
Chong Zhange4fbd322016-03-01 14:44:03 -08002523 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002524 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002525 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2526 false /* deferResume */);
2527 }
2528
2529 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2530 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002531 final TaskRecord task = anyTaskForIdLocked(taskId);
2532 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002533 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002534 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002535 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002536
Andrii Kulian02b7a832016-10-06 23:11:56 -07002537 final ActivityStack currentStack = task.getStack();
2538 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002539 // You are already in the right stack silly...
2540 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002541 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002542 }
2543
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002544 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2545 throw new IllegalArgumentException("moveTaskToStack:"
2546 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2547 }
2548
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002549 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002550 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002551 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002552 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002553 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002554 // We are about to relaunch the activity because its configuration changed due to
2555 // being maximized, i.e. size change. The activity will first remove the old window
2556 // and then add a new one. This call will tell window manager about this, so it can
2557 // preserve the old window until the new one is drawn. This prevents having a gap
2558 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002559 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002560 // Note here we always set the replacing window first, as the flags might be needed
2561 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002562 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002563 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002564
2565 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002566 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002567 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002568 try {
2569 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002570 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002571 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002572
Wale Ogunwale961f4852016-02-01 20:25:54 -08002573 if (!animate) {
2574 stack.mNoAnimActivities.add(topActivity);
2575 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002576
Wale Ogunwale961f4852016-02-01 20:25:54 -08002577 // We might trigger a configuration change. Save the current task bounds for freezing.
2578 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2579
2580 // Make sure the task has the appropriate bounds/size for the stack it is in.
2581 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002582 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2583 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002584 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2585 Rect bounds = task.getLaunchBounds();
2586 if (bounds == null) {
2587 stack.layoutTaskInStack(task, null);
2588 bounds = task.mBounds;
2589 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002590 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2591 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002592 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002593 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2594 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002595 }
2596 } finally {
2597 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002598 }
2599
2600 if (mightReplaceWindow) {
2601 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2602 // window), we need to clear the replace window settings. Otherwise, we schedule a
2603 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002604 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002605 }
2606
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002607 if (!deferResume) {
2608
2609 // The task might have already been running and its visibility needs to be synchronized with
2610 // the visibility of the stack / windows.
2611 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2612 resumeFocusedStackTopActivityLocked();
2613 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002614
Jorim Jaggid53f0922016-04-06 22:16:23 -07002615 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002616
2617 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002618 }
2619
Wale Ogunwale079a0042015-10-24 11:44:07 -07002620 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2621 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2622 if (stack == null) {
2623 throw new IllegalArgumentException(
2624 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2625 }
2626
2627 final ActivityRecord r = stack.topRunningActivityLocked();
2628 if (r == null) {
2629 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2630 + " in stack=" + stack);
2631 return false;
2632 }
2633
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002634 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002635 Slog.w(TAG,
2636 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002637 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002638 return false;
2639 }
2640
Wale Ogunwale480dca02016-02-06 13:58:29 -08002641 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002642 return true;
2643 }
2644
Wale Ogunwale480dca02016-02-06 13:58:29 -08002645 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2646 mWindowManager.deferSurfaceLayout();
2647 try {
2648 final TaskRecord task = r.task;
2649
Andrii Kulian02b7a832016-10-06 23:11:56 -07002650 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002651 // An activity can't be pinned and visible behind at the same time. Go ahead and
2652 // release it from been visible behind before pinning.
2653 requestVisibleBehindLocked(r, false);
2654 }
2655
Wale Ogunwale480dca02016-02-06 13:58:29 -08002656 // Need to make sure the pinned stack exist so we can resize it below...
2657 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2658
2659 // Resize the pinned stack to match the current size of the task the activity we are
2660 // going to be moving is currently contained in. We do this to have the right starting
2661 // animation bounds for the pinned stack to the desired bounds the caller wants.
2662 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2663 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002664 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002665
2666 if (task.mActivities.size() == 1) {
2667 // There is only one activity in the task. So, we can just move the task over to
2668 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002669 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2670 // Move the home stack forward if the task we just moved to the pinned stack
2671 // was launched from home so home should be visible behind it.
2672 moveHomeStackToFront(reason);
2673 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002674 moveTaskToStackLocked(
2675 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2676 } else {
2677 stack.moveActivityToStack(r);
2678 }
2679 } finally {
2680 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002681 }
2682
Wale Ogunwale480dca02016-02-06 13:58:29 -08002683 // The task might have already been running and its visibility needs to be synchronized
2684 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002685 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002686 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002687
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002688 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002689 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002690 }
2691
Chong Zhang6cda19c2016-06-14 19:07:56 -07002692 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2693 if (r == null || !r.isFocusable()) {
2694 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2695 "moveActivityStackToFront: unfocusable r=" + r);
2696 return false;
2697 }
2698
2699 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002700 final ActivityStack stack = r.getStack();
2701 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002702 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2703 + r + " task=" + task);
2704 return false;
2705 }
2706
Chong Zhang6cda19c2016-06-14 19:07:56 -07002707 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2708 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2709 "moveActivityStackToFront: already on top, r=" + r);
2710 return false;
2711 }
2712
2713 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2714 "moveActivityStackToFront: r=" + r);
2715
2716 stack.moveToFront(reason, task);
2717 return true;
2718 }
2719
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002720 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2721 final TaskRecord task = anyTaskForIdLocked(taskId);
2722 if (task == null) {
2723 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2724 return;
2725 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002726 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2727
2728 task.updateOverrideConfigurationForStack(stack);
2729
2730 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002731 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002732 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002733 // The task might have already been running and its visibility needs to be synchronized with
2734 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002735 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002736 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002737 }
2738
Craig Mautnerac6f8432013-07-17 13:24:59 -07002739 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002740 mTmpFindTaskResult.r = null;
2741 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002742 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2744 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2746 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002747 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002748 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002749 continue;
2750 }
2751 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002752 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2753 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002754 continue;
2755 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002756 stack.findTaskLocked(r, mTmpFindTaskResult);
2757 // It is possible to have task in multiple stacks with the same root affinity.
2758 // If the match we found was based on root affinity we keep on looking to see if
2759 // there is a better match in another stack. We eventually return the match based
2760 // on root affinity if we don't find a better match.
2761 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2762 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002763 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002764 }
2765 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002766 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2767 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002768 }
2769
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002770 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2771 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002772 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2773 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002775 final ActivityRecord ar = stacks.get(stackNdx)
2776 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002777 if (ar != null) {
2778 return ar;
2779 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002780 }
2781 }
2782 return null;
2783 }
2784
Craig Mautner8d341ef2013-03-26 09:03:27 -07002785 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002786 scheduleSleepTimeout();
2787 if (!mGoingToSleep.isHeld()) {
2788 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002789 if (mLaunchingActivity.isHeld()) {
2790 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2791 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002792 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002793 mLaunchingActivity.release();
2794 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002795 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002797 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 }
2799
2800 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002801 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002802
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002803 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002804 final long endTime = System.currentTimeMillis() + timeout;
2805 while (true) {
2806 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2810 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2811 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002812 }
2813 if (cantShutdown) {
2814 long timeRemaining = endTime - System.currentTimeMillis();
2815 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002816 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002817 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002818 } catch (InterruptedException e) {
2819 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002820 } else {
2821 Slog.w(TAG, "Activity manager shutdown timed out");
2822 timedout = true;
2823 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002824 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002825 } else {
2826 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827 }
2828 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002829
2830 // Force checkReadyForSleep to complete.
2831 mSleepTimeout = true;
2832 checkReadyForSleepLocked();
2833
Craig Mautner8d341ef2013-03-26 09:03:27 -07002834 return timedout;
2835 }
2836
2837 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002838 removeSleepTimeouts();
2839 if (mGoingToSleep.isHeld()) {
2840 mGoingToSleep.release();
2841 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002842 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2843 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002844 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2845 final ActivityStack stack = stacks.get(stackNdx);
2846 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002847 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002848 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002849 }
Craig Mautner5314a402013-09-26 12:40:16 -07002850 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002851 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002852 mGoingToSleepActivities.clear();
2853 }
2854
2855 void activitySleptLocked(ActivityRecord r) {
2856 mGoingToSleepActivities.remove(r);
2857 checkReadyForSleepLocked();
2858 }
2859
2860 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002861 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002862 // Do not care.
2863 return;
2864 }
2865
2866 if (!mSleepTimeout) {
2867 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002868 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2869 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002870 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2871 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2872 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002873 }
2874
2875 if (mStoppingActivities.size() > 0) {
2876 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002877 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002878 + mStoppingActivities.size() + " activities");
2879 scheduleIdleLocked();
2880 dontSleep = true;
2881 }
2882
2883 if (mGoingToSleepActivities.size() > 0) {
2884 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002885 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002886 + mGoingToSleepActivities.size() + " activities");
2887 dontSleep = true;
2888 }
2889
2890 if (dontSleep) {
2891 return;
2892 }
2893 }
2894
Wei Wang65c7a152016-06-02 18:51:22 -07002895 // Send launch end powerhint before going sleep
2896 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2897
Craig Mautnere0a38842013-12-16 16:14:02 -08002898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2899 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2901 stacks.get(stackNdx).goToSleep();
2902 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002903 }
2904
2905 removeSleepTimeouts();
2906
2907 if (mGoingToSleep.isHeld()) {
2908 mGoingToSleep.release();
2909 }
2910 if (mService.mShuttingDown) {
2911 mService.notifyAll();
2912 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 }
2914
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002915 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002916 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002917 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002918 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002919 }
2920 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002921 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002922 mWindowManager.executeAppTransition();
2923 return true;
2924 }
2925 return false;
2926 }
2927
Craig Mautner8d341ef2013-03-26 09:03:27 -07002928 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2930 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002931 int stackNdx = stacks.size() - 1;
2932 while (stackNdx >= 0) {
2933 stacks.get(stackNdx).handleAppCrashLocked(app);
2934 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002935 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002936 }
2937 }
2938
Jose Lima4b6c6692014-08-12 17:41:12 -07002939 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002940 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002941 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002942 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2943 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002944 return false;
2945 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002946
2947 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2948 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2949 + " visible=" + visible + " stackId=" + stack.mStackId
2950 + " can't contain visible behind activities");
2951 return false;
2952 }
2953
Jose Lima4b6c6692014-08-12 17:41:12 -07002954 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002955 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2956 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002957
2958 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002959 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002960 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002961 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002962 return true;
2963 }
2964
2965 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002966 if (visible && top.fullscreen) {
2967 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002968 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2969 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2970 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2971 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002972 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002973 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2974 // Only the activity set as currently visible behind should actively reset its
2975 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002976 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2977 "requestVisibleBehind: returning visible=" + visible
2978 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2979 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002980 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002981 }
2982
Jose Lima4b6c6692014-08-12 17:41:12 -07002983 stack.setVisibleBehindActivity(visible ? r : null);
2984 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002985 // If there is a translucent home activity, we need to force it stop being translucent,
2986 // because we can't depend on the application to necessarily perform that operation.
2987 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002988 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002989 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002990 mService.convertFromTranslucent(next.appToken);
2991 }
2992 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002993 if (top.app != null && top.app.thread != null) {
2994 // Notify the top app of the change.
2995 try {
2996 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2997 } catch (RemoteException e) {
2998 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002999 }
3000 return true;
3001 }
3002
Craig Mautnerbb742462014-07-07 15:28:55 -07003003 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003004 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003005 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003006 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003007
3008 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003009 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003010 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003011 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Craig Mautnerbb742462014-07-07 15:28:55 -07003012 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07003013
3014 // When launching tasks behind, update the last active time of the top task after the new
3015 // task has been shown briefly
3016 final ActivityRecord top = stack.topActivity();
3017 if (top != null) {
3018 top.task.touchActiveTime();
3019 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003020 }
3021
3022 void scheduleLaunchTaskBehindComplete(IBinder token) {
3023 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3024 }
3025
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003026 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3027 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003028 mKeyguardController.beginActivityVisibilityUpdate();
3029 try {
3030 // First the front stacks. In case any are not fullscreen and are in front of home.
3031 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3032 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3033 final int topStackNdx = stacks.size() - 1;
3034 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3035 final ActivityStack stack = stacks.get(stackNdx);
3036 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3037 }
Craig Mautner580ea812013-04-25 12:58:38 -07003038 }
Jorim Jaggife762342016-10-13 14:33:27 +02003039 } finally {
3040 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003041 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003042 }
3043
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003044 void invalidateTaskLayers() {
3045 mTaskLayersChanged = true;
3046 }
3047
3048 void rankTaskLayersIfNeeded() {
3049 if (!mTaskLayersChanged) {
3050 return;
3051 }
3052 mTaskLayersChanged = false;
3053 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3055 int baseLayer = 0;
3056 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3057 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3058 }
3059 }
3060 }
3061
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003062 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3063 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3064 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3065 final int topStackNdx = stacks.size() - 1;
3066 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3067 final ActivityStack stack = stacks.get(stackNdx);
3068 stack.clearOtherAppTimeTrackers(except);
3069 }
3070 }
3071 }
3072
Craig Mautner8d341ef2013-03-26 09:03:27 -07003073 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003074 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3075 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003076 final int numStacks = stacks.size();
3077 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3078 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003079 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003080 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003081 }
3082 }
3083
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003084 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3085 // Examine all activities currently running in the process.
3086 TaskRecord firstTask = null;
3087 // Tasks is non-null only if two or more tasks are found.
3088 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003089 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3090 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003091 ActivityRecord r = app.activities.get(i);
3092 // First, if we find an activity that is in the process of being destroyed,
3093 // then we just aren't going to do anything for now; we want things to settle
3094 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003095 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003096 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003097 return;
3098 }
3099 // Don't consider any activies that are currently not in a state where they
3100 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003101 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3102 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003103 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003104 continue;
3105 }
3106 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003107 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003108 + " from " + r);
3109 if (firstTask == null) {
3110 firstTask = r.task;
3111 } else if (firstTask != r.task) {
3112 if (tasks == null) {
3113 tasks = new ArraySet<>();
3114 tasks.add(firstTask);
3115 }
3116 tasks.add(r.task);
3117 }
3118 }
3119 }
3120 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003121 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003122 return;
3123 }
3124 // If we have activities in multiple tasks that are in a position to be destroyed,
3125 // let's iterate through the tasks and release the oldest one.
3126 final int numDisplays = mActivityDisplays.size();
3127 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3128 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3129 // Step through all stacks starting from behind, to hit the oldest things first.
3130 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3131 final ActivityStack stack = stacks.get(stackNdx);
3132 // Try to release activities in this stack; if we manage to, we are done.
3133 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3134 return;
3135 }
3136 }
3137 }
3138 }
3139
Amith Yamasani37a40c22015-06-17 13:25:42 -07003140 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003141 final int focusStackId = mFocusedStack.getStackId();
3142 // We dismiss the docked stack whenever we switch users.
3143 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3144
3145 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003146 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003147 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003148
Craig Mautner858d8a62013-04-23 17:08:34 -07003149 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003150 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3151 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003152 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003153 final ActivityStack stack = stacks.get(stackNdx);
3154 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003155 TaskRecord task = stack.topTask();
3156 if (task != null) {
3157 mWindowManager.moveTaskToTop(task.taskId);
3158 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003159 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003160 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003161
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003162 ActivityStack stack = getStack(restoreStackId);
3163 if (stack == null) {
3164 stack = mHomeStack;
3165 }
3166 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003167 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003168 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003169 } else {
3170 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003171 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003172 }
Craig Mautner93529a42013-10-04 15:03:13 -07003173 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003174 }
3175
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003176 /** Checks whether the userid is a profile of the current user. */
3177 boolean isCurrentProfileLocked(int userId) {
3178 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003179 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003180 }
3181
Craig Mautnerde4ef022013-04-07 19:01:33 -07003182 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003183 ArrayList<ActivityRecord> stops = null;
3184
3185 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003186 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3187 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003188 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3189 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003190 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003191 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003192 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3193 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003194 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003195 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003196 if (s.finishing) {
3197 // If this activity is finishing, it is sitting on top of
3198 // everyone else but we now know it is no longer needed...
3199 // so get rid of it. Otherwise, we need to go through the
3200 // normal flow and hide it once we determine that it is
3201 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003202 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003203 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003204 }
3205 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003206 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003207 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003208 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003209 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003210 }
3211 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003212 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003213 }
3214 }
3215
3216 return stops;
3217 }
3218
Craig Mautnercf910b02013-04-23 11:23:27 -07003219 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003220 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3221 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3222 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3223 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003224 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003225 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003226 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003227 if (r == null) Slog.e(TAG,
3228 "validateTop...: null top activity, stack=" + stack);
3229 else {
3230 final ActivityRecord pausing = stack.mPausingActivity;
3231 if (pausing != null && pausing == r) Slog.e(TAG,
3232 "validateTop...: top stack has pausing activity r=" + r
3233 + " state=" + state);
3234 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3235 "validateTop...: activity in front not resumed r=" + r
3236 + " state=" + state);
3237 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003238 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003239 final ActivityRecord resumed = stack.mResumedActivity;
3240 if (resumed != null && resumed == r) Slog.e(TAG,
3241 "validateTop...: back stack has resumed activity r=" + r
3242 + " state=" + state);
3243 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3244 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003245 }
3246 }
3247 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003248 }
3249
Craig Mautnere0570202015-05-13 13:06:11 -07003250 private String lockTaskModeToString() {
3251 switch (mLockTaskModeState) {
3252 case LOCK_TASK_MODE_LOCKED:
3253 return "LOCKED";
3254 case LOCK_TASK_MODE_PINNED:
3255 return "PINNED";
3256 case LOCK_TASK_MODE_NONE:
3257 return "NONE";
3258 default: return "unknown=" + mLockTaskModeState;
3259 }
3260 }
3261
Craig Mautner27084302013-03-25 08:05:25 -07003262 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003263 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003264 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003265 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003266 pw.print(prefix);
3267 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003268 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003269 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003270 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003271 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3272 if (packages.size() > 0) {
3273 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3274 for (int i = 0; i < packages.size(); ++i) {
3275 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3276 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3277 }
3278 }
3279 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3280 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003281 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003282
Winson43d1f262016-06-14 16:05:55 -07003283 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003284 * Dump all connected displays' configurations.
3285 * @param prefix Prefix to apply to each line of the dump.
3286 */
3287 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3288 pw.print(prefix); pw.println("Display override configurations:");
3289 final int displayCount = mActivityDisplays.size();
3290 for (int i = 0; i < displayCount; i++) {
3291 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3292 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3293 pw.println(activityDisplay.getOverrideConfiguration());
3294 }
3295 }
3296
3297 /**
Winson43d1f262016-06-14 16:05:55 -07003298 * Dumps the activities matching the given {@param name} in the either the focused stack
3299 * or all visible stacks if {@param dumpVisibleStacks} is true.
3300 */
3301 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3302 if (dumpVisibleStacks) {
3303 ArrayList<ActivityRecord> activities = new ArrayList<>();
3304 int numDisplays = mActivityDisplays.size();
3305 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3306 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3307 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3308 ActivityStack stack = stacks.get(stackNdx);
3309 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3310 activities.addAll(stack.getDumpActivitiesLocked(name));
3311 }
3312 }
3313 }
3314 return activities;
3315 } else {
3316 return mFocusedStack.getDumpActivitiesLocked(name);
3317 }
Craig Mautner20e72272013-04-01 13:45:53 -07003318 }
3319
Dianne Hackborn390517b2013-05-30 15:03:32 -07003320 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3321 boolean needSep, String prefix) {
3322 if (activity != null) {
3323 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3324 if (needSep) {
3325 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003326 }
3327 pw.print(prefix);
3328 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003329 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003330 }
3331 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003332 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003333 }
3334
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3336 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003337 boolean printed = false;
3338 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003339 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3340 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003341 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003342 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003343 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003344 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003345 final ActivityStack stack = stacks.get(stackNdx);
3346 StringBuilder stackHeader = new StringBuilder(128);
3347 stackHeader.append(" Stack #");
3348 stackHeader.append(stack.mStackId);
3349 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003350 stackHeader.append("\n");
3351 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3352 stackHeader.append("\n");
3353 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003354 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3355 needSep, stackHeader.toString());
3356 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3357 !dumpAll, false, dumpPackage, true,
3358 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003359
Craig Mautner4a1cb222013-12-04 16:14:06 -08003360 needSep = printed;
3361 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3362 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003363 if (pr) {
3364 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003365 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003366 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003367 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3368 " mResumedActivity: ");
3369 if (pr) {
3370 printed = true;
3371 needSep = false;
3372 }
3373 if (dumpAll) {
3374 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3375 " mLastPausedActivity: ");
3376 if (pr) {
3377 printed = true;
3378 needSep = true;
3379 }
3380 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3381 needSep, " mLastNoHistoryActivity: ");
3382 }
3383 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003384 }
3385 }
3386
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003387 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3388 false, dumpPackage, true, " Activities waiting to finish:", null);
3389 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3390 false, dumpPackage, true, " Activities waiting to stop:", null);
3391 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3392 false, dumpPackage, true, " Activities waiting for another to become visible:",
3393 null);
3394 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3395 false, dumpPackage, true, " Activities waiting to sleep:", null);
3396 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3397 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003398
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003399 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003400 }
3401
Dianne Hackborn390517b2013-05-30 15:03:32 -07003402 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003403 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003404 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003405 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003406 String innerPrefix = null;
3407 String[] args = null;
3408 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003409 for (int i=list.size()-1; i>=0; i--) {
3410 final ActivityRecord r = list.get(i);
3411 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3412 continue;
3413 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003414 if (innerPrefix == null) {
3415 innerPrefix = prefix + " ";
3416 args = new String[0];
3417 }
3418 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003419 final boolean full = !brief && (complete || !r.isInHistory());
3420 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003421 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 needNL = false;
3423 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003424 if (header1 != null) {
3425 pw.println(header1);
3426 header1 = null;
3427 }
3428 if (header2 != null) {
3429 pw.println(header2);
3430 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003431 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003432 if (lastTask != r.task) {
3433 lastTask = r.task;
3434 pw.print(prefix);
3435 pw.print(full ? "* " : " ");
3436 pw.println(lastTask);
3437 if (full) {
3438 lastTask.dump(pw, prefix + " ");
3439 } else if (complete) {
3440 // Complete + brief == give a summary. Isn't that obvious?!?
3441 if (lastTask.intent != null) {
3442 pw.print(prefix); pw.print(" ");
3443 pw.println(lastTask.intent.toInsecureStringWithClip());
3444 }
3445 }
3446 }
3447 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3448 pw.print(" #"); pw.print(i); pw.print(": ");
3449 pw.println(r);
3450 if (full) {
3451 r.dump(pw, innerPrefix);
3452 } else if (complete) {
3453 // Complete + brief == give a summary. Isn't that obvious?!?
3454 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3455 if (r.app != null) {
3456 pw.print(innerPrefix); pw.println(r.app);
3457 }
3458 }
3459 if (client && r.app != null && r.app.thread != null) {
3460 // flush anything that is already in the PrintWriter since the thread is going
3461 // to write to the file descriptor directly
3462 pw.flush();
3463 try {
3464 TransferPipe tp = new TransferPipe();
3465 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003466 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003467 // Short timeout, since blocking here can
3468 // deadlock with the application.
3469 tp.go(fd, 2000);
3470 } finally {
3471 tp.kill();
3472 }
3473 } catch (IOException e) {
3474 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3475 } catch (RemoteException e) {
3476 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3477 }
3478 needNL = true;
3479 }
3480 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003481 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003482 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003483
Craig Mautnerf3333272013-04-22 10:55:53 -07003484 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003485 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3486 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003487 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3488 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003489 }
3490
3491 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003492 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003493 }
3494
3495 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003496 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3497 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003498 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3499 }
3500
Craig Mautner05d29032013-05-03 13:40:13 -07003501 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003502 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3503 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3504 }
Craig Mautner05d29032013-05-03 13:40:13 -07003505 }
3506
Craig Mautner0eea92c2013-05-16 13:35:39 -07003507 void removeSleepTimeouts() {
3508 mSleepTimeout = false;
3509 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3510 }
3511
3512 final void scheduleSleepTimeout() {
3513 removeSleepTimeouts();
3514 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3515 }
3516
Craig Mautner4a1cb222013-12-04 16:14:06 -08003517 @Override
3518 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003519 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003520 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3521 }
3522
3523 @Override
3524 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003525 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003526 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3527 }
3528
3529 @Override
3530 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003531 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3533 }
3534
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003535 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003536 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003538 newDisplay = mActivityDisplays.get(displayId) == null;
3539 if (newDisplay) {
3540 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003541 if (activityDisplay.mDisplay == null) {
3542 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3543 return;
3544 }
Craig Mautner4504de52013-12-20 09:06:56 -08003545 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003546 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003547 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 }
Craig Mautner4504de52013-12-20 09:06:56 -08003549 if (newDisplay) {
3550 mWindowManager.onDisplayAdded(displayId);
3551 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003552 }
3553
Andrii Kulianca007a62016-11-22 16:33:28 -08003554 /** Check if display with specified id is added to the list. */
3555 boolean isDisplayAdded(int displayId) {
3556 return mActivityDisplays.get(displayId) != null;
3557 }
3558
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003559 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003560 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003561 mService.mContext.getResources().getDimensionPixelSize(
3562 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003563 }
3564
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003565 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003566 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003567 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3568 if (activityDisplay != null) {
3569 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian03c403d2016-11-11 11:14:12 -08003571 final ActivityStack stack = stacks.get(stackNdx);
3572 // TODO: Implement proper stack removal and ability to choose the behavior -
3573 // remove stack completely or move it to other display.
3574 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003576 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003577 }
3578 }
3579 mWindowManager.onDisplayRemoved(displayId);
3580 }
3581
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003582 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003583 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003584 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3585 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003586 // TODO: Update the bounds.
3587 }
3588 }
3589 mWindowManager.onDisplayChanged(displayId);
3590 }
3591
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003592 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003593 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003594 StackInfo info = new StackInfo();
3595 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003596 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003597 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003598 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003599 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003600 info.position = display != null
3601 ? display.mStacks.indexOf(stack)
3602 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003603
3604 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3605 final int numTasks = tasks.size();
3606 int[] taskIds = new int[numTasks];
3607 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003608 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003609 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 for (int i = 0; i < numTasks; ++i) {
3611 final TaskRecord task = tasks.get(i);
3612 taskIds[i] = task.taskId;
3613 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3614 : task.realActivity != null ? task.realActivity.flattenToString()
3615 : task.getTopActivity() != null ? task.getTopActivity().packageName
3616 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003617 taskBounds[i] = new Rect();
3618 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003619 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 }
3621 info.taskIds = taskIds;
3622 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003623 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003624 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003625
3626 final ActivityRecord top = stack.topRunningActivityLocked();
3627 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 return info;
3629 }
3630
3631 StackInfo getStackInfoLocked(int stackId) {
3632 ActivityStack stack = getStack(stackId);
3633 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003634 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003635 }
3636 return null;
3637 }
3638
3639 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003640 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003641 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3642 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003643 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003644 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003645 }
3646 }
3647 return list;
3648 }
3649
Craig Mautner15df08a2015-04-01 12:17:18 -07003650 TaskRecord getLockedTaskLocked() {
3651 final int top = mLockTaskModeTasks.size() - 1;
3652 if (top >= 0) {
3653 return mLockTaskModeTasks.get(top);
3654 }
3655 return null;
3656 }
3657
3658 boolean isLockedTask(TaskRecord task) {
3659 return mLockTaskModeTasks.contains(task);
3660 }
3661
3662 boolean isLastLockedTask(TaskRecord task) {
3663 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3664 }
3665
3666 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003667 if (!mLockTaskModeTasks.remove(task)) {
3668 return;
3669 }
3670 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3671 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003672 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003673 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3674 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003675 final Message lockTaskMsg = Message.obtain();
3676 lockTaskMsg.arg1 = task.userId;
3677 lockTaskMsg.what = LOCK_TASK_END_MSG;
3678 mHandler.sendMessage(lockTaskMsg);
3679 }
3680 }
3681
Andrii Kulianc27916642016-04-12 17:59:27 -07003682 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3683 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3684 false /* forceNonResizable */);
3685 }
3686
Jorim Jaggid53f0922016-04-06 22:16:23 -07003687 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003688 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003689 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3690 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003691 return;
3692 }
3693
Jorim Jaggicd13d332016-04-27 15:40:20 -07003694 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003695 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003696 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003697 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003698
Andrii Kulianc27916642016-04-12 17:59:27 -07003699 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3700 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003701 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003702 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3703 && !topActivity.noDisplay) {
3704 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003705 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3706 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003707 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003708 }
3709
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003710 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003711 if (mLockTaskNotify != null) {
3712 mLockTaskNotify.showToast(mLockTaskModeState);
3713 }
Jason Monka8f569c2014-07-07 12:15:21 -04003714 }
3715
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003716 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3717 if (mLockTaskModeTasks.contains(task)) {
3718 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3719 }
3720 }
3721
Craig Mautner432f64e2015-05-20 14:59:57 -07003722 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3723 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003724 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003725 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003726 final TaskRecord lockedTask = getLockedTaskLocked();
3727 if (lockedTask != null) {
3728 removeLockedTaskLocked(lockedTask);
3729 if (!mLockTaskModeTasks.isEmpty()) {
3730 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003731 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3732 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003733 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003734 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003735 return;
3736 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003737 }
Craig Mautnere0570202015-05-13 13:06:11 -07003738 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3739 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003740 return;
3741 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003742
3743 // Should have already been checked, but do it again.
3744 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003745 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3746 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003747 return;
3748 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003749 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003750 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003751 return;
3752 }
3753
3754 if (mLockTaskModeTasks.isEmpty()) {
3755 // First locktask.
3756 final Message lockTaskMsg = Message.obtain();
3757 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3758 lockTaskMsg.arg1 = task.userId;
3759 lockTaskMsg.what = LOCK_TASK_START_MSG;
3760 lockTaskMsg.arg2 = lockTaskModeState;
3761 mHandler.sendMessage(lockTaskMsg);
3762 }
3763 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003764 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3765 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003766 mLockTaskModeTasks.remove(task);
3767 mLockTaskModeTasks.add(task);
3768
3769 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003770 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003771 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003772
3773 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003774 findTaskToMoveToFrontLocked(task, 0, null, reason,
3775 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003776 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01003777 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07003778 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003779 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003780 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003781 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003782 }
3783
3784 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003785 return isLockTaskModeViolation(task, false);
3786 }
3787
3788 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3789 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003790 return false;
3791 }
3792 final int lockTaskAuth = task.mLockTaskAuth;
3793 switch (lockTaskAuth) {
3794 case LOCK_TASK_AUTH_DONT_LOCK:
3795 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003796 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003797 case LOCK_TASK_AUTH_LAUNCHABLE:
3798 case LOCK_TASK_AUTH_WHITELISTED:
3799 return false;
3800 case LOCK_TASK_AUTH_PINNABLE:
3801 // Pinnable tasks can't be launched on top of locktask tasks.
3802 return !mLockTaskModeTasks.isEmpty();
3803 default:
3804 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3805 return true;
3806 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003807 }
3808
Craig Mautner15df08a2015-04-01 12:17:18 -07003809 void onLockTaskPackagesUpdatedLocked() {
3810 boolean didSomething = false;
3811 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3812 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003813 final boolean wasWhitelisted =
3814 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3815 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003816 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003817 final boolean isWhitelisted =
3818 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3819 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3820 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003821 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003822 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3823 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003824 removeLockedTaskLocked(lockedTask);
3825 lockedTask.performClearTaskLocked();
3826 didSomething = true;
3827 }
3828 }
3829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3830 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3832 final ActivityStack stack = stacks.get(stackNdx);
3833 stack.onLockTaskPackagesUpdatedLocked();
3834 }
3835 }
Craig Mautnere0570202015-05-13 13:06:11 -07003836 final ActivityRecord r = topRunningActivityLocked();
3837 final TaskRecord task = r != null ? r.task : null;
3838 if (mLockTaskModeTasks.isEmpty() && task != null
3839 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3840 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003841 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3842 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3843 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3844 false);
3845 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003846 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003847 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003848 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003849 }
3850 }
3851
Benjamin Franz43261142015-02-11 15:59:44 +00003852 int getLockTaskModeState() {
3853 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003854 }
3855
Jorim Jaggife89d122015-12-22 16:28:44 +01003856 void activityRelaunchedLocked(IBinder token) {
3857 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003858 if (mService.isSleepingOrShuttingDownLocked()) {
3859 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3860 if (r != null) {
3861 r.setSleeping(true, true);
3862 }
3863 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003864 }
3865
3866 void activityRelaunchingLocked(ActivityRecord r) {
3867 mWindowManager.notifyAppRelaunching(r.appToken);
3868 }
3869
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003870 void logStackState() {
3871 mActivityMetricsLogger.logWindowState();
3872 }
3873
Andrii Kulian933076d2016-03-29 17:04:42 -07003874 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003875 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3876 final ActivityRecord r = task.mActivities.get(i);
3877 if (r.app != null && r.app.thread != null) {
3878 mMultiWindowModeChangedActivities.add(r);
3879 }
3880 }
3881
3882 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3883 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3884 }
3885 }
3886
Andrii Kulian933076d2016-03-29 17:04:42 -07003887 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003888 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003889 if (prevStack == null || prevStack == stack
3890 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3891 return;
3892 }
3893
3894 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3895 final ActivityRecord r = task.mActivities.get(i);
3896 if (r.app != null && r.app.thread != null) {
3897 mPipModeChangedActivities.add(r);
3898 }
3899 }
3900
3901 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3902 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3903 }
3904 }
3905
Tony Mak853304c2016-04-18 15:17:41 +01003906 void setDockedStackMinimized(boolean minimized) {
3907 mIsDockMinimized = minimized;
3908 if (minimized) {
3909 // Docked stack is not visible, no need to confirm credentials for its top activity.
3910 return;
3911 }
3912 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3913 if (dockedStack == null) {
3914 return;
3915 }
3916 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3917 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3918 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3919 }
3920 }
3921
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003922 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003923
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003924 public ActivityStackSupervisorHandler(Looper looper) {
3925 super(looper);
3926 }
3927
Craig Mautnerf3333272013-04-22 10:55:53 -07003928 void activityIdleInternal(ActivityRecord r) {
3929 synchronized (mService) {
3930 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3931 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003932 }
3933
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003934 @Override
3935 public void handleMessage(Message msg) {
3936 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003937 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3938 synchronized (mService) {
3939 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3940 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003941 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003942 }
3943 }
3944 } break;
3945 case REPORT_PIP_MODE_CHANGED_MSG: {
3946 synchronized (mService) {
3947 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3948 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003949 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003950 }
3951 }
3952 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003953 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003954 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3955 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003956 if (mService.mDidDexOpt) {
3957 mService.mDidDexOpt = false;
3958 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3959 nmsg.obj = msg.obj;
3960 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3961 return;
3962 }
3963 // We don't at this point know if the activity is fullscreen,
3964 // so we need to be conservative and assume it isn't.
3965 activityIdleInternal((ActivityRecord)msg.obj);
3966 } break;
3967 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003968 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003969 activityIdleInternal((ActivityRecord)msg.obj);
3970 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003971 case RESUME_TOP_ACTIVITY_MSG: {
3972 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003973 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003974 }
3975 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003976 case SLEEP_TIMEOUT_MSG: {
3977 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003978 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003979 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3980 mSleepTimeout = true;
3981 checkReadyForSleepLocked();
3982 }
3983 }
3984 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003985 case LAUNCH_TIMEOUT_MSG: {
3986 if (mService.mDidDexOpt) {
3987 mService.mDidDexOpt = false;
3988 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3989 return;
3990 }
3991 synchronized (mService) {
3992 if (mLaunchingActivity.isHeld()) {
3993 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3994 if (VALIDATE_WAKE_LOCK_CALLER
3995 && Binder.getCallingUid() != Process.myUid()) {
3996 throw new IllegalStateException("Calling must be system uid");
3997 }
3998 mLaunchingActivity.release();
3999 }
4000 }
4001 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004003 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 } break;
4005 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004006 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 } break;
4008 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004009 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004010 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004011 case CONTAINER_CALLBACK_VISIBILITY: {
4012 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004013 final IActivityContainerCallback callback = container.mCallback;
4014 if (callback != null) {
4015 try {
4016 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4017 } catch (RemoteException e) {
4018 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004019 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004020 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004021 case LOCK_TASK_START_MSG: {
4022 // When lock task starts, we disable the status bars.
4023 try {
Jason Monk62515be2014-05-21 16:06:19 -04004024 if (mLockTaskNotify == null) {
4025 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004026 }
Jason Monk62515be2014-05-21 16:06:19 -04004027 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004028 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004029 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004030 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004031 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004032 flags = StatusBarManager.DISABLE_MASK
4033 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004034 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004035 flags = StatusBarManager.DISABLE_MASK
4036 & (~StatusBarManager.DISABLE_BACK)
4037 & (~StatusBarManager.DISABLE_HOME)
4038 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004039 }
4040 getStatusBarService().disable(flags, mToken,
4041 mService.mContext.getPackageName());
4042 }
4043 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004044 if (getDevicePolicyManager() != null) {
4045 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004046 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004047 }
justinzhang5286d3f2014-05-12 17:06:01 -04004048 } catch (RemoteException ex) {
4049 throw new RuntimeException(ex);
4050 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004051 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004052 case LOCK_TASK_END_MSG: {
4053 // When lock task ends, we enable the status bars.
4054 try {
Jason Monk62515be2014-05-21 16:06:19 -04004055 if (getStatusBarService() != null) {
4056 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4057 mService.mContext.getPackageName());
4058 }
4059 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004060 if (getDevicePolicyManager() != null) {
4061 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4062 msg.arg1);
4063 }
Jason Monk62515be2014-05-21 16:06:19 -04004064 if (mLockTaskNotify == null) {
4065 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4066 }
4067 mLockTaskNotify.show(false);
4068 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004069 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004070 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004071 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004072 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004073 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004074 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004075 new LockPatternUtils(mService.mContext)
4076 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004077 }
4078 } catch (SettingNotFoundException e) {
4079 // No setting, don't lock.
4080 }
justinzhang5286d3f2014-05-12 17:06:01 -04004081 } catch (RemoteException ex) {
4082 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004083 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004084 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004085 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004086 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004087 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4088 if (mLockTaskNotify == null) {
4089 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4090 }
4091 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4092 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004093 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4094 final ActivityContainer container = (ActivityContainer) msg.obj;
4095 final IActivityContainerCallback callback = container.mCallback;
4096 if (callback != null) {
4097 try {
4098 callback.onAllActivitiesComplete(container.asBinder());
4099 } catch (RemoteException e) {
4100 }
4101 }
4102 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004103 case LAUNCH_TASK_BEHIND_COMPLETE: {
4104 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004105 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004106 if (r != null) {
4107 handleLaunchTaskBehindCompleteLocked(r);
4108 }
4109 }
4110 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004111
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004112 }
4113 }
4114 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004115
Ying Wangb081a592014-04-22 15:20:16 -07004116 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004117 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4118 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004120 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004121 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004122 ActivityRecord mParentActivity = null;
4123 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004124
Craig Mautnere3a00d72014-04-16 08:31:19 -07004125 boolean mVisible = true;
4126
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004128 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004130 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4131 final static int CONTAINER_STATE_NO_SURFACE = 1;
4132 final static int CONTAINER_STATE_FINISHING = 2;
4133 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4134
4135 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004136 synchronized (mService) {
4137 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004138 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004139 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004140 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004141 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004142 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004143
Andrii Kulian839def92016-11-02 10:58:58 -07004144 /**
4145 * Adds the stack to specified display. Also calls WindowManager to do the same from
4146 * {@link ActivityStack#addToDisplay(ActivityDisplay, boolean)}.
4147 * @param activityDisplay The display to add the stack to.
4148 * @param onTop If true the stack will be place at the top of the display, else at the
4149 * bottom.
4150 */
4151 void addToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
4152 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004153 + " to display=" + activityDisplay + " onTop=" + onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004154 if (mActivityDisplay != null) {
4155 throw new IllegalStateException("ActivityContainer is already attached, " +
4156 "displayId=" + mActivityDisplay.mDisplayId);
4157 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004158 mActivityDisplay = activityDisplay;
Andrii Kulian839def92016-11-02 10:58:58 -07004159 mStack.addToDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004160 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004161 }
4162
4163 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004164 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004165 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004166 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4167 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004168 return;
4169 }
Andrii Kulian839def92016-11-02 10:58:58 -07004170 addToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 }
4172 }
4173
4174 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004175 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004176 synchronized (mService) {
4177 if (mActivityDisplay != null) {
4178 return mActivityDisplay.mDisplayId;
4179 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004180 }
4181 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004182 }
4183
Jeff Brownca9bc702014-02-11 14:32:56 -08004184 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004185 public int getStackId() {
4186 synchronized (mService) {
4187 return mStackId;
4188 }
4189 }
4190
4191 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004192 public boolean injectEvent(InputEvent event) {
4193 final long origId = Binder.clearCallingIdentity();
4194 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004195 synchronized (mService) {
4196 if (mActivityDisplay != null) {
4197 return mInputManagerInternal.injectInputEvent(event,
4198 mActivityDisplay.mDisplayId,
4199 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4200 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004201 }
4202 return false;
4203 } finally {
4204 Binder.restoreCallingIdentity(origId);
4205 }
4206 }
4207
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004208 @Override
4209 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004210 synchronized (mService) {
4211 if (mContainerState == CONTAINER_STATE_FINISHING) {
4212 return;
4213 }
4214 mContainerState = CONTAINER_STATE_FINISHING;
4215
Craig Mautnerd163e752014-06-13 17:18:47 -07004216 long origId = Binder.clearCallingIdentity();
4217 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004218 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004219 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004220 } finally {
4221 Binder.restoreCallingIdentity(origId);
4222 }
4223 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004224 }
4225
Andrii Kulian03c403d2016-11-11 11:14:12 -08004226 /**
4227 * Remove the stack completely. Must be called only when there are no tasks left in it,
4228 * as this method does not finish running activities.
4229 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004230 void removeLocked() {
4231 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004232 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004233 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004234 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004235 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004236 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 }
4238
Andrii Kulian839def92016-11-02 10:58:58 -07004239 /**
4240 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4241 * completely or re-parented.
4242 */
4243 private void removeFromDisplayLocked() {
4244 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4245 + " current displayId=" + mActivityDisplay.mDisplayId);
4246
4247 mActivityDisplay.detachActivitiesLocked(mStack);
4248 mActivityDisplay = null;
4249 }
4250
4251 /**
4252 * Move the stack to specified display.
4253 * @param activityDisplay Target display to move the stack to.
4254 */
4255 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4256 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4257 + mActivityDisplay + " to display=" + activityDisplay
4258 + " Callers=" + Debug.getCallers(2));
4259
4260 removeFromDisplayLocked();
4261
4262 mActivityDisplay = activityDisplay;
4263 mStack.moveToDisplay(activityDisplay);
4264 activityDisplay.attachActivities(mStack, ON_TOP);
4265 }
4266
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004268 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004269 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004270 }
4271
4272 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004273 public final int startActivityIntentSender(IIntentSender intentSender)
4274 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004275 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4276
4277 if (!(intentSender instanceof PendingIntentRecord)) {
4278 throw new IllegalArgumentException("Bad PendingIntent object");
4279 }
4280
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004281 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004282 Binder.getCallingUid(), mCurrentUser, false,
4283 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004284
4285 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4286 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4287 pendingIntent.key.requestResolvedType);
4288
4289 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4290 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4291 }
4292
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004293 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004294 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004295 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004296 throw new SecurityException(
4297 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4298 }
4299 }
4300
Craig Mautnerdf88d732014-01-27 09:21:32 -08004301 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004302 public IBinder asBinder() {
4303 return this;
4304 }
4305
Craig Mautner4504de52013-12-20 09:06:56 -08004306 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004307 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004308 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004309 }
4310
Craig Mautner4a1cb222013-12-04 16:14:06 -08004311 ActivityStackSupervisor getOuter() {
4312 return ActivityStackSupervisor.this;
4313 }
4314
Craig Mautnerd163e752014-06-13 17:18:47 -07004315 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004316 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004317 }
4318
Craig Mautner6985bad2014-04-21 15:22:06 -07004319 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004320 void setVisible(boolean visible) {
4321 if (mVisible != visible) {
4322 mVisible = visible;
4323 if (mCallback != null) {
4324 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4325 0 /* unused */, this).sendToTarget();
4326 }
4327 }
4328 }
4329
Craig Mautner6985bad2014-04-21 15:22:06 -07004330 void setDrawn() {
4331 }
4332
Craig Mautner1b4bf852014-05-26 15:06:32 -07004333 // You can always start a new task on a regular ActivityStack.
4334 boolean isEligibleForNewTasks() {
4335 return true;
4336 }
4337
Craig Mautnerd163e752014-06-13 17:18:47 -07004338 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004339 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004340 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004341 }
4342
Craig Mautner34b73df2014-01-12 21:11:08 -08004343 @Override
4344 public String toString() {
4345 return mIdString + (mActivityDisplay == null ? "N" : "A");
4346 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004347 }
4348
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004349 private class VirtualActivityContainer extends ActivityContainer {
4350 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004351 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004352
4353 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4354 super(getNextStackId());
4355 mParentActivity = parent;
4356 mCallback = callback;
4357 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004358 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004359 }
4360
4361 @Override
4362 public void setSurface(Surface surface, int width, int height, int density) {
4363 super.setSurface(surface, width, height, density);
4364
4365 synchronized (mService) {
4366 final long origId = Binder.clearCallingIdentity();
4367 try {
4368 setSurfaceLocked(surface, width, height, density);
4369 } finally {
4370 Binder.restoreCallingIdentity(origId);
4371 }
4372 }
4373 }
4374
4375 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4376 if (mContainerState == CONTAINER_STATE_FINISHING) {
4377 return;
4378 }
4379 VirtualActivityDisplay virtualActivityDisplay =
4380 (VirtualActivityDisplay) mActivityDisplay;
4381 if (virtualActivityDisplay == null) {
4382 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004383 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004384 mActivityDisplay = virtualActivityDisplay;
4385 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004386 addToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004387 }
4388
4389 if (mSurface != null) {
4390 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004391 }
4392
Craig Mautner6985bad2014-04-21 15:22:06 -07004393 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004394 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004395 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004396 } else {
4397 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004398 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004399 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004400 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004401 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004402 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004403
Craig Mautnerd163e752014-06-13 17:18:47 -07004404 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004405
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004406 if (DEBUG_STACK) Slog.d(TAG_STACK,
4407 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004408 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004409
Craig Mautner6985bad2014-04-21 15:22:06 -07004410 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004411 boolean isAttachedLocked() {
4412 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004413 }
4414
4415 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004416 void setDrawn() {
4417 synchronized (mService) {
4418 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004419 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004420 }
4421 }
4422
Craig Mautner1b4bf852014-05-26 15:06:32 -07004423 // Never start a new task on an ActivityView if it isn't explicitly specified.
4424 @Override
4425 boolean isEligibleForNewTasks() {
4426 return false;
4427 }
4428
Craig Mautnerd163e752014-06-13 17:18:47 -07004429 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004430 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004431 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4432 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4433 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4434 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4435 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004436 }
4437 }
4438
Craig Mautner4a1cb222013-12-04 16:14:06 -08004439 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4440 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004441 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004443 int mDisplayId;
4444 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004445 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4446 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004447
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4449 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004450 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004451
Jose Lima4b6c6692014-08-12 17:41:12 -07004452 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004453
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004454 ActivityDisplay() {
4455 }
Craig Mautner4504de52013-12-20 09:06:56 -08004456
Craig Mautner1a70a162014-09-13 12:09:31 -07004457 // After instantiation, check that mDisplay is not null before using this. The alternative
4458 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004459 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004460 final Display display = mDisplayManager.getDisplay(displayId);
4461 if (display == null) {
4462 return;
4463 }
4464 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004465 }
4466
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004467 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004468 mDisplay = display;
4469 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004470 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004471
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004472 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004473 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004474 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4475 + " onTop=" + onTop);
4476 if (onTop) {
4477 mStacks.add(stack);
4478 } else {
4479 mStacks.add(0, stack);
4480 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004481 }
4482
Craig Mautnere0a38842013-12-16 16:14:02 -08004483 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004484 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004485 + " from displayId=" + mDisplayId);
4486 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004487 }
4488
Jose Lima4b6c6692014-08-12 17:41:12 -07004489 void setVisibleBehindActivity(ActivityRecord r) {
4490 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004491 }
4492
Jose Lima4b6c6692014-08-12 17:41:12 -07004493 boolean hasVisibleBehindActivity() {
4494 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004495 }
4496
Craig Mautner34b73df2014-01-12 21:11:08 -08004497 @Override
4498 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004499 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4500 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004501
4502 @Override
4503 protected int getChildCount() {
4504 return mStacks.size();
4505 }
4506
4507 @Override
4508 protected ConfigurationContainer getChildAt(int index) {
4509 return mStacks.get(index);
4510 }
4511
4512 @Override
4513 protected ConfigurationContainer getParent() {
4514 return ActivityStackSupervisor.this;
4515 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004516 }
4517
4518 class VirtualActivityDisplay extends ActivityDisplay {
4519 VirtualDisplay mVirtualDisplay;
4520
Craig Mautner6985bad2014-04-21 15:22:06 -07004521 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004522 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004523 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4524 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4525 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4526 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004527
4528 init(mVirtualDisplay.getDisplay());
4529
4530 mWindowManager.handleDisplayAdded(mDisplayId);
4531 }
4532
4533 void setSurface(Surface surface) {
4534 if (mVirtualDisplay != null) {
4535 mVirtualDisplay.setSurface(surface);
4536 }
4537 }
4538
4539 @Override
4540 void detachActivitiesLocked(ActivityStack stack) {
4541 super.detachActivitiesLocked(stack);
4542 if (mVirtualDisplay != null) {
4543 mVirtualDisplay.release();
4544 mVirtualDisplay = null;
4545 }
4546 }
4547
4548 @Override
4549 public String toString() {
4550 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004551 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004552 }
Jose Lima58e66d62014-05-27 20:00:27 -07004553
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004554 ActivityStack findStackBehind(ActivityStack stack) {
4555 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004556 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004557 if (display == null) {
4558 return null;
4559 }
4560 final ArrayList<ActivityStack> stacks = display.mStacks;
4561 for (int i = stacks.size() - 1; i >= 0; i--) {
4562 if (stacks.get(i) == stack && i > 0) {
4563 return stacks.get(i - 1);
4564 }
4565 }
4566 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4567 + " in=" + stacks);
4568 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004569
Jorim Jaggic69bd222016-03-15 14:38:37 +01004570 /**
4571 * Puts a task into resizing mode during the next app transition.
4572 *
4573 * @param taskId the id of the task to put into resizing mode
4574 */
4575 private void setResizingDuringAnimation(int taskId) {
4576 mResizingTasksDuringAnimation.add(taskId);
4577 mWindowManager.setTaskDockedResizing(taskId, true);
4578 }
4579
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004580 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4581 final TaskRecord task;
4582 final int callingUid;
4583 final String callingPackage;
4584 final Intent intent;
4585 final int userId;
4586 final ActivityOptions activityOptions = (bOptions != null)
4587 ? new ActivityOptions(bOptions) : null;
4588 final int launchStackId = (activityOptions != null)
4589 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004590 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004591 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004592 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004593 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004594
4595 if (launchStackId == DOCKED_STACK_ID) {
4596 mWindowManager.setDockedStackCreateState(
4597 activityOptions.getDockCreateMode(), null /* initialBounds */);
4598
4599 // Defer updating the stack in which recents is until the app transition is done, to
4600 // not run into issues where we still need to draw the task in recents but the
4601 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004602 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004603 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4604 }
4605
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004606 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4607 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004608 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004609 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004610 throw new IllegalArgumentException(
4611 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4612 }
4613
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004614 // Since we don't have an actual source record here, we assume that the currently focused
4615 // activity was the source.
4616 final ActivityStack focusedStack = getFocusedStack();
4617 final ActivityRecord sourceRecord =
4618 focusedStack != null ? focusedStack.topActivity() : null;
4619
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004620 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004621 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004622 moveTaskToStackLocked(
4623 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4624 ANIMATE);
4625 }
4626 }
4627
4628 // If the user must confirm credentials (e.g. when first launching a work app and the
4629 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4630 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4631 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004632 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004633 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004634 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004635 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4636 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004637
4638 // If we are launching the task in the docked stack, put it into resizing mode so
4639 // the window renders full-screen with the background filling the void. Also only
4640 // call this at the end to make sure that tasks exists on the window manager side.
4641 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004642 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004643 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004644
4645 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4646 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004647 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4648 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004649 return ActivityManager.START_TASK_TO_FRONT;
4650 }
4651 callingUid = task.mCallingUid;
4652 callingPackage = task.mCallingPackage;
4653 intent = task.intent;
4654 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4655 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004656 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4657 null, null, 0, 0, bOptions, userId, null, task);
4658 if (launchStackId == DOCKED_STACK_ID) {
4659 setResizingDuringAnimation(task.taskId);
4660 }
4661 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004662 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004663
4664 /**
4665 * @return a list of activities which are the top ones in each visible stack. The first
4666 * entry will be the focused activity.
4667 */
4668 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004669 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4670 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004671 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004672 if (display == null) {
4673 return Collections.EMPTY_LIST;
4674 }
4675 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4676 final ArrayList<ActivityStack> stacks = display.mStacks;
4677 for (int i = stacks.size() - 1; i >= 0; i--) {
4678 ActivityStack stack = stacks.get(i);
4679 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4680 ActivityRecord top = stack.topActivity();
4681 if (top != null) {
4682 if (stack == mFocusedStack) {
4683 topActivityTokens.add(0, top.appToken);
4684 } else {
4685 topActivityTokens.add(top.appToken);
4686 }
4687 }
4688 }
4689 }
4690 return topActivityTokens;
4691 }
Craig Mautner27084302013-03-25 08:05:25 -07004692}